r - 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
专栏
解决方案
推荐阅读
- php - 如何在 Laravel (MYSQL) 中添加间隔 30 天
- javascript - 引导轮播不隐藏其他元素并同时显示所有元素
- python - 安排 Airflow DAG 仅在周一至周五运行,即仅在工作日运行
- android - How to solve layout issues in android studio
- ruby-on-rails - Create belongs_to association to another model from a concern
- python - How to type-hint an Enum of strings in Python
- python - Return multiple dataframes based on filtering on a looped key
- python - 如何对列表中存在的列使用alter table drop columns?
- python - 下面给出的代码中的 strftime 类型。方法是否嵌套在方法 now() 中
- reactjs - 使用 React Hooks 时,如何处理来自父组件的函数参数?