I would like to fit a power model of type:
y = b*x(^z)
My data is Animals data from mass packages.
My code is here:
library(MASS)
nls(brain~b*body^z,start = list(b = 0, z = 1),data=Animals)
It has an error I don't know how to fix
Error in nlsModel(formula, mf, start, wts) :
singular gradient matrix at initial parameter estimates
And I want to ask is this right to fit this model use my function.
Thanks for your help!
You have two mistakes. One is the singular gradient due to b=0, the other is the fact that there are two different types of animals that follow completely different distributions.
Look at the distribution:
Fixing the singular gradient still produces an error:
> nls(brain~b*body^z,start = list(b = 0.1, z = 1),data=Animals)
Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an infinity produced when evaluating the model
Therefore, you should also do this:
subset(Animals, ! body > 9000) -> mammals
nls(brain~b*body^z,start = list(b = 0.1, z = 1),data=mammals)
Nonlinear regression model
model: brain ~ b * body^z
data: mammals
b z
15.5540 0.6795
residual sum-of-squares: 4301588
Number of iterations to convergence: 13
Achieved convergence tolerance: 3.321e-06