我想使用 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")
感谢您的帮助。
您可以从回归中减去显式截距,然后拟合无截距模型:
> intercept <- 1.0
> fit <- lm(I(x - intercept) ~ 0 + y, lin)
> summary(fit)
The 0 +
抑制截距拟合lm
.
edit要绘制拟合图,请使用
> abline(intercept, coef(fit))
附:模型中的变量看起来是错误的:通常是y ~ x
, not x ~ y
(即回归数应位于左侧,回归量应位于右侧)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)