View source: R/parsnip-bayesian_structural_reg.R
| bayesian_structural_reg | R Documentation |
bayesian_structural_reg() is a way to generate a specification of a Bayesian Structural Time Series Model
before fitting and allows the model to be created using
different packages. Currently the only package is bsts.
bayesian_structural_reg(mode = "regression", distribution = NULL)
mode |
A single character string for the type of model. The only possible value for this model is "regression". |
distribution |
The model family for the observation equation. Non-Gaussian model families use data augmentation to recover a conditionally Gaussian model. |
The data given to the function are not saved and are only used
to determine the mode of the model. For bayesian_structural_reg(), the
mode will always be "regression".
The model can be created using the fit() function using the
following engines:
"stan" (default) - Connects to bsts::bsts()
Main Arguments
Other options and argument can be
set using set_engine() (See Engine Details below).
If parameters need to be modified, update() can be used
in lieu of recreating the object from scratch.
stan (default engine)
The engine uses bsts::bsts().
Parameter Notes:
xreg - This is supplied via the parsnip / modeltime fit() interface
(so don't provide this manually). See Fit Details (below).
A model spec
Date and Date-Time Variable
It's a requirement to have a date or date-time variable as a predictor.
The fit() interface accepts date and date-time features and handles them internally.
fit(y ~ date)
Univariate (No xregs, Exogenous Regressors):
For univariate analysis, you must include a date or date-time feature. Simply use:
Formula Interface (recommended): fit(y ~ date) will ignore xreg's.
Multivariate (xregs, Exogenous Regressors)
The xreg parameter is populated using the fit() or fit_xy() function:
Only factor, ordered factor, and numeric data will be used as xregs.
Date and Date-time variables are not used as xregs
character data should be converted to factor.
Xreg Example: Suppose you have 3 features:
y (target)
date (time stamp),
month.lbl (labeled month as a ordered factor).
The month.lbl is an exogenous regressor that can be passed to the arima_reg() using
fit():
fit(y ~ date + month.lbl) will pass month.lbl on as an exogenous regressor.
Note that date or date-time class values are excluded from xreg.
fit.model_spec(), set_engine()
## Not run:
library(dplyr)
library(parsnip)
library(rsample)
library(timetk)
library(modeltime)
library(bayesmodels)
# Data
m750 <- m4_monthly %>% filter(id == "M750")
m750
# Split Data 80/20
splits <- initial_time_split(m750, prop = 0.8)
ss <- AddLocalLinearTrend(list(), training(splits)$value)
# Model Spec
model_spec <- bayesian_structural_reg() %>%
set_engine("stan", state.specification = ss)
# Fit Spec
model_fit <- model_spec %>%
fit(log(value) ~ date, data = training(splits))
model_fit
predict(model_fit, testing(splits))
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.