如何使 R poly() 评估(或 "predict")多元新数据(正交或原始数据)?

How do you make R poly() evaluate (or "predict") multivariate new data (orthogonal or raw)?

使用 R 中的 poly 函数,我如何 计算 多元多项式?


(A):直接使用 predict

此方法失败,显然是由于一些意外的 class 输入。我知道这些特定的 x1 和 x2 值是共线的,对于一般拟合来说并不理想(我只是想让 predict 机器运行)。 predict 的使用受到 this SO post 的启发。 (Q1) 是否可以直接调用predict方法来计算这个多项式?

> 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) 直接评估仅适用于原始多项式(非正交)

由于 (A),我尝试了一种直接调用 poly() 的解决方法。对于 raw 多项式,我可以让它工作,但我必须为每个变量重复数据。以下显示(1st)单个数据点的失败,(2nd)重复值的成功。 (Q2) 有什么方法可以避免第二个列表中数据的冗余重复,从而使原始 poly() 正确评估?

> 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

如果我使用 正交 多项式尝试类似的冗余列表数据方法,我会产生 "hey, your data's redundant!" 错误(如果我只列出每个变量的值一次)。 (Q3) 是否可以通过直接调用 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) 无法从多元正交多项式中提取 alpha 和范数系数 最后,我知道 predict.poly 有一个 coefs 输入变量。我知道 coefs 是从正交多项式拟合输出的 alpha 和范数值。但是,我只能从 单变量 多项式拟合中提取那些...当我拟合 多变量 正交(或原始)时,return 来自 poly 的值没有系数。 (Q4) 是否可以从调用 poly() 中提取 alphanorm 系数,用于正交多项式拟合 多元 数据?

> 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

如果我可以澄清,请告诉我。非常感谢您提供的任何帮助。

郑重声明,这似乎已经修复

> 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)

使用 ModelMatrixModel 包。 ModelMatrixModel() in 类似于model.matrix(),但它保存了转换参数,可以应用新数据。

library(ModelMatrixModel) 
traindf=data.frame(x1 = seq(1,  10,  by=0.2),
                   x2 = seq(1.1,10.1,by=0.2))

mm=ModelMatrixModel(~poly(x1,2)+poly(x2,3),traindf,sparse=F)
mm$x[1:2,] #output matrix
##   poly_x1__2_1 poly_x1__2_2 poly_x2__3_1 poly_x2__3_2 poly_x2__3_3
## 1   -0.2498843    0.3088653   -0.2498843    0.3088653   -0.3423492
## 2   -0.2387784    0.2676833   -0.2387784    0.2676833   -0.2510561
predict(mm,traindf[1:2,])$x
##   poly_x1__2_1 poly_x1__2_2 poly_x2__3_1 poly_x2__3_2 poly_x2__3_3
## 1   -0.2498843    0.3088653   -0.2498843    0.3088653   -0.3423492
## 2   -0.2387784    0.2676833   -0.2387784    0.2676833   -0.2510561