
Extract or compute hindcasts and forecasts for a fitted mvgam
object
Source: R/forecast.mvgam.R
forecast.mvgam.Rd
Extract or compute hindcasts and forecasts for a fitted mvgam
object
Usage
forecast(object, ...)
# S3 method for mvgam
forecast(object, newdata, data_test, n_cores = 1, type = "response", ...)
Arguments
- object
list
object of classmvgam
orjsdgam
. Seemvgam()
- ...
Ignored
- newdata
Optional
dataframe
orlist
of test data containing the same variables that were included in the originaldata
used to fit the model. If included, the covariate information innewdata
will be used to generate forecasts from the fitted model equations. If this samenewdata
was originally included in the call tomvgam
, then forecasts have already been produced by the generative model and these will simply be extracted and plotted. However if nonewdata
was supplied to the original model call, an assumption is made that thenewdata
supplied here comes sequentially after the data supplied in the original model (i.e. we assume there is no time gap between the last observation of series 1 in the original data and the first observation for series 1 innewdata
)- data_test
Deprecated. Still works in place of
newdata
but users are recommended to usenewdata
instead for more seamless integration intoR
workflows- n_cores
Deprecated. Parallel processing is no longer supported
- type
When this has the value
link
(default) the linear predictor is calculated on the link scale. Ifexpected
is used, predictions reflect the expectation of the response (the mean) but ignore uncertainty in the observation process. Whenresponse
is used, the predictions take uncertainty in the observation process into account to return predictions on the outcome scale. Whenvariance
is used, the variance of the response with respect to the mean (mean-variance relationship) is returned. Whentype = "terms"
, each component of the linear predictor is returned separately in the form of alist
(possibly with standard errors, ifsummary = TRUE
): this includes parametric model components, followed by each smooth component, but excludes any offset and any intercept. Two special cases are also allowed: typelatent_N
will return the estimated latent abundances from an N-mixture distribution, while typedetection
will return the estimated detection probability from an N-mixture distribution
Value
An object of class mvgam_forecast
containing hindcast and forecast distributions.
See mvgam_forecast-class
for details.
Details
Posterior predictions are drawn from the fitted mvgam
and used to simulate a forecast distribution
Examples
# \donttest{
simdat <- sim_mvgam(n_series = 3, trend_model = AR())
mod <- mvgam(y ~ s(season, bs = 'cc', k = 6),
trend_model = AR(),
noncentred = TRUE,
data = simdat$data_train,
chains = 2,
silent = 2)
# Hindcasts on response scale
hc <- hindcast(mod)
str(hc)
#> List of 15
#> $ call :Class 'formula' language y ~ s(season, bs = "cc", k = 6)
#> .. ..- attr(*, ".Environment")=<environment: 0x55c8e281c250>
#> $ trend_call : NULL
#> $ family : chr "poisson"
#> $ trend_model :List of 7
#> ..$ trend_model: chr "AR1"
#> ..$ ma : logi FALSE
#> ..$ cor : logi FALSE
#> ..$ unit : chr "time"
#> ..$ gr : chr "NA"
#> ..$ subgr : chr "series"
#> ..$ label : language AR()
#> ..- attr(*, "class")= chr "mvgam_trend"
#> $ drift : logi FALSE
#> $ use_lv : logi FALSE
#> $ fit_engine : chr "stan"
#> $ type : chr "response"
#> $ series_names : chr [1:3] "series_1" "series_2" "series_3"
#> $ train_observations:List of 3
#> ..$ series_1: int [1:75] 0 0 1 0 1 0 1 0 3 2 ...
#> ..$ series_2: int [1:75] 0 1 1 0 0 1 3 0 2 2 ...
#> ..$ series_3: int [1:75] 1 1 0 0 0 0 1 9 3 5 ...
#> $ train_times :List of 3
#> ..$ series_1: int [1:75] 1 2 3 4 5 6 7 8 9 10 ...
#> ..$ series_2: int [1:75] 1 2 3 4 5 6 7 8 9 10 ...
#> ..$ series_3: int [1:75] 1 2 3 4 5 6 7 8 9 10 ...
#> $ test_observations : NULL
#> $ test_times : NULL
#> $ hindcasts :List of 3
#> ..$ series_1: num [1:1000, 1:75] 0 0 0 0 0 0 1 0 4 2 ...
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : NULL
#> .. .. ..$ : chr [1:75] "ypred[1,1]" "ypred[2,1]" "ypred[3,1]" "ypred[4,1]" ...
#> ..$ series_2: num [1:1000, 1:75] 1 0 0 2 1 1 1 1 2 1 ...
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : NULL
#> .. .. ..$ : chr [1:75] "ypred[1,2]" "ypred[2,2]" "ypred[3,2]" "ypred[4,2]" ...
#> ..$ series_3: num [1:1000, 1:75] 0 2 3 1 1 0 0 2 0 5 ...
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : NULL
#> .. .. ..$ : chr [1:75] "ypred[1,3]" "ypred[2,3]" "ypred[3,3]" "ypred[4,3]" ...
#> $ forecasts : NULL
#> - attr(*, "class")= chr "mvgam_forecast"
# Use summary() to extract hindcasts / forecasts for custom plotting
head(summary(hc), 12)
#> # A tibble: 12 × 7
#> series time predQ50 predQ2.5 predQ97.5 truth type
#> <fct> <int> <dbl> <dbl> <dbl> <int> <chr>
#> 1 series_1 1 1 0 3 0 response
#> 2 series_1 2 0 0 3 0 response
#> 3 series_1 3 0 0 2 1 response
#> 4 series_1 4 0 0 2 0 response
#> 5 series_1 5 0 0 2 1 response
#> 6 series_1 6 0 0 2 0 response
#> 7 series_1 7 1 0 4 1 response
#> 8 series_1 8 1 0 5 0 response
#> 9 series_1 9 2 0 6 3 response
#> 10 series_1 10 2 0 6 2 response
#> 11 series_1 11 1 0 4 1 response
#> 12 series_1 12 1 0 3 0 response
# Or just use the plot() function for quick plots
plot(hc, series = 1)
#> No non-missing values in test_observations; cannot calculate forecast score
plot(hc, series = 2)
#> No non-missing values in test_observations; cannot calculate forecast score
plot(hc, series = 3)
#> No non-missing values in test_observations; cannot calculate forecast score
# Forecasts on response scale
fc <- forecast(mod,
newdata = simdat$data_test)
str(fc)
#> List of 16
#> $ call :Class 'formula' language y ~ s(season, bs = "cc", k = 6)
#> .. ..- attr(*, ".Environment")=<environment: 0x55c8e281c250>
#> $ trend_call : NULL
#> $ family : chr "poisson"
#> $ family_pars : NULL
#> $ trend_model :List of 7
#> ..$ trend_model: chr "AR1"
#> ..$ ma : logi FALSE
#> ..$ cor : logi FALSE
#> ..$ unit : chr "time"
#> ..$ gr : chr "NA"
#> ..$ subgr : chr "series"
#> ..$ label : language AR()
#> ..- attr(*, "class")= chr "mvgam_trend"
#> $ drift : logi FALSE
#> $ use_lv : logi FALSE
#> $ fit_engine : chr "stan"
#> $ type : chr "response"
#> $ series_names : Factor w/ 3 levels "series_1","series_2",..: 1 2 3
#> $ train_observations:List of 3
#> ..$ series_1: int [1:75] 0 0 1 0 1 0 1 0 3 2 ...
#> ..$ series_2: int [1:75] 0 1 1 0 0 1 3 0 2 2 ...
#> ..$ series_3: int [1:75] 1 1 0 0 0 0 1 9 3 5 ...
#> $ train_times :List of 3
#> ..$ series_1: int [1:75] 1 2 3 4 5 6 7 8 9 10 ...
#> ..$ series_2: int [1:75] 1 2 3 4 5 6 7 8 9 10 ...
#> ..$ series_3: int [1:75] 1 2 3 4 5 6 7 8 9 10 ...
#> $ test_observations :List of 3
#> ..$ series_1: int [1:25] 0 0 0 2 4 1 1 0 1 1 ...
#> ..$ series_2: int [1:25] 0 0 0 1 4 4 3 0 1 1 ...
#> ..$ series_3: int [1:25] 0 1 0 2 8 2 6 1 3 2 ...
#> $ test_times :List of 3
#> ..$ series_1: int [1:25] 76 77 78 79 80 81 82 83 84 85 ...
#> ..$ series_2: int [1:25] 76 77 78 79 80 81 82 83 84 85 ...
#> ..$ series_3: int [1:25] 76 77 78 79 80 81 82 83 84 85 ...
#> $ hindcasts :List of 3
#> ..$ series_1: num [1:1000, 1:75] 0 0 0 0 0 0 1 0 4 2 ...
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : NULL
#> .. .. ..$ : chr [1:75] "ypred[1,1]" "ypred[2,1]" "ypred[3,1]" "ypred[4,1]" ...
#> ..$ series_2: num [1:1000, 1:75] 1 0 0 2 1 1 1 1 2 1 ...
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : NULL
#> .. .. ..$ : chr [1:75] "ypred[1,2]" "ypred[2,2]" "ypred[3,2]" "ypred[4,2]" ...
#> ..$ series_3: num [1:1000, 1:75] 0 2 3 1 1 0 0 2 0 5 ...
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : NULL
#> .. .. ..$ : chr [1:75] "ypred[1,3]" "ypred[2,3]" "ypred[3,3]" "ypred[4,3]" ...
#> $ forecasts :List of 3
#> ..$ series_1: int [1:1000, 1:25] 0 0 0 0 0 1 0 0 1 1 ...
#> ..$ series_2: int [1:1000, 1:25] 0 0 0 0 0 0 0 1 0 1 ...
#> ..$ series_3: int [1:1000, 1:25] 0 0 0 1 0 0 0 0 0 0 ...
#> - attr(*, "class")= chr "mvgam_forecast"
head(summary(fc), 12)
#> # A tibble: 12 × 7
#> series time predQ50 predQ2.5 predQ97.5 truth type
#> <fct> <int> <dbl> <dbl> <dbl> <int> <chr>
#> 1 series_1 1 1 0 3 0 response
#> 2 series_1 2 0 0 3 0 response
#> 3 series_1 3 0 0 2 1 response
#> 4 series_1 4 0 0 2 0 response
#> 5 series_1 5 0 0 2 1 response
#> 6 series_1 6 0 0 2 0 response
#> 7 series_1 7 1 0 4 1 response
#> 8 series_1 8 1 0 5 0 response
#> 9 series_1 9 2 0 6 3 response
#> 10 series_1 10 2 0 6 2 response
#> 11 series_1 11 1 0 4 1 response
#> 12 series_1 12 1 0 3 0 response
plot(fc, series = 1)
#> Out of sample DRPS:
#> 9.69664
plot(fc, series = 2)
#> Out of sample DRPS:
#> 9.70004
plot(fc, series = 3)
#> Out of sample DRPS:
#> 25.049484
# Forecasts as expectations
fc <- forecast(mod,
newdata = simdat$data_test,
type = 'expected')
head(summary(fc), 12)
#> # A tibble: 12 × 6
#> series time predQ50 predQ2.5 predQ97.5 type
#> <fct> <int> <dbl> <dbl> <dbl> <chr>
#> 1 series_1 1 0.900 0.437 1.50 expected
#> 2 series_1 2 0.577 0.271 1.12 expected
#> 3 series_1 3 0.391 0.183 0.864 expected
#> 4 series_1 4 0.260 0.125 0.547 expected
#> 5 series_1 5 0.261 0.117 0.622 expected
#> 6 series_1 6 0.406 0.175 0.850 expected
#> 7 series_1 7 0.915 0.451 1.60 expected
#> 8 series_1 8 1.59 0.793 2.70 expected
#> 9 series_1 9 2.19 1.27 3.98 expected
#> 10 series_1 10 1.90 1.08 3.35 expected
#> 11 series_1 11 1.37 0.734 2.44 expected
#> 12 series_1 12 0.893 0.442 1.45 expected
plot(fc, series = 1)
plot(fc, series = 2)
plot(fc, series = 3)
# Dynamic trend extrapolations
fc <- forecast(mod,
newdata = simdat$data_test,
type = 'trend')
head(summary(fc), 12)
#> # A tibble: 12 × 6
#> series time predQ50 predQ2.5 predQ97.5 type
#> <fct> <int> <dbl> <dbl> <dbl> <chr>
#> 1 series_1 1 -0.0557 -0.718 0.388 trend
#> 2 series_1 2 -0.0380 -0.748 0.565 trend
#> 3 series_1 3 0.0277 -0.533 0.697 trend
#> 4 series_1 4 0.00146 -0.668 0.547 trend
#> 5 series_1 5 0.0364 -0.629 0.768 trend
#> 6 series_1 6 -0.0133 -0.688 0.610 trend
#> 7 series_1 7 -0.0209 -0.670 0.539 trend
#> 8 series_1 8 -0.0963 -0.749 0.398 trend
#> 9 series_1 9 0.0315 -0.537 0.626 trend
#> 10 series_1 10 -0.00243 -0.583 0.548 trend
#> 11 series_1 11 -0.0352 -0.688 0.472 trend
#> 12 series_1 12 -0.0572 -0.788 0.400 trend
plot(fc, series = 1)
plot(fc, series = 2)
plot(fc, series = 3)
# }