https://github.com/cran/sandwich
Raw File
Tip revision: 39824702dada9466719a12af5cd16b6577b2482d authored by Achim Zeileis on 02 October 2020, 09:40:02 UTC
version 3.0-0
Tip revision: 3982470
vcovPC.Rout.save

R version 3.5.1 (2018-07-02) -- "Feather Spray"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library("sandwich")
> data("PetersenCL", package = "sandwich")
> m <- lm(y ~ x, data = PetersenCL)
> sandwich::vcovPC(m, cluster = ~ firm + year)
              (Intercept)             x
(Intercept)  4.928685e-04 -4.396037e-05
x           -4.396037e-05  6.388754e-04
> 
> PU <- subset(PetersenCL, !(firm == 1 & year == 10))
> u_m <- lm(y ~ x, data = PU)
> sandwich::vcovPC(u_m, cluster = ~ firm + year, pairwise = TRUE)
              (Intercept)             x
(Intercept)  4.870754e-04 -4.566605e-05
x           -4.566605e-05  6.419998e-04
> sandwich::vcovPC(u_m, cluster = ~ firm + year, pairwise = FALSE)
              (Intercept)             x
(Intercept)  5.108853e-04 -8.787789e-05
x           -8.787789e-05  6.371175e-04
> 
> 
> ## vcovPC compared with Stata's xtpcse (xtscc y x) standard errors
> pc1 <- c(0.0222006 , 0.025276)
> names(pc1) <- c("(Intercept)", "x")
> pc1
(Intercept)           x 
  0.0222006   0.0252760 
> (pc2 <- sqrt(diag(sandwich::vcovPC(m, cluster = PetersenCL$firm, order.by = PetersenCL$year))))
(Intercept)           x 
 0.02220064  0.02527598 
> all.equal(pc1, pc2, tolerance = 1e-5)
[1] TRUE
> 
> 
> ## sandwich::vcovPC compared to pcse::vcovPC
> (pc3 <- pcse::vcovPC(u_m, PU$firm, PU$year, pairwise = FALSE))
              X.Intercept.             x
X.Intercept.  5.108853e-04 -8.787789e-05
x            -8.787789e-05  6.371175e-04
> (pc4 <- sandwich::vcovPC(u_m, cluster = ~ firm + year, pairwise = FALSE))
              (Intercept)             x
(Intercept)  5.108853e-04 -8.787789e-05
x           -8.787789e-05  6.371175e-04
> (pc5 <- pcse::vcovPC(u_m, PU$firm, PU$year, pairwise = TRUE))
              X.Intercept.             x
X.Intercept.  4.870754e-04 -4.566605e-05
x            -4.566605e-05  6.419998e-04
> (pc6 <- sandwich::vcovPC(u_m, cluster = ~ firm + year, pairwise = TRUE))
              (Intercept)             x
(Intercept)  4.870754e-04 -4.566605e-05
x           -4.566605e-05  6.419998e-04
> rownames(pc3) <- colnames(pc3) <- rownames(pc5) <- colnames(pc5) <- c("(Intercept)", "x")
> all.equal(pc3, pc4)
[1] TRUE
> all.equal(pc5, pc6)
[1] TRUE
> 
> proc.time()
   user  system elapsed 
  2.726   0.995   3.707 
back to top