## ----setup, include = FALSE--------------------------------------------------- has_stan <- requireNamespace("rstan", quietly = TRUE) eval_fits <- identical(Sys.getenv("DCVAR_EVAL_VIGNETTES"), "true") && has_stan knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = eval_fits ) ## ----mixed-margins------------------------------------------------------------ # library(dcvar) # # sim_mix <- simulate_dcvar( # n_time = 200, # rho_trajectory = rho_constant(200, rho = 0.6), # margins = c("normal", "exponential"), # skew_direction = c(1, 1), # seed = 11 # ) # # fit_mix <- dcvar_constant( # sim_mix$Y_df, # vars = c("y1", "y2"), # margins = c("normal", "exponential"), # skew_direction = c(1, 1), # seed = 11 # ) # # # Each dimension is reported under its own family: sigma_eps for the normal # # variable, sigma_exp for the exponential variable. # coef(fit_mix)[c("sigma_eps", "sigma_exp")] ## ----tv-fit------------------------------------------------------------------- # library(dcvar) # # # A series with declining coupling to illustrate the time-varying fits # sim_tv <- simulate_dcvar( # n_time = 150, # rho_trajectory = rho_decreasing(150, rho_start = 0.7, rho_end = 0.3), # seed = 3 # ) # # # Only the autoregressive coefficients evolve over time # fit_ar <- dcvar( # sim_tv$Y_df, # vars = c("y1", "y2"), # tv_phi = "ar", # seed = 4 # ) # # # Time-varying coefficients and time-varying scales together, with a # # right-skewed second margin (the exponential dimension uses the soft barrier) # fit_full <- dcvar( # sim_tv$Y_df, # vars = c("y1", "y2"), # margins = c("normal", "exponential"), # skew_direction = c(1, 1), # tv_phi = TRUE, # tv_sigma = TRUE, # seed = 5 # ) ## ----tv-extract--------------------------------------------------------------- # # Posterior summaries of phi11(t), phi12(t), phi21(t), phi22(t) # head(phi_trajectory(fit_ar)) # # # Per-variable residual-scale paths # head(sigma_trajectory(fit_full)) # # # Facetted trajectory plots # plot_phi_trajectory(fit_ar) # plot_sigma_trajectory(fit_full) ## ----fit-all------------------------------------------------------------------ # library(dcvar) # # # Simulate data with a step-function trajectory # sim <- simulate_dcvar( # n_time = 200, # rho_trajectory = rho_step(200, rho_before = 0.7, rho_after = 0.3), # seed = 42 # ) # # # Fit all three models on the same data # fit_dc <- dcvar(sim$Y_df, vars = c("y1", "y2"), seed = 1) # fit_hmm <- dcvar_hmm(sim$Y_df, vars = c("y1", "y2"), K = 2, seed = 2) # fit_con <- dcvar_constant(sim$Y_df, vars = c("y1", "y2"), seed = 3) ## ----compare------------------------------------------------------------------ # dcvar_compare(dcvar = fit_dc, hmm = fit_hmm, constant = fit_con) ## ----tidy--------------------------------------------------------------------- # # Full parameter summary as a data frame # param_df <- as.data.frame(fit_dc) # head(param_df) # # # Predictions with marginal intervals # pred_df <- predict(fit_hmm) # head(pred_df) ## ----hmm---------------------------------------------------------------------- # # State posteriors, Viterbi path, transition matrix # states <- hmm_states(fit_hmm) # # # State-specific rho values # states$rho_state # # # Transition matrix # states$A # # # Visualise state posteriors # plot(fit_hmm, type = "states") # # # Transition matrix heatmap # plot(fit_hmm, type = "transition") ## ----interpret---------------------------------------------------------------- # interpret_rho_trajectory(fit_dc) # interpret_rho_trajectory(fit_hmm) # interpret_rho_trajectory(fit_con)