【发布时间】:2014-11-30 22:19:09
【问题描述】:
我正在对集群数据运行以下加权回归,并且我不断使用 clx 函数为我的集群标准错误获取 NA。 请注意,我使用的是 na.action = na.exclude ,原因是我最终将每个观察值的残差附加到数据帧以进行进一步操作。
#partialing the data
partialData <- data[data$group_code2 %in% group_list,]
#running the regression and calculating clustered standard errors
reg1 <- lm(employed ~ age + eduction , data=partialData, weights = w, na.action=na.exclude)
clx(fm=reg1, dfcw = 1, cluster= partialData$group_code2)
clx <- function(fm, dfcw, cluster){
# R-codes (www.r-project.org) for computing
# clustered-standard errors. Mahmood Arai, Jan 26, 2008.
# The arguments of the function are:
# fitted model, cluster1 and cluster2
# You need to install libraries `sandwich' and `lmtest'
# reweighting the var-cov matrix for the within model
library(sandwich);library(lmtest)
M <- length(unique(cluster))
N <- length(cluster)
K <- fm$rank
dfc <- (M/(M-1))*((N-1)/(N-K))
uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)*dfcw
coeftest(fm, vcovCL) }
我最初怀疑 na.exclude 给我带来了麻烦,因为一些残差的值会是 NA。那么有没有办法让 clx 忽略集群和对象 reg1 中的 NA?
请注意,我了解我可以在运行任何回归之前从数据集中删除不完整的案例,这将解决问题,但我正在避免这种情况,因为我正在尝试许多可能的规范(因此我的协变量不断变化)我试图避免为每组可能的协变量更改代码。
【问题讨论】:
标签: r