Isolating coefficients from polynomial fit in r -
i have fitted simple 2nd order polynomial time series data in following form:
polyfit <- lm(y ~ poly(x,2))
i wish extract respective coefficients (a, b , c) fitted polynomial of form y = ax^2 + bx + c. naturally thought answer found in polyfit$coefficients within polyfit object these coefficients not correct. have tried simple data sets , compared excel , whilst poly curve fits identical in r , excel, a,b , c coefficints obtained excel correct obtained polyfit object arent? have extracted incorrect information polyfit object? more convenient me extract coefficients directly r purposes? can help?
by default poly
fits orthogonal polynomials. uses following ideas...
x <- 1:10 # in case same x-mean(x) y1 <- residuals(lm(x ~ 1)) # normalize 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)
to construct set of orthonormal vectors still produce same predictions. if want polynomial in regular way want specify raw=true
parameter.
y <- rnorm(20) x <- 1:20 o <- lm(y ~ poly(x, 2, raw = true)) # alternatively 'by hand' o.byhand <- lm(y ~ x + i(x^2))
Comments
Post a Comment