# [R] nls power law help

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Jul 14 17:11:40 CEST 2013

```On 14/07/2013 14:30, JenPool wrote:
> Hi,
>
> I am trying to use a power law y=bx^a as a nls model as below, however I
> keep getting 'singular gradient' error. I have tried multiple different
> starting values but always get an error.

That is not the model you tried to fit. b*x*exp(a) is always
over-parametrized.

There are lots of errors in your code: please do try it before posting.
Maybe you intended

> df <- data.frame(x=x2,y=y2)
> nls(y ~ b*x^a, data = df, start = list(a=0.7,b=3000))
Nonlinear regression model
model: y ~ b * x^a
data: df
a         b
0.4312 1506.0120

I've no idea where you got your starting values from: taking logs

> lm(log(y) ~ log(x), data = df)

Call:
lm(formula = log(y) ~ log(x), data = df)

Coefficients:
(Intercept)       log(x)
7.4999       0.3776

suggests a=0.38 and b=exp(7.5)=1800

>
>> x2 <-
>> c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
>> 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63)
>> y2 <-
>> c(2326.0,2635.5,2875.0,3096.0,3265.5,3455.0,3618.0,3785.0,3936.0,4087.0,4214.0,4355.0,
>> 4488.0,4615.0,4735.5,4866.0,4981.5,5089.5,5202.5,5318.0,5438.0,5537.0,5636.5,5748.0,5853.0,
>> 5967.0,6042.5,6166.0,6259.5,6358.0,6452.0,6551.5,6645.0,6742.5,6839.0,6930.0,7026.0,7123.0,
>> 7207.0,7300.0,7387.5,7481.5,7574.5,7663.0,7752.0,7831.5,7915.0,8005.0,
>> 8091.5,8183.5,8260.0,8360.0,8441.0,8525.5,8606.0,8691.5,8779.0,8863.5,8941.5,9030.0,9108.0,9195.0,9271.0)
>
>
>> df <- dataframe(x=x2,y=y2)
>> model <- nls(y2~b*x2*exp(a),data=data2,start=list(a=0.7,b=3000))
> Error in nlsModel(formula, mf, start, wts) :
>    singular gradient matrix at initial parameter estimates
> 1: In min(x) : no non-missing arguments to min; returning Inf
> 2: In max(x) : no non-missing arguments to max; returning -Inf
>
> Any ideas?
>
> Thanks,
> Jen
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/nls-power-law-help-tp4671526.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```