你如何使R poly()评估(或"预测")多变量新数据(正交或原始)?

bla*_*ake 5 regression r orthogonal polynomials

使用polyR中的函数,如何评估多元多项式?

  • 这篇文章共有4个问题,下面重点介绍.
  • 我正在寻求评估poly()输出对象(正交或原始多项式)的输出.这样我就可以使用多项式生成一个类似于我的模型矩阵的行,我可以用它来评估结果(即,我试图通过调用推送多变量测试数据值,poly()以便可以类似于我的回归方法矩阵的一行).
  • 我的背景:我对R,R poly()和R的回归程序相对较新.
  • 我已经尝试了几种方法,并希望对每种方法都有所帮助:

(A):直接接近 predict

这种方法失败了,显然是由于一些意想不到的输入类别.我知道这些特殊的x1和x2值是共线的,对于一般的拟合来说并不理想(我只是想让predict机器运转起来).使用的predict灵感来自这篇 SO帖子. (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')"
Run Code Online (Sandbox Code Playgroud)

(B)直接评估仅适用于原始多项式(非正交)

由于(A),我尝试了直接调用poly()的变通方法.对于原始多项式,我可以让它工作,但我必须为每个相应的变量重复数据.以下显示(第一个)单个数据点的失败,(第二个)重复该值的成功. (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
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用正交多项式的类似冗余列表数据方法,我会招致"嘿,你的数据是多余的!" 错误(如果我只列出每个变量的值一次,我也会招致).(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
Run Code Online (Sandbox Code Playgroud)

(C)无法从多元正交多项式中提取alpha和范数系数 最后,我知道有一个coefs输入变量predict.poly.我理解这coefs是从正交多项式拟合输出的alpha和norm值.但是,我只能从单变量多项式拟合中提取...当我拟合多元正交(或原始)时,返回值poly不具有coefs. (Q4)是否有可能从调用正交多项式拟合到多变量数据中提取alphanorm系数?poly()

> 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
Run Code Online (Sandbox Code Playgroud)

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

Ben*_*sen 1

根据记录,这似乎已被修复

> 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)
Run Code Online (Sandbox Code Playgroud)