简体   繁体   English

我打算通过for循环附加我的线性回归结果,但出现错误。 我该如何解决?

[英]I've intended attach my linear regression result by for loop but got an error. How can I solve it?

I'm a rookie with R. I have a question. 我是R的新秀。我有一个问题。
I need test all gene expression values (dat[,28:63] - numeric) , according to various clinical variables (dat[,1:27] - factor) . 我需要根据各种临床变量(dat[,1:27] - factor) (dat[,28:63] - numeric)测试所有基因表达值(dat[,28:63] - numeric) (dat[,1:27] - factor) My initial code was 我的初始代码是

dat <- readRDS("TCGA GLUT data.rds")
str(dat)

a <- round(summary(lm(SLC2A1 ~ Gender, data=dat))$coefficients, 5)
b <- round(summary(lm(SLC2A1 ~ Race, data=dat))$coefficients, 5)
c <- round(summary(lm(SLC2A1 ~ Age_Dx, data=dat))$coefficients, 5)
d <- round(summary(lm(SLC2A1 ~ Recurrence, data=dat))$coefficients, 5)
e <- round(summary(lm(SLC2A1 ~ Vital_Status, data=dat))$coefficients, 5)
f <- round(summary(lm(SLC2A1 ~ Hashimoto, data=dat))$coefficients, 5)
g <- round(summary(lm(SLC2A1 ~ Histologic_Dx, data=dat))$coefficients, 5)
h <- round(summary(lm(SLC2A1 ~ Max_Size, data=dat))$coefficients, 5)    
i <- round(summary(lm(SLC2A1 ~ Metastatic_LN, data=dat))$coefficients, 5)
j <- round(summary(lm(SLC2A1 ~ ETE, data=dat))$coefficients, 5)
k <- round(summary(lm(SLC2A1 ~ T_stage, data=dat))$coefficients, 5)
l <- round(summary(lm(SLC2A1 ~ N_stage, data=dat))$coefficients, 5)
m <- round(summary(lm(SLC2A1 ~ Stage, data=dat))$coefficients, 5)
n <- round(summary(lm(SLC2A1 ~ BRAF_V600E, data=dat))$coefficients, 5)

SLC2A1.result <- rbind(a,b,c,d,e,f,g,h,i,j,k,l,m,n)
SLC2A1.result

This job is so hard work which was changing all the gene name manually (SLC2A1 -> SLC2A2 -> SLC2A3...) So I've made a for loop like this. 这项工作非常辛苦,需要手动更改所有基因名称(SLC2A1-> SLC2A2-> SLC2A3 ...),所以我做了一个for循环。

result <- data.frame()
for (i in 28:63){
 a <- summary(lm(dat[,i] ~ Gender, data=dat))$coefficients
 b <- summary(lm(dat[,i] ~ Race, data=dat))$coefficients
 c <- summary(lm(dat[,i] ~ Age_Dx, data=dat))$coefficients
 d <- summary(lm(dat[,i] ~ Recurrence, data=dat))$coefficients
 e <- summary(lm(dat[,i] ~ Vital_Status, data=dat))$coefficients
 f <- summary(lm(dat[,i] ~ Hashimoto, data=dat))$coefficients
 g <- summary(lm(dat[,i] ~ Histologic_Dx, data=dat))$coefficients
 h <- summary(lm(dat[,i] ~ Max_Size, data=dat))$coefficients     
 i <- summary(lm(dat[,i] ~ Metastatic_LN, data=dat))$coefficients
 j <- summary(lm(dat[,i] ~ ETE, data=dat))$coefficients
 k <- summary(lm(dat[,i] ~ T_stage, data=dat))$coefficients
 l <- summary(lm(dat[,i] ~ N_stage, data=dat))$coefficients
 m <- summary(lm(dat[,i] ~ Stage, data=dat))$coefficients
 n <- summary(lm(dat[,i] ~ BRAF_V600E, data=dat))$coefficients 
 result[i] <- rbind(a,b,c,d,e,f,g,h,i,j,k,l,m,n)
 }

However, I got an error. 但是,我遇到了一个错误。

Error in `[.data.frame`(dat, , i) : undefined columns selected

I can't realized that where is my error and how can I solve it. 我无法意识到我的错误在哪里以及如何解决。 Please help me!! 请帮我!!

You should understand that summary(lm(...))$coefficients is a 2x4 matrix. 您应该了解summary(lm(...))$coefficients是2x4矩阵。 So the rbind(a,b,c,...) in your code builds a 28x4 matrix. 因此,您代码中的rbind(a,b,c,...)构建了一个28x4的矩阵。 Then, if you write result[i] <- rbind(a,b,c,...) you are assigning a matrix to the i -th column of your result data.frame . 然后,如果您编写result[i] <- rbind(a,b,c,...) ,则将矩阵分配给result data.framei列。

I would advise that you create a matrix for every gene, like you did in your first example and build a list of matrices for every gene. 我建议您像在第一个示例中那样为每个基因创建一个矩阵,并为每个基因建立一个矩阵列表。 You could then assign names to the list indices corresponding to the names of your genes. 然后,您可以将名称分配给与基因名称相对应的列表索引。 This would result in code like the following. 这将导致如下代码。

result <- list()
offset <- 27
for (i in 28:63){
  a <- summary(lm(dat[,i] ~ Gender, data=dat))$coefficients
  b <- summary(lm(dat[,i] ~ Race, data=dat))$coefficients
  c <- summary(lm(dat[,i] ~ Age_Dx, data=dat))$coefficients
  d <- summary(lm(dat[,i] ~ Recurrence, data=dat))$coefficients
  # more...
  gene.mat <- rbind(a,b,c,d,e,f,g,h,i,j,k,l,m,n)
  result[[i - offset]] <- round(gene.mat, 5)
}
# name the indices by creating a character vector "SLC2A1", "SLC2A2", ...
names(result) <- paste0("SLC2A", 1:36)

Then you can access a matrix by using result$SLC2A1 for example. 然后,您可以使用例如result $ SLC2A1访问矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将因子更改为数字变量或以其他方式处理我在线性回归中遇到的这个错误 - How can I change the factors to numeric variables or otherwise deal with this error I'm getting in my linear regression 如何执行线性回归而没有错误? - How can I perform Linear regression without error? 如何为这个线性 model 构建回归? - How can I build a regression for this linear model? 如何匹配 R 中的线性回归结果与 group_by dplyr 相同的 output? - How can I match the result of linear regression in R to be the same output as group_by dplyr? 如何在滚动回归中解决此错误? - How do I solve this error in a rolling regression? 我怎样才能解决这个线性方程组? - How can I solve this system of linear equations? 如何在 R 循环中更改线性回归中的预测变量? - How do I change predictors in linear regression in loop in R? 如何为添加到所有输入要素中的较大(未知)常数校正线性回归? - How can I correct linear regression for a large (unknown) constant added to all of my input features? 如何为我的 3 个图在一张图中拟合一条线性回归线? - How can I fit one linear regression line in one graph for my 3 plots? 如何基于另一个线性回归从线性回归中过滤出行 - How can I filter out rows from linear regression based on another linear regression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM