Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a linear regression to a double logarithmic R plot?

I have the following data:

someFactor = 500
x = c(1:250)
y = x^-.25 * someFactor

which I show in a double logarithmic plot:

plot(x, y, log="xy")

Now I "find out" the slope of the data using a linear model:

model = lm(log(y) ~ log(x))
model

which gives:

Call:
lm(formula = log(y) ~ log(x))

Coefficients:
(Intercept)       log(x)  
      6.215       -0.250  

Now I'd like to plot the linear regression as a red line, but abline does not work:

abline(model, col="red")

What is the easiest way to add a regression line to my plot?

like image 795
R_User Avatar asked Dec 15 '22 04:12

R_User


1 Answers

lines(log(x), exp(predict(model, newdata=list(x=log(x)))) ,col="red")

The range of values for x plotted on the log-scale and for log(x) being used as the independent variable are actually quite different. This will give you the full range:

lines(x, exp(predict(model, newdata=list(x=x))) ,col="red")

enter image description here

like image 159
IRTFM Avatar answered Jan 16 '23 07:01

IRTFM