绘制Y = ax ^ b的幂拟合

时间:2014-03-26 14:53:21

标签: r function plot regression estimation

我有重量(Wg)和总长度(TLcm)的q列表。我需要绘制它们并估计曲线和“a”和“b”值。 我试着这样做:

f <- function (TLcm,a,b) {a*TLcm^b} 
fit <- nls(Wg~f(TLcm,a,b),start=c(a=0,b=0),data=rel) 
co <- coef(fit) 
curve(WLmodel,add=TRUE,col="red",lwd=2) 

它不起作用,因为我不确定我提供的起始a和b值是否正确。我到达第二步,它停止了。

以下是我的数据的一部分:

>Wg
714
410
465
628
760
1357
900
1023
3750
3600
1000
1200
1100
2750
3100
1500
700
1500
1700
2000

>TLcm
62
50
54
58.5
62
74
65.5
69
104.5
104
70
73
70
91.5
98
80
60
76.5
83
85

抱歉,我不能把它作为表格形式,但考虑Wg是Y而TLcm是X.考虑到TLcm = 62cm的第一个值Wg = 714g,依此类推。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

不要使用c给出初始参数,但要list

> fit <- nls(Wg~a*TLcm^b,data=rel,start=list(a=1,b=1)) 
> fit
Nonlinear regression model
  model: Wg ~ a * TLcm^b
   data: rel
       a        b 
0.002109 3.095466 
 residual sum-of-squares: 140847

Number of iterations to convergence: 31 
Achieved convergence tolerance: 5.658e-07

现在你可以预测:

> plot(rel$TLcm, rel$Wg)
> x <- sort(rel$TLcm)
> lines(x, predict(fit, list(TLcm=x)), col="red", lwd=2)

enter image description here