slidingWindowTest: Obtain the predictions of a model using a sliding window...

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/experiments.R

Description

This function implements the sliding window learning method that is frequently used in time series forecasting. The function allows applying this methodology to any modelling technique. The function returns the predictions of this technique, when applied using a sliding window approach, for the given test set.

Usage

1
slidingWindowTest(learner, form, train, test, relearn.step = 1, verbose = T)

Arguments

learner

This is an object of the class learner (type "class?learner" for details) representing the system to evaluate.

form

A formula describing the prediction problem.

train

A data frame with the initial training data. The size of this training set will also determine the size of the sliding window.

test

A data frame with the test set for which we want predictions.

relearn.step

A number indicating the number of test cases until a new model is re-learned by sliding the training window to cases that are nearest to the current test case.

verbose

A boolean determining the level of verbosity of the function.

Details

The sliding window is a method frequently used to handle time series prediction problems. The basic motivation is that as time goes by the data gets "old" and thus the models should be re-learned to re-adjust for "fresher" data. This function implements this general idea for any modelling technique.

The function receives an initial training set whose size will determine the size of the sliding window. Using this initial set a first model is obtained with the supplied modelling technique. This model is applied to obtain predictions for the first relearn.step test cases. Afterwards a new model is obtained with the more recent training cases. This new set will have the same size as the initially provided training set. It is thus as if the training set slided forward relearn.step observations. This second model is again used to obtain predictions for another set of relearn.step test cases. The sliding process keeps going until we obtain predictions for all provided test cases.

Value

A vector with the predictions for the test set. Note that if the target variable is a factor this vector will also be a factor.

Author(s)

Luis Torgo ltorgo@dcc.fc.up.pt

References

Torgo, L. (2010) Data Mining using R: learning with case studies, CRC Press (ISBN: 9781439810187).

http://www.liaad.up.pt/~ltorgo/DataMiningWithR

See Also

growingWindowTest,monteCarlo

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
data(swiss)

## Obtain the predictions of model rpartXse() for the last 22 rows of
## the swiss data set, when used with a sliding window of 25 cases with
## a relearning step of 3

## The base learner used in the experiment
learnAndTest.rpartXse <- function(form, train, test, ...) {
    model <- rpartXse(form, train, ...)
    predict(model, test)
}

preds <- slidingWindowTest(learner('learnAndTest.rpartXse',pars=list(se=0.5)),
                           Infant.Mortality ~ .,
                           swiss[1:25,],
                           swiss[26:nrow(swiss),],
                           3)

## Some statistics of these predictions
regr.eval(swiss[26:nrow(swiss),'Infant.Mortality'],preds,stats = c("mae", "mse", "rmse"))

Example output

Loading required package: lattice
Loading required package: grid
********
      mae       mse      rmse 
 2.424054 10.474111  3.236373 

DMwR documentation built on May 1, 2019, 9:17 p.m.