gpt4 book ai didi

r - 如何在 R 中定义 abline 线性回归的图形边界

转载 作者:行者123 更新时间:2023-12-05 08:34:30 24 4
gpt4 key购买 nike

我正在尝试截断 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,遵循相同的斜率。

最佳答案

您可以剪辑绘图区域。我不能使用你的数据,因为你的例子不可重现,但这里有一个例子:

> plot(1,1,type='n',xlim=c(0,400), ylim=c(0,10))
> clip(1,365,0,10)
> abline(h=5,col='red')

在坐标框 x0=1,x1=365,y0=0,y1=10 内生成一条线:

enter image description here

关于r - 如何在 R 中定义 abline 线性回归的图形边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26472563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com