【发布时间】:2014-12-15 20:14:39
【问题描述】:
我正在尝试截断 abline 的末端,这实际上只是我的数据的线性回归。
fit1=lm(logy~logx)
> fit1
Call:
lm(formula = logy ~ logx)
Coefficients:
(Intercept) logx
-5.339 -2.115
其中 logx 是 log10(x[1:365] 转换的。 logy 遵循相同的代码。当我使用abline(fit1,col="red") 绘图时,我得到了我想要的线,但线超出了我最初设置的边界[1:365]。我已经尝试过par=xpd,但这并没有将线路减少到我想要的限制。我玩过segments() 无济于事。也许是line() 参数?
编辑 这是新的解决方案:
#the following vectors x and y store our data that we want to plot
x<-(1:10)
y<-(10:1)
plot(x,y,type="l",log="xy")
#we want to do a linear regression on our log10 transformed data and add the line to the plot
logy=log10(y[3:8])
logx=log10(x[3:8])
fit1=lm(logy~logx)
#finally, we want the regression line to only seem like it applies over the range from 3 to 8
clip(3,8,8,3)
abline(fit1,col="red")
这会产生一个图,其中有一条线(现在,没有)在我们的 x 轴上延伸超过 3 到 8。我只希望回归线显示从 x=3 到 x=8,遵循相同的斜率。
【问题讨论】:
-
请提供一个可重现的例子。
-
您可能必须使用
segment()并在 x = 3 和 x = 8 处进行评估 -
我尝试使用
segment(),但我认为我不理解 R 帮助数据库中的语法。需要详细说明吗?
标签: r plot linear-regression