This vignette provides a short overview of the basics of forecast evaluation with the functions from the onlineforecast package. It follows up on the vignettes setup-data and setup-and-use-model, and continues the building load forecast modelling presented there. If something is introduced in the present text, but not explained, then have a look in the two preceding vignettes to find an explanation.

First Load the package, setup the model and calculate the forecasts:

Just start by:

Set the `scoreperiod`

as a logical vector to control which points will be included in score calculations.

Use it to exclude a burn-in period of one week:

One fundamental caveat in data-driven modelling is over-fitting a model. This can easily happen when the model is fitted (trained) and evaluated on the same data. There are essentially two ways of dealing with this: Penalize increased model complexity or divide into a training set and test set (cross-validation).

In most forecasting applications the easiest and most transparent approach is some cross-validation approach - many methods for dividing into sets have been suggested. For online forecasting it is luckily quite straight forward, when a model is fitted using a recursive estimation method, like the RLS. In each time step the following happens:

Data for the new time point becomes available, both observations and new forecasts.

The parameters in the model are updated.

A new forecast is calculated.

Hence, those forecasts are only calculated based on past data, so there is no need for dividing into a training set and a test set!

However, the parameters (like the forgetting factor and low-pass filter coefficients) are optimized on a particular period, hence over-fitting is possible, however it’s most often very few parameters compared to the number of observations - so the it’s very unlikely to over-fit a recursive fitted model in this setup.

```
# Define a new model with low-pass filtering of the Ta input
model <- forecastmodel$new()
model$output = "y"
model$add_inputs(Ta = "lp(Ta, a1=0.9)",
I = "lp(I, a1=0.9)",
mu = "one()")
model$add_prmbounds(Ta__a1 = c(0.8, 0.9, 0.99),
I__a1 = c(0.6, 0.9, 0.99),
lambda = c(0.9, 0.99, 0.9999))
model$add_regprm("rls_prm(lambda=0.9)")
model$kseq <- c(3,18)
# Optimize the parameters
model$prm <- rls_optim(model, D)$par
## ----------------
## Ta__a1 I__a1 lambda
## 0.90 0.90 0.99
## k3 k18 sum
## 0.814 0.812 1.626
## ----------------
## Ta__a1 I__a1 lambda
## 0.901 0.900 0.990
## k3 k18 sum
## 0.814 0.812 1.626
## ...output cropped
```

Fit for all horizons and see the fit summary:

```
# Fit for all horizons
model$kseq <- 1:36
# Fit with RLS
fit1 <- rls_fit(model$prm, model, D)
## ----------------
## Ta__a1 I__a1 lambda
## 0.897 0.920 0.991
# Check the fit
summary(fit1)
##
## Output: y
## Inputs: Ta = lp(Ta, a1=0.897)
## I = lp(I, a1=0.92)
## mu = one()
##
## Regression parameters:
## lambda = 0.991
##
## Scoreperiod: 1656 observations are included.
##
## RLS coeffients summary stats (cannot be used for significance tests):
## mean sd min max
## Ta -0.1900 0.0400 -0.370 -0.0050
## I -0.0075 0.0056 -0.024 0.0031
## mu 5.4000 0.2800 4.800 6.4000
##
## RMSE:
## k1 k2 k3 k4 k5 k6 k7 k8 k9 k10 k11 k12 k13 k14 k15 k16
## 0.81 0.81 0.81 0.81 0.81 0.82 0.82 0.82 0.82 0.82 0.82 0.81 0.81 0.81 0.81 0.81
## k17 k18 k19 k20 k21 k22 k23 k24 k25 k26 k27 k28 k29 k30 k31 k32
## 0.81 0.81 0.81 0.81 0.81 0.81 0.81 0.82 0.82 0.83 0.83 0.83 0.83 0.83 0.83 0.82
## k33 k34 k35 k36
## 0.82 0.82 0.82 0.82
```

Let us extend the model by adding a new input: A diurnal pattern comprised by Fourier series. It can simply be added to the current model object:

```
# Add a diurnal curve using fourier series
model$add_inputs(mu_tday = "fs(tday/24, nharmonics=4)")
model$kseq <- c(3,18)
# Optimize the parameters
model$prm <- rls_optim(model, D)$par
## ----------------
## Ta__a1 I__a1 lambda
## 0.90 0.90 0.99
## k3 k18 sum
## 0.734 0.741 1.475
## ----------------
## Ta__a1 I__a1 lambda
## 0.901 0.900 0.990
## k3 k18 sum
## 0.734 0.741 1.475
## ...output cropped
```

Fit for all horizons and see the fit summary:

```
# Fit for all horizons
model$kseq <- 1:36
# Fit with RLS
fit2 <- rls_fit(model$prm, model, D)
## ----------------
## Ta__a1 I__a1 lambda
## 0.921 0.871 0.989
# Check the fit
summary(fit2)
##
## Output: y
## Inputs: Ta = lp(Ta, a1=0.921)
## I = lp(I, a1=0.871)
## mu = one()
## mu_tday = fs(tday/24, nharmonics=4)
##
## Regression parameters:
## lambda = 0.989
##
## Scoreperiod: 1656 observations are included.
##
## RLS coeffients summary stats (cannot be used for significance tests):
## mean sd min max
## Ta -0.1800 0.0520 -0.5100 0.620
## I -0.0027 0.0066 -0.0490 0.073
## mu 5.3000 0.2900 4.6000 6.900
## mu_tday.sin1 0.1700 0.1700 -0.7300 1.900
## mu_tday.cos1 -0.2400 0.2400 -1.0000 0.760
## mu_tday.sin2 -0.1900 0.1200 -1.1000 0.240
## mu_tday.cos2 -0.1100 0.0820 -0.7500 0.180
## mu_tday.sin3 0.1200 0.0880 -0.1000 0.440
## mu_tday.cos3 0.2300 0.1200 0.0047 0.740
## mu_tday.sin4 -0.0110 0.0790 -0.2400 0.190
## mu_tday.cos4 -0.1200 0.0920 -0.4100 0.092
##
## RMSE:
## k1 k2 k3 k4 k5 k6 k7 k8 k9 k10 k11 k12 k13 k14 k15 k16
## 0.73 0.73 0.73 0.73 0.73 0.74 0.74 0.74 0.74 0.74 0.74 0.74 0.74 0.74 0.74 0.74
## k17 k18 k19 k20 k21 k22 k23 k24 k25 k26 k27 k28 k29 k30 k31 k32
## 0.74 0.74 0.74 0.74 0.75 0.75 0.75 0.75 0.76 0.76 0.76 0.76 0.76 0.76 0.76 0.76
## k33 k34 k35 k36
## 0.76 0.76 0.76 0.76
```

Keep the forecasts for plotting and later analysis:

Plot the full score period:

```
# Plot to see the forecasts for the shortest and the longest horizon
plot_ts(subset(D,D$scoreperiod), c("^y|^Yhat1","^y|^Yhat2"), kseq = c(1,36))
```

Plot the full the first 14 days of the score period:

```
# Plot to see the forecasts for the shortest and the longest horizon
plot_ts(subset(D,which(D$scoreperiod)[1:(14*24)]), c("^y|^Yhat1","^y|^Yhat2"), kseq = c(1,36))
```

We can see how adding the diurnal pattern enables to track the morning shower peaks.

The performance of a forecast model should be compared to a reference model. This is however not at all trivial, since the suitable reference model depends on the particular case of forecasting, e.g. the suitable reference model for wind power forecasting is not the same as for solar power forecasting - even within the same application the suitable reference model can be different depending on particular conditions etc.

In general the fundamental reference model should be the simplest reasonable model not relying on any inputs, hence either a model based on a mean calculation or some persistence should used. It can also be, that the study is about concluding the value of using NWPs as input, and in that case the reference model should be the best model without the NWPs.

We will here demonstrate how to generate persistence forecasts, both a persistence with the current model output and a diurnal persistence, which uses the latest value lagged a given period from the forecast time point.

First the simple persistence:

```
# Just keep the horizons
kseq <- 1:36
# The simple persistence
D$YhatP <- persistence(D$y, kseq)
# Plot a few horizons
plot_ts(D, c("^y$|YhatP$"), c("2011-01-05","2011-01-10"), kseq=c(1,24,36))
```

Remember that the forecasts are lagged in the plot. Maybe it’s even more obvious to see that it’s simply the current value for all horizons:

```
D$YhatP[1:4, 1:8]
## k1 k2 k3 k4 k5 k6 k7 k8
## 1 5.92 5.92 5.92 5.92 5.92 5.92 5.92 5.92
## 2 5.85 5.85 5.85 5.85 5.85 5.85 5.85 5.85
## 3 5.85 5.85 5.85 5.85 5.85 5.85 5.85 5.85
## 4 5.88 5.88 5.88 5.88 5.88 5.88 5.88 5.88
```

A diurnal (i.e. 24 hours) persistence is: Take the value from the most recent time point, at the same time of day, as the forecast time point (i.e. `tod(t+k)`

). It can be obtained by:

```
# Use the argument perlen to set the period length
D$YhatDP <- persistence(D$y, kseq, perlen=24)
# Plot a few horizons
plot_ts(D, c("^y$|YhatDP$"), c("2011-01-05","2011-01-10"), kseq=c(1,24,36))
```

Note how going beyond the `perlen`

value, then the forecasts are the 48 hours lag values (going >48 the forecasts will be 72 lagged and so fourth).

Now it’s just a matter of calculating the score, as a function of the horizon, for each model and compare them.

We have kept the forecasts in the same format for each model, we can find them by:

```
# Find the forecasts in D
nms <- grep("^Yhat", names(D), value=TRUE)
nms
## [1] "Yhat1" "Yhat2" "YhatP" "YhatDP"
```

So it’s the small model, large model, and simple and diurnal persistence, respectively.

One quite important point: When comparing forecasts from different models exactly the same forecast points must be included. When NAs are present, not all models predict the same values, e.g. a persistence model will leave forecasts after NAs, also as NAs.

So to make sure that exactly the same points are included in the score calculation, we must only forecast points where all forecasts are available (i.e. non-NA):

```
# The non-NA for the first forecast
ok <- !is.na(D[[nms[1]]])
# Go through the remaining: all must be non-NA for a point
for(nm in nms[-1]){
ok <- ok & !is.na(D[[nm]])
}
ok <- as.data.frame(ok)
names(ok) <- pst("k",kseq)
# Lag to match resiuduals in time
ok <- lagdf(ok, "+k")
# Only the score period
ok <- ok & D$scoreperiod
# Finally, the vector with TRUE for all points with no NAs for any forecast
ok <- apply(ok, 1, all)
```

How many points are left?

Now the residuals can be calculated and the score:

```
# Use the residuals function
R <- residuals(D$Yhat1, D$y)
# And the score as a function of the horizon
score(R, scoreperiod=ok)$scoreval
## k1 k2 k3 k4 k5 k6 k7 k8 k9 k10 k11 k12 k13
## 0.806 0.807 0.813 0.814 0.815 0.816 0.816 0.816 0.816 0.816 0.816 0.814 0.814
## k14 k15 k16 k17 k18 k19 k20 k21 k22 k23 k24 k25 k26
## 0.814 0.814 0.813 0.812 0.812 0.813 0.811 0.810 0.809 0.810 0.815 0.820 0.826
## k27 k28 k29 k30 k31 k32 k33 k34 k35 k36
## 0.829 0.829 0.827 0.826 0.825 0.824 0.823 0.823 0.823 0.822
```

Calculated the score (default is RMSE) for all models:

Plot the RMSE as a function of the horizon:

```
RMSE <- as.data.frame(RMSE)
names(RMSE) <- nms
plot(0, type="n", xlim=range(kseq), ylim=range(RMSE), xlab="Horizon k", ylab="RMSE (kW)")
for(i in 1:length(RMSE)){
points(kseq, RMSE[ ,i], type="b", col=i)
}
```

As explained, it is most times not necessary to divide in a train and a test set, when fitting recursively, however it can be useful sometimes.

An easy approach is to set a logical vector, which is TRUE until the end of the training period:

then optimize the parameters only on this period, by taking a subset:

```
model$kseq <- c(3,18)
# Optimize the parameters
model$prm <- rls_optim(model, subset(D,D$trainperiod))$par
## ----------------
## Ta__a1 I__a1 lambda
## 0.90 0.90 0.99
## k3 k18 sum
## 0.607 0.618 1.224
## ----------------
## Ta__a1 I__a1 lambda
## 0.901 0.900 0.990
## k3 k18 sum
## 0.606 0.618 1.224
## ...output cropped
```

and then fit on the entire set:

```
# Fit for all horizons
model$kseq <- 1:36
# Fit with RLS
fittmp <- rls_fit(model$prm, model, D)
## ----------------
## Ta__a1 I__a1 lambda
## 0.925 0.697 0.988
```

Finally, the score can be calculated on the period following the train period by:

```
score_fit(fittmp, !D$trainperiod)$scoreval
## k1 k2 k3 k4 k5 k6 k7 k8 k9 k10 k11 k12 k13
## 0.906 0.890 0.891 0.893 0.895 0.893 0.892 0.891 0.890 0.890 0.889 0.889 0.890
## k14 k15 k16 k17 k18 k19 k20 k21 k22 k23 k24 k25 k26
## 0.889 0.893 0.896 0.897 0.899 0.902 0.906 0.908 0.914 0.914 0.920 0.922 0.921
## k27 k28 k29 k30 k31 k32 k33 k34 k35 k36
## 0.915 0.914 0.915 0.914 0.915 0.917 0.920 0.922 0.923 0.924
```

In this way it’s rather easy to set up different schemes, like optimizing the parameters once a week etc.

In the process of developing good forecasting models it is the always an interesting and informative experience (and necessary) to investigate the results of a model. Most of the time it boils down to investigating if there are any significant patterns left in the residuals - and how, if any, they can be described by extending the model.

Plot for the small model:

In the second plot we see the residuals and it’s clear that there is a diurnal pattern - and in the lower three plots the coefficients has also a diurnal pattern.

Plot for the larger model (plots not included here):

A shorter period (plots not included here):

```
xlim <- c("2011-01-01","2011-01-14")
plot_ts(fit1, xlim=xlim, kseq=kseq)
plot_ts(fit2, xlim=xlim, kseq=kseq)
```

The data plotted is returned:

```
tmp <- plot_ts(fit2, kseq=kseq, plotit=FALSE)
class(tmp)
## [1] "data.list"
names(tmp)
## [1] "y" "Yhat" "Residuals" "CumAbsResiduals"
## [5] "Ta" "I" "mu" "mu_tday.sin1"
## [9] "mu_tday.cos1" "mu_tday.sin2" "mu_tday.cos2" "mu_tday.sin3"
## [13] "mu_tday.cos3" "mu_tday.sin4" "mu_tday.cos4" "t"
# Residuals
plot_ts(tmp, c("^Residuals"), kseq=kseq)
```