Why is `poly` complaining about degree less than n

2020-07-03 06:48发布

问题:

I am trying to generate orthogonal polynomials in R, but I keep getting an error I don't understand

> poly(1:1000, 50)
Error in poly(1:1000, 50) : 
  'degree' must be less than number of unique points

Surely the number of unique points is 1000? What does it mean? Is this a bug, and if so does anyone know I work around?

Edit: This appears to kick in for degree > 27 for any number of points - is this an undocumented limit?

回答1:

Numerical overflow. If you look at the code for poly, you'll see it's generating the individual polynomial terms as an intermediate step:

X <- outer(x, seq_len(n) - 1, "^")

and when n (the degree of the polynomial you want) is 50, the resulting terms go up to 1e132.