0
$\begingroup$

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

# add noise
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.

$\endgroup$
5
  • 1
    $\begingroup$ Your second question is answered at stats.stackexchange.com/questions/31858. $\endgroup$
    – whuber
    Oct 1, 2018 at 16:54
  • 1
    $\begingroup$ @whuber Thanks. I appreciate you pointing that out. $\endgroup$
    – PM.
    Oct 1, 2018 at 17:02
  • 1
    $\begingroup$ A search on stats.stackexchange.com/questions/258307/… found answers to your first question. $\endgroup$
    – whuber
    Oct 1, 2018 at 17:04
  • $\begingroup$ @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). $\endgroup$
    – PM.
    Oct 1, 2018 at 21:14
  • 1
    $\begingroup$ 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. $\endgroup$
    – whuber
    Oct 1, 2018 at 21:18

0

Browse other questions tagged or ask your own question.