我正在尝试对一组文件进行自动化,实现线性回归(lm)和相应的图形。 1个文件的示例:
score;EGFR_12;EGFR_24;EGFR_36;EGFR_48;EGFR_60;pa
-0,5992442;67,56217938;53,61312383;52,93430604;;;1
-0,6795702;53,28459074;57,23583761;43,94840102;51,36407098;;2
代码R:
if (!require(devtools)) {
install.packages("devtools")
library(devtools)
}
install_github("larmarange/JLutils")
library(ggplot2)
library(JLutils)
ggplotRegression <- function (fit) {
require(ggplot2)
ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) +
geom_point() +
stat_smooth(method = "lm", col = "red") +
labs(title = paste("Adj R2 = ",signif(summary(fit)$adj.r.squared, 5),
"Intercept =",signif(fit$coef[[1]],5 ),
" Slope =",signif(fit$coef[[2]], 5),
" P =",signif(summary(fit)$coef[2,4], 5)))
}
setwd("/Users/Desktop/global")
files <- list.files(path = "data", pattern = (".csv$"))
for (k in 1:length(files)) {
fname <- files[k]
cat(paste0("Now analyse data/", fname, "...\n"))
data <- read.csv2(paste0("data/", fname), header = T, stringsAsFactors = F, dec = ",")
x<-summary(lm(data$EGFR_12 ~ data$score))$coefficients
write.csv(x, file = paste0("summary/summary_egfr12", fname))
p1<- ggplotRegression(lm(data$EGFR_12 ~ data$score))
rm(data, x)
Sys.sleep(5)
y<-summary(lm(data$EGFR_24 ~ data$score))$coefficients
write.csv(y, file = paste0("summary/summary_egfr24", fname))
p2<- ggplotRegression(lm(data$EGFR_24 ~ data$score))
rm(data, y)
Sys.sleep(5)
z<-summary(lm(data$EGFR_36 ~ data$score))$coefficients
write.csv(z, file = paste0("summary/summary_egfr36", fname))
p3<- ggplotRegression(lm(data$EGFR_36 ~ data$score))
rm(data, z)
Sys.sleep(5)
a<-summary(lm(data$EGFR_48 ~ data$score))$coefficients
write.csv(a, file = paste0("summary/summary_egfr48", fname))
p4<- ggplotRegression(lm(data$EGFR_48 ~ data$score))
rm(data, a)
Sys.sleep(5)
b<-summary(lm(data$EGFR_60 ~ data$score))$coefficients
write.csv(b, file = paste0("summary/summary_egfr60", fname))
p5 <- ggplotRegression(lm(data$EGFR_60 ~ data$score))
rm(data, b)
Sys.sleep(5)
png(file = paste0("graphe_png/", fname), width = 350, height = "350")
multiplot(p1, p2, p3, p4, p5, cols = 5)
dev.off()
pdf(file = paste0("graphe_pdf/", fname))
multiplot(p1, p2, p3, p4, p5, cols = 5)
dev.off()
}
因此,我尝试为数据文件夹中的每个文件创建一个循环,通过调用ggplotRegression函数创建一个lm然后一个图形。
错误:
Now analyse data/commun_GLOBALalloscore_norm-imput-avechla.csv...
Error in data$EGFR_24 : object of type 'closure' is not subsettable
如何解决此错误?
提前谢谢