rls_optim {onlineforecast} | R Documentation |
Optimize parameters for onlineforecast model fitted with RLS
Description
Optimize parameters (transformation stage) of RLS model
Usage
rls_optim(
model,
data,
kseq = NA,
scorefun = rmse,
cachedir = "",
cachererun = FALSE,
printout = TRUE,
method = "L-BFGS-B",
...
)
Arguments
model |
The onlineforecast model, including inputs, output, kseq, p |
data |
The data.list which holds the data on which the model is fitted. |
kseq |
The horizons to fit for (if not set, then model$kseq is used) |
scorefun |
The function to be score used for calculating the score to be optimized. |
cachedir |
A character specifying the path (and prefix) of the cache file name. If set to |
cachererun |
A logical controlling whether to run the optimization even if the cache exists. |
printout |
A logical determining if the score function is printed out in each iteration of the optimization. |
method |
The method argument for |
... |
Additional parameters to |
Details
This is a wrapper for optim
to enable easy use of bounds and caching in the optimization.
One smart trick, is to cache the optimization results. Caching can be done by providing a path to the
cachedir
argument (relative to the current working directory).
E.g. rls_optim(model, D, cachedir="cache")
will write a file in the folder 'cache', such that
next time the same call is carried out, then the file is read instead of running the optimization again.
See the example in https://onlineforecasting.org/vignettes/nice-tricks.html.
Value
Result object of optim().
Parameters resulting from the optimization can be found from result$par
See Also
link{optim}
for how to control the optimization.
Examples
# Take data
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)")
# Before fitting the model, define which points 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
# Now we can fit the model and get the score, as it is
rls_fit(model=model, data=D, scorefun=rmse, returnanalysis=FALSE)
# Or we can change the lambda
rls_fit(c(lambda=0.9), model, D, rmse, returnanalysis=FALSE)
# This could be passed to optim() (or any optimizer, see forecastmodel$insert_prm()).
optim(c(lambda=0.98), rls_fit, model=model, data=D, scorefun=rmse, returnanalysis=FALSE,
lower=c(lambda=0.9), upper=c(lambda=0.999), method="L-BFGS-B")
# rls_optim is simply a helper, it's makes using bounds easiere and enables caching of the results
# First add bounds for lambda (lower, init, upper)
model$add_prmbounds(lambda = c(0.9, 0.98, 0.999))
# Now the same optimization as above can be done by
val <- rls_optim(model, D)
val