Last active
April 16, 2023 12:28
-
-
Save tappek/4cb65ab25d64f019ec629df5d11bd2bc to your computer and use it in GitHub Desktop.
fwildclusterboot and sandwich::vcovBS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("plm") | |
data("Grunfeld", package = "plm") | |
## set some observations of the dependent variable to missing | |
Grunfeld$inv[Grunfeld$year<=1936] <- NA | |
fe <- plm(inv ~ value + capital, data = Grunfeld, model = "within") | |
library(sandwich) | |
sandwich::vcovBS(fe, cluster=~firm, R = 999) # errors | |
# Error in vcovBS.default(fe, cluster = ~firm, R = 999) : | |
# number of observations in 'cluster' and 'nobs()' do not match | |
## Workaround | |
Gr <- Grunfeld[complete.cases(Grunfeld), ] # workaround | |
fe2 <- plm(inv ~ value + capital, data = Gr, model = "within") | |
# works due to workaround (needs plm dev version 2023-04-15) | |
sandwich::vcovBS(fe2, cluster=~firm, R = 999) | |
## subset cases | |
Grunfeld$sample <- complete.cases(Grunfeld[ , c('inv', 'value', 'capital')]) | |
fe_complete <- plm(inv ~ value + capital, data = Grunfeld, subset = Grunfeld$sample, model = "within") | |
## works with plm dev version / 2023-04-15) | |
sandwich::vcovBS(fe_complete, cluster=~firm, R = 999) | |
library(fwildclusterboot) | |
## case with dropped NA's | |
## fails, just like like sandwich::vcovBS | |
fe_wild <- boottest(fe, param="value",clustid=c("firm"), | |
B=101, type="rademacher", impose_null=TRUE, p_val_type="two-tailed") | |
summary(fe_wild) | |
## works due to workaround | |
fe_wild2 <- boottest(fe2, param="value",clustid=c("firm"), | |
B=101, type="rademacher", impose_null=TRUE, p_val_type="two-tailed") | |
summary(fe_wild2) | |
nobs(fe2) | |
## subset case | |
fe_wild_complete <- boottest(fe_complete, param="value",clustid=c("firm"), | |
B=101, type="rademacher", impose_null=TRUE, p_val_type="two-tailed") | |
summary(fe_wild_complete) |
Ah, I just read the plm
PR and realized that you are already aware of what is going on =)
Thank you! Yes, somewhat ;). Proper na.action
implemenation "was long on the list"...
(Ref: ycroissant/plm#44)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, after looking at this for a bit, I think the root source of the error with clusters is the following:
lm(Y~X)
and cluster group g), bothsandwich
andfwildclusterboot
need to grab the clustering variable from the input data set, which is of course not yet processed by the regression function.lm
andlfe::felm
, this is e.g. done asplm
report an index of dropped columns? Then I could easily add an additional if clause, as I do forfixest
. But the easiest way to makeplm
compatible with bothfwildclusterboot
andsandwich
would be to output ana.action
aslm
.