R代码可通过一次回归测试回归系数之间的差异

joh*_*zhj 4 r regression-testing

我想测试一种线性回归中的系数是否彼此不同,或者它们中至少有一个与一个特定值(例如0)显着不同,这在Stata中似乎很直观。例如

webuse iris reg iris seplen sepwid petlen seplen==sepwid==petlen seplen==sepwid==petlen==0

我想知道如果要在R中测试它怎么办?

Car*_*lli 5

car软件包具有执行此操作的简单功能。

首先,拟合您的模型:

model <- lm(Sepal.Length ~ Sepal.Width + Petal.Length, data = iris)
Run Code Online (Sandbox Code Playgroud)

比您可以使用linearHypothesis函数测试不同的线性假设,例如:

library(car)

# tests if the coefficient of Sepal.Width = Petal.Length
linearHypothesis(model, "Sepal.Width = Petal.Length")
Linear hypothesis test

Hypothesis:
Sepal.Width - Petal.Length = 0

Model 1: restricted model
Model 2: Sepal.Length ~ Sepal.Width + Petal.Length

  Res.Df    RSS Df Sum of Sq      F  Pr(>F)  
1    148 16.744                              
2    147 16.329  1    0.4157 3.7423 0.05497 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

# Other examples:
# tests if the coefficient of Sepal.Width = 0
linearHypothesis(model, "Sepal.Width = 0")

# tests if the coefficient of Sepal.Width = 0.5
linearHypothesis(model, "Sepal.Width = 0.5")

# tests if both are equal to zero
linearHypothesis(model, c("Sepal.Width = 0", "Petal.Length = 0"))
Run Code Online (Sandbox Code Playgroud)