R Under development (unstable) (2024-09-14 r87148 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 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 > if(!require("pcse")) q() Loading required package: pcse Attaching package: 'pcse' The following object is masked from 'package:sandwich': 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.76 1.18 3.93