How to find the 95% confidence interval for the glm model in R?

R ProgrammingServer Side ProgrammingProgramming

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). Check out the below examples to see the output of confint for a glm model.

Example1

Live Demo

> set.seed(3214)
> x1<-rpois(20,5)
> y1<-sample(0:1,20,replace=TRUE)
> Model1<-glm(y1~x1,family="binomial")
> summary(Model1)

Output

Call:
glm(formula = y1 ~ x1, family = "binomial")

Deviance Residuals:
Min 1Q Median 3Q Max
-1.6360 -1.4156 0.7800 0.8567 0.9946

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.34851 1.17554 0.296 0.767
x1 0.09794 0.21421 0.457 0.648

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 24.435 on 19 degrees of freedom
Residual deviance: 24.221 on 18 degrees of freedom
AIC: 28.221

Number of Fisher Scoring iterations: 4

> confint(Model1)
Waiting for profiling to be done...
2.5 % 97.5 %
(Intercept) -1.9946211 2.8014055
x1 -0.3179604 0.5537196

Example2

Live Demo

> x2<-runif(200,2,5)
> y2<-sample(0:1,200,replace=TRUE)
> Model2<-glm(y2~x2,family="binomial")
> summary(Model2)

Output

Call:
glm(formula = y2 ~ x2, family = "binomial")

Deviance Residuals:
Min 1Q Median 3Q Max
-1.162 -1.152 -1.145 1.202 1.211

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.009208 0.631317 -0.015 0.988
x2 -0.013999 0.169522 -0.083 0.934

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 277.08 on 199 degrees of freedom
Residual deviance: 277.07 on 198 degrees of freedom
AIC: 281.07

Number of Fisher Scoring iterations: 3

> confint(Model2)
Waiting for profiling to be done...
2.5 % 97.5 %
(Intercept) -1.2517839 1.2317974
x2 -0.3473611 0.3192271

Example3

Live Demo

> x3<-runif(5000,2,5)
> y3<-sample(0:1,5000,replace=TRUE)
> Model3<-glm(y3~x3,family="binomial")
> summary(Model3)

Output

Call:
glm(formula = y3 ~ x3, family = "binomial")

Deviance Residuals:
Min 1Q Median 3Q Max
-1.177 -1.166 -1.156 1.188 1.199

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.03254 0.11913 0.273 0.785
x3 -0.01674 0.03288 -0.509 0.611

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 6930.6 on 4999 degrees of freedom
Residual deviance: 6930.3 on 4998 degrees of freedom
AIC: 6934.3

Number of Fisher Scoring iterations: 3

> confint(Model3)
Waiting for profiling to be done...
2.5 % 97.5 %
(Intercept) -0.20096508 0.2660569
x3 -0.08119495 0.0476911