【发布时间】:2017-06-08 07:33:35
【问题描述】:
我是 R 新手,无法找到这个(看似)简单问题的答案。我已经搜索了几天,并且确实阅读了几篇论文和帮助页面。
我已经能够画出一条线(红色)。
我想绘制另一条适合后点的线。我希望这条线看起来像这张图片中的黑线(Křivan 和 Priyadarshi,2015 年)。
但是,我无法绘制这条线。
我尝试使用以下代码拟合直线,但图表上没有显示任何内容:
我想通过的值:
Prey_isocline_x <- c(8.2, 7.15, 7.65, 10.6, 7.947368421, 5.35,
6, 8.2, 7.473684211, 1.5, 1.3, 0.95, 1.85,
1.15, 0.6, 2.7, 1.3, 0.25, 0.25, 6.263157895,
4, 0.3, 5.1, 4.15, 1.15, 1.6, 1.6, 1.55)
Prey_isocline_y <- c(0.45, 0.3, 0.2, 0.2, 0.105263158, 0.8, 0.5,
0.15, 0.052631579, 0.642857143, 1, 1, 1.15,
0.7, 0.55, 0.35, 0.8, 1.15, 1.55, 0.578947368,
0.5, 2.55, 0.15, 0.25, 0.45, 2.45, 2.45, 1.3)
Prey_isocline <- data.frame(Prey_isocline_x, Prey_isocline_y)
Predator_isocline_x <- c(0.25, 0.15, 0.3, 0.7, 0.25, 0.25, 0.05, 0.5, 0.45,
0.5, 0.5, 0.15, 0.6, 1.4, 0.85, 0.15, 0.15, 0.6)
Predator_isocline_y <- c(2.35, 2.9, 3.6, 3.6, 2.35, 4.45, 1.45, 1.7, 1.65,
1.7, 2.9, 1.8, 1.9, 2.35, 2.9, 2.8, 2.5, 3.05)
Predator_isocline <- data.frame(Predator_isocline_x, Predator_isocline_y)
第一次尝试绘图:
plot(Prey_isocline_x, Prey_isocline_y,
axes = F,
xlab= "",
ylab= "",
pch=1, col="black")
fit <- nls(Prey_isocline_y ~ SSlogis(Prey_isocline_x, Asym, xmid, scal),
data=Prey_isocline,
trace = TRUE)
summary(fit)
curve(predict(fit, newdata = data.frame(Prey_isocline_y=x)), add=TRUE)
输出第一次尝试:
> par(new=T)
> plot(Prey_isocline_x, Prey_isocline_y,
+ axes = F,
+ xlab= "",
+ ylab= "",
+ pch=1, col="black")
> fit <- nls(Prey_isocline_y ~ SSlogis(Prey_isocline_x, Asym, xmid, scal),
+ data=Prey_isocline,
+ trace = TRUE)
Error in nls(y ~ 1/(1 + exp((xmid - x)/scal)), data = xy, start = list(xmid = aux[1L], :
step factor 0.000488281 reduced below 'minFactor' of 0.000976562
> summary(fit)
Error in summary(fit) : object 'fit' not found
> curve(predict(fit, newdata = data.frame(Prey_isocline_y=x)), add=TRUE)
Error in predict(fit, newdata = data.frame(Prey_isocline_y = x)) :
object 'fit' not found
第二次尝试:
model <- loess(formula=Prey_isocline_x~Prey_isocline_y,
data=Predator_isocline)
abline(model, col="black")
第二次输出:
> model <- loess(formula=Prey_isocline_x~Prey_isocline_y, data=Predator_isocline)
> abline(model, col="black")
第三次尝试:
nls_fit <- nls(Prey_isocline_y ~ (b*Prey_isocline_x) - (b*Prey_isocline_x*Prey_isocline_x/K) -
(Predator_isocline_y*(Prey_isocline_x^k/(x^k+C^k)*(l*x/(1+l*h*x)))),
data = Prey_isocline,
start = list(b = 2.2,
e = 1.5,
K = 30,
k = 20,
l = 0.1,
h = 0.25,
C = 1,
m = 1.0))
lines(Prey_isocline_x, predict(nls_fit), col = "green")
第三个输出:
> nls_fit <- nls(Prey_isocline_y ~ (b*Prey_isocline_x) - (b*Prey_isocline_x*Prey_isocline_x/K) -
+ (Predator_isocline_y*(Prey_isocline_x^k/(x^k+C^k)*(l*x/(1+l*h*x)))),
+ data = Prey_isocline,
+ start = list(b = 2.2,
+ e = 1.5,
+ K = 30,
+ k = 20,
+ l = 0.1,
+ h = 0.25,
+ C = 1,
+ m = 1.0))
Error in nlsModel(formula, mf, start, wts) :
singular gradient matrix at initial parameter estimates
In addition: There were 30 warnings (use warnings() to see them)
> lines(Prey_isocline_x, predict(nls_fit), col = "green")
Error in predict(nls_fit) : object 'nls_fit' not found
第四次尝试:
nls_fit <- nls(Prey_isocline_y ~ a + b * Prey_isocline_x^(-c), Prey_isocline,
start = list(a = 80, b = 20, c = 0.2))
lines(Prey_isocline_x, predict(nls_fit), col = "green")
第四个输出:
> nls_fit <- nls(Prey_isocline_y ~ a + b * Prey_isocline_x^(-c), Prey_isocline,
+ start = list(a = 80, b = 20, c = 0.2))
Error in nls(Prey_isocline_y ~ a + b * Prey_isocline_x^(-c), Prey_isocline, :
step factor 0.000488281 reduced below 'minFactor' of 0.000976562
> lines(Prey_isocline_x, predict(nls_fit), col = "green")
Error in predict(nls_fit) : object 'nls_fit' not found
我完全迷路了,希望有人能帮助我。
【问题讨论】:
-
我的意思是,通常是你所有的 nls 公式。这样的公式有依据吗?您示例中的 nls 公式均无效...
-
你是说这个吗? Prey_isocline_y ~ a + b * Prey_isocline_x^(-c) 或这个: (Prey_isocline_y ~ (bPrey_isocline_x) - (bPrey_isocline_xPrey_isocline_x/K) - (Predator_isocline_y*(Prey_isocline_x^k/ (x^k+C^k)*(lx/(1+lhx)))) 第一个是我在互联网上找到的,希望它能起作用。第二个是基于捕食者-猎物模型的微分方程。我认为这个公式会给出最可靠的拟合,但我不知道如何应用它。
-
如果您认为像捕食者-猎物模型这样的微分方程模型应该适合您的 dara,则需要求解微分方程。可以很容易地找到如何使用 R 拟合捕食者-猎物模型的教程,例如stackoverflow.com/questions/43861974/….
-
嗨@Roland,感谢您的回复。我已经拟合了我的模型(y 轴上的捕食者和猎物种群和 x 轴上的时间)。现在我想绘制捕食者 - 猎物等倾线。具体来说,我想通过黑点拟合一条线。
标签: r plot non-linear-regression