# How to find the 95% confidence interval for the slope of regression line in R?

R ProgrammingServer Side ProgrammingProgramming

The slope of the regression line is a very important part of regression analysis, by finding the slope we get an estimate of the value by which the dependent variable is expected to increase or decrease. But the confidence interval provides the range of the slope values that we expect 95% of the times when the sample size is same. To find the 95% confidence for the slope of regression line we can use confint function with regression model object.

## Example

Consider the below data frame −

set.seed(1)
x <-rnorm(20)
y <-rnorm(20,2.5)
df <-data.frame(x,y)
df

## Output

      x       y
1 -0.62645381 3.4189774
2 0.18364332 3.2821363
3 -0.83562861 2.5745650
4 1.59528080 0.5106483
5 0.32950777 3.1198257
6 -0.82046838 2.4438713
7 0.48742905 2.3442045
8 0.73832471 1.0292476
9 0.57578135 2.0218499
10 -0.30538839 2.9179416
11 1.51178117 3.8586796
12 0.38984324 2.3972123
13 -0.62124058 2.8876716
14 -2.21469989 2.4461950
15 1.12493092 1.1229404
16 -0.04493361 2.0850054
17 -0.01619026 2.1057100
18 0.94383621 2.4406866
19 0.82122120 3.6000254
20 0.59390132 3.2631757

Creating regression model to predict y from x −

## Example

RegressionModel <-lm(y~x,data=df)
summary(RegressionModel)

## Output

Call:
lm(formula = y ~ x, data = df)

Residuals:
Min 1Q Median 3Q Max
-1.69133 -0.43739 -0.07132 0.68033 1.63937

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.5331 0.1998 12.677 2.08e-10 ***
x -0.2075 0.2195 -0.946 0.357
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.8738 on 18 degrees of freedom
Multiple R-squared: 0.04732, Adjusted R-squared: -0.00561
F-statistic: 0.894 on 1 and 18 DF, p-value: 0.3569

Finding the 95% confidence interval for the slope of the regression line −

## Example

confint(RegressionModel,'x',level=0.95)
2.5 % 97.5 %
x -0.6687129 0.2536177
Lets’ have a look at another example:
BloodPressure <-c(165,170,190,195,220)
Weight <-c(50,75,64,60,62)
data <-data.frame(BloodPressure,Weight)
data

## Output

BloodPressure Weight
1 165 50
2 170 75
3 190 64
4 195 60
5 220 62

## Example

RegM <-lm(BloodPressure~Weight,data=data)
summary(RegM)

## Output

Call:
lm(formula = BloodPressure ~ Weight, data = data)
Residuals:
1 2 3 4 5
-21.783 -19.277 1.820 7.219 32.020
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 181.79551 88.73672 2.049 0.133
Weight 0.09975 1.41495 0.070 0.948
Residual standard error: 25.34 on 3 degrees of freedom
Multiple R-squared: 0.001654, Adjusted R-squared: -0.3311
F-statistic: 0.00497 on 1 and 3 DF, p-value: 0.9482

## Example

confint(RegM,'Weight',level=0.95)
2.5 % 97.5 %
Weight -4.403255 4.602756

## Output

2.5 % 97.5 %
Weight -4.403255 4.602756