CmdStanGQ objects — CmdStanGQ • cmdstanr
Description: A CmdStanGQ object is the fitted model object returned by the $generate_quantities() method of a CmdStanModel object.

# CmdStanGQ objects

Source: `R/fit.R`
`CmdStanGQ.Rd`

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:

- Stan documentation: mc-stan.org/users/documentation

- CmdStan User’s Guide: mc-stan.org/docs/cmdstan-guide

Other fitted model objects: `CmdStanDiagnose`, `CmdStanMCMC`, `CmdStanMLE`, `CmdStanVB`

## Examples

    # \dontrun{
    # first fit a model using MCMC
    mcmc_program <- write_stan_file(
      "data {
        int<lower=0> N;
        int<lower=0,upper=1> y[N];
      }
      parameters {
        real<lower=0,upper=1> theta;
      }
      model {
        y ~ bernoulli(theta);
      }"
    )
    mod_mcmc <- cmdstan_model(mcmc_program)
    #> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model-16218796affe.stan', line 3, column 4: Declaration
    #>     of arrays by placing brackets after a variable name is deprecated and
    #>     will be removed in Stan 2.33.0. Instead use the array keyword before the
    #>     type. This can be changed automatically using the auto-format flag to
    #>     stanc

    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;
        int<lower=0,upper=1> y[N];
      }
      parameters {
        real<lower=0,upper=1> theta;
      }
      generated quantities {
        int y_rep[N] = bernoulli_rng(rep_vector(theta, N));
      }"
    )

    mod_gq <- cmdstan_model(gq_program)
    #> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model-162182f764088.stan', line 3, column 4: Declaration
    #>     of arrays by placing brackets after a variable name is deprecated and
    #>     will be removed in Stan 2.33.0. Instead use the array keyword before the
    #>     type. This can be changed automatically using the auto-format flag to
    #>     stanc
    #> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model-162182f764088.stan', line 9, column 4: Declaration
    #>     of arrays by placing brackets after a variable name is deprecated and
    #>     will be removed in Stan 2.33.0. Instead use the array keyword before the
    #>     type. This can be changed automatically using the auto-format flag to
    #>     stanc
    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 0 1 0 1 0 0 0 ...
    #>  - 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.4.1
    #>
    #> 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        0        0        0
    #> 2         0        1        1        1        1        1        1        1
    #> 3         0        0        0        0        0        1        1        1
    #> 4         0        0        0        0        0        1        0        1
    #> 5         1        1        1        1        1        0        1        0
    #> 6         0        0        0        0        1        0        1        1
    #> 7         1        0        0        0        1        1        1        0
    #> 8         0        1        0        0        0        1        0        1
    #> 9         0        1        0        1        1        1        1        1
    #> 10        0        1        0        1        0        0        0        0
    #> # ... with 3990 more draws, and 2 more variables
    #> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
    # }