0
$\begingroup$

I am having trouble using a linear (log-log) model to approximate the predictions of a nonlinear (power) model.

I wish to plot the predictions of the linear model on untransformed axes, and I believe they should approximate those of the nonlinear model. But, I can't make it work.

Here is a toy example with reproducible code:

set.seed(50)
x<-rnorm(100,10,2) #make random x data
z<-2+rnorm(100,1,0.1) #make random z coefficient
y<-3*x^z #make random y data
plot(x,y) #plot

#fit nonlinear (power) model, and plot predictions
m<-nls(y~c*x^z,start=list(c=3,z=2))
plot(y~x);xx<-seq(min(x),max(x),length=100);lines(xx,predict(m,list(x=xx)))

#fit linear (log-log) model, and add its predictions to existing plot
m2<-lm(log(y,10)~log(x,10))
xx<-seq(min(x),max(x),length=100);lines(x,predict(m2,x=xx),col=2)

As you can see in the image below, the predictions of the nonlinear (black line) and linear model (red line) are not even close.

I am sure I'm doing something wrong, and so would really appreciate any help or tips you might be able to provide! Thank you.

Mark

enter image description here

$\endgroup$
1
  • $\begingroup$ The output from predict(m2,x=xx) is going to be based on the formula in m2, so you need to raise it to some power. $\endgroup$
    – user44764
    Mar 11 '16 at 3:07
1
$\begingroup$

Thank you Matt. I came to a similar conclusion last night, and I've have included the solution below. The predictions, which are in logged space, have to be transformed back into units of the raw data before plotting them.

set.seed(50)
x<-rnorm(100,10,2) #make random x data
z<-2+rnorm(100,1,0.1) #make random z coefficient
y<-3*x^z #make random y data
plot(x,y) #plot

#fit nonlinear (power) model, and plot predictions
m<-nls(y~c*x^z,start=list(c=3,z=2))
plot(y~x);xx<-seq(min(x),max(x),length=100);lines(xx,predict(m,list(x=xx)))

#fit linear (log-log) model, and add its predictions to existing plot
m2<-lm(log(y,10)~log(x,10))
#xx<-seq(min(x),max(x),length=100);lines(x,predict(m2,x=xx),col=2) #the wrong way
pred<-data.frame(x,y);pred$xx<-x;pred$yy<-10^predict(m2,x=xx);pred<-pred[order(pred$xx),];lines(pred$xx,pred$yy,col=2) #the right way
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.