首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中一个图中的多个模型回归

R中一个图中的多个模型回归
EN

Stack Overflow用户
提问于 2020-09-26 00:40:05
回答 1查看 66关注 0票数 1

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

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

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

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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2020-09-26 01:16:53

使用ggplot2和一些简化/自动化。我做了一个循环,这样就不会重复同样的事情。

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

代码语言:javascript
复制
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)
}
代码语言:javascript
复制
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)

奖励-使用多元回归:

代码语言:javascript
复制
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)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64068166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档