Skip to content

Instantly share code, notes, and snippets.

@alexpghayes
Created September 27, 2022 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpghayes/136d1ed869b288018c3494fb498e3ef2 to your computer and use it in GitHub Desktop.
Save alexpghayes/136d1ed869b288018c3494fb498e3ef2 to your computer and use it in GitHub Desktop.
library(sandwich)

n <- 10
d <- 3

p <- 4

X <- matrix(
  rnorm(n * d),
  nrow = n
)

colnames(X) <- paste0("X", 1:d)

W <- matrix(
  rnorm(n * p),
  nrow = n
)

colnames(W) <- paste0("W", 1:p)

fit <- lm(X ~ W)
vcovHC(fit)
#>                X1:(Intercept)      X1:WW1      X1:WW2      X1:WW3      X1:WW4
#> X1:(Intercept)     0.18714970  0.15293138  0.06169381 -0.38212806 -0.22950836
#> X1:WW1             0.15293138  0.49385910 -0.02602991 -0.53316335 -0.35271756
#> X1:WW2             0.06169381 -0.02602991  0.27638523 -0.10620895 -0.18127797
#> X1:WW3            -0.38212806 -0.53316335 -0.10620895  1.18649740  0.70805486
#> X1:WW4            -0.22950836 -0.35271756 -0.18127797  0.70805486  0.55109106
#> X2:(Intercept)    -0.14126611 -0.11682535 -0.05207978  0.31605816  0.16974471
#> X2:WW1            -0.11682535 -0.43120204  0.11708660  0.53658906  0.32340267
#> X2:WW2            -0.05207978  0.11708660 -0.29256194  0.06199894  0.13393105
#> X2:WW3             0.31605816  0.53658906  0.06199894 -0.86083183 -0.55507897
#> X2:WW4             0.16974471  0.32340267  0.13393105 -0.55507897 -0.36717613
#> X3:(Intercept)    -0.08813760 -0.06539844 -0.02930969  0.15181419  0.10600367
#> X3:WW1            -0.06539844 -0.14694863 -0.03626268  0.22590876  0.15644827
#> X3:WW2            -0.02930969 -0.03626268 -0.07351578  0.07698120  0.09152829
#> X3:WW3             0.15181419  0.22590876  0.07698120 -0.45184556 -0.29255035
#> X3:WW4             0.10600367  0.15644827  0.09152829 -0.29255035 -0.22565183
#>                X2:(Intercept)      X2:WW1      X2:WW2      X2:WW3      X2:WW4
#> X1:(Intercept)    -0.14126611 -0.11682535 -0.05207978  0.31605816  0.16974471
#> X1:WW1            -0.11682535 -0.43120204  0.11708660  0.53658906  0.32340267
#> X1:WW2            -0.05207978  0.11708660 -0.29256194  0.06199894  0.13393105
#> X1:WW3             0.31605816  0.53658906  0.06199894 -0.86083183 -0.55507897
#> X1:WW4             0.16974471  0.32340267  0.13393105 -0.55507897 -0.36717613
#> X2:(Intercept)     0.40191096 -0.06091335  0.23822081 -0.34080260 -0.11450519
#> X2:WW1            -0.06091335  0.54881849 -0.29881666 -0.41679041 -0.27599640
#> X2:WW2             0.23822081 -0.29881666  0.46758116 -0.04838034 -0.06426989
#> X2:WW3            -0.34080260 -0.41679041 -0.04838034  0.80002079  0.42321220
#> X2:WW4            -0.11450519 -0.27599640 -0.06426989  0.42321220  0.36810231
#> X3:(Intercept)     0.05572581  0.05294422  0.01821791 -0.12725431 -0.07519097
#> X3:WW1             0.05294422  0.12394003  0.01497755 -0.19789478 -0.12432547
#> X3:WW2             0.01821791  0.01497755  0.06582784 -0.05653749 -0.06794701
#> X3:WW3            -0.12725431 -0.19789478 -0.05653749  0.35530787  0.23303872
#> X3:WW4            -0.07519097 -0.12432547 -0.06794701  0.23303872  0.17493851
#>                X3:(Intercept)      X3:WW1      X3:WW2      X3:WW3      X3:WW4
#> X1:(Intercept)    -0.08813760 -0.06539844 -0.02930969  0.15181419  0.10600367
#> X1:WW1            -0.06539844 -0.14694863 -0.03626268  0.22590876  0.15644827
#> X1:WW2            -0.02930969 -0.03626268 -0.07351578  0.07698120  0.09152829
#> X1:WW3             0.15181419  0.22590876  0.07698120 -0.45184556 -0.29255035
#> X1:WW4             0.10600367  0.15644827  0.09152829 -0.29255035 -0.22565183
#> X2:(Intercept)     0.05572581  0.05294422  0.01821791 -0.12725431 -0.07519097
#> X2:WW1             0.05294422  0.12394003  0.01497755 -0.19789478 -0.12432547
#> X2:WW2             0.01821791  0.01497755  0.06582784 -0.05653749 -0.06794701
#> X2:WW3            -0.12725431 -0.19789478 -0.05653749  0.35530787  0.23303872
#> X2:WW4            -0.07519097 -0.12432547 -0.06794701  0.23303872  0.17493851
#> X3:(Intercept)     0.06912245  0.01043979  0.03014932 -0.05168263 -0.06249814
#> X3:WW1             0.01043979  0.06605560  0.01289635 -0.10186627 -0.05834196
#> X3:WW2             0.03014932  0.01289635  0.03662464 -0.03032570 -0.05168183
#> X3:WW3            -0.05168263 -0.10186627 -0.03032570  0.19453563  0.11634054
#> X3:WW4            -0.06249814 -0.05834196 -0.05168183  0.11634054  0.10951608

image(bread(fit))  # block diagonal bread

image(meat(fit))   # meat is not block diagonal

Created on 2022-09-27 with reprex v2.0.2.9000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment