How do you make R poly () evaluate (or “predict”) multidimensional new data (orthogonal or raw)?

Using the poly function in R, how can I evaluate a multidimensional polynomial?

  • This post contains 4 questions highlighted below.
  • I want to evaluate the output of a poly() -output object (orthogonal or raw polynomials). This would give me the opportunity to use polynomials to generate a string similar to that of my model, which I can use to evaluate the result (i.e. I'm trying to push multidimensional values ​​of test data through a call to poly() so that it can be evaluated in the same way row of my regression method matrix).
  • My experience: I am relatively new to R, R poly() and R regression procedures.
  • I tried several approaches and would like to help with each:

(A): direct approach with predict

This method is CORRECT, apparently due to an unexpected input class. I know that these specific values ​​of x1 and x2, which are collinear, are not ideal for a general fit (I'm just trying to get the machine predict to work). Using predict was inspired by this SO post. (Q1) Is it possible to directly call the predict method to evaluate this polynomial?

 > x1 = seq(1, 10, by=0.2) > x2 = seq(1.1,10.1,by=0.2) > t = poly(cbind(x1,x2),degree=2,raw=T) > predict(t,newdata=data.frame(x1=2.03,x2=2.03)) Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "c('matrix', 'double', 'numeric')" 

(B) Direct estimation only works for source polynomials (not orthogonal)

Due to (A), I tried a workaround with a direct call to poly (). For the original polynomials, I could make it work, but I had to repeat the data for each corresponding variable. After displaying the (1st) failure with a single data point (2nd), success with repeating the value. (Q2) Is there a way to avoid redundant data repetition in the second listing to make raw poly() properly priced?

 > poly(cbind(x1=c(2.03),x2=c(2.13)),degree=2,raw=T) Error in `colnames<-`(`*tmp*`, value = apply(z, 1L, function(x) paste(x, : attempt to set 'colnames' on an object with less than two dimensions > poly(cbind(x1=c(2.03,2.03),x2=c(2.13,2.13)),degree=3,raw=T) 1.0 2.0 3.0 0.1 1.1 2.1 0.2 1.2 0.3 [1,] 2.03 4.1209 8.365427 2.13 4.3239 8.777517 4.5369 9.209907 9.663597 [2,] 2.03 4.1209 8.365427 2.13 4.3239 8.777517 4.5369 9.209907 9.663597 attr(,"degree") [1] 1 2 3 1 2 3 2 3 3 

If I try a similar approach with redundantly-listed data with orthogonal polynomials, I assume "hey, your data is redundant!" error (which also occurs if I only list each variable value once). (Q3) Is it possible to evaluate multidimensional orthogonal polynomials by directly calling poly() ?

 > poly(cbind(x1=c(2.03, 2.03),x2=c(2.13, 2.13)),degree=2) Error in poly(dots[[1L]], degree, raw = raw) : 'degree' must be less than number of unique points 

(C) Inability to extract alpha coefficients and norms from multidimensional orthogonal polynomials Finally, I know that there is an input variable coefs in predict.poly . I understand that coefs should be alpha and norm values ​​derived from orthogonal polynomial correspondence. However, I can only extract them from the one-dimensional polynomial ... when I come to multi-dimensional orthogonal (or unprocessed), the return value from poly does NOT have coefficients. (Q4) Is it possible to extract the coefficients alpha and norm from the call poly() for an orthogonal polynomial suitable for multidimensional data?

 > t = poly(cbind(x1),degree=2) # univariate orthog poly --> WORKS > attributes(t)$coefs $alpha [1] 5.5 5.5 $norm2 [1] 1.000 46.000 324.300 1826.458 > t = poly(cbind(x1,x2),degree=2) # multivariate orthog poly --> DOES NOT WORK > attributes(t)$coefs NULL 

Please let me know if I can clarify. Thank you for any help you can provide.

+5
source share
1 answer

For the record, this seems to be fixed.

 > x1 = seq(1, 10, by=0.2) > x2 = seq(1.1,10.1,by=0.2) > t = poly(cbind(x1,x2),degree=2,raw=T) > > class(t) # has a class now [1] "poly" "matrix" > > # does not throw error > predict(t, newdata = cbind(x1,x2)[1:2, ]) 1.0 2.0 0.1 1.1 0.2 [1,] 1.0 1.00 1.1 1.10 1.21 [2,] 1.2 1.44 1.3 1.56 1.69 attr(,"degree") [1] 1 2 1 2 2 attr(,"class") [1] "poly" "matrix" > > # and gives the same > t[1:2, ] 1.0 2.0 0.1 1.1 0.2 [1,] 1.0 1.00 1.1 1.10 1.21 [2,] 1.2 1.44 1.3 1.56 1.69 > > sessionInfo() R version 3.4.1 (2017-06-30) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows >= 8 x64 (build 9200) 
+1
source

All Articles