jepusto / clubSandwich

Cluster-robust (sandwich) variance estimators with small-sample corrections
http://jepusto.github.io/clubSandwich/
47 stars 8 forks source link

Issue when a coefficient is just estimated by a single observation #86

Closed wviechtb closed 1 year ago

wviechtb commented 2 years ago

I recently came across a situation in the context of a meta-analysis where a model coefficient is just estimated by a single observation. Consider the following example:

library(metafor)
dat <- dat.bcg
dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat, subset=-5)
res <- rma(yi, vi, data=dat, mods = ~ 0 + alloc)
res

The coefficient for allocalternate is just the observed effect for this level (i.e., dat[5,]). As a result, when using the sandwich method, the variance for this coefficient is equal to zero:

library(clubSandwich)
vcovCR(res, cluster=dat$trial, type="CR2")

Both coef_test() and conf_int() run and give NaN/NA where applicable:

coef_test(res, cluster=dat$trial, vcov="CR2")
conf_int(res, cluster=dat$trial, vcov="CR2")

But Wald_test() craps out:

Wald_test(res, cluster=dat$trial, vcov="CR2", constraints=constrain_zero(1:3))

Obviously, it can't actually run the test, but it might be nice if it handles this edge case a bit more gracefully.

jepusto commented 2 years ago

Thanks for this suggestion. I'll look into it.