Skip to content

Recommended way to fit crossvalidated models #177

@hongooi73

Description

@hongooi73

This touches on fable, fabletools and tsibble so I'm not sure where to put it....

What is the best way to use time series CV to build and test models in the tidyverts framework? Right now, my code is rather manual: I build a sequence of datasets for training and testing, fit models to each training set, and predict on the test set.

subset_oj_data <- function(start, end)
{
    start <- yearweek(start_date + start*7)
    end <- yearweek(start_date + end*7)
    filter(oj_data, week >= start, week <= end)
}

# oj_train is a list of tsibbles
oj_train <- lapply(train_periods,
    function(i) subset_oj_data(settings$FIRST_WEEK, i))

# oj_test is a list of tsibbles
oj_test <- lapply(train_periods,
    function(i) subset_oj_data(i + settings$GAP, i + settings$GAP + settings$HORIZON - 1))

# a list of models, one for each training set
oj_models <- lapply(oj_train,
    function(df) model(df, ar=arima(...))

# a list of forecast results, one for each model/test set
oj_fcasts <- Map(forecast, oj_models, oj_test)

The text at https://otexts.com/fpp3/tscv.html results in null models and error messages when I run it, so it may have been obsoleted since it was written. It also runs forecast with a horizon arg, whereas I'm looking for something to predict on an actual test dataset.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions