Skip to content
Snippets Groups Projects
setup-and-use-model.Rmd 14.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • pbac's avatar
    pbac committed
    ---
    title: "Setup and use onlineforecast models"
    author: "Peder Bacher"
    date: "`r Sys.Date()`"
    output:
      rmarkdown::html_vignette:
        toc: true
        toc_debth: 3
    vignette: >
      %\VignetteIndexEntry{Setup and use onlineforecast models}
      %\VignetteEngine{knitr::rmarkdown}
      %\VignetteEncoding{UTF-8}
    ---
    
    ```{r external-code, cache=FALSE, include=FALSE, purl = FALSE}
    # Have to load the knitr to use hooks
    library(knitr)
    # This vignettes name
    vignettename <- "setup-and-use-model"
    
    pbac's avatar
    pbac committed
    # REMEMBER: IF CHANGING IN THE shared-init (next block), then copy to the others!
    
    pbac's avatar
    pbac committed
    ```
    
    pbac's avatar
    pbac committed
    ```{r init, cache=FALSE, include=FALSE, purl=FALSE}
    
    # Width will scale all
    figwidth <- 12
    # Scale the wide figures (100% out.width)
    figheight <- 4
    # Heights for stacked time series plots
    figheight1 <- 5
    figheight2 <- 6.5
    figheight3 <- 8
    figheight4 <- 9.5
    figheight5 <- 11
    # Set the size of squared figures (same height as full: figheight/figwidth)
    owsval <- 0.35
    ows <- paste0(owsval*100,"%")
    ows2 <- paste0(2*owsval*100,"%")
    # 
    fhs <- figwidth * owsval
    
    # Set for square fig: fig.width=fhs, fig.height=fhs, out.width=ows}
    # If two squared the:  fig.width=2*fhs, fig.height=fhs, out.width=ows2
    
    # Check this: https://bookdown.org/yihui/rmarkdown-cookbook/chunk-styling.html
    # Set the knitr options
    knitr::opts_chunk$set(
      collapse = TRUE,
      comment = "##    ",
      prompt = FALSE,
      cache = TRUE,
    
    pbac's avatar
    pbac committed
      cache.path = paste0("../tmp/vignettes/tmp-",vignettename,"/"),
    
    pbac's avatar
    pbac committed
      fig.path = paste0("../tmp/vignettes/tmp-",vignettename,"/"),
    
      fig.height = figheight,
      fig.width = figwidth,
      out.width = "100%"
    )
    options(digits=3)
    
    hook_output <- knit_hooks$get("output")
    knit_hooks$set(output = function(x, options) {
      lines <- options$output.lines
      if (is.null(lines)) {
        return(hook_output(x, options))  # pass to default hook
      }
      x <- unlist(strsplit(x, "\n"))
      more <- "## ...output cropped"
      if (length(lines)==1) {        # first n lines
        if (length(x) > lines) {
          # truncate the output, but add ....
          x <- c(head(x, lines), more)
        }
      } else {
        x <- c(more, x[lines], more)
      }
      # paste these lines together
      x <- paste(c(x, ""), collapse = "\n")
      hook_output(x, options)
    })
    
    
    pbac's avatar
    pbac committed
    ```
    
    
    [onlineforecasting]: https://onlineforecasting.org/articles/onlineforecasting.pdf
    [building heat load forecasting]: https://onlineforecasting.org/examples/building-heat-load-forecasting.html
    [onlineforecasting.org]: https://onlineforecasting.org
    <!--shared-init-end-->
    
    pbac's avatar
    pbac committed
    
    
    ## Intro
    This vignette explains how to setup and use an onlineforecast
    
    pbac's avatar
    pbac committed
    model. This takes offset in the example of [building heat load
    forecasting] and assumes that the data is setup correctly, as explained in
    [setup-data](setup-data.html) vignette. The R code is available
    [here](setup-and-use-model.R). More information on [onlineforecasting.org].
    
    pbac's avatar
    pbac committed
    
    
    pbac's avatar
    pbac committed
    Start by loading the package:
    
    pbac's avatar
    pbac committed
    ```{r}
    
    pbac's avatar
    pbac committed
    # Load the package
    
    pbac's avatar
    pbac committed
    library(onlineforecast)
    
    pbac's avatar
    pbac committed
    # Set the data in D to simplify notation
    D <- Dbuilding
    
    pbac's avatar
    pbac committed
    ```
    
    
    ## Score period
    
    Set the `scoreperiod` as a logical vector with same length as `t`. It controls
    
    pbac's avatar
    pbac committed
    which points are included in score calculations in functions for optimization
    etc. It must be set.
    
    pbac's avatar
    pbac committed
    
    Use it to exclude a burn-in period of one week:
    ```{r}
    # Print the first time point
    D$t[1]
    # Set the score period 
    D$scoreperiod <- in_range("2010-12-22", D$t)
    # Plot to see it
    plot(D$t, D$scoreperiod, xlab="Time", ylab="Scoreperiod")
    ```
    Other periods, which should be excluded from score calculations, can simply
    also be set to `FALSE`. E.g.:
    ```{r}
    # Exclude other points example
    scoreperiod2 <- D$scoreperiod
    scoreperiod2[in_range("2010-12-30",D$t,"2011-01-02")] <- FALSE
    ```
    would exclude the days around new year (must of course be set in
    `D$scoreperiod`, not in `scoreperiod2` to have an effect).
    
    
    
    
    
    ## Setting up a model
    
    A simple onlineforecast model can be setup by:
    ```{r}
    # Generate new object (R6 class)
    model <- forecastmodel$new()
    # Set the model output
    model$output = "heatload"
    # Inputs (transformation step)
    model$add_inputs(Ta = "Ta",
    
    pbac's avatar
    pbac committed
                     mu = "one()")
    
    pbac's avatar
    pbac committed
    # Regression step parameters
    model$add_regprm("rls_prm(lambda=0.9)")
    # Optimization bounds for parameters
    model$add_prmbounds(lambda = c(0.9, 0.99, 0.9999))
    # Set the horizons for which the model will be fitted
    model$kseq <- c(3,18)
    ```
    
    
    ### Steps in setting up a model
    
    Let's go through the steps of setting up the model.
    
    First a new forecastmodel object is generated and the model output is set (per
    default it is `"y"`):
    ```{r}
    # Generate new object
    model <- forecastmodel$new()
    # Set the model output
    model$output = "heatload"
    ```
    The output is simply the variable name from `D` we want to forecast.
    
    The model inputs are defined by:
    ```{r}
    # Inputs (transformation step)
    model$add_inputs(Ta = "Ta",
    
    pbac's avatar
    pbac committed
                     mu = "one()")
    
    pbac's avatar
    pbac committed
    ```
    So this is really where the structure of the model is specified. The inputs are
    
    pbac's avatar
    pbac committed
    given a name (`Ta` and `mu`), which each are set as an R expression (given as a
    
    pbac's avatar
    pbac committed
    string). The expressions defines the **transformation step**: they will each
    be evaluated in an environment with a given `data.list`. This means that the
    
    pbac's avatar
    pbac committed
    variables from the data can be used in the expressions (e.g. `Ta` is in `D`) - below in [Input transformations] we will detail this evaluation.
    
    pbac's avatar
    pbac committed
    
    Next step for setting up the model is to set the parameters for the **regression
    
    pbac's avatar
    pbac committed
    step** by providing an expression, which returns the regression
    
    pbac's avatar
    pbac committed
    parameter values. In the present case we will use the Recursive Least Squares
    
    pbac's avatar
    pbac committed
    (RLS) when regressing, and we need to set the forgetting factor `lambda` by:
    
    pbac's avatar
    pbac committed
    ```{r}
    # Regression step parameters
    model$add_regprm("rls_prm(lambda=0.9)")
    ```
    
    The expression is just of a function, which returns
    
    pbac's avatar
    pbac committed
    a list - in this case with the value of `lambda` (see [onlineforecasting]). The result of it begin evaluated is kept in:
    
    pbac's avatar
    pbac committed
    ```{r}
    # The evaluation happens with
    eval(parse(text="rls_prm(lambda=0.9)"))
    # and the result is stored in
    model$regprm 
    ```
    
    We will tune the parameters, for this model it's only the forgetting
    factor, so we set the parameter bounds (lower, init, upper) for it by:
    ```{r}
    # Optimization bounds for parameters
    model$add_prmbounds(lambda = c(0.9, 0.99, 0.9999))
    ```
    
    Finally, we set the horizons for which to fit:
    ```{r}
    # Set the horizons for which the model will be fitted
    model$kseq <- c(3,18)
    ```
    The horizons to fit for is actually not directly related to the model, but
    rather the fitting of the model. In principle, it would be more "clean" if the
    model, data and fit was kept separate, however for recursive fitting this
    becomes un-feasible.
    
    
    ### Tune the parameters
    
    We have set up the model and can now tune the `lambda` with the `rls_optim()`,
    which is a wrapper for the `optim()` function:
    ```{r, output.lines=15}
    # Call the optim() wrapper
    model$prm <- rls_optim(model, D)$par
    ```
    Note, how it only calculated a score for the 3 and 18 steps
    horizons - as we specified with `model$kseq` above. The parameters could be
    optimized separately for each horizon, for example it is often such that for the
    first horizons a very low forgetting factor is optimal (e.g. 0.9). Currently,
    however, the parameters can only be optimized together. By optimizing for a
    short (3 steps) and a long horizon (18 steps), we obtain a balance - using less computations compared to optimizing on all horizons.
    
    The optimization converge and the tuned parameter becomes:
    ```{r}
    # Optimized lambda
    model$prm
    ```
    
    Now we can fit with the optimized `lambda` on all horizons over the entire period:
    ```{r}
    # Set to fit for all horizons
    model$kseq <- 1:36
    # Fit for all on entire period in D
    fit1 <- rls_fit(model$prm, model, D)
    ```
    
    See the summary of the fit:
    ```{r}
    # See the summary of the fit
    summary(fit1)
    ```
    See `?summary.rls_fit` for details.
    
    
    Plot the forecasts (`Yhat` adheres to the forecast matrix format and in
    `plot_ts()` the forecasts are lagged `k` steps to be aligned with the observations):
    ```{r}
    # Put the forecasts in D
    D$Yhat1 <- fit1$Yhat
    # Plot them for selected horizons
    
    pbac's avatar
    pbac committed
    plot_ts(D, c("^heatload$|^Y"), kseq = c(1,6,18,36))
    
    pbac's avatar
    pbac committed
    ```
    We clearly see the burn-in period, where the forecasts vary a lot, 
    
    Plot a forecast for a particular time point and forward in time:
    ```{r, fig.height=4}
    # Select a point
    i <- 996-48
    # and kseq steps ahead
    iseq <- i+model$kseq
    # The observations ahead in time
    plot(D$t[iseq], D$heatload[iseq], type = "b", xlab = "t", ylab = "y")
    title(main=pst("Forecast available at ",D$t[i]))
    # The forecasts
    lines(D$t[iseq], D$Yhat1[i, ], type = "b", col = 2)
    legend("topright", c("Observations",pst("Predictions (",min(model$kseq)," to ",max(model$kseq)," steps ahead)")), lty = 1, col = 1:2)
    ```
    
    
    ## Input transformations
    
    The inputs can be transformations of the variables in the data, i.e. `D` in this
    
    pbac's avatar
    pbac committed
    example. The function `one()` generate a forecast matrix of 1 for the needed
    
    pbac's avatar
    pbac committed
    horizons. It cannot be called directly:
    ```{r, eval=FALSE}
    # This will give error
    
    pbac's avatar
    pbac committed
    one()
    
    pbac's avatar
    pbac committed
    ```
    (the code above was not executed)
    
    however we can see the result of the evaluation by:
    ```{r}
    # Evaluate input expressions
    datatr <- model$transform_data(D)
    # See what came out
    summary(datatr)
    
    pbac's avatar
    pbac committed
    # In particular for the mu = "one()"
    
    pbac's avatar
    pbac committed
    head(datatr$mu)
    ```
    
    If we wanted to debug we could:
    ```{r, eval=FALSE}
    # Set to debug
    
    pbac's avatar
    pbac committed
    #debug(one)
    # Run the input transformation now and it will stop in one()
    
    pbac's avatar
    pbac committed
    datatr <- model$transform_data(D)
    # Set to undebug
    
    pbac's avatar
    pbac committed
    #undebug(one)
    
    pbac's avatar
    pbac committed
    ```
    (the code above was not executed).
    
    
    Let's extend the model by adding a low-pass filter transformation of the
    ambient temperature forecasts. We could just update the input by:
    ```{r}
    # Just update the Ta input by
    model$add_inputs(Ta = "lp(Ta, a1=0.9)")
    ```
    
    but let's just repeat the whole model definition for clarification - including
    the new transformation: 
    ```{r} 
    # Define a new model with low-pass filtering of the Ta input
    model <- forecastmodel$new()
    model$output = "heatload"
    model$add_inputs(Ta = "lp(Ta, a1=0.9)",
    
    pbac's avatar
    pbac committed
                     mu = "one()")
    
    model$add_regprm("rls_prm(lambda=0.99)")
    
    pbac's avatar
    pbac committed
    model$add_prmbounds(Ta__a1 = c(0.5, 0.9, 0.9999),
                        lambda = c(0.9, 0.99, 0.9999))
    model$kseq <- c(3,18)
    ```
    Note how also a new set of parameter bounds were added in `add_prmbounds()`
    following a neat little syntax: `Ta__a1` indicates that the first appearance of `a1` in the `Ta` input expression, will be changed in the optimization.
    
    We can see the parameter bounds with:
    ```{r}
    model$prmbounds
    ```
    
    To inspect the result of low-pass filtering:
    ```{r}
    # Low-pass filter Ta (with a1=0.9 as defined above)
    datatr <- model$transform_data(D)
    # Actually, lp() can be called directly (although two warnings are thrown)
    Talp <- lp(D$Ta, a1=0.99)
    ```
    and to see the result we could: 
    ```{r}
    # Plot the Ta$k1 forecasts
    plot(D$t, D$Ta$k1, type="l")
    # Add the filtered with a1=0.9
    lines(D$t, datatr$Ta[ ,"k1"], col=2)
    # Add the filtered with a1=0.99
    lines(D$t, Talp[ ,"k1"], col=3)
    ```
    hence with a low-pass coefficient `a1=0.99`, which is very high (max is 1), the
    Ta forecast is really smoothed, which models a system with a time constant
    (i.e. slow dynamics, e.g. well insulated and building with lots of concrete).
    
    There are quite a few functions available for input transformations:
    
    
    pbac's avatar
    pbac committed
    - `one()` generates an matrix of ones (for including an intercept).
    
    pbac's avatar
    pbac committed
    - `fs()` generate Fourier series for modelling harmonic functions.
    - `bspline()` wraps the `bs()` function for generating base splines.
    
    pbac's avatar
    pbac committed
    - `pbspline()` wraps the `pbs()` function for generating periodic base splines.
    
    pbac's avatar
    pbac committed
    - `AR()` generates auto-regressive model inputs.
    
    
    pbac's avatar
    pbac committed
    and they can even be combined, see more details in [onlineforecasting] and in their help
    
    pbac's avatar
    pbac committed
    description, e.g. `?fs`.
    
    
    Tuning the two parameters: the low-pass filter coefficient `a1` and the
    forgetting factor `lambda`, can now be done:
    ```{r, output.lines=15}
    # Optimize the parameters
    model$prm <- rls_optim(model, D)$par
    ```
    
    
    Plot the forecasts (Yhat adheres to the forecast matrix format and in `plot_ts` the forecasts are lagged `k` steps to sync with the observations)
    ```{r, fig.height=4}
    # Fit for all horizons
    model$kseq <- 1:36
    # Fit with RLS
    fit2 <- rls_fit(model$prm, model, D)
    # Take the forecasts
    D$Yhat2 <- fit2$Yhat
    # Plot all
    plot_ts(D, c("^heatload$|^Y"), kseq = c(1,18))
    ```
    
    We can see the summary:
    ```{r}
    summary(fit2)
    ```
    
    but more interesting is it to see if an improvement was achieved with the
    low-pass filtering, so calculate the RMSE for both models:
    ```{r}
    # Calculate the score
    RMSE1 <- summary(fit1, printit=FALSE)$scoreval
    RMSE2 <- summary(fit2, printit=FALSE)$scoreval
    ```
    Now, this is calculated for the points included in the `scoreperiod`, so it's
    important to make sure that exactly the same values are forecasted. A check can
    be done by:
    ```{r}
    # Check that all NAs in the scoreperiod are at the same positions
    all(is.na(fit1$Yhat[fit1$data$scoreperiod, ]) == is.na(fit2$Yhat[fit2$data$scoreperiod, ]))
    ```
    
    Finally, plot the RMSE for the two models:
    ```{r}
    # Plot the score for the two models
    plot(RMSE1, xlab="Horizon k", ylab="RMSE", type="b", ylim=range(RMSE1,RMSE2))
    lines(RMSE2, type="b", col=2)
    legend("topleft", c("Input: Ta","Input: Low-pass Ta"), lty=1, col=1:2)
    ```
    We can see, that we obtained improvements. Around 3-4% for the longer horizons.
    
    
    pbac's avatar
    pbac committed
    For more on evaluation, see the vignette [forecast-evaluation](forecast-evaluation.html).
    
    pbac's avatar
    pbac committed
    
    
    pbac's avatar
    pbac committed
    See more on how to extend this model even further in [building heat load forecasting].
    
    pbac's avatar
    pbac committed
    
    
    ## Time of day and using observations as input
    
    ### Time of day as input
    
    Often we need to have the time of day as an input to a forecastmodel:
    ```{r, output.lines=28}
    make_tday(D$t, kseq=1:3)
    ```
    So we can use it like this:
    ```{r}
    D$tday <- make_tday(D$t, 1:36)
    ```
    See the help `?make_tday` for more details.
    
    
    ### Using observations as input
    
    If we want to use observations in inputs to a model, we can use e.g.:
    ```{r}
    
    D$Tao <- make_input(D$Taobs, kseq=1:36)
    
    pbac's avatar
    pbac committed
    model$add_inputs(Tao = "lp(Tao, a1=0.99)")
    ```
    
    
    ## Caching of optimized parameters
    
    Working with time consuming calculations caching can be very
    valuable. The optimization results can be cached by providing a path to a
    
    directory, by setting the argument 'cachedir' to e.g. "cache". See the vignette
    
    pbac's avatar
    pbac committed
    [nice-tricks](https://onlineforecasting.org/vignettes/nice-tricks.html) for an
    
    example with code.
    
    pbac's avatar
    pbac committed
    
    
    ## Deep clone model
    
    Usually, an object of an R6 class can be copied (in memory) deeply with
    '$clone(deep=TRUE)', however that will result in problems with the
    forecastmodels, therefore the deep clone must be done by:
    ```{r}
    m1 <- model$clone_deep()
    ```
    See `?R6` for details on R6 objects.