I am using lm
in r for linear regression. I would like to plot and report the x intercept. I know that I could use algebra and solve for x by setting y = 0, but is there a way to have r report it to me? Also, how can I 'tell' r to plot the x intercept? Would this just entail extending the x axis range to include it? Thanks.
# example r code
plot(y~x)
fit <- lm(y~x)
abline(fit)
If you want to plot the x-intercept, extend the plot as you said. You might need to extend it in both the x and y dimensions (use xlim=c(0,100)
and ylim=c(0,100)
or whatever), and you should note that R does not plot lines for the axes. I supposed you can add them in manually with hline
and vline
if you want.
To get the numerical value of the x-intercept, you'll have to do algebra.
> coef(fit)
(Intercept) x
0.8671534 0.4095524
Gives the y-intercept and the slope, and you can easily find the x-intercept from there.