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

## Output

     1          2       3
0.2490503 0.4313678 0.3435835

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

## Output

     1          2       3
0.2490503 0.4313678 0.3435835

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

## Output

      1       2          3
0.2490503 0.4313678 0.3435835

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

## Output

      1       2       3
0.2490503 0.4313678 0.3435835

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

## Output

      1       2          3
0.2490503 0.4313678 0.3435835

## Output

[1] 1.113487

Updated on: 17-Oct-2020

427 Views