I'm trying to fit a simple power law model to a data set that is as follows:
mydf
:
rev weeks
17906.4 1
5303.72 2
2700.58 3
1696.77 4
947.53 5
362.03 6
The goal being to pass the power line through and use it to predict rev
vlaues for future weeks. A bunch of research has led me to the nls
function, which I implemented as follows.
newMod <- nls(rev ~ a*weeks^b, data=modeldf, start = list(a=1,b=1))
predict(newMod, newdata = data.frame(weeks=c(1,2,3,4,5,6,7,8,9,10)))
While this works for an lm
model, I get a singular gradient
error, which I understand has to do with my starting values a
and b
. I tried different values, even going so far as to plot this in Excel, pass a lone, get an equation, then use the values from the equation, but I still get the error. I looked at a bunch of answers like this one and tried the second answer (couldn't understand the first), but to no result.
I could really use some help here on how to find the right starting values. Or alternatively, what other function I can use instead of nls.
In case you want to recreate mydf
with ease:
mydf <- data.frame(rev=c(17906.4, 5303.72, 2700.58 ,1696.77 ,947.53 ,362.03), weeks=c(1,2,3,4,5,6))
Best Answer
This is a common problem with nonlinear least squares models; if your start values are very far from the optimum the algorithm may not converge, even though it may be well behaved near the optimum.
If you start by taking logs of both sides and fit a linear model, you get estimates of $\log(a)$ and $b$ as the slope and intercept ( 9.947 and -2.011 ) (edit: that's natural log)
If you use those to guide the starting values for $a$ and $b$ everything seems to work okay: