rls_fit {onlineforecast} | R Documentation |
Fit an onlineforecast model with Recursive Least Squares (RLS).
Description
This function fits the onlineforecast model to the data and returns either: model validation data or just the score value.
Usage
rls_fit(
prm = NA,
model,
data,
scorefun = NA,
returnanalysis = TRUE,
runcpp = TRUE,
printout = TRUE
)
Arguments
prm |
vector with the parameters for fitting. Deliberately as the first element to be able to use |
model |
as an object of class forecastmodel: The model to be fitted. |
data |
as a data.list with the data to fit the model on. |
scorefun |
as a function (optional), default is |
returnanalysis |
as a logical. If FALSE then the sum of the scoreval on all horizons are returned, if TRUE a list with values for analysis. |
runcpp |
logical: If true the c++ implementation of RLS is run, if false the R implementation is run (slower). |
printout |
logical: If TRUE the offline parameters and the score function value are printed. |
Details
This function has three main purposes (in the examples these three are demonstrated in the examples):
- Returning model validation data, such as residuals and recursive estimated parameters.
- For optimizing the parameters using an R optimizer function. The parameters to optimize for is given in prm
- Fitting a model to data and saving the final state in the model object (such that from that point the model can be updated recursively as new data is received).
Note, if the scorefun
is given the data$scoreperiod
must be set to (int or logical) define which points to be evaluated in the scorefun.
Value
Depends on:
- If returnanalysis
is TRUE a list containing:
* Yhat
: data.frame with forecasts for model$kseq
horizons.
* model
: The forecastmodel object cloned deep, so can be modified without changing the original object.
* data
: data.list with the data used, see examples on how to obtain the transformed data.
* Lfitval
: list with RLS coefficients in a data.frame for each horizon, use plot_ts.rls_fit
to plot them and to obtain them as a data.frame for each coefficient.
* scoreval
: data.frame with the scorefun result on each horizon (only scoreperiod is included).
- If returnanalysis
is FALSE (and scorefun
is given): The sum of the score function on all horizons (specified with model$kseq).
See Also
For optimizing parameters rls_optim()
, for summary summary.rls_fit
, for plotting plot_ts.rls_fit()
, and the other functions starting with 'rls_'.
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$output <- "y"
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 with RLS and get the model validation analysis data
fit <- rls_fit(model = model, data = D)
# What did we get back?
names(fit)
# The one-step forecast
plot(D$y, type="l")
lines(fit$Yhat$k1, col=2)
# The one-step RLS coefficients over time (Lfitval is a list of the fits for each horizon)
plot(fit$Lfitval$k1$Ta, type="l")
# A summary
summary(fit)
# Plot the fit
plot_ts(fit, kseq=1)
# Fitting with lower lambda makes the RLS coefficients change faster
fit2 <- rls_fit(prm = c(lambda=0.9), model, D)
plot_ts(fit2, kseq=1)
# It can return a score
rls_fit(c(lambda=0.9), model, D, scorefun=rmse, returnanalysis=FALSE)
# Such that it can be passed to an optimzer (see ?rls_optim for a nice wrapper of optim)
val <- optim(c(lambda=0.99), rls_fit, model = model, data = D, scorefun = rmse,
returnanalysis=FALSE)
val$par
# Which can then simply be applied
rls_fit(val$par, model, D, scorefun=rmse, returnanalysis=FALSE)
# see ?rls_optim, how optim is wrapped for a little easiere use
# See rmse as a function of horizon
fit <- rls_fit(val$par, model, D, scorefun = rmse)
plot(fit$scoreval, xlab="Horizon k", ylab="RMSE")
# See ?score for a little more consistent way of calculating this
# Try adding a low-pass filter to Ta
model$add_inputs(Ta = "lp(Ta, a1=0.92)")
# To obtain the transformed data, i.e. the data which is used as input to the RLS
model$reset_state()
# Generate the the transformed data
datatr <- model$transform_data(D)
# What did we get?
str(datatr)
# See the effect of low-pass filtering
plot(D$Ta$k1, type="l")
lines(datatr$Ta$k1, col=2)
# Try changing the 'a1' coefficient and rerun
# ?rls_optim for how to optimize also this coefficient