Skip to contents

A CmdStanGQ object is the fitted model object returned by the $generate_quantities() method of a CmdStanModel object.

Methods

CmdStanGQ objects have the following associated methods, all of which have their own (linked) documentation pages.

Extract contents of generated quantities object

MethodDescription
$draws()Return the generated quantities as a draws_array.
$fitted_params_files()Return paths to the fitted-parameter CSV files.
$metadata()Return a list of metadata gathered from the CmdStan CSV files.
$profiles()Return profiling data.
$num_chains()Return the number of chains used for generated quantities.
$code()Return Stan code as a character vector.

Summarize inferences

MethodDescription
$print()Print a summary of the generated quantities.
$summary()Run posterior::summarise_draws().

Save fitted model object and temporary files

MethodDescription
$materialize()Read all generated quantities into memory.
$save_object()Save fitted model object to a file.
$output_files()Return paths to output CSV files.
$save_output_files()Save output CSV files to a specified location.
$data_file()Return the path to the JSON data file.
$save_data_file()Save JSON data file to a specified location.
$profile_files()Return paths to profiling CSV files.
$save_profile_files()Save profiling CSV files to a specified location.

Report run times, console output, return codes

MethodDescription
$time()Report total and process-specific run times.
$output()Return the stdout and stderr of all chains or pretty print the output for a single chain.
$return_codes()Return the return codes from the CmdStan runs.
$command()Return the commands that ran CmdStan.

Expose Stan functions and additional methods to R

MethodDescription
$expose_functions()Expose Stan functions for use in R.
$init_model_methods()Expose methods for log-probability, gradients, parameter constraining and unconstraining.
$log_prob()Calculate log-prob.
$grad_log_prob()Calculate log-prob and gradient.
$hessian()Calculate log-prob, gradient, and Hessian.
$constrain_variables()Transform a set of unconstrained parameter values to the constrained scale.
$unconstrain_variables()Transform a set of parameter values to the unconstrained scale.
$unconstrain_draws()Transform all parameter draws to the unconstrained scale.

See also

The CmdStanR website (mc-stan.org/cmdstanr) for online documentation and tutorials.

The Stan and CmdStan documentation:

Other fitted model objects: CmdStanDiagnose, CmdStanLaplace, CmdStanMCMC, CmdStanMLE, CmdStanPathfinder, CmdStanVB

Examples

# \dontrun{
# first fit a model using MCMC
mcmc_program <- write_stan_file(
  "data {
    int<lower=0> N;
    array[N] int<lower=0,upper=1> y;
  }
  parameters {
    real<lower=0,upper=1> theta;
  }
  model {
    y ~ bernoulli(theta);
  }"
)
mod_mcmc <- cmdstan_model(mcmc_program)

data <- list(N = 10, y = c(1,1,0,0,0,1,0,1,0,0))
fit_mcmc <- mod_mcmc$sample(data = data, seed = 123, refresh = 0)
#> Running MCMC with 4 sequential chains...
#> 
#> Chain 1 finished in 0.0 seconds.
#> Chain 2 finished in 0.0 seconds.
#> Chain 3 finished in 0.0 seconds.
#> Chain 4 finished in 0.0 seconds.
#> 
#> All 4 chains finished successfully.
#> Mean chain execution time: 0.0 seconds.
#> Total execution time: 0.6 seconds.
#> 

# stan program for standalone generated quantities
# (could keep model block, but not necessary so removing it)
gq_program <- write_stan_file(
  "data {
    int<lower=0> N;
    array[N] int<lower=0,upper=1> y;
  }
  parameters {
    real<lower=0,upper=1> theta;
  }
  generated quantities {
    array[N] int y_rep = bernoulli_rng(rep_vector(theta, N));
  }"
)

mod_gq <- cmdstan_model(gq_program)
fit_gq <- mod_gq$generate_quantities(fit_mcmc, data = data, seed = 123)
#> Running standalone generated quantities after 4 MCMC chains, 1 chain at a time ...
#> 
#> Chain 1 finished in 0.0 seconds.
#> Chain 2 finished in 0.0 seconds.
#> Chain 3 finished in 0.0 seconds.
#> Chain 4 finished in 0.0 seconds.
#> 
#> All 4 chains finished successfully.
#> Mean chain execution time: 0.0 seconds.
#> Total execution time: 0.5 seconds.
str(fit_gq$draws())
#>  'draws_array' int [1:1000, 1:4, 1:10] 0 0 0 1 1 0 1 1 0 1 ...
#>  - attr(*, "dimnames")=List of 3
#>   ..$ iteration: chr [1:1000] "1" "2" "3" "4" ...
#>   ..$ chain    : chr [1:4] "1" "2" "3" "4"
#>   ..$ variable : chr [1:10] "y_rep[1]" "y_rep[2]" "y_rep[3]" "y_rep[4]" ...

library(posterior)
#> This is posterior version 1.7.0
#> 
#> Attaching package: ‘posterior’
#> The following objects are masked from ‘package:stats’:
#> 
#>     mad, sd, var
#> The following objects are masked from ‘package:base’:
#> 
#>     %in%, match
as_draws_df(fit_gq$draws())
#> # A draws_df: 1000 iterations, 4 chains, and 10 variables
#>    y_rep[1] y_rep[2] y_rep[3] y_rep[4] y_rep[5] y_rep[6] y_rep[7] y_rep[8]
#> 1         0        0        0        0        0        1        1        1
#> 2         0        0        0        0        1        1        0        0
#> 3         0        0        0        1        0        0        1        1
#> 4         1        1        0        0        0        0        1        0
#> 5         1        0        1        0        1        0        1        0
#> 6         0        0        0        1        1        0        0        0
#> 7         1        1        0        1        1        1        0        0
#> 8         1        1        1        1        1        0        1        1
#> 9         0        1        0        1        0        1        1        0
#> 10        1        1        1        1        1        1        1        1
#> # ... with 3990 more draws, and 2 more variables
#> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
# }