Find start values for NLS function in R [duplicate

2019-09-08 19:03发布

This question already has an answer here:

i am trying to make a fit for a power function using the NLS function in R but i am failing to find good start values.

This is part of my data "CentroM":

Wg  TLcm
3200    79
2650    77
2750    74
870     45
1480    52
3400    80.5
2400    76
2800    76.5
2900    77.5
2700    76
3215    76
3300    83
3100    79
3000    78.5
2800    76
2700    77
2500    74.5
2300    69
2700    73.5
3350    79

and here is the script i used:

plot(CentroM$TLcm,CentroM$Wg,xlab="Total Length(cm)",ylab="Total Weight(g)",pch=1,type="p")
f<-function(TLcm,a,b){a*TLcm^b}
fit<-nls(CentroM$Wg~f(CentroM$TLcm,a,b),start=list(a=0.5,b=0.5),data=CentroM)

and here is what i get:

Error in model.frame.default(formula = ~CentroM + Wg + TLcm, data = CentroM) : invalid type (list) for variable 'CentroM'

Any help please...

标签: r formula nls
1条回答
戒情不戒烟
2楼-- · 2019-09-08 19:36

You could take the logs, fit a linear model and use the coef from there a starting values:

df <- read.table(header = TRUE, text = 'Wg  TLcm
3200    79
2650    77
2750    74
870     45
1480    52
3400    80.5
2400    76
2800    76.5
2900    77.5
2700    76
3215    76
3300    83
3100    79
3000    78.5
2800    76
2700    77
2500    74.5
2300    69
2700    73.5
3350    79')


mod1 <- lm(log(Wg) ~ log(TLcm), data = df)
fit <- nls(Wg ~ a*TLcm^b, 
         start = list(a = exp(coef(mod1)[1]),
                      b = coef(mod1)[2]), 
         data = df)
查看更多
登录 后发表回答