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