2014-11-30 1 views
1

클러스터 된 데이터에 대해 다음과 같은 가중 회귀 분석을 실행하고 있으며 CLX 함수를 사용하여 클러스터 된 표준 오류에 대한 NAs를 계속 가져옵니다. na.action = na.exclude를 사용하고 있습니다. 그 이유는 결국 각 관측치의 잔차를 추가 조작을 위해 데이터 프레임에 추가한다는 것입니다.clx는 na.action = na.exclude와 함께 사용할 때 NA를 반환합니다.

#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가 될 수 있기 때문에 na.exclude가 문제가되는 것으로 추측했습니다. 그래서 clx가 클러스터와 객체 reg1에서 NA를 무시하도록하는 방법이 있습니까?

회귀 분석을 실행하기 전에 데이터 집합에서 불완전한 사례를 제거 할 수 있다는 점을 유의하십시오. 문제가 해결 될 수는 있지만 많은 공백을 피하기 위해 공제를 계속합니다.) 그리고 가능한 모든 공변량 집합에 대한 코드를 변경해야하는 필요성을 피하려고합니다.

답변

0

예를 재현 할 수 없기 때문에 유용한 답변을 제공하기가 어렵습니다. 그러나 대답의 핵심은 Mahmood Arai의 클러스터 된 표준 오류에 대한 원래 코드가 NA를 처리하지 않는다는 것입니다. 또한

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)) 

참조 :

이 경우 당신은 당신의 사건을 처리해야하는의 multiwayvcov 패키지를 사용할 수 있습니다

관련 문제