Here is an example of how to do this using the lalonde
dataset in cobalt
, where treat
is a binary variable.
data("lalonde", package = "cobalt")
fit <- glm(treat ~ age + educ, data = lalonde, family = binomial)
est <- lmtest::coeftest(fit, vcov. = sandwich::vcovHC)
#Coefficient estimates and CIs
cbind(est, confint(est))
#> Estimate Std. Error z value Pr(>|z|) 2.5 %
#> (Intercept) -0.266058710 0.424816044 -0.6262916 0.53112371 -1.09868286
#> age -0.023963432 0.008351086 -2.8694987 0.00411123 -0.04033126
#> educ 0.006699987 0.031100402 0.2154309 0.82943139 -0.05425568
#> 97.5 %
#> (Intercept) 0.566565436
#> age -0.007595603
#> educ 0.067655654
#Odds ratios and CIs
exp(cbind(est[,1], confint(est)))
#> 2.5 % 97.5 %
#> (Intercept) 0.7663941 0.3333098 1.7622042
#> age 0.9763214 0.9604712 0.9924332
#> educ 1.0067225 0.9471899 1.0699968
Created on 2021-12-06 by the reprex package (v2.0.1)
Here I used HC3 robust SEs as implemented in vcovHC()
in the sandwich
package. lmtest::coeftest()
provides a nice interface to incorporate the robust standard errors, and you can use confint()
on its output to extract the Wald confidence intervals. Alternatively, you could have used lmtest::coefci(fit, vcov. = sandwich::vcovHC)
to get the confidence intervals directly.
To get the odds ratios and their confidence intervals, I exponentiated the coefficients stored in the first column of est
and their confidence intervals. It's not appropriate to exponentiate the standard errors.
glm
orsummary(model)
will not calculate robust sandwich standard errors. You would have to use another package, for examplesandwich
andlmtest
. Then, you could writecoeftest(model, vcov = sandwich)
or something like this. $\endgroup$exp(cbind(OR = coef(model), confint(model)))
but I can't seem to incorporate thecoeftest
andvcov
into this code. $\endgroup$coefci()
function fromlmtest()
to get confidence intervals. You can supply the functions used to construct the standard errors (e.g.,sandwich::vcovHC
) to itsvcov.
argument. $\endgroup$