In this vignette we provide a few tricks making it easier to work with the onlineforecast package.

Debug transformation functions

First define a model to be used in the examples:

# Load the package and data
library(onlineforecast)
D <- subset(Dbuilding, c("2010-12-15", "2011-01-01"))
D$y <- D$heatload

# Define a simple model 
model <- forecastmodel$new()
model$add_inputs(Ta = "Ta",
                 mu = "one()")
model$add_regprm("rls_prm(lambda=0.99)")
# Period to include in the evaluation of the score function
D$scoreperiod <- in_range("2010-12-20", D$t)
# And the sequence of horizons to fit for
model$kseq <- 1:6
# Add bounds for lambda (lower, init, upper)
model$add_prmbounds(lambda = c(0.9, 0.98, 0.999))

It’s actually easy to debug a function used in transformation by:

# Set debugging of the one() function
debug(one)
# Do the transformation, which will stop when entering one()
model$transform_data(D)
# Stop the debugging of one()
undebug(one)

Caching optimization results

Optimize the parameter and see the result:

# The optimization optimization of lambda
rls_optim(model, D)$par
##     lambda 
##      0.996

Instead of calculating the result each time, then cache the result:

tstart <- Sys.time()
# Caching can be done by providing a path
rls_optim(model, D, cachedir="cache")$par
##     lambda 
##      0.996
Sys.time() - tstart
##     Time difference of 0.75 secs

A file with the result of optim is now stored in the folder:

# See the cache files
dir("cache")
##     [1] "rls_optim_fa36ecaf6a59aa68a179d930d33aebf4_4586d7b25e1932d00f13d2290deb5c7f.RDS"

Even in this very simple example it’s much faster to read the cache instead of doing the optimization:

tstart <- Sys.time()
# So running again the result is read from the file
rls_optim(model, D, cachedir="cache")$par
##     lambda 
##      0.996
Sys.time() - tstart
##     Time difference of 0.011 secs

If anything affecting the results are changed, then optimization runs and the new result is cached:

# Change the lower bound for optimization
model$add_prmbounds(lambda = c(0.89, 0.98, 0.999))
# New