首页 > 解决方案 > R - lm() 函数不返回系数的 p 值

问题描述

我正在尝试对一些癌症数据进行分析。我正在尝试使用有关患者的元信息来理解变量。但是该lm()功能没有给出我期望的输出。根据这些帖子(线性回归系数信息作为数据框或矩阵),“lm”变量的系数槽应该是一个矩阵。但是,我的只是一个向量。以下是您可以尝试的可重现示例。您将不得不安装一个包来为您获取元数据。那个包是TCGAbiolinks。您可以使用 Bioconductor 安装它。如果没有BiocManager,则必须先安装它。我带来的不便表示歉意。

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install("TCGAbiolinks")

安装后TCGAbiolinks,您可以下载数据

query <- GDCquery(project = "TCGA-BRCA", 
                  data.category = "Clinical",
                  data.type = "Clinical Supplement", 
                  data.format = "BCR Biotab")
GDCdownload(query)
clinData <- GDCprepare(query)
clinData <- as.data.frame(clinData$clinical_patient_brca)
clinData <- clinData[-c(1:2),]

然后运行以下代码。输出变量是numGenes。我正在尝试使用上面数据中的列来估计它。在这个可重现的示例中,您不会得到任何相关性,因为我只是对numGenes. 但是这个例子说明了我的问题。它没有给我变量coefficients槽的矩阵。lm

patientMeta <- clinData
patientMeta$Patient <- sapply(clinData[,2], function(x) return(strsplit(x, "-")[[1]][3]))

numGenes <- data.frame(sample(1:1500, nrow(patientMeta)))
row.names(numGenes) <- patientMeta$Patient
names(numGenes) <- "numGenes"

patientMeta <- merge(patientMeta, numGenes, by.x = 113, by.y = 0, all =F)

# Change string columns to factors and numeric columns to numeric
patientMeta2 <- as.data.frame(patientMeta)
charCols <- which(apply(patientMeta2, 2, function(x) {
  if(is.na(as.numeric(x[1]))) {
    return(T)
  } else {
    return(F)
  }
}))
charCols <- names(patientMeta2)[charCols]
for (i in charCols) {
  patientMeta2[,i] <- as.factor(patientMeta2[,i])
}
numCols <- which(!(names(patientMeta2) %in% charCols))
numCols <- names(patientMeta2)[numCols]
for (i in numCols) {
  patientMeta2[,i] <- as.numeric(patientMeta2[,i])
}
# Remove columns that have no contrasts
patientMeta2 <- patientMeta2[,-which(sapply(1:ncol(patientMeta2), function(x) return(length(unique(patientMeta2[,x])))) == 1)]
# Remove columns that have na values
patientMeta2 <- patientMeta2[,which(apply(patientMeta2, 2, function(x) if(length(which(is.na(x))) > 0) return(F) else return(T)))]
# Remove columns that have incomplete information
patientMeta2 <- patientMeta2[,which(apply(patientMeta2, 2, function(x) length(grep("\\[Not", x))) < 10)]
# Remove ID columns
patientMeta2 <- patientMeta2[,-c(1,2,3,4,21)]

# Create formula for regression
lmFitExpression <- paste0(names(patientMeta2)[-ncol(patientMeta2)], collapse = " + ")
lmFitExpression <- paste("numGenes ~", lmFitExpression)
lmFitExpression <- formula(lmFitExpression)
# Do linear regression
theLM <- lm(lmFitExpression, patientMeta2)

现在,如果您要查看系数

> head(theLM$coefficients)
                (Intercept)    prospective_collectionNO   prospective_collectionYES 
                 1780.36026                  -115.93932                  -109.12016 
 retrospective_collectionNO retrospective_collectionYES                  genderMALE 
                         NA                          NA                    75.11251 

你可以看到它不是一个矩阵。我不知道为什么要以这种形式获取数据。我对p-value专栏感兴趣,但这似乎只是给Estimate专栏

标签: rlinear-regression

解决方案


推荐阅读