【问题标题】:using lm(poly) to get formula coeff [duplicate]使用 lm(poly) 获得公式系数 [重复]
【发布时间】:2013-07-01 17:26:56
【问题描述】:

我正在尝试使用 lm(poly) 对某些点进行多项式回归,但对它返回的回归公式系数有一些疑问。

这样的示例:

x=seq(1,100) y=x^2+3*x+7 适合=lm(y~poly(x,2))

结果是:

lm(formula = y ~ poly(x, 2))

系数:

(截取)poly(x, 2)1 poly(x, 2)2 3542 30021 7452

为什么系数不是 7,3,2?

非常感谢!

【问题讨论】:

  • 试试这个:lm(y~x+I(x^2))lm(y~poly(x, 2, raw=TRUE))
  • @Ferdinand.kraft Oups 不知道,所以让我们结束这个问题
  • 危险!危险,Will Robinson::: 如果您将多项式回归用于推理目的,那么使用原始变量将非常具有误导性。
  • @42- 你能解释为什么,或者链接到解释吗?
  • 查找“正交多项式”。

标签: r regression


【解决方案1】:

如果您不想使用正交多项式,则需要将 raw 参数设置为 TRUE 这是默认的

set.seed(101)
N <- 100
x <- rnorm(N, 10, 3)
epsilon <- rnorm(N)
y <- 7 + 3 * x + x^2 + epsilon

coef(lm(y ~ poly(x, 2, raw = TRUE)))

##             (Intercept) poly(x, 2, raw = TRUE)1 
##                  7.8104                  2.7538 
## poly(x, 2, raw = TRUE)2 
##                  1.0150

借助poly 功能,您拥有

说明:

 Returns or evaluates orthogonal polynomials of degree 1 to
 ‘degree’ over the specified set of points ‘x’. These are all
 orthogonal to the constant polynomial of degree 0.  Alternatively,
 evaluate raw polynomials.

raw:如果为真,则使用原始而不是正交多项式。

但是您也可以使用费迪南德建议的方法,它有效。

coef(lm(y ~ x + I(x^2)))
## (Intercept)           x      I(x^2) 
##      7.8104      2.7538      1.0150 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2013-06-27
    • 2021-05-27
    相关资源
    最近更新 更多