Link Search Menu Expand Document (external link)

How to compute a confidence interval for a regression coefficient (in R)

See all solutions.

Task

Say we have a linear regression model, either single variable or multivariate. How do we compute a confidence interval for the coefficient of one of the explanatory variables in the model?

Related tasks:

Solution

We’ll assume that you have fit a single linear model to your data, as in the code below, which uses fake example data. You can replace it with your actual data.

1
2
3
x <- c(34, 9, 78, 60, 22, 45, 83, 59, 25)
y <- c(126, 347, 298, 309, 450, 187, 266, 385, 400)
model <- lm(y ~ x)

We can use R’s confint() function to find the confidence interval for the model coefficients. You can change the level parameter to specify a different confidence level. Note that if you have a multiple regression model, it will make confidence intervals for all of the coefficient values.

1
confint(model, level = 0.95)   # or choose any confidence level; here we use 0.95
1
2
3
            2.5 %      97.5 %    
(Intercept) 172.638075 535.526421
x            -4.491961   2.473935

The 95% confidence interval for the regression coefficient is $[-4.491961, 2.473935]$.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)