CmdStanGQ: CmdStanGQ objects

CmdStanGQR Documentation

CmdStanGQ objects

Description

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

Method Description
$draws() Return the generated quantities as a draws_array.
$metadata() Return a list of metadata gathered from the CmdStan CSV files.
$code() Return Stan code as a character vector.

Summarize inferences

Method Description
$summary() Run posterior::summarise_draws().

Save fitted model object and temporary files

Method Description
$save_object() Save fitted model object to a file.
$save_output_files() Save output CSV files to a specified location.
$save_data_file() Save JSON data file to a specified location.

Report run times, console output, return codes

Method Description
$time() Report the total run time.
$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.

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

## Not run: 
# 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)

# 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)
str(fit_gq$draws())

library(posterior)
as_draws_df(fit_gq$draws())

## End(Not run)


stan-dev/cmdstanr documentation built on Aug. 13, 2024, 5:13 p.m.