如何解决用 ggplot2 绘制一个用 NLS 获得的非线性回归
我试图用 ggplot2 内插一个内插函数并将其重叠到单个值的点图上。
我得到了一个我无法理解的错误,就像我绑定了两个不同长度的向量一样。
3: Computation failed in `stat_smooth()`:
arguments imply differing number of rows: 80,6
完整代码如下:
library(ggplot2)
tabella <- data.frame("Tempo" = c(0,15,30,60,90,120),"Visc" = c(500,9125,11250,10875,11325,10375))
attach(tabella)
Visc.mod <- nls((Visc ~ 500 + (k1*Tempo/(k2+Tempo))),start=list(k1=100,k2=100),trace=TRUE)
cor(Visc,predict(Visc.mod))
predict(Visc.mod)
summary(Visc.mod)
ggplot(tabella,aes(x=Tempo,y=Visc)) +
geom_point() +
stat_smooth(method = "nls",method.args = list(formula = "Visc ~ 500 + (k1*Tempo/(k2+Tempo))",start = list(k1=100,k2=100)),data = tabella,se = FALSE)
我真的不明白错误在哪里。
预先感谢您的每一个回复!
解决方法
我通过移动公式参数让它运行起来没有错误。不过合身看起来不是特别好。
library(ggplot2)
tabella <- data.frame("Tempo" = c(0,15,30,60,90,120),"Visc" = c(500,9125,11250,10875,11325,10375))
ggplot(tabella,aes(x=Tempo,y=Visc)) +
geom_point() +
stat_smooth(method = "nls",formula = y ~ 500 + (k1 * x / (k2 + x)),method.args = list(start = list(k1=100,k2=100)),data = tabella,se = FALSE)
由 reprex package (v1.0.0) 于 2021 年 4 月 14 日创建
,您的代码的一个问题是公式是 nls 的参数,您需要将公式对象传递给它而不是字符。
其次,ggplot2 将 y
和 x
传递给 nls 而不是 Visc
和 Tempo
ggplot(tabella,aes(x = Tempo,y = Visc)) +
geom_point()+
geom_smooth(
method = "nls",method.args = list(start = c(k1 = 100,k2 = 100)),se=FALSE)
当@teunbrand 在我之前时,我正在输入我的答案。但是,我使用 geom_smooth
而不是 stat_smooth
放置它
结果一样。不太合适
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。