In this vignette we provide a few tricks making it easier to work with the onlineforecast package.
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)
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.332 secs
A file with the result of optim is now stored in the folder:
# See the cache files
dir("cache")
## [1] "rls_optim_78decf3d78ccc75a9a8d2c265f4e40b8_3d542f9acd88f4a302faf96dbfc6bfc8.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.00717 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 optimization results are calculated and cached
val <- rls_optim(model, D, cachedir="cache")
dir("cache")
## [1] "rls_optim_78decf3d78ccc75a9a8d2c265f4e40b8_3d542f9acd88f4a302faf96dbfc6bfc8.RDS"
## [2] "rls_optim_78decf3d78ccc75a9a8d2c265f4e40b8_a3dd6fd69438d3727f2422909e895eed.RDS"
The cache files can easily be removed by:
# To delete the cache folder
unlink("cache", recursive=TRUE)
This is also implemented in the `lm_optim()’ function.
The plot_ts
can be use plotly for plotting:
D <- Dbuilding
# Use plotly
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
# usely=TRUE
L <- plot_ts(D, c("heatload","Ta"), kseq=c(1,24), usely=TRUE, xlab="Time",
ylabs=c("Heat (kW)","Temperature (C)"))
# or exact same
L <- plotly_ts(D, c("heatload","Ta"), kseq=c(1,24), xlab="Time",
ylabs=c("Heat (kW)","Temperature (C)"))
#
# From plotly the figures are returned and can be further manipulated
# e.g. put the legend in the top by
L[[length(L)]] <- L[[length(L)]] %>% layout(legend = list(x = 100, y = 0.98))
print(subplot(L, shareX=TRUE, nrows=length(L), titleY = TRUE))
#' \donttest{
#' D <- Dbuilding
#'
#' plotly_ts(D, c("heatload","Ta"), kseq=c(1,24))
#' plotly_ts(D, c("heatload","Ta$|Taobs$"), kseq=c(1,24))
#' }
Also for a fit:
D$scoreperiod <- in_range("2010-12-22", D$t)
model <- forecastmodel$new()
model$output = "heatload"
model$add_inputs(Ta = "Ta",
mu = "one()")
model$add_regprm("rls_prm(lambda=0.9)")
model$kseq <- c(3,18)
fit1 <- rls_fit(NA, model, D, returnanalysis = TRUE)
## ----------------
## Argument 'prm' is NA, so parameters in 'model$prm' are used.
# Plot it
plot_ts(fit1)
# Plot it with plotly
plot_ts(fit1, usely=TRUE)