# How to manually compute response variable using regression with poly [duplicate]

I believe I can manually compute response values from coefficients obtained using 'raw' polynomial predictor variables.

Example R code is

x <- seq(-2,2,length=100)

# true function
y <- 1.2*x^2 + 2.3*x + 3.4

eps <- rnorm(100, 0, 1)
y <- y + eps

df <- data.frame(x=x, y=y)

# fit quadratic to noisy data points
qfit <- lm(y ~ x + I(x^2), data=df)

# manual predictions
xmat <- cbind(1, x, x^2)
yhat <- xmat %*% qfit\$coeff


This yields the coefficients:

Coefficients:
(Intercept)            x       I(x^2)
3.385        2.362        1.277


I could port that manual calculation to another programming language.

However, it appears using the function 'poly' is preferred on account of the correlation between x and x^2 in the above model, leading to the new fit

newfit <- lm( y ~ poly(x, degree=2), data=df)


and this yields different coefficients:

     (Intercept) poly(x, degree = 2)1 poly(x, degree = 2)2
5.122729            27.549886            15.536953


Questions:

The first question is whether it is really better or even necessary that poly with its orthogonal polynomials is used instead of raw predictors?

The second question is how to manually compute responses using the coefficients from the regression using the poly function? I need to do this because the model will be implemented in another program in another language.

• Your second question is answered at stats.stackexchange.com/questions/31858. – whuber Oct 1 '18 at 16:54
• @whuber Thanks. I appreciate you pointing that out. – PM. Oct 1 '18 at 17:02
• A search on stats.stackexchange.com/questions/258307/… found answers to your first question. – whuber Oct 1 '18 at 17:04
• @whuber Thanks once again. Do you think the question should be deleted? I'm inclined not to delete it. I feel it is somewhat useful as a signpost to the other answers. I did not find them but admittedly the first question was unplanned and I only really searched for an answer to the second question, yet failed despite trying multiple combinations of relevant keywords (manual, fitted, predicted, response, poly iirc). – PM. Oct 1 '18 at 21:14
• You don't have to delete it. I found the duplicates by searching our site for poly. I can't remember the exact search (because in my comment I inadvertently pasted a link to a thread rather than to the search), but it would have been a variation of stats.stackexchange.com/search?q=poly+score%3A1+is%3Aanswer. – whuber Oct 1 '18 at 21:18