繁体   English   中英

R中的一个图中的几个模型回归

[英]Several model regressions in one plot in R

我正在使用 drc-package https://cran.r-project.org/web/packages/drc/drc.pdf

这个包将 sigmoidal 回归添加到我的数据集中,通常我可以通过 plot(model) 函数绘制模型回归。 但是,我的代码中现在有 4 个回归模型,并尝试在一个图中编译它们。 到目前为止,points() 和 lines() 函数对我来说不能正常工作。

现在,我通过 par(new = TRUE) 函数在一个图中编译所有曲线,但 y 轴包含每个图的 4 个表示法。

你知道我如何在一个带有适当 y 轴的图中获得所有 4 个回归模型吗?

conc <- c(0.001, 0.04, 0.1, 0.4, 1, 4)
#meanOD1 <-c(1.69355, 1.43355, 0.847025, 0.74654, 0.70775, 0.57331)
#meanOD2 <-c(1.4211666667, 1.2064333333, 0.9020533333, 0.7642633333, 0.5352066667, 0.332945)
#meanOD3 <-c(1.07451, 0.98538, 1.2969333333, 0.9872366667, 0.78839, 0.51288)
#meanOD4 <-c(1.2974333333, 1.3021333333, 1.2579333333, 1.138, 0.9517233333, 0.332945)
meanOD1 <-c(100, 84.6476336689, 50.0147618907, 44.0813675416, 41.7909125801, 33.8525582357)
meanOD2 <-c(100, 84.8903483054, 63.4727336695, 53.7771783746, 37.6596692858, 23.4275829717)
meanOD3 <-c(100, 89.7321264979, 95.0517302454, 72.3541842256, 57.7807907948, 37.5887720327)
meanOD4 <-c(100, 100.3622536803, 96.9555275801, 87.7116357937, 73.3543149295, 25.6618194898)

df.ELISA1<-data.frame(meanOD1, conc)
df.ELISA1
df.ELISA2<-data.frame(meanOD2, conc)
df.ELISA2
df.ELISA3<-data.frame(meanOD3, conc)
df.ELISA3
df.ELISA4<-data.frame(meanOD4, conc)
df.ELISA4
model1 <- drm(df.ELISA1, fct = LL.4())
model1
summary(model1)
modelFit(model1)

model2 <- drm(df.ELISA2, fct = LL.4())
model2
summary(model2)
modelFit(model2)

model3 <- drm(df.ELISA3, fct = LL.4())
model3
summary(model3)
modelFit(model3)
model4 <- drm(df.ELISA4, fct = LL.4())
model4
summary(model4)
modelFit(model4)


plot(model1, type="average", col="red", pch = "o")
par(new=TRUE)
plot(model2, type="average", col="dark green",  pch = "+" )
par(new=TRUE)
plot(model3, type="average", col="blue",  pch = "-" )
par(new=TRUE)
plot(model4, type="average", col="orange",  pch = "*")

legend(1,100,legend=c("V1","V2","V3", "V4"), col=c("red","green","blue", "orange"),
       pch=c("o","+","-", "*"),lty=c(1,2,3,4), ncol=1)

# plot the first curve by calling plot() function
# First curve is plotted
#plot(model1, type="o", col="blue", pch="o", lty=1, ylim=c(0,110) )

# Add second curve to the same plot by calling points() and lines()
# Use symbol '*' for points.
#points(model2, col="red", pch="*")
#lines(model2, col="red",lty=2)

# Add Third curve to the same plot by calling points() and lines()
# Use symbol '+' for points.
#points(model3, col="dark red",pch="+")
#lines(model3, col="dark red", lty=3)

# Add Third curve to the same plot by calling points() and lines()
# Use symbol '+' for points.
#points(model4, col="green",pch="-")
#lines(model4, col="green", lty=3)

使用ggplot2和一些简化/自动化。 我做了一个循环,不重复相同的东西。

我为 plot.drc 添加了解决方案 - 对于第二个和下一个图,您必须简单地使用等于 true 的 add 参数。

df.ELISA<- list(data.frame(meanOD1, conc),
                data.frame(meanOD2, conc),
                data.frame(meanOD3, conc),
                data.frame(meanOD4, conc))

library(drc)

results <- list()

for (i in seq_len(length(df.ELISA))) {
model <- drm(df.ELISA[[i]], fct = LL.4())
modelFit(model)
datas <-  cbind(model$data, i)
colnames(datas) <- c("conc","meanOD","a","b", "weights", "i")
results[[i]] <- list(model = model, summy = summary(model), data = datas)
}
plot(results[[1]]$model, type="average", col="red", pch = "o")
plot(results[[2]]$model, type="average", col="dark green",  pch = "+", add = TRUE )
plot(results[[3]]$model, type="average", col="blue",  pch = "-", add = TRUE )
plot(results[[4]]$model, type="average", col="orange",  pch = "*", add = TRUE)

legend(0.3,100,legend=c("V1","V2","V3", "V4"), col=c("red","green","blue", "orange"),
       pch=c("o","+","-", "*"),lty=c(1,2,3,4), ncol=1)

在此处输入图片说明

奖励 - 使用多元回归:

data_plot <- do.call(rbind, lapply(1:length(results), function(i) results[[i]]$data))
data_plot$iter <- factor(data_plot$i)

library(ggplot2)

formula <- y ~ poly(x, 2, raw = TRUE)

ggplot(data_plot, aes(x= log(conc), y = meanOD, col = iter, group = iter, shape = iter))  +
  geom_point() +
  stat_smooth(method = "lm", formula = formula, level = 0.1)

在此处输入图片说明

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM