[英]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.