6

我正在尝试使用 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?

非常感谢!

4

1 回答 1

11

您需要将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 
于 2013-07-03T20:59:18.333 回答