Package 'mvdlm'

Title: Multivariate Dynamic Linear Modelling With Stan
Description: Fits multivariate dynamic linear models in a Bayesian framework using Stan.
Authors: Eric J. Ward [aut, cre]
Maintainer: Eric J. Ward <[email protected]>
License: GPL (>=3)
Version: 0.1.0
Built: 2024-11-25 17:30:00 UTC
Source: https://github.com/atsa-es/mvdlm

Help Index


The 'mvdlm' package.

Description

Multivariate dynamic linear models fit with Stan

Author(s)

Maintainer: Eric J. Ward [email protected] (ORCID)

References

Stan Development Team (2020). RStan: the R interface to Stan. R package version 2.21.2. https://mc-stan.org

See Also

Useful links:


Fit a Bayesian multivariate dynamic linear model with Stan

Description

Fit a Bayesian multivariate dynamic linear model with Stan that optionally includes covariates to estimate effects, extremes (Student-t distribution), etc.

Usage

fit_dlm(
  formula = NULL,
  time_varying = NULL,
  time = "year",
  est_df = FALSE,
  family = c("normal", "binomial", "poisson", "nbinom2", "gamma", "lognormal"),
  correlated_rw = FALSE,
  data,
  chains = 3,
  iter = 2000,
  warmup = floor(iter/2),
  ...
)

Arguments

formula

The model formula for the fixed effects; at least this formula or time_varying needs to have the response included

time_varying

The model formula for the time-varying effects; at least this formula or formula needs to have the response included

time

String describing the name of the variable corresponding to time, defaults to "year"

est_df

Whether or not to estimate deviations of B as Student - t with estimated degrees of freedom, defaults to FALSE

family

The name of the family used for the response; can be one of "normal","binomial","possion","nbinom2","gamma","lognormal"

correlated_rw

Whether to estimate time-varying parameters as correlated random walk, defaults to FALSE

data

The data frame including response and covariates for all model components

chains

Number of mcmc chains, defaults to 3

iter

Number of mcmc iterations, defaults to 2000

warmup

Number iterations for mcmc warmup, defaults to 1/2 of the iterations

...

Any other arguments to pass to rstan::sampling().

Value

A list containing the fitted model and arguments and data used to fit the model. These include model (the fitted model object of class stanfit),

Examples

set.seed(123)
N = 20
data = data.frame("y" = runif(N),
                  "cov1" = rnorm(N),
                  "cov2" = rnorm(N),
                  "year" = 1:N,
                  "season" = sample(c("A","B"), size=N, replace=TRUE))
b_1 = cumsum(rnorm(N))
b_2 = cumsum(rnorm(N))
data$y = data$cov1*b_1 + data$cov2*b_2
time_varying = y ~ cov1 + cov2
formula = NULL

# fit a model with a time varying component
fit <- fit_dlm(formula = formula,
               time_varying = time_varying,
               time = "year",
               est_df = FALSE,
               family = c("normal"),
               data=data, chains = 1, iter = 20)

# fit a model with a time varying and fixed component (here, fixed intercept)
fit <- fit_dlm(formula = y ~ 1,
               time_varying = y ~ -1 + cov1 + cov2,
               time = "year",
               est_df = FALSE,
               family = c("normal"),
               data=data, chains = 1, iter = 20)

#' # fit a model with deviations modeled with a multivariate Student-t
fit <- fit_dlm(formula = y ~ 1,
               time_varying = y ~ -1 + cov1 + cov2,
               time = "year",
               est_df = TRUE,
               family = c("normal"),
               data=data, chains = 1, iter = 20)

#' #' # fit a model with deviations modeled with a multivariate Student-t
fit <- fit_dlm(formula = y ~ 1,
               time_varying = y ~ -1 + cov1 + cov2,
               time = "year",
               est_df = TRUE,
               family = c("normal"),
               data=data, chains = 1, iter = 20)