R中系数二阶多项式的解释

问题描述

我使用 R 中的 lm 和 poly 函数制作了一个趋势曲面。 我的结果总结如下:

> summary(tls.lm)

Call:
lm(formula = DEM_small_domain_TLS_5cm ~ poly(x,y,degree = 2),data = tlsDF)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.09543 -0.01378 -0.00087  0.01260  0.10404 

Coefficients:
                           Estimate Std. Error  t value Pr(>|t|)    
(Intercept)                2.08e+01   4.98e-05 416945.4   <2e-16 ***
poly(x,degree = 2)1.0 -1.51e+01   2.00e-02   -754.0   <2e-16 ***
poly(x,degree = 2)2.0  6.79e-01   2.00e-02     33.9   <2e-16 ***
poly(x,degree = 2)0.1 -2.01e+01   2.00e-02  -1007.3   <2e-16 ***
poly(x,degree = 2)1.1 -7.17e+02   8.04e+00    -89.2   <2e-16 ***
poly(x,degree = 2)0.2 -6.02e-01   2.00e-02    -30.1   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.02 on 161446 degrees of freedom
Multiple R-squared:  0.908,Adjusted R-squared:  0.908 
F-statistic: 3.19e+05 on 5 and 161446 DF,p-value: <2e-16

如何将系数转换为方程?我尝试了以下方法:

截距 + coeff1.0 * x + coeff2.0 * x^2 +coeff1.1 * x * y + coeff 0.1 * y + coeff 0.2 * y^2

我也尝试过替换上面公式中的 x 和 y,但是用一些提取的 x 和 y 值填充这些方程的结果与(正确构造的)趋势面结果完全不同。

编辑 在下图中,显示了 poly(...,raw=F) 和 poly(...,raw=T) 构建的趋势表面之间的差异。建议我用raw=T,不过我更喜欢raw=F的曲面。还有办法理解上面的系数吗? Poly_raw_difference

解决方法

按照@Rui 的建议,您必须将 raw=T 作为参数添加到 poly:这里是一个带有内置数据集 mtcars 的示例。

data("mtcars")
fit <- lm(formula = mpg ~ poly(disp,hp,degree = 2,raw = T),data = mtcars)
summary(fit)
#coefficient of regression
coef <- coef(fit)
#use coefficient for predict outcome
p <- function(coef,x,y) {
  coef[1]+coef[2]*x+coef[3]*x^2+coef[4]*y+coef[5]*x*y+coef[6]*y^2
}

p1 <- round(p(coef,mtcars$disp,mtcars$hp),11)
p2 <- round(unname(predict(fit,mtcars[,3:4])),11)
identical(p1,p2)

两个向量相同,小数点后 11 位

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...