Table of Contents
Naive forecasting in R is a powerful tool that can be used to predict future values based on past data. The process involves first loading the data into R, then computing the mean or median of the past values, and finally, using the mean or median to forecast the future values. Examples of naive forecasting in R can be found in the package “forecast”. This package contains functions for computing the mean and median of past values, as well as functions for forecasting future values. In addition, the package contains several useful examples that demonstrate how to use the functions in real-world situations.
A naive forecast is one in which the forecast for a given period is simply equal to the value observed in the previous period.
For example, suppose we have the following sales of a given product during the first three months of the year:
The forecast for sales in April would simply be equal to the actual sales from the previous month of March:
Although this method is simple, it tends to work surprisingly well in practice.
This tutorial provides a step-by-step example of how to perform naive forecasting in R.
Step 1: Enter the Data
First, we’ll enter the sales data for a 12-month period at some imaginary company:
#create vector to hold actual sales data
actual <- c(34, 37, 44, 47, 48, 48, 46, 43, 32, 27, 26, 24)
Step 2: Generate the Naive Forecasts
Next, we’ll use the following formulas to create naive forecasts for each month:
#generate naive forecasts forecast <- c(NA, actual[-length(actual)]) #view naive forecasts forecast [1] NA 34 37 44 47 48 48 46 43 32 27 26
Note that we simply used NA for the first forecasted value.
Step 3: Measure the Accuracy of the Forecasts
Lastly, we need to measure the accuracy of the forecasts. Two common metrics used to measure accuracy include:
- Mean absolute percentage error (MAPE)
- Mean absolute error (MAE)
#calculate MAPE mean(abs((actual-forecast)/actual), na.rm=T) * 100 [1] 9.898281 #calculate MAE mean(abs(actual-forecast), na.rm=T) [1] 3.454545
The mean absolute percentage error is 9.898% and the mean absolute error is 3.45
To know if this forecast is useful, we can compare it to other forecasting models and see if the accuracy measurements are better or worse.
Step 4: Visualize the Forecasts
Lastly, we can create a simple line plot to visualize the differences between the actual sales and the naive forecasts for the sales during each period:
#plot actual sales plot(actual, type='l', col = 'red', main='Actual vs. Forecasted Sales', xlab='Sales Period', ylab='Sales') #add line for forecasted sales lines(forecast, type='l', col = 'blue') #add legend legend('topright', legend=c('Actual', 'Forecasted'), col=c('red', 'blue'), lty=1)
Notice that the forecasted sales line is basically a lagged version of the actual sales line.
This is exactly what we would expect the plot to look like since the naive forecast simply forecasts the sales in the current period to be equal to the sales in the previous period.