首页 > 解决方案 > 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, y, degree = 2)1.0 -1.51e+01   2.00e-02   -754.0   <2e-16 ***
poly(x, y, degree = 2)2.0  6.79e-01   2.00e-02     33.9   <2e-16 ***
poly(x, y, degree = 2)0.1 -2.01e+01   2.00e-02  -1007.3   <2e-16 ***
poly(x, y, degree = 2)1.1 -7.17e+02   8.04e+00    -89.2   <2e-16 ***
poly(x, y, 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

标签: rregressionlmpolynomialspoly

解决方案


正如@Rui 所建议的,您必须将raw=T其作为参数添加到poly:here an example with the built-in dataset 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)

这 2 个向量在小数点后 11 位是相同的


推荐阅读