Search code examples
rplotstatisticsglm

R: Fit curve to points: what linear/non-linear model to use?


I have a data which should follow the power law distribution.

x = distance 
y = %

I want to create a model and to add the fitted line to my plot.

My aim to recreate something like this:enter image description here

As author uses R-square; I assume they applied linear models, as R^2 is not suitable for non-linear models. http://blog.minitab.com/blog/adventures-in-statistics-2/why-is-there-no-r-squared-for-nonlinear-regression

However, I can't find out how to "curve" my line to the points; how to add the formula y ~ a*x^(-b) to my model.

Instead of curly line I got back the line as from the simple linear regression. enter image description here

My questions are:

  • Do I correctly assume the model y ~ a*x^(-b) used by author is linear?
  • what type of model to use to recreate my example: lm, glm, nls, etc. ?

I generated the dummy data, including the applied power law formula from the plot above:

set.seed(42)
scatt<-runif(10)

x<-seq(1, 1000, 100)
b = 1.8411
a = 133093
y = a*x^(-b) + scatt  # add some variability in my dependent variable

plot(y ~ x)

and tried to create a glm model.

# formula for non-linear model
m<-m.glm<-glm(y ~ x^2, data = dat) # 

# add predicted line to plot
lines(x,predict(m),col="red",lty=2,lwd=3)

This is my first time to model, so I am really confused and I don't know where to start... thank you for any suggestion or directions, I really appreciate it...


Solution

  • I personally think this question a dupe of this: `nls` fails to estimate parameters of my model but I would be cold-blooded if I close it (as OP put a bounty). Anyway, bounty question can not be closed.

    So the best I could think of, is to post a community wiki answer (I don't want to get this bounty).

    As you want to fit a model of this form y ~ a*x^(-b), it often benefit from taking log transform on both sides and fit a linear model log(y) ~ log(x).

    fit <- lm(log(y) ~ log(x))
    

    As you have already known how to use curve to plot regression curve and are happy with it, I will now show how to make plot.

    Some people call this log-log regression. Here are some other links I have for such kind of regression: