How to find the confidence interval for the predictive value using regression model in R?


The confidence interval for the predictive value using regression model can be found with the help of predict function, we just need to use interval argument for confidence and the appropriate level for that. For example, if we have a model M and the data frame for the values of independent variable is named as newdata then we can use the following syntax for the confidence interval −

predict(M,newdata,se.fit=TRUE,interval="confidence",level=0.95)

Example

Consider the below data frame −

 Live Demo

set.seed(1234)
x1<-sample(0:10,20,replace=TRUE)
y1<-sample(6:10,20,replace=TRUE)
df1<-data.frame(x1,y1)
df1

Output

 x1 y1
1  9 8
2  5 9
3  4 10
4  8 7
5  4 10
6  5 7
7  3 8
8  1 9
9  6 9
10 5 8
11 9 6
12 5 8
13 3 9
14 7 7
15 3 8
16 3 7
17 4 10
18 7 6
19 3 8
20 7 6

Example

M1<-lm(y1~x1,data=df1)
summary(M1)

Output

Call:
lm(formula = y1 ~ x1, data = df1)
Residuals:
   Min      1Q   Median   3Q    Max
-1.6617 -0.6775 -0.1775 1.0566 1.6611
Coefficients:
           Estimate Std.   Error t value Pr(>|t|)
(Intercept) 9.6299 0.6342 15.185 1.05e-11 ***
      x1   -0.3228 0.1155 -2.795 0.012 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.113 on 18 degrees of freedom
Multiple R-squared: 0.3026, Adjusted R-squared: 0.2638
F-statistic: 7.809 on 1 and 18 DF, p-value: 0.01198

Creating new data and finding the 95% confidence interval for the fitted values of that data −

Example

newdata_x1<-data.frame(x1=c(5,2,3))
predict(M1,newdata_x1,se.fit=TRUE,interval="confidence",level=0.95)
$fit

Output

      fit      lwr    upr
1 8.016138 7.492902 8.539373
2 8.984400 8.078130 9.890670
3 8.661646 7.939804 9.383488

Example

$se.fit

Output

     1          2       3
0.2490503 0.4313678 0.3435835

Example

$df

Output

[1] 18

Example

$residual.scale

Output

[1] 1.113487

Finding the 90% confidence interval for the fitted values of that data −

Example

predict(M1,newdata_x1,se.fit=TRUE,interval="confidence",level=0.90)
$fit

Output

      fit      lwr    upr
1 8.016138 7.584269 8.448007
2 8.984400 8.236381 9.732419
3 8.661646 8.065850 9.257442

Example

$se.fit

Output

     1          2       3
0.2490503 0.4313678 0.3435835

Example

$df

Output

[1] 18

Example

$residual.scale

Output

[1] 1.113487

Finding the 80% confidence interval for the fitted values of that data −

Example

predict(M1,newdata_x1,se.fit=TRUE,interval="confidence",level=0.80)
$fit

Output

      fit      lwr     upr
1 8.016138 7.684803 8.347472
2 8.984400 8.410512 9.558288
3 8.661646 8.204546 9.118746

Example

$se.fit

Output

      1       2          3
0.2490503 0.4313678 0.3435835

Example

$df

Output

[1] 18

Example

$residual.scale

Output

[1] 1.113487

Finding the 75% confidence interval for the fitted values of that data −

Example

predict(M1,newdata_x1,se.fit=TRUE,interval="confidence",level=0.75)
$fit

Output

      fit       lwr    upr
1 8.016138 7.720089 8.312187
2 8.984400 8.471628 9.497172
3 8.661646 8.253224 9.070068

Example

$se.fit

Output

      1       2       3
0.2490503 0.4313678 0.3435835

Example

$df

Output

[1] 18

Example

$residual.scale

Output

[1] 1.113487

Finding the 99% confidence interval for the fitted values of that data −

Example

predict(M1,newdata_x1,se.fit=TRUE,interval="confidence",level=0.99)
$fit

Output

      fit       lwr    upr
1 8.016138 7.299261 8.733014
2 8.984400 7.742734 10.226067
3 8.661646 7.672662 9.650631

Example

$se.fit

Output

      1       2          3
0.2490503 0.4313678 0.3435835

Example

$df

Output

[1] 18

Example

$residual.scale

Output

[1] 1.113487

Updated on: 17-Oct-2020

438 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements