I would like to calculate Wald confidence intervals of the coefficients of a glm on a somewhat large data set, and use broom
for a tidy output.
mydata <- data.frame(y = rbinom(1e5,1,0.8),
x1 = rnorm(1e5),
x2 = rnorm(1e5))
glm.1 <- glm(y ~ x1 + x2, data = mydata, family = "binomial")
Using broom::tidy
takes a lot of time on large data, since it uses confint.glm
, which calculates the confidence intervals based on the profiled log-likelihood function.
tidy(glm.1, conf.int = TRUE) # can take literally hours
To find the confidence interval for a lm model (linear regression model), we can use confint function and there is no need to pass the confidence level because the default is 95%. This can be also used for a glm model (general linear model).
For a 95% confidence interval, z is 1.96. This confidence interval is also known commonly as the Wald interval.
confint
and confint.glm
respectively do not take an argument for the method used to calculate the confidence intervals. If you want to use another method, you need to use a different function, e.g. confint.default
for Wald.
broom::tidy
in turn does not have an argument for the function used (or did I miss something?), it always calls confint.glm
for glm.
To calculate confidence intervals with a different function, broom
has confint_tidy
, where you can specify the function you want to use:
confint_tidy(glm.1, func = stats::confint.default)
Put this together with the estimates:
cbind(tidy(glm.1), confint_tidy(glm.1, func = stats::confint.default))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With