我已经为非线性函数创建了最佳拟合。它似乎工作正常:
#define a function
fncTtr <- function(n,d) (d/n)*((sqrt(1+2*(n/d))-1))
#fit
dFit <- nls(dData$ttr~fncTtr(dData$n,d),data=dData,start=list(d=25),trace=T)
summary(dFit)
plot(dData$ttr~dData$n,main="Fitted d value",pch=19,)
xl <- seq(min(dData$n),max(dData$n), (max(dData$n) - min(dData$n))/1000)
lines(xl,predict(dFit,newdata=xl,col=blue)
我观察的情节是正确的。我无法在我的绘图上显示最佳拟合曲线。我创建了具有 1000 个值的 xl 自变量,并且我想使用最佳拟合来定义新值。当我调用“行”过程时,我收到错误消息:
xy.coords(x, y) 中的错误:“x”和“y”长度不同如果我尝试仅执行预测函数:
a <-predict(dFit,newdata=xl)
str(a)
我可以看到 xl 有 1000 个组件,但“a”只有 16 个组件。我不应该在 a 中有相同数量的值吗?
使用的数据:
n ttr d
1 35 0.6951 27.739
2 36 0.6925 28.072
3 37 0.6905 28.507
4 38 0.6887 28.946
5 39 0.6790 28.003
6 40 0.6703 27.247
7 41 0.6566 25.735
8 42 0.6605 26.981
9 43 0.6567 27.016
10 44 0.6466 26.026
11 45 0.6531 27.667
12 46 0.6461 27.128
13 47 0.6336 25.751
14 48 0.6225 24.636
15 49 0.6214 24.992
16 50 0.6248 26.011