R:使用mousesadds lm.cluster聚集的鲁棒标准错误-带有子集和权重的错误 [英] R: Clustered robust standard errors using miceadds lm.cluster - error with subset and weights
问题描述
我正在尝试在mouseadds包中使用lm.cluster函数,以为乘插补数据集获得可靠的聚类标准误差.
I am trying to use the lm.cluster function in the package miceadds to get robust clustered standard errors for a multiply imputed dataset.
我能够运行它的标准版本,但是当我尝试添加一个子集或权重时出现以下错误:
I am able to get the standard version of it to run but I get the following error when I try to add a subset or weights:
Error in eval(substitute(subset), data, env) :
..1 used in an incorrect context, no ... to look in
没有子集或权重的示例:
Example that works without subset or weights:
require("mice")
require("miceadds")
data(data.ma01)
# imputation of the dataset: use six imputations
dat <- data.ma01[ , - c(1:2) ]
imp <- mice::mice( dat , maxit=3 , m=6 )
datlist <- miceadds::mids2datlist( imp )
# linear regression with cluster robust standard errors
mod <- lapply(datlist, FUN = function(data){miceadds::lm.cluster( data=data ,
formula=read ~ paredu+ female , cluster = data.ma01$idschool )} )
# extract parameters and covariance matrix
betas <- lapply( mod , FUN = function(rr){ coef(rr) } )
vars <- lapply( mod , FUN = function(rr){ vcov(rr) } )
# conduct statistical inference
summary(pool_mi( qhat = betas, u = vars ))
以子集结尾的示例:
mod <- lapply(datlist, FUN = function(data){miceadds::lm.cluster( data=data ,
formula=read ~ paredu+ female , cluster = data.ma01$idschool, subset=
(data.ma01$urban==1))} )
Error during wrapup: ..1 used in an incorrect context, no ... to look in
以重量计的示例:
mod <- lapply(datlist, FUN = function(data){miceadds::lm.cluster( data=data ,
formula=read ~ paredu+ female , cluster = data.ma01$idschool,
weights=data.ma01$studwgt)} )
Error during wrapup: ..1 used in an incorrect context, no ... to look in
通过搜索,我认为通过lm或glm包装器传递这些命令时遇到了与其他人类似的问题(例如: R:在R函数内部将参数传递给glm 或
From searching, I think I am encountering similar issues as others when passing these commands through an lm or glm wrapper (such as: Passing Argument to lm in R within Function or R : Pass argument to glm inside an R function or Passing the weights argument to a regression function inside an R function)
但是,我不确定如何使用估算的数据集解决该问题.现有的lm.cluster命令.
However, I am not sure how to address the issue with the imputed datasets & existing lm.cluster command.
谢谢
推荐答案
这与 estimatr CRAN上的包和estimatr::lm_robust()
函数.有两个注意事项:(1)您可以使用se_type =
更改标准错误的类型,并且(2)我将idschool
保留在数据中,因为我们希望聚类与适合模型的聚类在相同的data.frame
中.
This works fine with the estimatr package which is on CRAN and the estimatr::lm_robust()
function. Two notes: (1) you can change the type of standard errors using se_type =
and (2) I keep idschool
in the data because we like the clusters to be in the same data.frame
as we fit the model on.
library(mice)
library(miceadds)
library(estimatr)
# imputation of the dataset: use six imputations
data(data.ma01)
dat <- data.ma01[, -c(1)] # note I keep idschool in data
imp <- mice::mice( dat , maxit = 3, m = 6)
datlist <- miceadds::mids2datlist(imp)
# linear regression with cluster robust standard errors
mod <- lapply(
datlist,
function (dat) {
estimatr::lm_robust(read ~ paredu + female, dat, clusters = idschool)
}
)
# subset
mod <- lapply(
datlist,
function (dat) {
estimatr::lm_robust(read ~ paredu + female, dat, clusters = idschool, subset = urban == 1)
}
)
# weights
mod <- lapply(
datlist,
function (dat) {
estimatr::lm_robust(read ~ paredu + female, dat, clusters = idschool, weights = studwgt)
}
)
# note that you can use the `se_type` argument of lm_robust()
# to change the vcov estimation
# extract parameters and covariance matrix
betas <- lapply(mod, coef)
vars <- lapply(mod, vcov)
# conduct statistical inference
summary(pool_mi( qhat = betas, u = vars ))
这篇关于R:使用mousesadds lm.cluster聚集的鲁棒标准错误-带有子集和权重的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!