2

This is a generalization of Isolating coefficients from polynomial fit in r (which does not cover the case of polynomials) and (in a sense) a duplicate of Isolating coefficients from polynomial fit in r (which, after 4 years, only has a partial answer/"workaround"; see my remark about raw = TRUE below).

Suppose I have a polynomial fit:

l <- lm(mpg ~ poly(cyl, disp, hp, degree = 2), mtcars)
summary(l)

Is there an easy way to obtain the polynomial formula behind it?

I understand that poly works with orthogonal polynomials and that the option raw = TRUE will work with monomials. Thus, with raw = TRUE it is much easier to extract a formula. However, I have been told that raw = TRUE will, in general, give a different (worse?) fit.

jarauh
  • 1,836
  • 22
  • 30
  • You say that the second cited post has no accepted answer after 4 years. That is technically true. However, the OP made a comment that one of the answers "works perfectly". I think that could be counted as an answer. – G5W Dec 21 '17 at 13:39
  • @G5W Agreed, I will reformulate – jarauh Dec 21 '17 at 14:20
  • I guess, I don't understand what exactly do we mean by *the coefficients of the polynomial*? – storaged Dec 21 '17 at 16:04
  • The coefficients of `x - 2 x^2` are 1 and -2. But if `lm` returns `poly(x, degree = 2)1 = 1` and `poly(x, degree = 2)2 = -2`, then these coefficients are not the coefficients that appear in front of `x` or `x^2`. Instead, everything depends on the data: `poly` (or `lm`?) constructs orthogonal polynomials of degree 1 and 2, and the final polynomial is a combination of these orthogonal polynomials, with certain coefficients. It is these coefficients that are reported. – jarauh Dec 21 '17 at 17:02

0 Answers0