You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否用marginaleffects包的hypotheses函数进行参数联合检验?

使用marginaleffects::hypotheses实现联合检验

可以用marginaleffects::hypotheses()函数实现你需要的联合F检验、似然比检验(LRT)和Wald检验,功能完全对标car::linearHypothesis()和multcomp::glht()。以下是对应你示例代码的实现:

基础准备

首先加载包并拟合模型:

library(marginaleffects)

lmfit <- lm(mpg ~ as.factor(cyl) * hp, data = mtcars)

1. 联合F检验

对应car::linearHypothesis(..., test = "F")和multcomp::glht(..., test = Ftest()):

hypotheses(lmfit,
           hypothesis = c("as.factor(cyl)6:hp = 0",
                         "as.factor(cyl)8:hp = 0"),
           test = "F")

2. Wald卡方检验

对应car::linearHypothesis(..., test = "Chisq")和multcomp::glht(..., test = Chisqtest()):

# 两种写法效果一致,默认test为"Wald"
hypotheses(lmfit,
           hypothesis = c("as.factor(cyl)6:hp = 0",
                         "as.factor(cyl)8:hp = 0"),
           test = "Wald")

# 也可以直接指定test="Chisq"
hypotheses(lmfit,
           hypothesis = c("as.factor(cyl)6:hp = 0",
                         "as.factor(cyl)8:hp = 0"),
           test = "Chisq")

3. 似然比检验(LRT)

marginaleffects直接支持似然比检验,只需指定test = "LRT":

hypotheses(lmfit,
           hypothesis = c("as.factor(cyl)6:hp = 0",
                         "as.factor(cyl)8:hp = 0"),
           test = "LRT")

额外说明

  • 如果是更复杂的线性假设,可以将hypothesis参数设为矩阵形式(行对应假设,列对应模型系数)。
  • test参数的可选值包括"F"、"Wald"/"Chisq"、"LRT",分别对应三种检验类型。

内容的提问来源于stack exchange,提问作者Stefan Hansen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 17:02:41