Learning to Calculate Prediction Intervals Using R


The regression model is arguably the most essential tool in the quantitative analyst’s toolkit. It offers two core capabilities that drive informed decision-making across scientific research and business intelligence:

  • Quantifying the precise relationship between a set of predictor variables and a target response variable.
  • Generating reliable forecasts for future outcomes or unknown values based on the established relationship.

While point estimates—a single forecasted number—provide a specific target, they inherently fail to capture the uncertainty and variability that are unavoidable in real-world data. When utilizing a model for forecasting, particularly for individual instances, relying solely on a single predicted value can lead to flawed conclusions and overly optimistic projections. Statisticians must, therefore, define a plausible range of outcomes, which is formally known as a prediction interval. This interval establishes a probabilistic boundary for where a single new observation is likely to fall.

To illustrate, imagine developing a simple linear model to predict a student’s exam score based on their hours studied. If the model generates a point estimate predicting a score of 91 for a student who studies for 6 hours, this exact score is statistically improbable due to inherent biological and environmental variability. A more useful output is a 95% prediction interval, which might state that there is a 95% chance that the student’s actual score will be between 85 and 97. This range provides a far more pragmatic and actionable estimate, successfully accounting for the real-world uncertainty inherent in individual forecasting.

Deconstructing the Prediction Interval: Sources of Uncertainty

A prediction interval (PI) is designed specifically to quantify the total uncertainty surrounding a single, new data point drawn from the population. This requirement makes the PI fundamentally different from other statistical intervals. Crucially, the PI must account for two distinct sources of error:

  1. Model Error: The uncertainty associated with the estimation of the mean relationship itself—the fact that the fitted regression line is merely an estimate based on sample data, not the true population line.
  2. Random Observation Error: The inherent, irreducible random variation of any single, individual data point around the true regression line.

This dual accounting for uncertainty is why prediction intervals are the gold standard for practical forecasting applications aimed at individual outcomes. The width of the PI is a direct consequence of the specified confidence level. For instance, a standard 95% prediction interval implies that if the process of sampling and modeling were repeated numerous times, 95% of the resulting intervals would successfully capture the true value of the future observation. Increasing the confidence level to 99% necessitates a wider interval to guarantee the higher required certainty.

The R statistical environment provides powerful, streamlined functions for generating these essential intervals efficiently. The workflow involves first establishing a robust regression model using sample data, and subsequently employing R’s built-in prediction functions, ensuring that the request specifies an interval estimate rather than a simple point forecast. The subsequent sections will guide you through this necessary process using a standard dataset available within R.

Step-by-Step Guide: Fitting the Regression Model in R

To provide a clear demonstration of calculating a prediction interval, we will leverage the classic mtcars dataset, which is conveniently built into R and provides performance statistics for 32 automobiles. Before initiating the modeling process, it is standard practice to examine the data structure to familiarize ourselves with the variables and ensure data quality.

We begin by loading and inspecting the initial six rows of the mtcars dataset. This quick overview gives us insight into relevant variables such as mpg (miles per gallon), disp (engine displacement), and hp (horsepower), among others.

# View the first six rows of the mtcars dataset
head(mtcars)

#                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

For the purposes of this tutorial, we will fit a simple linear regression model. Our objective is to predict the fuel efficiency, mpg, based on the engine size, disp (engine displacement, measured in cubic inches). This modeling step is executed using R’s standard `lm()` function, specifying the formula that defines the relationship between the response and predictor variables. Reviewing the model summary is essential, as it provides critical diagnostic metrics such as coefficient estimates, statistical significance (p-values), and the overall goodness-of-fit statistics like R-squared.

# Fit simple linear regression model: mpg predicted by disp
model <- lm(mpg ~ disp, data = mtcars)

# View summary of the fitted model
summary(model)

#Call:
#lm(formula = mpg ~ disp, data = mtcars)
#
#Residuals:
#    Min      1Q  Median      3Q     Max 
#-4.8922 -2.2022 -0.9631  1.6272  7.2305 
#
#Coefficients:
#             Estimate Std. Error t value Pr(>|t|)    
#(Intercept) 29.599855   1.229720  24.070  < 2e-16 ***
#disp        -0.041215   0.004712  -8.747 9.38e-10 ***
#---
#Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#
#Residual standard error: 3.251 on 30 degrees of freedom
#Multiple R-squared:  0.7183,	Adjusted R-squared:  0.709 
#F-statistic: 76.51 on 1 and 30 DF,  p-value: 9.38e-10

Generating and Interpreting Prediction Intervals in R

With the linear model successfully fitted, the subsequent step is to utilize it for prediction. We will define a small dataset containing three hypothetical, representative values for the predictor variable disp (150, 200, and 250). Initially, we generate the basic point predictions using the `predict()` function without specifying any interval type.

# Create data frame with three new values for disp
new_disp <- data.frame(disp= c(150, 200, 250))

# Predict the expected mean value for mpg based on these three new displacement values
predict(model, newdata = new_disp)

#       1        2        3 
#23.41759 21.35683 19.29607 

These point estimates represent the average expected mpg for a vehicle group possessing the corresponding displacement. For example, a new car with a disp of 150 is predicted to achieve an average mpg of 23.41759. However, as established, these precise values offer no measure of uncertainty for an individual vehicle.

The true forecasting power of the regression model for individual cars is unlocked by calculating the prediction intervals. This is straightforward in R: we simply add the argument `interval = “predict”` within the `predict()` function call. By default, R generates a 95% interval, presenting three outputs for each prediction: the fitted value (`fit`), the lower boundary (`lwr`), and the upper boundary (`upr`).

# Calculate 95% prediction intervals around the predicted values
predict(model, newdata = new_disp, interval = "predict")

#       fit      lwr      upr
#1 23.41759 16.62968 30.20549
#2 21.35683 14.60704 28.10662
#3 19.29607 12.55021 26.04194

The interpretation of these 95% prediction intervals is crucial for practical applications: for a single new car with a disp of 250, we can be 95% confident that its actual mpg will fall between 12.55021 and 26.04194.

While 95% is the statistical default, the confidence level of the prediction interval can be easily modified using the level command. If we require a higher degree of certainty—say, 99%—the interval must necessarily widen to maintain that increased probability of capturing the true outcome. We demonstrate this by setting `level = 0.99`.

# Create 99% prediction intervals around the predicted values
predict(model, newdata = new_disp, interval = "predict", level = 0.99)

#       fit      lwr      upr
#1 23.41759 14.27742 32.55775
#2 21.35683 12.26799 30.44567
#3 19.29607 10.21252 28.37963

A direct comparison confirms the fundamental statistical trade-off: the 99% prediction intervals are noticeably wider than the 95% intervals. This expansion is essential because a higher confidence level requires a larger range to increase the likelihood that the interval successfully encompasses the true, yet unknown, value of the future individual observation.

Visualizing Prediction Intervals for Enhanced Insight

While examining numerical tables is instructive, visualizing the fitted regression line alongside its uncertainty bands offers the most profound and intuitive understanding of the model’s performance and predictive limitations. In the R statistical environment, the industry-standard tool for generating high-quality statistical graphics is the ggplot2 library.

The following code sequence prepares the necessary data by binding the original observations with the newly calculated prediction intervals. We then utilize `ggplot2` to construct a layered visualization. The initial layer plots the raw data points, followed by the fitted regression line, and finally, the boundaries of the prediction interval, which are clearly demarcated as dashed lines.

The resulting visual display synthesizes several crucial pieces of information simultaneously, making the model’s performance transparent:

  • A scatterplot showing the relationship between disp and mpg for the observed data.
  • The fitted regression line (often shown in blue).
  • The gray confidence bands (automatically included by `stat_smooth`), representing the uncertainty around the mean estimate.
  • The prediction bands (plotted as dashed coral lines), representing the much wider boundaries of the prediction interval.
# Define dataset, selecting only the relevant columns
data <- mtcars[ , c("mpg", "disp")]

# Create simple linear regression model
model <- lm(mpg ~ disp, data = mtcars)

# Use model to create prediction intervals across the range of observed disp values
predictions <- predict(model, interval = "predict")

# Combine original data along with prediction intervals into one data frame
all_data <- cbind(data, predictions)

# Load ggplot2 library
library(ggplot2)

# Create the visualization
ggplot(all_data, aes(x = disp, y = mpg)) + # Define x and y axis variables
  geom_point() + # Add scatterplot points
  stat_smooth(method = lm) + # Add the fitted regression line and confidence bands
  geom_line(aes(y = lwr), col = "coral2", linetype = "dashed") + # Plot the lower prediction interval bound
  geom_line(aes(y = upr), col = "coral2", linetype = "dashed") # Plot the upper prediction interval bound

Prediction interval in R

Prediction Interval vs. Confidence Interval: Essential Distinctions

A frequent point of confusion in regression analysis involves distinguishing between the prediction interval (PI) and the confidence interval (CI). Although both provide a range of likely values, their objectives are fundamentally different and are tied to what aspect of the model they are estimating.

The PI is designed to quantify the uncertainty surrounding a single, future observation. Conversely, the CI quantifies the uncertainty surrounding the mean predicted value of the response variable for a specific set of predictor values. Because the prediction interval must integrate both the uncertainty of the estimated regression line (the CI component) and the inherent random scatter of individual data points (the error term), a prediction interval will always be significantly wider than a confidence interval calculated at the same confidence level for the same predicted value.

The choice between these two intervals must align precisely with the analytic objective. If the goal is to estimate the average response for a large group of observations (e.g., estimating the average mpg for all cars with a 200 disp), the confidence interval is the correct choice. However, if the aim is to forecast the outcome for a specific, individual instance (e.g., forecasting the mpg for one specific car with a 200 disp), the PI is mandatory. Using a CI for individual prediction is a critical error, as it provides a range that is too narrow, resulting in an unrealistically optimistic assessment of certainty and a high probability that the interval will fail to contain the actual individual outcome.

Cite this article

Mohammed looti (2025). Learning to Calculate Prediction Intervals Using R. PSYCHOLOGICAL STATISTICS. Retrieved from https://statistics.arabpsychology.com/create-a-prediction-interval-in-r/

Mohammed looti. "Learning to Calculate Prediction Intervals Using R." PSYCHOLOGICAL STATISTICS, 9 Nov. 2025, https://statistics.arabpsychology.com/create-a-prediction-interval-in-r/.

Mohammed looti. "Learning to Calculate Prediction Intervals Using R." PSYCHOLOGICAL STATISTICS, 2025. https://statistics.arabpsychology.com/create-a-prediction-interval-in-r/.

Mohammed looti (2025) 'Learning to Calculate Prediction Intervals Using R', PSYCHOLOGICAL STATISTICS. Available at: https://statistics.arabpsychology.com/create-a-prediction-interval-in-r/.

[1] Mohammed looti, "Learning to Calculate Prediction Intervals Using R," PSYCHOLOGICAL STATISTICS, vol. X, no. Y, ص Z-Z, November, 2025.

Mohammed looti. Learning to Calculate Prediction Intervals Using R. PSYCHOLOGICAL STATISTICS. 2025;vol(issue):pages.

Download Post (.PDF)
Scroll to Top