cmdstanr_example: Fit models for use in examples

View source: R/example.R

cmdstanr_exampleR Documentation

Fit models for use in examples

Description

Fit models for use in examples

Usage

cmdstanr_example(
  example = c("logistic", "schools", "schools_ncp"),
  method = c("sample", "optimize", "laplace", "variational", "pathfinder", "diagnose"),
  ...,
  quiet = TRUE,
  force_recompile = getOption("cmdstanr_force_recompile", default = FALSE)
)

print_example_program(example = c("logistic", "schools", "schools_ncp"))

Arguments

example

(string) The name of the example. The currently available examples are

  • "logistic": logistic regression with intercept and 3 predictors.

  • "schools": the so-called "eight schools" model, a hierarchical meta-analysis. Fitting this model will result in warnings about divergences.

  • "schools_ncp": non-centered parameterization of the "eight schools" model that fixes the problem with divergences.

To print the Stan code for a given example use print_example_program(example).

method

(string) Which fitting method should be used? The default is the "sample" method (MCMC).

...

Arguments passed to the chosen method. See the help pages for the individual methods for details.

quiet

(logical) If TRUE (the default) then fitting the model is wrapped in utils::capture.output().

force_recompile

Passed to the $compile() method.

Value

The fitted model object returned by the selected method.

Examples

## Not run: 
print_example_program("logistic")
fit_logistic_mcmc <- cmdstanr_example("logistic", chains = 2)
fit_logistic_mcmc$summary()

fit_logistic_optim <- cmdstanr_example("logistic", method = "optimize")
fit_logistic_optim$summary()

fit_logistic_vb <- cmdstanr_example("logistic", method = "variational")
fit_logistic_vb$summary()

print_example_program("schools")
fit_schools_mcmc <- cmdstanr_example("schools")
fit_schools_mcmc$summary()

print_example_program("schools_ncp")
fit_schools_ncp_mcmc <- cmdstanr_example("schools_ncp")
fit_schools_ncp_mcmc$summary()

# optimization fails for hierarchical model
cmdstanr_example("schools", "optimize", quiet = FALSE)

## End(Not run)


stan-dev/cmdstanr documentation built on April 15, 2024, 9:11 p.m.