### Install chkptstanr from GitHub Source: https://github.com/venpopov/chkptstanr/blob/master/README.md Install the development version of the package from GitHub to access the latest fixes. ```r remotes::install_github("venpopov/chkptstanr") ``` -------------------------------- ### Reset Sampling with reset = TRUE Source: https://github.com/venpopov/chkptstanr/blob/master/README.md To reset sampling and start from scratch, use `reset = TRUE`. This is permissible as long as key arguments like formula, data, or family have not changed. The `stop_after` argument can be modified. ```r fit1 <- chkpt_brms(count ~ zAge + zBase * Trt + (1|patient), data = epilepsy, family = poisson(), iter_per_chkpt = 200, path = 'checkpoints/epilepsy', stop_after = 1600, reset = TRUE) ``` -------------------------------- ### Load required packages Source: https://github.com/venpopov/chkptstanr/blob/master/README.md Load the necessary libraries to begin using chkptstanr with brms and lme4. ```r library(chkptstanr) library(brms) library(lme4) ``` -------------------------------- ### make_brmsfit Source: https://context7.com/venpopov/chkptstanr/llms.txt Converts checkpoint output files into a standard brmsfit object. ```APIDOC ## make_brmsfit ### Description Convert checkpoint output files into a standard brmsfit object. Primarily used internally but can be called directly if needed. ### Parameters #### Request Body - **formula** (formula) - Required - The model formula. - **data** (data.frame) - Required - The dataset used for fitting. - **family** (family) - Required - The response distribution family. - **path** (string) - Required - The path to the checkpoint directory. ### Response - **brmsfit** (object) - A standard brmsfit object. ``` -------------------------------- ### Create brmsfit Object from Checkpoints Source: https://context7.com/venpopov/chkptstanr/llms.txt Convert checkpoint output files into a standard brmsfit object. This is typically done automatically by chkpt_brms but can be called directly. ```r library(chkptstanr) library(brms) # After checkpointed sampling is complete, create brmsfit # (typically done automatically by chkpt_brms) fit <- make_brmsfit( formula = count ~ zAge + zBase * Trt + (1|patient), data = epilepsy, family = poisson(), path = "checkpoints/epilepsy" ) # Result is a standard brmsfit object class(fit) #> [1] "brmsfit" summary(fit) ``` -------------------------------- ### Fit Custom Stan Model with Checkpointing Source: https://context7.com/venpopov/chkptstanr/llms.txt Fit a custom Stan model using checkpointing. Provide the Stan model code, data, and checkpointing parameters to chkpt_stan. ```r library(chkptstanr) library(posterior) library(bayesplot) # Define custom Stan model (Eight Schools example) stan_code <- " data { int n; array[n] real y; array[n] real sigma; } parameters { real mu; real tau; vector[n] eta; } transformed parameters { vector[n] theta; theta = mu + tau * eta; } model { target += normal_lpdf(eta | 0, 1); target += normal_lpdf(y | theta, sigma); } " # Prepare data as a list stan_data <- list( n = 8, y = c(28, 8, -3, 7, -1, 1, 18, 12), sigma = c(15, 10, 16, 11, 9, 11, 10, 18) ) # Fit model with checkpointing fit <- chkpt_stan( model_code = stan_code, data = stan_data, iter_warmup = 1000, iter_sampling = 1000, iter_per_chkpt = 250, parallel_chains = 4, seed = 1234, path = "checkpoints/eight_schools" ) # Combine checkpoint draws into a single draws_array draws <- combine_chkpt_draws(fit) # Summarize with posterior package posterior::summarise_draws(draws) # Visualize traces with bayesplot bayesplot::mcmc_trace(draws, pars = c("mu", "tau")) ``` -------------------------------- ### Configure Checkpoint Structure Source: https://context7.com/venpopov/chkptstanr/llms.txt Calculate and display the checkpoint structure before fitting. Useful for planning sampling strategies. Access individual components like warmup_chkpts and sample_chkpts. ```r library(chkptstanr) # Calculate checkpoint structure setup <- chkpt_setup( iter_warmup = 2000, iter_sampling = 5000, iter_per_chkpt = 100 ) print(setup) # Access individual components setup$warmup_chkpts # 20 setup$sample_chkpts # 50 setup$total_chkpts # 70 setup$iter_per_chkpt # 100 ``` -------------------------------- ### Fit brms Model with Custom Priors and Checkpointing Source: https://context7.com/venpopov/chkptstanr/llms.txt Fit a brms model with custom priors using checkpointing. Define priors using the brms prior syntax and pass them to chkpt_brms. ```r library(chkptstanr) library(brms) # Define custom priors custom_priors <- prior(constant(1), class = "b") + prior(constant(2), class = "b", coef = "zBase") + prior(constant(0.5), class = "sd") # Fit model with custom priors fit <- chkpt_brms( formula = count ~ zAge + zBase + (1|patient), data = epilepsy, family = poisson(), prior = custom_priors, iter_warmup = 1000, iter_sampling = 1000, iter_per_chkpt = 250, path = "checkpoints/epilepsy_priors" ) # Verify priors were applied prior_summary(fit) ``` -------------------------------- ### Resume brms Model Fitting After Interruption Source: https://context7.com/venpopov/chkptstanr/llms.txt Demonstrates how to interrupt and resume brms model fitting with checkpointing. Rerunning the same chkpt_brms call will continue sampling from the last saved checkpoint. ```r library(chkptstanr) library(brms) # Start sampling - can be interrupted at any time (Ctrl+C) fit <- chkpt_brms( formula = count ~ zAge + zBase * Trt + (1|patient), data = epilepsy, family = poisson(), iter_per_chkpt = 200, path = "checkpoints/epilepsy" ) # User presses Ctrl+C after checkpoint 6... #> Chkpt: 6 / 10; Iteration: 1200 / 2000 (sample) #> Sampling aborted. You can examine the results or continue sampling by rerunning the same code. # Examine partial results (if past warmup) summary(fit) #> Draws: 4 chains, each with iter = 1200; warmup = 1000; thin = 1; #> total post-warmup draws = 800 # Resume sampling by running the same code fit <- chkpt_brms( formula = count ~ zAge + zBase * Trt + (1|patient), data = epilepsy, family = poisson(), iter_per_chkpt = 200, path = "checkpoints/epilepsy" ) #> Chkpt: 7 / 10; Iteration: 1400 / 2000 (sample) #> Chkpt: 8 / 10; Iteration: 1600 / 2000 (sample) #> Chkpt: 9 / 10; Iteration: 1800 / 2000 (sample) #> Chkpt: 10 / 10; Iteration: 2000 / 2000 (sample) #> Checkpointing complete ``` -------------------------------- ### Fit brms Model with Checkpointing Source: https://context7.com/venpopov/chkptstanr/llms.txt Fit a brms model using checkpointing. Specify the formula, data, family, and checkpointing parameters like iter_per_chkpt and path. ```r fit <- chkpt_brms( formula = count ~ zAge + zBase * Trt + (1|patient), data = epilepsy, family = poisson(), iter_per_chkpt = 200, path = "checkpoints/epilepsy" ) ``` -------------------------------- ### Summarize and Visualize Draws Source: https://context7.com/venpopov/chkptstanr/llms.txt Use the posterior and bayesplot packages to analyze draws extracted from a checkpointed model. ```r posterior::summarise_draws(draws) posterior::mcse_mean(draws) ``` ```r bayesplot::mcmc_dens(draws) bayesplot::mcmc_pairs(draws, pars = c("mu", "tau")) ``` -------------------------------- ### Fit brms Models with Checkpointing Source: https://context7.com/venpopov/chkptstanr/llms.txt Fit a Poisson mixed model using brms with checkpointing. Specify iterations per checkpoint and the path to save checkpoints. The output can be examined using standard brms functions. ```r library(chkptstanr) library(brms) # Fit a Poisson mixed model with checkpointing fit <- chkpt_brms( formula = count ~ zAge + zBase * Trt + (1|patient), data = epilepsy, family = poisson(), iter_warmup = 1000, # Warmup iterations per chain iter_sampling = 1000, # Sampling iterations per chain iter_per_chkpt = 200, # Iterations between checkpoints parallel_chains = 4, # Number of parallel chains seed = 1234, path = "checkpoints/epilepsy_model" ) # Output during fitting: #> Initial Warmup (Typical Set) #> Chkpt: 1 / 10; Iteration: 200 / 2000 (warmup) #> Chkpt: 2 / 10; Iteration: 400 / 2000 (warmup) #> ... #> Chkpt: 10 / 10; Iteration: 2000 / 2000 (sample) #> Checkpointing complete # Examine results using standard brms functions summary(fit) #> Family: poisson #> Links: mu = log #> Formula: count ~ zAge + zBase * Trt + (1 | patient) #> Data: data (Number of observations: 236) #> Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1; #> total post-warmup draws = 4000 # Posterior predictive check pp_check(fit) # Model comparison with LOO-CV fit2 <- chkpt_brms( formula = count ~ zAge + zBase, data = epilepsy, family = poisson(), iter_per_chkpt = 200, path = "checkpoints/epilepsy_model2" ) loo(fit, fit2) ``` -------------------------------- ### Predetermine Stopping Point with stop_after Source: https://github.com/venpopov/chkptstanr/blob/master/README.md Use the `stop_after` argument to specify the total number of iterations after which the sampler should stop. This allows for predetermining the run's stopping point. ```r fit1 <- chkpt_brms(count ~ zAge + zBase * Trt + (1|patient), data = epilepsy, family = poisson(), iter_per_chkpt = 200, stop_after = 1400, path = 'checkpoints/epilepsy') ``` -------------------------------- ### Reset Checkpoints and Recompile Source: https://github.com/venpopov/chkptstanr/blob/master/README.md If essential arguments like the formula, data, or family are changed, an error will occur. In such cases, you must reset the checkpoints and recompile the model using `reset_checkpoints(path, recompile = TRUE)`. ```r reset_checkpoints('checkpoints/epilepsy', recompile = TRUE) ``` -------------------------------- ### Examine Partially Sampled Model Source: https://github.com/venpopov/chkptstanr/blob/master/README.md After manually aborting the sampling process, examine the summary of the partially fitted brmsfit object. ```r summary(fit1) ``` -------------------------------- ### Error on Changing Formula with reset = TRUE Source: https://github.com/venpopov/chkptstanr/blob/master/README.md Attempting to change the formula while `reset = TRUE` will result in an error, indicating that important arguments have been modified and a full reset is required. ```r fit1 <- chkpt_brms(count ~ 1 + (1|patient), data = epilepsy, family = poisson(), iter_per_chkpt = 200, path = 'checkpoints/epilepsy', stop_after = 1600, reset = TRUE) ``` -------------------------------- ### Extract Complete Stan State for Resuming Source: https://context7.com/venpopov/chkptstanr/llms.txt Extract the complete sampler state, including inverse metric, step size, and initial values, for continuing sampling. This is used internally for checkpointing. ```r library(chkptstanr) library(cmdstanr) # Fit a model fit <- cmdstanr_example("schools_ncp") # Extract state for resuming state <- extract_stan_state(fit, phase = "sample") # State contains: # - inv_metric: Inverse mass matrix for each chain # - step_size_adapt: Adapted step sizes # - inits: Initial values for next iteration (last draw from each chain) names(state) #> [1] "inv_metric" "step_size_adapt" "inits" ``` -------------------------------- ### Checkpoint brms Model Fitting Source: https://context7.com/venpopov/chkptstanr/llms.txt Fit a brms model with checkpointing enabled. This function accepts the same arguments as brm() and saves progress to a specified path. ```r fit_brms <- chkpt_brms( formula = count ~ zAge + zBase, data = epilepsy, family = poisson(), iter_per_chkpt = 200, path = "checkpoints/brms_model" ) draws_brms <- combine_chkpt_draws(fit_brms) ``` -------------------------------- ### Extract HMC Sampler Information Source: https://context7.com/venpopov/chkptstanr/llms.txt Extract the inverse metric and step size adaptation from CmdStanMCMC objects. This is primarily for advanced diagnostics. ```r library(chkptstanr) library(cmdstanr) # Fit a model with cmdstanr fit <- cmdstanr_example("schools_ncp") # Extract HMC information hmc_info <- extract_hmc_info(fit) # Access inverse metric for each chain hmc_info$inv_metric #> [[1]] #> [1] 0.0234 0.0156 0.0289 ... #> [[2]] #> [1] 0.0198 0.0145 0.0312 ... # Access adapted step sizes hmc_info$step_size_adapt #> [1] 0.892 0.756 0.834 0.901 ``` -------------------------------- ### extract_hmc_info Source: https://context7.com/venpopov/chkptstanr/llms.txt Extracts the inverse metric and step size adaptation from CmdStanMCMC objects for advanced diagnostics. ```APIDOC ## extract_hmc_info ### Description Extract the inverse metric and step size adaptation from CmdStanMCMC objects. Primarily used internally but available for advanced diagnostics. ### Parameters #### Request Body - **fit** (CmdStanMCMC) - Required - The fitted model object from cmdstanr. ### Response - **inv_metric** (list) - Inverse mass matrix for each chain. - **step_size_adapt** (numeric) - Adapted step sizes. ``` -------------------------------- ### Manually Abort brms Model Sampling Source: https://github.com/venpopov/chkptstanr/blob/master/README.md Run a brms model with checkpointing enabled and manually abort after a certain number of iterations. This saves intermediate samples. ```r fit1 <- chkpt_brms(count ~ zAge + zBase * Trt + (1|patient), data = epilepsy, family = poisson(), iter_per_chkpt = 200, path = 'checkpoints/epilepsy') ``` -------------------------------- ### Extract Draws from CmdStanMCMC Object Source: https://context7.com/venpopov/chkptstanr/llms.txt Extract draws from a CmdStanMCMC object, with options to include or exclude warmup draws. ```r library(chkptstanr) library(cmdstanr) # Fit a model fit <- cmdstanr_example("schools_ncp") # Extract only post-warmup draws draws_sample <- extract_chkpt_draws(fit, phase = "sample") dim(draws_sample) #> [1] 1000 4 19 # iterations x chains x variables # Extract including warmup draws draws_warmup <- extract_chkpt_draws(fit, phase = "warmup") dim(draws_warmup) #> [1] 2000 4 19 # includes warmup iterations ``` -------------------------------- ### Combine Checkpoint Draws Source: https://context7.com/venpopov/chkptstanr/llms.txt Combine posterior draws from all checkpoints into a single draws_array object. Compatible with posterior and bayesplot packages. Use after fitting with chkpt_stan. ```r library(chkptstanr) library(posterior) library(bayesplot) # After fitting with chkpt_stan fit <- chkpt_stan( model_code = stan_code, data = stan_data, iter_warmup = 1000, iter_sampling = 1000, iter_per_chkpt = 250, path = "checkpoints/model" ) # Combine draws from all checkpoints draws <- combine_chkpt_draws(fit) class(draws) ``` -------------------------------- ### Reset or Clear Checkpoints Source: https://context7.com/venpopov/chkptstanr/llms.txt Reset checkpoint data to restart sampling from the beginning. Optionally keep the compiled model to avoid recompilation for faster restarts. Can also be used directly in chkpt_brms. ```r library(chkptstanr) # Reset checkpoints but keep compiled model (faster restart) reset_checkpoints( path = "checkpoints/epilepsy", reset = TRUE, recompile = FALSE ) # Completely reset including recompiling the model reset_checkpoints( path = "checkpoints/epilepsy", reset = TRUE, recompile = TRUE ) # Can also use reset argument directly in chkpt_brms fit <- chkpt_brms( formula = count ~ zAge + zBase * Trt + (1|patient), data = epilepsy, family = poisson(), iter_per_chkpt = 200, path = "checkpoints/epilepsy", reset = TRUE # Start fresh without recompiling ) ``` -------------------------------- ### extract_chkpt_draws Source: https://context7.com/venpopov/chkptstanr/llms.txt Extracts draws from a CmdStanMCMC object, with options for including warmup draws. ```APIDOC ## extract_chkpt_draws ### Description Extract draws from a CmdStanMCMC object, with options for including warmup draws. ### Parameters #### Request Body - **fit** (CmdStanMCMC) - Required - The fitted model object. - **phase** (string) - Required - The phase to extract ("sample" or "warmup"). ### Response - **draws** (array) - An array of iterations x chains x variables. ``` -------------------------------- ### Stop brms Model Fitting After Predetermined Iterations Source: https://context7.com/venpopov/chkptstanr/llms.txt Use the `stop_after` argument in `chkpt_brms` to automatically halt sampling after a specified number of iterations. This is useful for testing convergence or managing computational time. ```r library(chkptstanr) library(brms) # Stop after 1400 iterations (7 checkpoints) fit <- chkpt_brms( formula = count ~ zAge + zBase * Trt + (1|patient), data = epilepsy, family = poisson(), iter_per_chkpt = 200, stop_after = 1400, path = "checkpoints/epilepsy" ) #> Sampling will stop after checkpoint 7 #> Initial Warmup (Typical Set) #> Chkpt: 1 / 10; Iteration: 200 / 2000 (warmup) #> ... #> Chkpt: 7 / 10; Iteration: 1400 / 2000 (sample) #> Sampling aborted. You can examine the results or continue sampling by rerunning the same code. # Check convergence at this point summary(fit) ``` -------------------------------- ### extract_stan_state Source: https://context7.com/venpopov/chkptstanr/llms.txt Extracts the complete sampler state including inverse metric, step size, and initial values for continuing sampling. ```APIDOC ## extract_stan_state ### Description Extract the complete sampler state including inverse metric, step size, and initial values for continuing sampling. Used internally for checkpointing. ### Parameters #### Request Body - **fit** (CmdStanMCMC) - Required - The fitted model object. - **phase** (string) - Required - The sampling phase (e.g., "sample"). ### Response - **inv_metric** (list) - Inverse mass matrix for each chain. - **step_size_adapt** (numeric) - Adapted step sizes. - **inits** (list) - Initial values for next iteration. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.