R lm() 适用于单行,但不适用于 for 循环

时间:2021-01-17 15:54:34

标签: r lm p-value

我正在尝试在基因表达值矩阵上使用 for 循环运行 lm()。数据集分为人类和黑猩猩,我正在比较它们的相对表达。可以使用 this link 下载原始数据集。以下是我在这篇文章中使用的前 6 行,

>matrix

                Human_AF_8 Human_EU_11 Human_EU_4 Chimpanzee_4 Chimpanzee_6 Chimpanzee_5
ENSG00000000003  0.1394345 -0.27961627 -0.6147440   -0.1857581   0.19963078    0.4290812
ENSG00000000005 -0.8167632 -0.81676316  0.5223724    2.6947268  -0.59724108   -0.7190366
ENSG00000000419  0.4391277  2.83122842 -0.2066077    0.7903616  -0.26222373   -0.5113423
ENSG00000000457 -1.4025076 -0.07813095 -0.6768202    1.9199726  -0.18687230    1.4537927
ENSG00000000460 -0.8636231 -0.02775471 -1.0507558    0.9997930  -0.01413707   -0.2064266
ENSG00000000938  1.7407105  0.51450595 -0.8887369   -1.1291976  -0.29129441   -0.4344628

在读取数据框并将其转换为矩阵(第一列变为行名称)后,我尝试拟合 lm() 函数并保存其统计摘要以计算 P 值。

测试一行工作正常

# Making reference
species <- c(rep("human",3), rep("chimp",3))

# Testing for one row works fine
species.lm.sum <- summary(lm(matrix[1, ] ~ species))

# P value
pval <- pf(species.lm.sum$fstatistic[1],
           species.lm.sum$fstatistic[2],
           species.lm.sum$fstatistic[3], 
           lower.tail = FALSE)
# Printing
pval

测试多行给出错误

p.values <- c()

for (i in 1:nrow(matrix)) {
  
  stat <- summary(lm(matrix[i, ] ~ species))
  
  pval <- pf(stat$fstatistic[1],
             stat$fstatistic[2],
             stat$fstatistic[3], 
             lower.tail = FALSE)
  
  p.values <- append(p.values, pval) 
}
p.values

错误: contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]]) 中的错误:对比只能应用于具有 2 的因素或更多级别

追溯:

6. stop("contrasts can be applied only to factors with 2 or more levels")
5. `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]])
4. model.matrix.default(mt, mf, contrasts)
3. model.matrix(mt, mf, contrasts)
2. lm(exp.meas.mat[i, ] ~ species)
1. summary(lm(exp.meas.mat[i, ] ~ species))

P.S. 如果尝试使用 original dataset,请按如下方式更改引用,

species <- c(rep("human", 14), rep("chimp", 6))

R markdown 块

```{r, "LM"}
p.values <- c()

for (i in 1:nrow(data)) {
  stat <- summary(lm(data[i, ] ~ species))
  pval <- pf(stat$fstatistic[1],
             stat$fstatistic[2],
             stat$fstatistic[3], 
             lower.tail=FALSE)
  p.values <- append(p.values, pval) 
}
head(p.values)
```

1 个答案:

答案 0 :(得分:1)

无法重现您的问题。我使用的是 R 版本 4.0.3。

tmp <- tempfile()
download.file("ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE45nnn/GSE45263/suppl/GSE45263_gene.expression.measurements.txt.gz", tmp)
data <- as.matrix(read.table(gzfile(tmp), header=TRUE, row.names=1))
unlink(tmp)

species <- c(rep("human", 14), rep("chimp", 6))
species.lm.sum <- summary(lm(data[1, ] ~ species))
pval <- pf(species.lm.sum$fstatistic[1],
           species.lm.sum$fstatistic[2],
           species.lm.sum$fstatistic[3], 
           lower.tail=FALSE)
pval
#     value 
# 0.7540272 

p.values <- c()

for (i in 1:nrow(data)) {
  stat <- summary(lm(data[i, ] ~ species))
  pval <- pf(stat$fstatistic[1],
             stat$fstatistic[2],
             stat$fstatistic[3], 
             lower.tail=FALSE)
  p.values <- append(p.values, pval) 
}
head(p.values)
#      value      value      value      value      value      value 
# 0.75402720 0.91408640 0.22396503 0.01239904 0.83018858 0.14561322 

来自rmarkdown

enter image description here

相关问题