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-08 05:16:47 UTC |
Source: | https://github.com/atsa-es/mvdlm |
Multivariate dynamic linear models fit with Stan
Maintainer: Eric J. Ward [email protected] (ORCID)
Stan Development Team (2020). RStan: the R interface to Stan. R package version 2.21.2. https://mc-stan.org
Useful links:
Summarize and plot time varying coefficients from the fitted model
dlm_trends(fitted_model)
dlm_trends(fitted_model)
fitted_model |
A fitted model object |
A list containing the plot and data used
to fit the model. These include plot
and b_varying
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 <- fit_dlm(formula = formula, time_varying = time_varying, time = "year", est_df = FALSE, family = c("normal"), data=data, chains = 1, iter = 20) dlm_trends(fit)
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 <- fit_dlm(formula = formula, time_varying = time_varying, time = "year", est_df = FALSE, family = c("normal"), data=data, chains = 1, iter = 20) dlm_trends(fit)
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 = FALSE, data, chains = 3, iter = 2000, warmup = floor(iter/2), ... )
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), ... )
formula |
The model formula for the fixed effects; at least this formula or |
time_varying |
The model formula for the time-varying effects; at least this formula or |
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 |
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 |
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
),
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)
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)