[英]How can I change the factors to numeric variables or otherwise deal with this error I'm getting in my linear regression
[英]I've intended attach my linear regression result by for loop but got an error. How can I solve it?
我是R的新秀。我有一个问题。
我需要根据各种临床变量(dat[,1:27] - factor)
(dat[,28:63] - numeric)
测试所有基因表达值(dat[,28:63] - numeric)
(dat[,1:27] - factor)
。 我的初始代码是
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
这项工作非常辛苦,需要手动更改所有基因名称(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)
}
但是,我遇到了一个错误。
Error in `[.data.frame`(dat, , i) : undefined columns selected
我无法意识到我的错误在哪里以及如何解决。 请帮我!!
您应该了解summary(lm(...))$coefficients
是2x4矩阵。 因此,您代码中的rbind(a,b,c,...)
构建了一个28x4的矩阵。 然后,如果您编写result[i] <- rbind(a,b,c,...)
,则将矩阵分配给result data.frame
第i
列。
我建议您像在第一个示例中那样为每个基因创建一个矩阵,并为每个基因建立一个矩阵列表。 然后,您可以将名称分配给与基因名称相对应的列表索引。 这将导致如下代码。
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)
然后,您可以使用例如result $ SLC2A1访问矩阵。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.