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.
我用lm来表示线性回归。我想要绘制并报告x轴截距。我知道我可以用代数来解出x通过设置y = 0,但是有办法让r报告给我吗?还有,我怎么能告诉r来画x轴截距?这是否需要扩展x轴范围来包含它?谢谢。
# example r code
plot(y~x)
fit <- lm(y~x)
abline(fit)
1 个解决方案
#1
2
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.
如果你想画出x轴截距,就像你说的那样展开。您可能需要将它扩展到x和y维度(使用xlim=c(0,100)和ylim=c(0,100)或其他任何东西),您应该注意到R不为坐标轴绘制直线。我想如果你想的话,可以用hline和vline手动添加。
To get the numerical value of the x-intercept, you'll have to do algebra.
为了得到x轴截距的数值,你需要做代数运算。
> 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.
给出y轴截距和斜率,很容易求出x轴截距。
#1
2
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.
如果你想画出x轴截距,就像你说的那样展开。您可能需要将它扩展到x和y维度(使用xlim=c(0,100)和ylim=c(0,100)或其他任何东西),您应该注意到R不为坐标轴绘制直线。我想如果你想的话,可以用hline和vline手动添加。
To get the numerical value of the x-intercept, you'll have to do algebra.
为了得到x轴截距的数值,你需要做代数运算。
> 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.
给出y轴截距和斜率,很容易求出x轴截距。