Learn R Programming

cosmic (version 0.5)

posterior: Extract posterior draws from a COSMIC model

Description

Extracts posterior samples from a fitted COSMIC model. This is a convenience wrapper around CmdStan draws extraction that avoids direct dependence on cmdstanr in user code.

Usage

posterior(object, ...)

# S3 method for cosmic_fit posterior(object, pars = NULL, tidy = FALSE, ...)

Value

If tidy = FALSE, a named list of posterior draws.

If tidy = TRUE, a data frame with one row per posterior draw and columns corresponding to parameters (expanded with indices where needed).

Arguments

object

A fitted object of class "cosmic_fit".

...

Additional arguments passed to object$fit$draws().

pars

Optional character vector of parameter names to extract (e.g., "lambda", "sDelta"). Defaults to all parameters.

tidy

Logical; if FALSE (default), returns a list of arrays as a named list of posterior draw arrays. If TRUE, returns a data frame with one row per draw.

Examples

Run this code
# \donttest{
d <- data.frame(
  id = c(1,1,2,2),
  idOff = c(1,2,1,2),
  y = c(1,2,1,3)
)

fit <- cosmic(d, id, idOff, y, iter = 300, chains = 1, cores = 1, threads = 1)

# raw posterior draws
draws <- posterior(fit)

# only lambda
lambda_draws <- posterior(fit, pars = "lambda")

# tidy format
df <- posterior(fit, tidy = TRUE)
# }

Run the code above in your browser using DataLab