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

5

如何使用R中的poly函数来评估多元多项式?

  • 本篇文章共有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')"

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

由于(A),我尝试了一个解决方法,直接调用poly()。对于原始多项式,我可以让它工作,但我必须为每个相应的变量重复数据。下面展示了(1)单个数据点的失败和(2)通过重复值成功的情况。(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

如果我尝试使用类似的冗余列出数据的方法来处理正交多项式,那么我会遇到“嘿,你的数据是冗余的!”的错误提示(如果我只列出每个变量的值,也会出现这个错误)。(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和norm系数 最后,我知道在predict.poly有一个coefs输入变量。我理解coefs应该是从正交多项式拟合输出的alpha和norm值。然而,当我进行多元正交(或原始)拟合时,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

请让我知道如果我需要澄清。非常感谢您所提供的任何帮助。


1
我已经在r-help邮件列表上发布了一篇相关的帖子,您可以通过nabble阅读http://r.789695.n4.nabble.com/predict-poly-for-multivariate-data-tp4709718.html。 - user20637
2个回答

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)

0
使用ModelMatrixModel包。ModelMatrixModel()函数类似于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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接