2014-12-02 157 views

回答

0

以下function計算聚集的標準錯誤,因爲它依賴於lm也可以合併權重(我檢查,它會產生與Stata相同的結果)。

cl <- function(dat,fm, cluster){ 
      require(sandwich, quietly = TRUE) 
      require(lmtest, quietly = TRUE) 
      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) 
      coeftest(fm, vcovCL) }