
Validate newdata against the training data of an mvgam model
Source: R/mvgam_core.R
check_newdata.RdRuns the structural checks that predict.mvgam() and
forecast.mvgam() apply to a newdata frame. A misaligned frame is
refused before an expensive prediction. The trend constructor in
trend_formula names the time and series columns, time and
series by default. Each of them the training data holds must
also be in newdata. A hierarchical trend identifies its series by
its gr and subgr columns. Those columns are then required. Series
levels absent from the training data are refused. The series column
of newdata is coerced to the training levels. The prediction
functions check the columns the formulas use.
Arguments
- newdata
A
data.frameof prediction covariates in the same shape as the training data.- data
The training
data.frameof the model, whose series levels define the valid set. A fitted model frommvgam()orjsdgam()is also accepted.newdatais then checked against every column the fitted formulas use.trend_formulais then ignored.- trend_formula
The trend formula passed to
mvgam(), orNULLfor a model without a trend.
Value
newdata with its series column coerced to the training
factor levels, returned invisibly, or NULL (invisibly) when
newdata is NULL. Checked against a fitted model, newdata
is returned with the columns that model predicts from.
Examples
train <- data.frame(
species = factor(rep(c("a", "b"), each = 3L)),
week = rep(1:3, times = 2L),
y = rnorm(6L)
)
test_weeks <- data.frame(
species = factor(rep(c("a", "b"), each = 2L)),
week = rep(4:5, times = 2L)
)
check_newdata(test_weeks, train, ~ AR(time = week, series = species))
# A species the model never saw is refused.
test_weeks$species <- factor(rep(c("a", "c"), each = 2L))
try(check_newdata(test_weeks, train, ~ AR(time = week, series = species)))
#> Error : Series in 'newdata' has levels absent from the training data.
#> Unseen: "c".
#> Fitted levels: "a" and "b".