r - 使用 ggplot 绘制非线性回归列表

标签 r list ggplot2 nlme

作为此链接的非线性回归分析的输出图

https://stats.stackexchange.com/questions/209087/non-linear-regression-mixed-model

有了这个数据集:

zz <-(" iso temp diam
 Itiquira   22  5.0
 Itiquira   22  4.7
 Itiquira   22  5.4
 Itiquira   25  5.8
 Itiquira   25  5.4
 Itiquira   25  5.0
 Itiquira   28  4.9
 Itiquira   28  5.2
 Itiquira   28  5.2
 Itiquira   31  4.2
 Itiquira   31  4.0
 Itiquira   31  4.1
 Londrina   22  4.5
 Londrina   22  5.0
 Londrina   22  4.4
 Londrina   25  5.0
 Londrina   25  5.5
 Londrina   25  5.3
 Londrina   28  4.6
 Londrina   28  4.3
 Londrina   28  4.9
 Londrina   31  4.4
 Londrina   31  4.1
 Londrina   31  4.4
    Sinop   22  4.5
    Sinop   22  5.2
    Sinop   22  4.6
    Sinop   25  5.7
    Sinop   25  5.9
    Sinop   25  5.8
    Sinop   28  6.0
    Sinop   28  5.5
    Sinop   28  5.8
    Sinop   31  4.5
    Sinop   31  4.6
    Sinop   31  4.3"
)
df <- read.table(text=zz, header = TRUE)

这个拟合模型,带有四个参数:

thx:最佳温度

thy: 最佳直径

thq:曲率

thc:偏度
library(nlme) 

df <- groupedData(diam ~ temp | iso, data = df, order = FALSE) 

n0 <- nlsList(diam ~ thy * exp(thq * (temp - thx)^2 + thc * (temp - thx)^3),               
      data = df, 
      start = c(thy = 5.5, thq = -0.01, thx = 25, thc = -0.001))

> n0
# Call:
#  Model: diam ~ thy * exp(thq * (temp - thx)^2 + thc * (temp - thx)^3) | iso 

# Coefficients:
              thy          thq      thx           thc
# Itiquira 5.403118 -0.007258245 25.28318 -0.0002075323
# Londrina 5.298662 -0.018291649 24.40439  0.0020454476
# Sinop    5.949080 -0.012501783 26.44975 -0.0002945292

# Degrees of freedom: 36 total; 24 residual
# Residual standard error: 0.2661453

有没有办法在ggplot中绘制拟合值,比如smooth()的特定函数?

enter image description here

我想我找到了...(基于 http://rforbiochemists.blogspot.com.br/2015/06/plotting-two-enzyme-plots-with-ggplot.html)
ip <- ggplot(data=daf,  aes(x=temp, y=diam, colour = iso)) +  
  geom_point() + facet_wrap(~iso)

ip + geom_smooth(method = "nls", 
                method.args = list(formula = y ~ thy * exp(thq * (x-thx)^2 + thc * (x - thx)^3), 
                                   start = list(thy=5.4, thq=-0.01, thx=25, thc=0.0008)),
                se = F, size = 0.5, data = subset(daf, iso=="Itiquira")) +

  geom_smooth(method = "nls", 
              method.args = list(formula = y ~ thy * exp(thq * (x-thx)^2 + thc * (x - thx)^3), 
                                 start = list(thy=5.4, thq=-0.01, thx=25, thc=0.0008)),
              se = F, size = 0.5, data = subset(daf, iso=="Londrina")) +

  geom_smooth(method = "nls", 
              method.args = list(formula = y ~ thy * exp(thq * (x-thx)^2 + thc * (x - thx)^3), 
                                 start = list(thy=5.4, thq=-0.01, thx=25, thc=0.0008)),
              se = F, size = 0.5, data = subset(daf, iso=="Sinop")) 

enter image description here

最佳答案

用稍微有原则的方式回答这个问题 ggplot方法(将输出组合成一个单一的数据帧,其结构与原始数据的结构相匹配)。不幸的是在 nls 上找到置信区间预测并不那么容易(搜索涉及引导或增量方法的解决方案):

tempvec <- seq(22,30,length.out=51)
pp <- predict(n0,newdata=data.frame(temp=tempvec))
## combine predictions with info about species, temp
pdf <- data.frame(iso=names(pp),
                  temp=rep(tempvec,3),
                  diam=pp)

创建图形:
library(ggplot2)
ggplot(df,aes(temp,diam,colour=iso))+
  stat_sum()+
  geom_line(data=pdf)+
  facet_wrap(~iso)+
  theme_bw()+
  scale_size(range=c(1,4))+
  scale_colour_brewer(palette="Dark2")+
  theme(legend.position="none",
        panel.spacing=grid::unit(0,"lines"))

enter image description here

关于r - 使用 ggplot 绘制非线性回归列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37122994/

相关文章:

r - 在 R 中使用 data.table,我可以从表中选择可变列的向量吗?

R - 过滤行 - 将数据保留在两个重复值之间

python - 有没有什么方法可以在Python中打乱字符串?

r - 增加雷达图中长轴标签的空间

arrays - 对 3 维数组进行子集化时不要删除维度

删除一行中的重复字符串

Bash:范围列表,如 a-b、C、k、m-n、x-y、z

python - 从除一列以外的所有列中删除逗号

r - ggplot2 没有按字母顺序排列条形图,而它应该这样做?

r - 使用 ggplot2/Rstudio 进行缓慢的图形渲染 - GPU 问题?