1

I have fitted a simple 2nd order polynomial to time series data in the following form:

polyfit <- lm(y ~ poly(x,2))

I wish to extract the respective coefficients (A, B and C) from the fitted polynomial of the form y = Ax^2 + Bx + C. I naturally thought the answer would be found in polyfit$coefficients within the polyfit object but these coefficients are not correct. I have tried some very simple data sets and compared with excel and whilst the poly curve fits are identical in R and excel, the A,B and C coefficints obtained from excel are correct but those obtained from the polyfit object arent? Have I extracted the incorrect information from the polyfit object? It would be more convenient for me to extract the coefficients directly from R for my purposes? Can anyone help?

4

1 回答 1

5

默认情况下poly拟合正交多项式。本质上它使用以下想法......

x <- 1:10

# in this case same as x-mean(x)
y1 <- residuals(lm(x ~ 1)) 
# normalize to have unit norm
y1 <- y1/sqrt(sum(y1^2))

y2 <- residuals(lm(y1^2 ~ y1))
y2 <- y2/sqrt(sum(y2^2))

y3 <- residuals(lm(x^3 ~ y2 + y1))
y3 <- y3/sqrt(sum(y3^2))

cbind(y1, y2, y3)    
poly(x, 3)

构造一组仍然产生相同预测的正交向量。如果您只想以常规方式获得多项式,那么您需要指定raw=TRUE为参数。

y <- rnorm(20)
x <- 1:20
o <- lm(y ~ poly(x, 2, raw = TRUE))
# alternatively do it 'by hand'
o.byhand <- lm(y ~ x + I(x^2))
于 2013-05-10T03:01:53.863 回答