【问题标题】:clx returns NA when used on lm with na.action=na.exclude在 lm 上使用 na.action=na.exclude 时,clx 返回 NA
【发布时间】: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


    【解决方案1】:

    很难给你一个有用的答案,因为你的例子是不可重现的。但答案的症结在于,Mahmood Arai 的集群标准错误的原始代码不处理 NA。

    在这种情况下,您可以使用 multiwayvcov 包,它应该可以处理您的情况:

    require(lmtest)
    require(multiwayvcov)
    
    reg1 <- lm(employed ~ age + eduction, data=partialData, 
        weights = w, na.action=na.exclude)
    coeftest(reg1, vcov=function(x) cluster.vcov(x, partialData$group_code2))  
    

    另见:

    【讨论】:

      猜你喜欢
      • 2013-11-09
      • 2012-01-16
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-09
      • 2018-02-05
      • 2016-01-03
      相关资源
      最近更新 更多