如果线性回归模型违反重要的前提条件,比如异方差性,并且审稿人揪住不放,那该怎么办?其中一个潜在的办法是提供鲁棒标准误(robust standard error)。 install.packages("sandwich")
install.packages("lmtest")
library(sandwich)
library(lmtest)
将使用R自带的数据集mtcars作为例子,查看数据集概况:
mymodel <- lm(mpg ~ hp + wt, data = mtcars)
summary(mymodel)
coeftest(mymodel, vcov = vcovHC(mymodel, type = "HC1"))
可以修改上述的type代码选择不同的类型,比如:"HC3", "const", "HC", "HC0", "HC1", "HC2", "HC4", "HC4m", "HC5"。 vcov_robust <- vcovHC(mymodel, type = "HC1")
ro_std_errors <- sqrt(diag(vcov_robust))
ro_std_errors
好啦,今天的内容就到这里。如果有帮助,记得分享给需要的人!https://cran.r-project.org/web/packages/sandwich/index.html
https://cran.r-project.org/web/packages/lmtest/index.html
公众号核心成员担任SCI杂志Associate Editor!▌课程相关咨询可添加R师妹微信: kefu_rstats