测试 R 中多个系数的相等性
Testing the equality of multiple coefficients in R
我有以下型号:
y = b1_group1*X1 + b1_group2*X1 + b2_group1*X2 + b2_group2*X2 + ... +
b10_group1*X10 + b10_group2*X10
在R中很容易制作如下:
OLS <- lm(Y ~ t1:Group + t2:Group + t3:Group + t4:Group + t5:Group + t6:Group +
t7:Group + t8:Group + t9:Group + t10:Group,weights = weight, Alldata)
在 STATA 中,我现在可以进行以下测试:
test (b1_group1=b1_group2) (b2_group1=b2_group2) (b3_group1=b3_group2)
- b1_group1 - b1_group2 = 0
- b2_group1 - b2_group2 = 0
- b3_group1 - b3_group2 = 0
这通过 F 检验告诉我来自 X1、X2 和 X3 的系数组是否在第 1 组和第 2 组之间共同不同。
有人可以告诉我如何在 R 中执行此操作吗?谢谢!
看这个例子:
library(car)
mod <- lm(mpg ~ disp + hp + drat*wt, mtcars)
linearHypothesis(mod, c("disp = hp", "disp = drat", "disp = drat:wt" ))
Linear hypothesis test
Hypothesis:
disp - hp = 0
disp - drat = 0
disp - drat:wt = 0
Model 1: restricted model
Model 2: mpg ~ disp + hp + drat * wt
Res.Df RSS Df Sum of Sq F Pr(>F)
1 29 211.80
2 26 164.67 3 47.129 2.4804 0.08337 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
有关指定测试的各种其他方法,请参阅 ?linearHypothesis
。
选择:
以上内容向您展示了一种快速简便的假设检验方法。对假设检验的代数有深入了解的用户可能会发现以下方法更方便,至少对于简单版本的检验而言。假设我们要测试 cyl
和 carb
上的系数是否相同。
mod <- lm(mpg ~ disp + hp + cyl + carb, mtcars)
以下测试是等效的:
测试一:
linearHypothesis(mod, c("cyl = carb" ))
Linear hypothesis test
Hypothesis:
cyl - carb = 0
Model 1: restricted model
Model 2: mpg ~ disp + hp + cyl + carb
Res.Df RSS Df Sum of Sq F Pr(>F)
1 28 238.83
2 27 238.71 1 0.12128 0.0137 0.9076
测试二:
rmod<- lm(mpg ~ disp + hp + I(cyl + carb), mtcars)
anova(mod, rmod)
Analysis of Variance Table
Model 1: mpg ~ disp + hp + cyl + carb
Model 2: mpg ~ disp + hp + I(cyl + carb)
Res.Df RSS Df Sum of Sq F Pr(>F)
1 27 238.71
2 28 238.83 -1 -0.12128 0.0137 0.9076
我有以下型号:
y = b1_group1*X1 + b1_group2*X1 + b2_group1*X2 + b2_group2*X2 + ... +
b10_group1*X10 + b10_group2*X10
在R中很容易制作如下:
OLS <- lm(Y ~ t1:Group + t2:Group + t3:Group + t4:Group + t5:Group + t6:Group +
t7:Group + t8:Group + t9:Group + t10:Group,weights = weight, Alldata)
在 STATA 中,我现在可以进行以下测试:
test (b1_group1=b1_group2) (b2_group1=b2_group2) (b3_group1=b3_group2)
- b1_group1 - b1_group2 = 0
- b2_group1 - b2_group2 = 0
- b3_group1 - b3_group2 = 0
这通过 F 检验告诉我来自 X1、X2 和 X3 的系数组是否在第 1 组和第 2 组之间共同不同。
有人可以告诉我如何在 R 中执行此操作吗?谢谢!
看这个例子:
library(car)
mod <- lm(mpg ~ disp + hp + drat*wt, mtcars)
linearHypothesis(mod, c("disp = hp", "disp = drat", "disp = drat:wt" ))
Linear hypothesis test
Hypothesis:
disp - hp = 0
disp - drat = 0
disp - drat:wt = 0
Model 1: restricted model
Model 2: mpg ~ disp + hp + drat * wt
Res.Df RSS Df Sum of Sq F Pr(>F)
1 29 211.80
2 26 164.67 3 47.129 2.4804 0.08337 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
有关指定测试的各种其他方法,请参阅 ?linearHypothesis
。
选择:
以上内容向您展示了一种快速简便的假设检验方法。对假设检验的代数有深入了解的用户可能会发现以下方法更方便,至少对于简单版本的检验而言。假设我们要测试 cyl
和 carb
上的系数是否相同。
mod <- lm(mpg ~ disp + hp + cyl + carb, mtcars)
以下测试是等效的:
测试一:
linearHypothesis(mod, c("cyl = carb" ))
Linear hypothesis test
Hypothesis:
cyl - carb = 0
Model 1: restricted model
Model 2: mpg ~ disp + hp + cyl + carb
Res.Df RSS Df Sum of Sq F Pr(>F)
1 28 238.83
2 27 238.71 1 0.12128 0.0137 0.9076
测试二:
rmod<- lm(mpg ~ disp + hp + I(cyl + carb), mtcars)
anova(mod, rmod)
Analysis of Variance Table
Model 1: mpg ~ disp + hp + cyl + carb
Model 2: mpg ~ disp + hp + I(cyl + carb)
Res.Df RSS Df Sum of Sq F Pr(>F)
1 27 238.71
2 28 238.83 -1 -0.12128 0.0137 0.9076