问题
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...
回答1:
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)
来源:https://stackoverflow.com/questions/24752254/find-start-values-for-nls-function-in-r