# How to Perform Naive Forecasting in R (With Examples)

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

 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)

We can use the following code to calculate both metrics:

```#calculate MAPE
mean(abs((actual-forecast)/actual), na.rm=T) * 100

 9.898281

#calculate MAE
mean(abs(actual-forecast), na.rm=T)

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

lines(forecast, type='l', col = 'blue') 