# Model selection

## Intro

This vignette provides a short overview on the functionalities for model selection in 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:

library(onlineforecast)

Just start by taking a rather short data set:

# The data, just a rather short period to keep running times short
D <- subset(Dbuilding, c("2010-12-15", "2011-02-01"))
# Set the score period
D$scoreperiod <- in_range("2010-12-22", D$t)
#
D$tday <- make_tday(D$t, 1:36)

As a test we generate a random sequence and will use that as an input. In the model selection this should not be selected in the final model:

# Generate an input which is just random noise, i.e. should be removed in the selection
set.seed(83792)
D$noise <- make_input(rnorm(length(D$t)), 1:36)

Set up a model, which will serve as the full model in the selection:

# The full model
model <- forecastmodel$new() # Set the model output model$output = "heatload"
# Inputs (transformation step)
model$add_inputs(Ta = "Ta", noise = "noise", mu_tday = "fs(tday/24, nharmonics=4)", mu = "one()") # 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)) Finally, set the horizons to run (just keep a vector for later use): # Select a model, just run it for a single horizon kseq <- 5 Now we can use the step_optim() function for the selection. In each step new models are generated, with either one removed input or one added input, and then all the generated models are optimized and their scores compared. If any new model have an improved score compared to the currently selected model, then the new is selected and the process is repeated until no new improvement is achieved. In addition to selecting inputs, then integer parameters can also be stepped through, e.g. the order of basis splined or the number of harmonics in Fourier series. Set which parameters to change in the step: # The range to select the number of harmonics parameter in prm <- list(mu_tday__nharmonics = c(min=2, max=6)) Several stepping procedures are implemented: • If the direction is “both”, which is default (same as “backwardboth”) then the stepping is: • In first step inputs are removed one-by-one. • In following steps, inputs still in the model are removed one-by-one, and inputs not in the model are added one-by-one. • If the direction is “backwards”: • Inputs are only removed in each step. • If the direction is “forwardboth”: • In the first step all inputs are removed. • In following steps (same as “both”). • If the direction is “forward”: • In the first step all inputs are removed and from there inputs are only added. The default procedure is backward selection with stepping in both directions: # Run the default selection, which is "both" and equivalent to "backwadboth" # Note the control argument, which is passed to optim, it's now set to few # iterations in the prm optimization Lboth <- step_optim(model, D, kseq, prm, direction="both", control=list(maxit=1)) We now have the models selected in each step in and we see that the final model is decreased: getse(Lboth, "model") ## [[1]] ## ## Output: heatload ## Inputs: Ta = Ta ## noise = noise ## mu_tday = fs(tday/24, nharmonics=5) ## mu = one() ## ## ## [[2]] ## ## Output: heatload ## Inputs: Ta = Ta ## mu_tday = fs(tday/24, nharmonics=5) ## mu = one() Forward selection: Lforward <- step_optim(model, D, kseq, prm, "forward", control=list(maxit=1)) getse(Lforward, "model") ## [[1]] ## ## Output: heatload ## Inputs: mu = one() ## ## ## [[2]] ## ## Output: heatload ## Inputs: mu = one() ## Ta = Ta ## ## ## [[3]] ## ## Output: heatload ## Inputs: mu = one() ## Ta = Ta ## mu_tday = fs(tday/24, nharmonics=2) ## ## ## [[4]] ## ## Output: heatload ## Inputs: mu = one() ## Ta = Ta ## mu_tday = fs(tday/24, nharmonics=3) ## ## ## [[5]] ## ## Output: heatload ## Inputs: mu = one() ## Ta = Ta ## mu_tday = fs(tday/24, nharmonics=4) ## ## ## [[6]] ## ## Output: heatload ## Inputs: mu = one() ## Ta = Ta ## mu_tday = fs(tday/24, nharmonics=5) Same model is selected, which is also the case in backwards selection: Lbackward <- step_optim(model, D, kseq, prm, "backward", control=list(maxit=1)) getse(Lbackward, "model") ## [[1]] ## ## Output: heatload ## Inputs: Ta = Ta ## noise = noise ## mu_tday = fs(tday/24, nharmonics=6) ## mu = one() ## ## ## [[2]] ## ## Output: heatload ## Inputs: Ta = Ta ## noise = noise ## mu_tday = fs(tday/24, nharmonics=5) ## mu = one() ## ## ## [[3]] ## ## Output: heatload ## Inputs: Ta = Ta ## mu_tday = fs(tday/24, nharmonics=5) ## mu = one() Give a starting model. The selection will start from this model: # Clone the model to make a starting model modelstart <- model$clone_deep()
# Remove two inputs
modelstart\$inputs[2:3] <- NULL
# Run the selection
L <- step_optim(model, D, kseq, prm, modelstart=modelstart, control=control)
##     Warning in is.na(modelstart): is.na() applied to non-(list or vector) of type
##     'environment'
getse(L, "model")
##     [[1]]
##
##     Inputs: Ta = Ta
##              mu = one()
##
##
##     [[2]]
##