Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast Wald confidence intervals for a glm with broom in R

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
like image 445
bebru Avatar asked Oct 31 '18 20:10

bebru


People also ask

How do you find the confidence interval for a GLM?

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).

What is Wald confidence limit?

For a 95% confidence interval, z is 1.96. This confidence interval is also known commonly as the Wald interval.


1 Answers

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))
like image 69
bebru Avatar answered Oct 08 '22 00:10

bebru