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?
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With