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

fit_dlm(
  formula = NULL,
  time_varying = NULL,
  time = "year",
  est_df = FALSE,
  family = c("normal", "binomial", "poisson", "nbinom2", "gamma", "lognormal"),
  correlated_rw = TRUE,
  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 TRUE

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

# \donttest{
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)
#> 
#> SAMPLING FOR MODEL 'dlm' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0.001949 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 19.49 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: WARNING: No variance estimation is
#> Chain 1:          performed for num_warmup < 20
#> Chain 1: 
#> Chain 1: Iteration:  1 / 20 [  5%]  (Warmup)
#> Chain 1: Iteration:  2 / 20 [ 10%]  (Warmup)
#> Chain 1: Iteration:  4 / 20 [ 20%]  (Warmup)
#> Chain 1: Iteration:  6 / 20 [ 30%]  (Warmup)
#> Chain 1: Iteration:  8 / 20 [ 40%]  (Warmup)
#> Chain 1: Iteration: 10 / 20 [ 50%]  (Warmup)
#> Chain 1: Iteration: 11 / 20 [ 55%]  (Sampling)
#> Chain 1: Iteration: 12 / 20 [ 60%]  (Sampling)
#> Chain 1: Iteration: 14 / 20 [ 70%]  (Sampling)
#> Chain 1: Iteration: 16 / 20 [ 80%]  (Sampling)
#> Chain 1: Iteration: 18 / 20 [ 90%]  (Sampling)
#> Chain 1: Iteration: 20 / 20 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 1.353 seconds (Warm-up)
#> Chain 1:                1.388 seconds (Sampling)
#> Chain 1:                2.741 seconds (Total)
#> Chain 1: 
#> Warning: The largest R-hat is NA, indicating chains have not mixed.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#r-hat
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess

# 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)
#> 
#> SAMPLING FOR MODEL 'dlm' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0.002437 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 24.37 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: WARNING: No variance estimation is
#> Chain 1:          performed for num_warmup < 20
#> Chain 1: 
#> Chain 1: Iteration:  1 / 20 [  5%]  (Warmup)
#> Chain 1: Iteration:  2 / 20 [ 10%]  (Warmup)
#> Chain 1: Iteration:  4 / 20 [ 20%]  (Warmup)
#> Chain 1: Iteration:  6 / 20 [ 30%]  (Warmup)
#> Chain 1: Iteration:  8 / 20 [ 40%]  (Warmup)
#> Chain 1: Iteration: 10 / 20 [ 50%]  (Warmup)
#> Chain 1: Iteration: 11 / 20 [ 55%]  (Sampling)
#> Chain 1: Iteration: 12 / 20 [ 60%]  (Sampling)
#> Chain 1: Iteration: 14 / 20 [ 70%]  (Sampling)
#> Chain 1: Iteration: 16 / 20 [ 80%]  (Sampling)
#> Chain 1: Iteration: 18 / 20 [ 90%]  (Sampling)
#> Chain 1: Iteration: 20 / 20 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 0.742 seconds (Warm-up)
#> Chain 1:                1.185 seconds (Sampling)
#> Chain 1:                1.927 seconds (Total)
#> Chain 1: 
#> Warning: The largest R-hat is NA, indicating chains have not mixed.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#r-hat
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess

#' # 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)
#> 
#> SAMPLING FOR MODEL 'dlm' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0.001717 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 17.17 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: WARNING: No variance estimation is
#> Chain 1:          performed for num_warmup < 20
#> Chain 1: 
#> Chain 1: Iteration:  1 / 20 [  5%]  (Warmup)
#> Chain 1: Iteration:  2 / 20 [ 10%]  (Warmup)
#> Chain 1: Iteration:  4 / 20 [ 20%]  (Warmup)
#> Chain 1: Iteration:  6 / 20 [ 30%]  (Warmup)
#> Chain 1: Iteration:  8 / 20 [ 40%]  (Warmup)
#> Chain 1: Iteration: 10 / 20 [ 50%]  (Warmup)
#> Chain 1: Iteration: 11 / 20 [ 55%]  (Sampling)
#> Chain 1: Iteration: 12 / 20 [ 60%]  (Sampling)
#> Chain 1: Iteration: 14 / 20 [ 70%]  (Sampling)
#> Chain 1: Iteration: 16 / 20 [ 80%]  (Sampling)
#> Chain 1: Iteration: 18 / 20 [ 90%]  (Sampling)
#> Chain 1: Iteration: 20 / 20 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 0.522 seconds (Warm-up)
#> Chain 1:                1.387 seconds (Sampling)
#> Chain 1:                1.909 seconds (Total)
#> Chain 1: 
#> Warning: There were 2 divergent transitions after warmup. See
#> https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
#> to find out why this is a problem and how to eliminate them.
#> Warning: Examine the pairs() plot to diagnose sampling problems
#> Warning: The largest R-hat is NA, indicating chains have not mixed.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#r-hat
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess

#' #' # 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)
#> 
#> SAMPLING FOR MODEL 'dlm' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0.001618 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 16.18 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: WARNING: No variance estimation is
#> Chain 1:          performed for num_warmup < 20
#> Chain 1: 
#> Chain 1: Iteration:  1 / 20 [  5%]  (Warmup)
#> Chain 1: Iteration:  2 / 20 [ 10%]  (Warmup)
#> Chain 1: Iteration:  4 / 20 [ 20%]  (Warmup)
#> Chain 1: Iteration:  6 / 20 [ 30%]  (Warmup)
#> Chain 1: Iteration:  8 / 20 [ 40%]  (Warmup)
#> Chain 1: Iteration: 10 / 20 [ 50%]  (Warmup)
#> Chain 1: Iteration: 11 / 20 [ 55%]  (Sampling)
#> Chain 1: Iteration: 12 / 20 [ 60%]  (Sampling)
#> Chain 1: Iteration: 14 / 20 [ 70%]  (Sampling)
#> Chain 1: Iteration: 16 / 20 [ 80%]  (Sampling)
#> Chain 1: Iteration: 18 / 20 [ 90%]  (Sampling)
#> Chain 1: Iteration: 20 / 20 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 4.432 seconds (Warm-up)
#> Chain 1:                2.939 seconds (Sampling)
#> Chain 1:                7.371 seconds (Total)
#> Chain 1: 
#> Warning: The largest R-hat is NA, indicating chains have not mixed.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#r-hat
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
# }