测量两个或多个系数组合的标准误差

机器算法验证 r 多重回归
2022-03-17 07:48:53

一个愚蠢的例子。

x1 <- as.factor(c(rep("dog", 3), rep("cat", 3), rep("mouse", 3)))
x2 <- as.factor(rep(c("happy", "sad", "angry"), 3))
x3 <- rnorm(9, 0, 1) + runif(9, 3, 5)
y <- rnorm(9, 10, 2)
Call:
lm(formula = y ~ x1 + x2 + x3)

Residuals:
       1        2        3        4        5        6        7        8        9 
-1.57949  1.51090  0.06859  1.59378 -2.50472  0.91094 -0.01429  0.99383 -0.97953 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept) 19.69066    6.98359   2.820   0.0668 .
x1dog       -5.89792    3.40777  -1.731   0.1819  
x1mouse     -2.05016    3.32847  -0.616   0.5815  
x2happy      1.57757    1.99707   0.790   0.4872  
x2sad       -0.02729    2.09737  -0.013   0.9904  
x3          -1.83281    1.19873  -1.529   0.2237  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.336 on 3 degrees of freedom
Multiple R-squared:  0.6874,    Adjusted R-squared:  0.1664 
F-statistic: 1.319 on 5 and 3 DF,  p-value: 0.4362

假设我想知道“快乐狗”对 y 的边际效应。我会添加x1dogand x2happy,然后说类似“与愤怒的猫相比,快乐的狗对 y 的边际效应为 -4.32”。

我的问题是,我会对这个估计给出什么标准误差?我认为我不应该只添加两个相应的 SE。方法是什么?

谢谢!

2个回答

1)系数的线性组合a具有的标准误差,其中是系数的方差协方差矩阵。(有关随机变量的线性组合的方差,请参阅此维基百科链接。)我们可以在 R 中使用 V,因此使用末尾中的可重现代码,我们得到:(aVa)VVvcovfm

a <- c(0, 1, 0, 1, 0, 0)
c(sqrt(t(a) %*% vcov(fm) %*% a))
## [1] 2.940084

2) delta 方法delta 方法可用于获得系数的一般(可微分)非线性函数的近似标准误差当函数是线性的,就像这里一样,那么基于 delta 方法的泰勒级数近似是精确的,并给出与上面相同的结果。

library(alr3)
deltaMethod(fm, "x1dog+x2happy")
##                   Estimate       SE     2.5 %   97.5 %
## x1dog + x2happy -0.2095575 2.940084 -5.972016 5.552901

3) 一般线性假设检验假设(其中是系数向量)涉及在 t 检验的分母中使用所需的标准误差,这表明我们寻找一个函数来执行这种假设检验。事实上,使用假设运行确实会显示所需的标准误差,如下所示:aβ=0βglhtx1dog + x2happy = 0

library(multcomp)
summary(glht(fm, "x1dog + x2happy = 0"))

给出以下标准。显示的错误是 x1dog + x2 happy 的标准错误。它等于在 (1) 和 (2) 中计算的标准误差。

         Simultaneous Tests for General Linear Hypotheses

Fit: lm(formula = y ~ x1 + x2 + x3)

Linear Hypotheses:
                     Estimate Std. Error t value Pr(>|t|)
x1dog + x2happy == 0  -0.2096     2.9401  -0.071    0.948
(Adjusted p values reported -- single-step method)

笔记

我们假设输入是以下我们set.seed用来使其可重现的地方。

set.seed(123)
x1 <- as.factor(c(rep("dog", 3), rep("cat", 3), rep("mouse", 3)))
x2 <- as.factor(rep(c("happy", "sad", "angry"), 3))
x3 <- rnorm(9, 0, 1) + runif(9, 3, 5)
y <- rnorm(9, 10, 2)
fm <- lm(y ~ x1 + x2 + x3)

coef(fm)
## (Intercept)       x1dog     x1mouse     x2happy       x2sad          x3 
##  12.4103736   0.1637373  -1.3375086  -0.3732948  -1.4833220  -0.3470490 

两个相关(联合)正态变量的总和本身就是正Z=X+YN(μX+Y,σX+Y)

分布的期望是简单的和,μX+Y=μX+μY

分布的 SD 并不那么简单,因为您需要知道两个分布的协方差,但仍然可以直接计算:σX+Y=σX2+σY2+2σXY

现在我们需要的只是协方差,它可以在R中使用(对于方差-协方差矩阵)在大多数模型对象上使用。σXYvcov()

set.seed(1)
x1 <- as.factor(c(rep("dog", 3), rep("cat", 3), rep("mouse", 3)))
x2 <- as.factor(rep(c("happy", "sad", "angry"), 3))
x3 <- rnorm(9, 0, 1) + runif(9, 3, 5)
y <- rnorm(9, 10, 2)


fit <- lm(formula = y ~ x1 + x2 + x3)

covariances <- vcov(fit)
            (Intercept)       x1dog      x1mouse      x2happy       x2sad           x3
(Intercept)   7.0133778 -0.67193955 -0.534382189  0.700431114  1.72549973 -1.801643527
x1dog        -0.6719396  1.13299205  0.565017653 -0.022183489 -0.04014533  0.031569426
x1mouse      -0.5343822  0.56501765  1.131288241  0.006502645  0.01176780 -0.009253944
x2happy       0.7004311 -0.02218349  0.006502645  1.395137989  1.04334118 -0.375713718
x2sad         1.7254997 -0.04014533  0.011767798  1.043341176  1.99575847 -0.679926841
x3           -1.8016435  0.03156943 -0.009253944 -0.375713718 -0.67992684  0.534679921

所以对的期望估计是, 或.SEhappy,dogsqrt(1.133 + 1.395 + 2*-0.022)1.576