attenuation = data.frame(km =
c(0,0,0.4,0.4,0.8,0.8,1.2,1.2,1.6,1.6,2,2,2.4,2.4,2.8,2.8,3.2,3.2,3.6,3.6,4,
4,4.4,4.4,4.8,4.8,5.2,5.2,5.6,5.6,6,6,6.4,6.4,6.8,6.8,7.2,7.2,7.6,7.6,8,8,
11.7,11.7,13,13), edna = c(76000,20000,0,0,6000,0,0,6880,10700,0,6000,
0,0,0,0,0,0,6000,0,0,0,0,0,0,0,0,6310,0,6000,6000,0,0,0,0,0,
0,0,0,0,0,0,6000,0,0,0,0))
#This worked great for a linear regression
ggplot(attenuation, aes(x = km, y = edna)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
xlab("Distance from Cage (km)") +
ylab("eDNA concentration (gene sequence/Liter)")
Run Code Online (Sandbox Code Playgroud)
但是线性回归似乎不太适合(r平方= 0.09)。因此,我想尝试其他方法。我还尝试了拟合度较差的其他一些回归,因此我想尝试一个非线性回归。
我研究了有关堆栈溢出的问题,并尝试了多种不同的选择,但是没有任何效果。我在下面提供的选项最有意义-但我想知道公式是否错误?还是需要修改开始列表?
就上下文而言,我试图探索河流距离与浓度之间的关系。
#This is not working for a nonlinear regression
ggplot(attenuation, aes(x = km, y = edna))+
geom_point() +
stat_smooth(method = 'nls', formula = 'y~a*x^b', method.args=list (start =
list(a = 1,b=1), se=FALSE))
Run Code Online (Sandbox Code Playgroud)
当我在以上计算中为nls运行代码时,我从r得到以下错误stat_smooth():可变长度不同(为'(se)'找到)
您有2个问题。首先是一个错位的“)”,因为它se=FALSE是的参数stat_smooth=,而不是method.args=:
ggplot(attenuation, aes(x = km, y = edna))+
geom_point() +
stat_smooth(method='nls', formula='y~a*x^b', method.args=list(start =
list(a=1, b=1)), se=FALSE)
Run Code Online (Sandbox Code Playgroud)
但这也不起作用,因为您的模型无法适应您的数据。看方程式。当x = 0时,y将等于0。对于x大于0的值,除非b为负,否则y将增加,但是x = 0则为Inf,因此该算法无法尝试使用负值。由于关系递减,因此需要指定一个为x = 0和合理的起始值定义的函数。这个参数比线性函数更适合您的数据(也可以将其定义为a*(x + 1)^-1本质上是在x上加上1的函数,以便将其定义为x = 0:
ggplot(attenuation, aes(x = km, y = edna))+
geom_point() +
stat_smooth(method = 'nls', formula = 'y~a/(x + 1)',
method.args=list(start=list(a=50000)), se=FALSE)
Run Code Online (Sandbox Code Playgroud)
[![一个参数[1]](https://i.stack.imgur.com/O1jht.png)
我将20,000和76,000之间的差额相除,从而选择了50000。最终估计约为20,000。您可以通过添加第二个参数来使曲线更急剧地弯曲,但是您有太多0值,取决于您尝试传达的内容,它可能太多了:
ggplot(attenuation, aes(x = km, y = edna))+
geom_point() +
stat_smooth(method='nls', formula='y~a*(1+x)^b', method.args=list(start =
list(a=50000, b=-1)), se=FALSE)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
618 次 |
| 最近记录: |