## ----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 ) ## ----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)