在R中使用PLM对象执行稳健F检验时遇报错求助
问题:在R中使用plm对象运行稳健F检验时出错
代码与错误信息
运行的代码:
library('wooldridge') data(wagepan,package='wooldridge') library('plm') pdata<-pdata.frame(wagepan,index=c("nr","year")) fdreg<-plm(lwage~educ*(d81+d82+d83+d84+d85+d86+d87)+union,data=pdata,model="fd") summary(fdreg) library('car') H0<-matchCoefs(fdreg,"educ") linearHypothesis(fdreg,H0) linearHypothesis(fdreg,H0,vcov=vcovHC(fdreg,"HC1"))
触发的错误:
'arg' should be one of “arellano”, “white1”, “white2”
错误原因
plm包内置的vcovHC()函数,针对一阶差分模型(model="fd")时,仅支持"arellano"、"white1"、"white2"这三种方差估计类型,你传入的"HC1"不在允许列表内,因此抛出错误。
解决方法
- 方法1:使用plm支持的方差类型
直接替换为plm允许的参数值,比如"white1"或"white2",修改后的代码:
linearHypothesis(fdreg, H0, vcov = vcovHC(fdreg, "white1"))
- 方法2:用sandwich包实现HC1稳健方差
如果必须使用HC1类型的稳健方差,可以调用sandwich包的vcovHC()函数(注意区分plm包同名函数),代码如下:
library(sandwich) # 计算HC1类型的稳健方差矩阵 hc1_vcov <- vcovHC(fdreg, type = "HC1") # 传入linearHypothesis linearHypothesis(fdreg, H0, vcov = hc1_vcov)
内容的提问来源于stack exchange,提问作者AKJ
相关产品推荐
相关产品推荐

