如何使R poly()计算(或“预测”)多元新数据(正交或原始)?
使用R中的如何使R poly()计算(或“预测”)多元新数据(正交或原始)?,r,regression,polynomials,orthogonal,R,Regression,Polynomials,Orthogonal,使用R中的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 这篇文章共有4个问题,下面突出显示 我试图计算poly()-output对象(正交多项式或原始多项式)的输出。这将使我能够使用
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
- 这篇文章共有4个问题,下面突出显示
- 我试图计算
-output对象(正交多项式或原始多项式)的输出。这将使我能够使用多项式生成一行类似于我的模型矩阵的行,我可以使用它来评估结果(即,我试图通过poly()
调用推送多元测试数据值,以便可以类似于我的回归方法矩阵的行进行评估)poly()
- 我的背景:我对R、R的
和R的回归例程比较陌生李>poly()
- 我已经尝试了几种方法,并希望每种方法都能提供帮助:
(A):直接接近
预测
此方法失败,显然是由于某些意外的输入类。我知道这些特定的x1和x2值是共线的,对于一般拟合来说并不理想(我只是想让预测机器运行)。predict
的使用受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')"
> 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
> 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
(B)直接求值仅适用于原始多项式(非正交)
由于(A),我尝试了一种直接调用poly()的变通方法。对于原始多项式,我可以让它工作,但我必须为每个变量重复数据。以下显示了(1)单数据点故障,(2)重复该值成功(第2个问题)有没有办法避免重复第2个清单中的数据,使原始poly()
正确计算?
> 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')"
> 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
> 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
(C)无法从多元正交多项式中提取α和范数系数
最后,我知道有一个coefs
输入变量用于predict.poly
。我知道coefs
是正交多项式拟合输出的alpha值和范数值。然而,我只能从一元多项式拟合中提取这些。。。当我拟合多元正交(或原始)时,poly
的返回值没有coefs(Q4)对于适合多元数据的正交多项式,是否可以从调用poly()
中提取alpha
和norm
系数?
> 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')"
> 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
> 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)
我已经在r-help邮件列表上发布了相关帖子,您可以通过nabble阅读