R 中已知固定截距的线性回归

发布于 2024-12-03 11:52:00 字数 513 浏览 1 评论 0原文

我想使用 R 中的 lm() 函数计算线性回归。此外,我想获得回归的斜率,其中我明确地将截距赋予 lm()

我在互联网上找到了一个例子,并尝试阅读 R-help“?lm”(不幸的是我无法理解它),但我没有成功。谁能告诉我我的错误在哪里?

lin <- data.frame(x = c(0:6), y = c(0.3, 0.1, 0.9, 3.1, 5, 4.9, 6.2))
plot (lin$x, lin$y)

regImp = lm(formula = lin$x ~ lin$y)
abline(regImp, col="blue")

# Does not work:
# Use 1 as intercept
explicitIntercept = rep(1, length(lin$x))
regExp = lm(formula = lin$x ~ lin$y + explicitIntercept)
abline(regExp, col="green")

感谢您的帮助。

I want to calculate a linear regression using the lm() function in R. Additionally I want to get the slope of a regression, where I explicitly give the intercept to lm().

I found an example on the internet and I tried to read the R-help "?lm" (unfortunately I'm not able to understand it), but I did not succeed. Can anyone tell me where my mistake is?

lin <- data.frame(x = c(0:6), y = c(0.3, 0.1, 0.9, 3.1, 5, 4.9, 6.2))
plot (lin$x, lin$y)

regImp = lm(formula = lin$x ~ lin$y)
abline(regImp, col="blue")

# Does not work:
# Use 1 as intercept
explicitIntercept = rep(1, length(lin$x))
regExp = lm(formula = lin$x ~ lin$y + explicitIntercept)
abline(regExp, col="green")

Thanls for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞳孔里扚悲伤 2024-12-10 11:52:00

您可以从回归中减去显式截距,然后拟合无截距模型:

> intercept <- 1.0
> fit <- lm(I(x - intercept) ~ 0 + y, lin)
> summary(fit)

0 + 通过 lm 抑制截距拟合。

编辑 要绘制拟合图,请使用

> abline(intercept, coef(fit))

P.S.模型中的变量看起来是错误的:通常是 y ~ x,而不是 x ~ y (即回归变量应位于左侧,回归变量应位于左侧)在右侧)。

You could subtract the explicit intercept from the regressand and then fit the intercept-free model:

> intercept <- 1.0
> fit <- lm(I(x - intercept) ~ 0 + y, lin)
> summary(fit)

The 0 + suppresses the fitting of the intercept by lm.

edit To plot the fit, use

> abline(intercept, coef(fit))

P.S. The variables in your model look the wrong way round: it's usually y ~ x, not x ~ y (i.e. the regressand should go on the left and the regressor(s) on the right).

月隐月明月朦胧 2024-12-10 11:52:00

我看到您已接受使用 I() 的解决方案。我原以为基于 offset() 的解决方案会更明显,但口味各不相同,在完成偏移解决方案后,我可以欣赏 I() 解决方案的经济性:

with(lin, plot(y,x) )
lm_shift_up <- lm(x ~ y +0 + 
                       offset(rep(1, nrow(lin))), 
             data=lin)
abline(1,coef(lm_shift_up))

I see that you have accepted a solution using I(). I had thought that an offset() based solution would have been more obvious, but tastes vary and after working through the offset solution I can appreciate the economy of the I() solution:

with(lin, plot(y,x) )
lm_shift_up <- lm(x ~ y +0 + 
                       offset(rep(1, nrow(lin))), 
             data=lin)
abline(1,coef(lm_shift_up))
糖果控 2024-12-10 11:52:00

我已经使用了 offset 和 I()。我还发现偏移量更容易使用(如 BondedDust),因为您可以设置截距。

假设截距为 10。

plot (lin$x, lin$y)
适合 <-lm(lin$y~0 +lin$x,offset=rep(10,length(lin$x)))
abline(fit,col="blue")

I have used both offset and I(). I also find offset easier to work with (like BondedDust) since you can set your intercept.

Assuming Intercept is 10.

plot (lin$x, lin$y)
fit <-lm(lin$y~0 +lin$x,offset=rep(10,length(lin$x)))
abline(fit,col="blue")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文