Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - plot power law line with x and y data

Tags:

r

I am trying to plot a power law line to fit x and y data that I already have in a data frame. I have tried power.law.fit in the igraph library but it isn't working. The data frame is:

dat=data.frame(
  x=1:8,
  ygm=c( 251.288, 167.739, 112.856, 109.705, 102.064, 94.331, 95.206, 91.415)
)
like image 844
joe Avatar asked Jul 06 '12 17:07

joe


1 Answers

I generally use one of two strategies here, I take the log and fit a linear model or I use nls. I think you could figure out the logged model if you wanted to, so Ill show the nls method here.

  nls1=nls(ygm~i*x^-z,start=list(i=-3,z=-2),data=dat)

Double check that is the formula you want, this method accepts a pretty broad class of formulas. Spend some time fooling with starting values. In particular try to think of frontiers where the likelihood surface could do weird things. Try values on both sides of the wierd places so you can be assured that you are not in a local optima.

> nls1
Nonlinear regression model
  model:  ygm ~ i * x^-z 
   data:  dat 
       i        z 
245.0356   0.5449 
 residual sum-of-squares: 811.4

 ... 

> predict(nls1)
[1] 245.03564 167.95574 134.66070 115.12256 101.94200  92.30101  84.86458
[8]  78.90891
> plot(dat)
> lines(predict(nls1))
like image 188
Seth Avatar answered Oct 04 '22 00:10

Seth