rpoly

Is there an inverse function for poly?


We can generate orthogonal polynomials in R with pp <- poly(cars$speed, 2) Is there a way to get the original values out of the result pp (inverse poly function)?

In other words, what should the function f look like that returns the following result:

f(poly(cars$speed, 2)) == cars$speed?


Solution

  • cars$speed must be of the form a + b * pp[, 1] for some scalars a and b and knowing that the coefs attribute of poly objects contains values which can be used for reconstruction we find the following reconstruction of cars$speed as speed.

    pp <- poly(cars$speed, 2)
    speed <- with(attr(pp, "coefs"), alpha[1] + sqrt(norm2)[3] * pp[, 1])
    
    all.equal(speed, cars$speed)
    ## [1] TRUE