【问题标题】:How do you make R poly() evaluate (or "predict") multivariate new data (orthogonal or raw)?你如何让 R poly() 评估(或“预测”)多元新数据(正交或原始)?
【发布时间】:2021-08-24 00:47:27
【问题描述】:

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

  • 这篇博文共有 4 个问题,在下方突出显示。
  • 我正在寻求评估poly()-output 对象的输出(正交或原始多项式)。这将使我能够使用多项式生成类似于我的模型矩阵的行,我可以用它来评估结果(即,我试图通过@987654325 推送多元测试数据值@ 调用,以便它可以类似于我的回归方法矩阵的一行进行评估)。
  • 我的背景:我对 R、R 的 poly() 和 R 的回归例程比较陌生。
  • 我尝试了几种方法,希望能得到帮助:

(A):直接使用predict

此方法失败,显然是由于某些意外的输入类。我知道这些特殊的 x1 和 x2 值是共线的,并不适合一般拟合(我只是想让predict 机器运行)。 predict 的使用受到 this 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() 的解决方法。对于 raw 多项式,我可以让它工作,但我必须为每个变量重复数据。以下显示(第 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 和范数系数 最后,我知道predict.poly 有一个coefs 输入变量。我知道coefs 是正交多项式拟合输出的 alpha 和 norm 值。但是,我只能从 univariate 多项式拟合中提取那些...当我拟合 multivariate 正交(或原始)时,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

如果我能澄清一下,请告诉我。感谢您提供的任何帮助。

【问题讨论】:

标签: r regression polynomials orthogonal


【解决方案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)

【讨论】:

    【解决方案2】:

    使用 ModelMatrixModel 包。 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
    

    【讨论】:

      猜你喜欢
      • 2018-12-06
      • 2018-03-10
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多