├── .Rbuildignore ├── .github ├── .gitignore ├── CONTRIBUTING.md ├── FUNDING.yml ├── ISSUE_TEMPLATE │ ├── bug_report.md │ ├── config.yml │ ├── feature_request.md │ └── other.md ├── dependabot.yml ├── pull_request_template.md └── workflows │ ├── R-CMD-check-wsl.yaml │ ├── R-CMD-check.yaml │ ├── Test-coverage.yaml │ └── cmdstan-tarball-check.yaml ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── NAMESPACE ├── NEWS.md ├── R ├── args.R ├── cmdstanr-package.R ├── cpp_opts.R ├── csv.R ├── data.R ├── example.R ├── fit.R ├── generics.R ├── install.R ├── knitr.R ├── model.R ├── options.R ├── path.R ├── run.R ├── utils.R └── zzz.R ├── README.md ├── _pkgdown.yml ├── cmdstanr.Rproj ├── docs ├── 404.html ├── CONTRIBUTING.html ├── LICENSE-text.html ├── LICENSE.html ├── apple-touch-icon-120x120.png ├── apple-touch-icon-152x152.png ├── apple-touch-icon-180x180.png ├── apple-touch-icon-60x60.png ├── apple-touch-icon-76x76.png ├── apple-touch-icon.png ├── articles │ ├── articles-online-only │ │ ├── opencl.html │ │ └── opencl_files │ │ │ ├── header-attrs-2.11 │ │ │ └── header-attrs.js │ │ │ ├── header-attrs-2.13 │ │ │ └── header-attrs.js │ │ │ └── header-attrs-2.9 │ │ │ └── header-attrs.js │ ├── children │ │ ├── settings-knitr.html │ │ └── settings-knitr_files │ │ │ └── header-attrs-2.9 │ │ │ └── header-attrs.js │ ├── cmdstanr-internals.html │ ├── cmdstanr-internals_files │ │ ├── accessible-code-block-0.0.1 │ │ │ └── empty-anchor.js │ │ ├── header-attrs-2.3 │ │ │ └── header-attrs.js │ │ ├── header-attrs-2.6 │ │ │ └── header-attrs.js │ │ ├── header-attrs-2.8 │ │ │ └── header-attrs.js │ │ └── header-attrs-2.9 │ │ │ └── header-attrs.js │ ├── cmdstanr.html │ ├── cmdstanr_files │ │ ├── accessible-code-block-0.0.1 │ │ │ └── empty-anchor.js │ │ ├── figure-html │ │ │ ├── draws-1.png │ │ │ ├── laplace-1.png │ │ │ ├── plot-compare-laplace-1.png │ │ │ ├── plot-compare-mcmc-1.png │ │ │ ├── plot-compare-pf-1.png │ │ │ ├── plot-compare-vb-1.png │ │ │ ├── plot-mle-1.png │ │ │ ├── plot-variational-1-1.png │ │ │ ├── plot-variational-1.png │ │ │ ├── plot-variational-2-1.png │ │ │ ├── plots-1.png │ │ │ ├── unnamed-chunk-3-1.png │ │ │ └── variational-1.png │ │ ├── header-attrs-2.3 │ │ │ └── header-attrs.js │ │ ├── header-attrs-2.6 │ │ │ └── header-attrs.js │ │ └── header-attrs-2.9 │ │ │ └── header-attrs.js │ ├── deprecations.html │ ├── deprecations_files │ │ ├── header-attrs-2.11 │ │ │ └── header-attrs.js │ │ └── header-attrs-2.9 │ │ │ └── header-attrs.js │ ├── index.html │ ├── posterior.html │ ├── posterior_files │ │ └── header-attrs-2.18 │ │ │ └── header-attrs.js │ ├── profiling.html │ ├── profiling_files │ │ ├── accessible-code-block-0.0.1 │ │ │ └── empty-anchor.js │ │ ├── anchor-sections-1.0 │ │ │ ├── anchor-sections.css │ │ │ └── anchor-sections.js │ │ ├── header-attrs-2.3 │ │ │ └── header-attrs.js │ │ ├── header-attrs-2.6 │ │ │ └── header-attrs.js │ │ └── header-attrs-2.9 │ │ │ └── header-attrs.js │ ├── r-markdown.html │ ├── r-markdown_files │ │ ├── accessible-code-block-0.0.1 │ │ │ └── empty-anchor.js │ │ ├── header-attrs-2.3 │ │ │ └── header-attrs.js │ │ ├── header-attrs-2.6 │ │ │ └── header-attrs.js │ │ └── header-attrs-2.9 │ │ │ └── header-attrs.js │ ├── r_markdown.html │ └── r_markdown_files │ │ └── accessible-code-block-0.0.1 │ │ └── empty-anchor.js ├── authors.html ├── bootstrap-toc.css ├── bootstrap-toc.js ├── docsearch.css ├── docsearch.js ├── favicon-16x16.png ├── favicon-32x32.png ├── favicon.ico ├── index.html ├── link.svg ├── logo.png ├── news │ └── index.html ├── pkgdown.css ├── pkgdown.js ├── pkgdown.yml ├── pull_request_template.html ├── reference │ ├── CmdStanDiagnose.html │ ├── CmdStanFit-method-save_output_files.html │ ├── CmdStanGQ.html │ ├── CmdStanLaplace.html │ ├── CmdStanMCMC.html │ ├── CmdStanMLE.html │ ├── CmdStanModel-1.png │ ├── CmdStanModel-2.png │ ├── CmdStanModel-3.png │ ├── CmdStanModel-method-compile.html │ ├── CmdStanModel-method-optimize.html │ ├── CmdStanModel-method-sample.html │ ├── CmdStanModel-method-variational.html │ ├── CmdStanModel.html │ ├── CmdStanPathfinder.html │ ├── CmdStanVB.html │ ├── Rplot001.png │ ├── Rplot002.png │ ├── Rplot003.png │ ├── as_draws.CmdStanMCMC.html │ ├── as_mcmc.list.html │ ├── cmdstan_coercion.html │ ├── cmdstan_default_install_path.html │ ├── cmdstan_default_path.html │ ├── cmdstan_model-1.png │ ├── cmdstan_model-2.png │ ├── cmdstan_model-3.png │ ├── cmdstan_model.html │ ├── cmdstan_path.html │ ├── cmdstanr-package-1.png │ ├── cmdstanr-package-2.png │ ├── cmdstanr-package-3.png │ ├── cmdstanr-package.html │ ├── cmdstanr_example.html │ ├── cmdstanr_global_options.html │ ├── draws_to_csv.html │ ├── eng_cmdstan.html │ ├── figures │ │ └── logo.png │ ├── fit-method-cmdstan_summary.html │ ├── fit-method-code.html │ ├── fit-method-constrain_variables.html │ ├── fit-method-diagnostic_summary.html │ ├── fit-method-draws-1.png │ ├── fit-method-draws-2.png │ ├── fit-method-draws.html │ ├── fit-method-grad_log_prob.html │ ├── fit-method-gradients.html │ ├── fit-method-hessian.html │ ├── fit-method-init.html │ ├── fit-method-init_model_methods.html │ ├── fit-method-inv_metric.html │ ├── fit-method-inverse_metric.html │ ├── fit-method-log_prob.html │ ├── fit-method-loo.html │ ├── fit-method-lp-1.png │ ├── fit-method-lp.html │ ├── fit-method-metadata.html │ ├── fit-method-mle.html │ ├── fit-method-num_chains.html │ ├── fit-method-output.html │ ├── fit-method-profiles.html │ ├── fit-method-return_codes.html │ ├── fit-method-sampler_diagnostics.html │ ├── fit-method-save_object.html │ ├── fit-method-save_output_files.html │ ├── fit-method-summary.html │ ├── fit-method-time.html │ ├── fit-method-unconstrain_draws.html │ ├── fit-method-unconstrain_variables.html │ ├── fit-method-variable_skeleton.html │ ├── index.html │ ├── install_cmdstan.html │ ├── model-method-check_syntax.html │ ├── model-method-compile.html │ ├── model-method-diagnose.html │ ├── model-method-expose_functions.html │ ├── model-method-format.html │ ├── model-method-generate-quantities.html │ ├── model-method-laplace-1.png │ ├── model-method-laplace.html │ ├── model-method-optimize-1.png │ ├── model-method-optimize-2.png │ ├── model-method-optimize-3.png │ ├── model-method-optimize.html │ ├── model-method-pathfinder-1.png │ ├── model-method-pathfinder-2.png │ ├── model-method-pathfinder-3.png │ ├── model-method-pathfinder.html │ ├── model-method-sample-1.png │ ├── model-method-sample-2.png │ ├── model-method-sample-3.png │ ├── model-method-sample.html │ ├── model-method-sample_mpi.html │ ├── model-method-variables.html │ ├── model-method-variational-1.png │ ├── model-method-variational-2.png │ ├── model-method-variational-3.png │ ├── model-method-variational.html │ ├── read_cmdstan_csv.html │ ├── read_sample_csv.html │ ├── register_knitr_engine.html │ ├── set_cmdstan_path.html │ ├── stan_threads.html │ ├── write_stan_file.html │ ├── write_stan_json.html │ └── write_stan_tempfile.html └── sitemap.xml ├── inst ├── include │ ├── base_rng.cpp │ ├── model_methods.cpp │ ├── rcpp_eigen_interop.hpp │ ├── rcpp_tuple_interop.hpp │ └── stan_rng.hpp ├── logistic.data.json ├── logistic.stan ├── schools.data.json ├── schools.stan ├── schools_ncp.data.json └── schools_ncp.stan ├── man-roxygen ├── model-common-args.R ├── model-sample-args.R └── seealso-docs.R ├── man ├── CmdStanDiagnose.Rd ├── CmdStanGQ.Rd ├── CmdStanLaplace.Rd ├── CmdStanMCMC.Rd ├── CmdStanMLE.Rd ├── CmdStanModel.Rd ├── CmdStanPathfinder.Rd ├── CmdStanVB.Rd ├── as_draws.CmdStanMCMC.Rd ├── as_mcmc.list.Rd ├── cmdstan_coercion.Rd ├── cmdstan_default_install_path.Rd ├── cmdstan_default_path.Rd ├── cmdstan_model.Rd ├── cmdstanr-package.Rd ├── cmdstanr_example.Rd ├── cmdstanr_global_options.Rd ├── draws_to_csv.Rd ├── eng_cmdstan.Rd ├── figures │ └── logo.png ├── fit-method-cmdstan_summary.Rd ├── fit-method-code.Rd ├── fit-method-constrain_variables.Rd ├── fit-method-diagnostic_summary.Rd ├── fit-method-draws.Rd ├── fit-method-grad_log_prob.Rd ├── fit-method-gradients.Rd ├── fit-method-hessian.Rd ├── fit-method-init.Rd ├── fit-method-init_model_methods.Rd ├── fit-method-inv_metric.Rd ├── fit-method-log_prob.Rd ├── fit-method-loo.Rd ├── fit-method-lp.Rd ├── fit-method-metadata.Rd ├── fit-method-mle.Rd ├── fit-method-num_chains.Rd ├── fit-method-output.Rd ├── fit-method-profiles.Rd ├── fit-method-return_codes.Rd ├── fit-method-sampler_diagnostics.Rd ├── fit-method-save_object.Rd ├── fit-method-save_output_files.Rd ├── fit-method-summary.Rd ├── fit-method-time.Rd ├── fit-method-unconstrain_draws.Rd ├── fit-method-unconstrain_variables.Rd ├── fit-method-variable_skeleton.Rd ├── install_cmdstan.Rd ├── model-method-check_syntax.Rd ├── model-method-compile.Rd ├── model-method-diagnose.Rd ├── model-method-expose_functions.Rd ├── model-method-format.Rd ├── model-method-generate-quantities.Rd ├── model-method-laplace.Rd ├── model-method-optimize.Rd ├── model-method-pathfinder.Rd ├── model-method-sample.Rd ├── model-method-sample_mpi.Rd ├── model-method-variables.Rd ├── model-method-variational.Rd ├── read_cmdstan_csv.Rd ├── read_sample_csv.Rd ├── register_knitr_engine.Rd ├── set_cmdstan_path.Rd ├── stan_threads.Rd ├── write_stan_file.Rd ├── write_stan_json.Rd └── write_stan_tempfile.Rd ├── pkgdown └── favicon │ ├── apple-touch-icon-120x120.png │ ├── apple-touch-icon-152x152.png │ ├── apple-touch-icon-180x180.png │ ├── apple-touch-icon-60x60.png │ ├── apple-touch-icon-76x76.png │ ├── apple-touch-icon.png │ ├── favicon-16x16.png │ ├── favicon-32x32.png │ └── favicon.ico ├── tests ├── testthat.R └── testthat │ ├── answers │ ├── json-boolean.json │ ├── json-df-matrix.json │ ├── json-factor.json │ ├── json-integer.json │ ├── json-matrix-lists.json │ ├── json-table-array.json │ ├── json-table-matrix.json │ ├── json-table-vector.json │ ├── json-unboxing.json │ ├── json-vector-lists.json │ ├── model-code-output.rds │ └── model-print-output.stan │ ├── helper-custom-expectations.R │ ├── helper-envvars-and-paths.R │ ├── helper-mock-cli.R │ ├── helper-models.R │ ├── resources │ ├── csv │ │ ├── bernoulli-1-laplace.csv │ │ ├── bernoulli-1-optimize.csv │ │ ├── bernoulli-1-variational.csv │ │ ├── bernoulli-3-diff_params.csv │ │ ├── bernoulli-fail.csv │ │ ├── bernoulli_ppc-1-gq.csv │ │ ├── logistic-diagnose.csv │ │ ├── model1-1-dense_e_metric.csv │ │ ├── model1-1-warmup.csv │ │ ├── model1-2-dense_e_metric.csv │ │ ├── model1-2-no-warmup.csv │ │ ├── model1-2-warmup.csv │ │ ├── model1-3-diff_args.csv │ │ ├── model1-3-no-params.csv │ │ └── model1-3-no-samples.csv │ ├── data │ │ ├── bernoulli.data.R │ │ ├── bernoulli.data.json │ │ ├── bernoulli.data.rds │ │ ├── bernoulli_log_lik.data.json │ │ ├── bernoulli_ppc.data.R │ │ ├── bernoulli_ppc.data.json │ │ ├── bernoulli_ppc.data.rds │ │ ├── info_message.data.rds │ │ ├── logistic.data.R │ │ ├── logistic.data.json │ │ ├── logistic.data.rds │ │ ├── loo_moment_match.data.json │ │ ├── schools.data.json │ │ └── schools.data.rds │ ├── init │ │ ├── bernoulli.init-1.json │ │ └── bernoulli.init-2.json │ ├── metric │ │ ├── bernoulli.inv_metric.dense_e.data.R │ │ ├── bernoulli.inv_metric.dense_e.json │ │ ├── bernoulli.inv_metric.diag_e.data.R │ │ └── bernoulli.inv_metric.diag_e.json │ └── stan │ │ ├── .gitignore │ │ ├── bernoulli.stan │ │ ├── bernoulli_external.stan │ │ ├── bernoulli_fp.stan │ │ ├── bernoulli_include.stan │ │ ├── bernoulli_log_lik.stan │ │ ├── bernoulli_ppc.stan │ │ ├── chain_fails.stan │ │ ├── divide_real_by_two.stan │ │ ├── fail.stan │ │ ├── info_message.stan │ │ ├── init_warnings.stan │ │ ├── logistic.stan │ │ ├── logistic_profiling.stan │ │ ├── logistic_simple.stan │ │ ├── loo_moment_match.stan │ │ ├── old_array_syntax.stan │ │ ├── parameter_types.stan │ │ └── schools.stan │ ├── teardown-remove-files.R │ ├── test-cpp_opts.R │ ├── test-csv.R │ ├── test-data.R │ ├── test-example.R │ ├── test-failed-chains.R │ ├── test-fit-gq.R │ ├── test-fit-init.R │ ├── test-fit-laplace.R │ ├── test-fit-mcmc.R │ ├── test-fit-mle.R │ ├── test-fit-shared.R │ ├── test-fit-vb.R │ ├── test-install.R │ ├── test-json.R │ ├── test-knitr.R │ ├── test-model-code-print.R │ ├── test-model-compile-user_header.R │ ├── test-model-compile.R │ ├── test-model-data.R │ ├── test-model-diagnose.R │ ├── test-model-expose-functions.R │ ├── test-model-generate_quantities.R │ ├── test-model-init.R │ ├── test-model-laplace.R │ ├── test-model-methods.R │ ├── test-model-optimize.R │ ├── test-model-output_dir.R │ ├── test-model-pathfinder.R │ ├── test-model-recompile-logic.R │ ├── test-model-sample-metric.R │ ├── test-model-sample.R │ ├── test-model-sample_mpi.R │ ├── test-model-variables.R │ ├── test-model-variational.R │ ├── test-opencl.R │ ├── test-path.R │ ├── test-profiling.R │ ├── test-threads.R │ ├── test-utils.R │ └── testdata │ └── Makefile └── vignettes ├── .gitignore ├── articles-online-only ├── opencl-files │ └── bernoulli_logit_glm.stan └── opencl.Rmd ├── children ├── _settings-knitr.Rmd └── comparison-with-rstan.md ├── cmdstanr-internals.Rmd ├── cmdstanr.Rmd ├── posterior.Rmd ├── profiling.Rmd └── r-markdown.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^\.travis\.yml$ 4 | ^\.codecov\.yml$ 5 | ^README\.Rmd$ 6 | ^_pkgdown\.yml$ 7 | ^docs$ 8 | ^pkgdown$ 9 | ^man-roxygen$ 10 | ^LICENSE\.md$ 11 | ^\.appveyor\.yml$ 12 | ^\.github$ 13 | ^vignettes/articles-online-only$ 14 | ^release-prep\.R$ 15 | ^\.vscode$ 16 | -------------------------------------------------------------------------------- /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to cmdstanr 2 | 3 | This outlines how to propose a change to cmdstanr and is based on similar 4 | instructions for tidyverse packages, including the contributing guidelines 5 | generated by `usethis::use_tidy_contributing()`. 6 | 7 | ## Fixing typos 8 | 9 | You can fix typos, spelling mistakes, or grammatical errors in the documentation 10 | directly using the GitHub web interface, as long as the changes are made in the 11 | _source_ file. This generally means you'll need to edit 12 | [roxygen2 comments](https://roxygen2.r-lib.org/articles/roxygen2.html) in an `.R`, 13 | not a `.Rd` file. You can find the `.R` file that generates the `.Rd` by reading 14 | the comment in the first line. 15 | 16 | ## Bigger changes 17 | 18 | If you want to make a bigger change, it's a good idea to first file an issue and 19 | make sure someone from the team agrees that it’s needed. If you’ve found a bug, 20 | please file an issue that illustrates the bug with a minimal reproducible 21 | example (see e.g. the [tidyverse reprex instructions](https://www.tidyverse.org/help/#reprex)). 22 | The tidyverse guide on [how to create a great issue](https://code-review.tidyverse.org/issues/) 23 | has more advice. 24 | 25 | ### Pull request process 26 | 27 | If you are new to creating pull requests here are some tips. Using the functions 28 | from the `usethis` package is not required but can be helpful if this process is 29 | new to you. 30 | 31 | * Fork the package and clone onto your computer. If you haven't done this before, we recommend using `usethis::create_from_github("stan-dev/cmdstanr", fork = TRUE)`. 32 | 33 | * Install all development dependencies with `devtools::install_dev_deps()`, and then make sure the package passes R CMD check by running `devtools::check()`. 34 | If R CMD check doesn't pass cleanly, it's a good idea to ask for help before continuing. 35 | * Create a Git branch for your pull request (PR). We recommend using `usethis::pr_init("brief-description-of-change")`. 36 | 37 | * Make your changes, commit to git, and then create a PR by running `usethis::pr_push()`, and following the prompts in your browser. 38 | The title of your PR should briefly describe the change. 39 | The body of your PR should contain `Fixes #issue-number`. 40 | 41 | * For user-facing changes, add a bullet to the top of `NEWS.md` (i.e. just below the first header). Follow the style already used in `NEWS.md`. 42 | 43 | ### Code style 44 | 45 | * New code should attempt to follow the style used in the package. When in doubt follow the tidyverse [style guide](https://style.tidyverse.org). 46 | 47 | * We use [roxygen2](https://cran.r-project.org/package=roxygen2), with [Markdown syntax](https://cran.r-project.org/web/packages/roxygen2/vignettes/rd-formatting.html), for documentation. 48 | 49 | * We use [testthat](https://cran.r-project.org/package=testthat) for unit tests. 50 | Contributions with test cases included are easier to accept. 51 | 52 | ## Code of Conduct 53 | 54 | Please note that the cmdstanr project follows the Stan project's 55 | [Code of Conduct](https://discourse.mc-stan.org/t/announcing-our-new-stan-code-of-conduct/23764). 56 | By contributing to this project you agree to abide by its terms. 57 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: stan-dev 2 | custom: https://mc-stan.org/support/ 3 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior. 15 | 16 | **Expected behavior** 17 | A description of what you expected to happen. 18 | 19 | **Operating system** 20 | Your operating system (e.g. mac os x 10.15, windows 10, etc.) 21 | 22 | **CmdStanR version number** 23 | Your CmdStanR version number (e.g. from `packageVersion("cmdstanr")`). 24 | 25 | **Additional context** 26 | Add any other context about the problem here. 27 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: true 2 | contact_links: 3 | - name: Stan Forums 4 | url: https://discourse.mc-stan.org/ 5 | about: For questions and discussion 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: feature 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/other.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Other 3 | about: Issues that don't fit under "Bug report" or "Feature request" 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the issue** 11 | 12 | 13 | **CmdStanR version number** 14 | Your CmdStanR version number (e.g. from `packageVersion("cmdstanr")`). 15 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | #### Submission Checklist 2 | 3 | - [ ] Run unit tests 4 | - [ ] Declare copyright holder and agree to license (see below) 5 | 6 | #### Summary 7 | 8 | Please describe the purpose of the pull request. 9 | 10 | #### Copyright and Licensing 11 | 12 | Please list the copyright holder for the work you are submitting 13 | (this will be you or your assignee, such as a university or company): 14 | **INSERT COPYRIGHT HOLDER HERE** 15 | 16 | 17 | By submitting this pull request, the copyright holder is agreeing to 18 | license the submitted work under the following licenses: 19 | 20 | - Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause) 21 | - Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/) 22 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check-wsl.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | # Github Actions workflow to check CmdStanR 3 | # yamllint disable rule:line-length 4 | 5 | name: Unit tests - WSL Backend 6 | 7 | 'on': 8 | push: 9 | branches: 10 | - master 11 | pull_request: 12 | branches: 13 | - master 14 | 15 | jobs: 16 | WSL-R-CMD-check: 17 | if: "! contains(github.event.head_commit.message, '[ci skip]')" 18 | runs-on: windows-latest 19 | 20 | name: windows-latest-WSL2 21 | 22 | env: 23 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 24 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 25 | NOT_CRAN: true 26 | CMDSTANR_OPENCL_TESTS: true 27 | 28 | steps: 29 | - name: cmdstan env vars 30 | run: | 31 | echo "CMDSTAN_PATH=${HOME}/.cmdstan" >> $GITHUB_ENV 32 | shell: bash 33 | 34 | - uses: n1hility/cancel-previous-runs@v3 35 | with: 36 | token: ${{ secrets.GITHUB_TOKEN }} 37 | if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'" 38 | 39 | - uses: actions/checkout@v4 40 | 41 | - uses: r-lib/actions/setup-r@v2 42 | - uses: r-lib/actions/setup-pandoc@v2 43 | 44 | - uses: r-lib/actions/setup-r-dependencies@v2 45 | with: 46 | extra-packages: any::rcmdcheck, local::. 47 | 48 | - uses: Vampire/setup-wsl@v5 49 | with: 50 | distribution: Ubuntu-22.04 51 | wsl-version: 2 52 | use-cache: 'true' 53 | set-as-default: 'true' 54 | 55 | - name: Install WSL Dependencies 56 | run: | 57 | sudo apt-get update 58 | sudo apt-get install -y build-essential libopenmpi-dev ocl-icd-opencl-dev pocl-opencl-icd 59 | shell: wsl-bash {0} 60 | 61 | - name: Install cmdstan 62 | run: | 63 | cmdstanr::check_cmdstan_toolchain(fix = TRUE) 64 | cmdstanr::install_cmdstan(cores = 2, wsl = TRUE, overwrite = TRUE) 65 | shell: Rscript {0} 66 | 67 | - name: Session info 68 | run: | 69 | options(width = 100) 70 | pkgs <- installed.packages()[, "Package"] 71 | sessioninfo::session_info(pkgs, include_base = TRUE) 72 | shell: Rscript {0} 73 | 74 | - uses: r-lib/actions/check-r-package@v2 75 | env: 76 | _R_CHECK_CRAN_INCOMING_: false 77 | 78 | - name: Show testthat output 79 | if: always() 80 | run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true 81 | shell: bash 82 | 83 | - name: Upload check results 84 | if: failure() 85 | uses: actions/upload-artifact@v4 86 | with: 87 | name: wsl-backend-results 88 | path: check 89 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | .vscode/* 6 | 7 | inst/doc 8 | dev-helpers.R 9 | release-prep.R 10 | *.DS_Store 11 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: cmdstanr 2 | Title: R Interface to 'CmdStan' 3 | Version: 0.9.0.9000 4 | Date: 2025-03-30 5 | Authors@R: 6 | c(person(given = "Jonah", family = "Gabry", role = "aut", 7 | email = "jsg2201@columbia.edu"), 8 | person(given = "Rok", family = "Češnovar", role = "aut", 9 | email = "rok.cesnovar@fri.uni-lj.si"), 10 | person(given = "Andrew", family = "Johnson", role = c("aut", "cre"), 11 | email = "andrew.johnson@arjohnsonau.com", 12 | comment = c(ORCID = "0000-0001-7000-8065")), 13 | person(given = "Steve", family = "Bronder", role = "aut"), 14 | person(given = "Ben", family = "Bales", role = "ctb"), 15 | person(given = "Mitzi", family = "Morris", role = "ctb"), 16 | person(given = "Mikhail", family = "Popov", role = "ctb"), 17 | person(given = "Mike", family = "Lawrence", role = "ctb"), 18 | person(given = c("William", "Michael"), family = "Landau", role = "ctb", 19 | email = "will.landau@gmail.com", comment = c(ORCID = "0000-0003-1878-3253")), 20 | person(given = "Jacob", family = "Socolar", role = "ctb"), 21 | person(given = "Martin", family = "Modrák", role = "ctb"), 22 | person(given = "Ven", family = "Popov", role = "ctb") 23 | ) 24 | Description: A lightweight interface to 'Stan' . 25 | The 'CmdStanR' interface is an alternative to 'RStan' that calls the command 26 | line interface for compilation and running algorithms instead of interfacing 27 | with C++ via 'Rcpp'. This has many benefits including always being compatible 28 | with the latest version of Stan, fewer installation errors, fewer unexpected 29 | crashes in RStudio, and a more permissive license. 30 | License: BSD_3_clause + file LICENSE 31 | URL: https://mc-stan.org/cmdstanr/, https://discourse.mc-stan.org 32 | BugReports: https://github.com/stan-dev/cmdstanr/issues 33 | Encoding: UTF-8 34 | LazyData: true 35 | RoxygenNote: 7.3.2 36 | Roxygen: list(markdown = TRUE, r6 = FALSE) 37 | SystemRequirements: CmdStan (https://mc-stan.org/users/interfaces/cmdstan) 38 | Depends: 39 | R (>= 3.5.0) 40 | Imports: 41 | checkmate, 42 | data.table, 43 | jsonlite (>= 1.2.0), 44 | posterior (>= 1.4.1), 45 | processx (>= 3.5.0), 46 | R6 (>= 2.4.0), 47 | withr (>= 2.5.0), 48 | rlang (>= 0.4.7) 49 | Suggests: 50 | bayesplot, 51 | ggplot2, 52 | knitr (>= 1.37), 53 | loo (>= 2.0.0), 54 | rmarkdown, 55 | testthat (>= 2.1.0), 56 | Rcpp 57 | VignetteBuilder: knitr 58 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2019 2 | COPYRIGHT HOLDER: Stan Developers and their Assignees 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | ===================== 3 | 4 | Copyright (c) 2019, Stan Developers and their Assignees 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright notice, this 11 | list of conditions and the following disclaimer. 12 | 13 | 2. Redistributions in binary form must reproduce the above copyright notice, 14 | this list of conditions and the following disclaimer in the documentation 15 | and/or other materials provided with the distribution. 16 | 17 | 3. Neither the name of the copyright holder nor the names of its 18 | contributors may be used to endorse or promote products derived from 19 | this software without specific prior written permission. 20 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(as_draws,CmdStanGQ) 4 | S3method(as_draws,CmdStanLaplace) 5 | S3method(as_draws,CmdStanMCMC) 6 | S3method(as_draws,CmdStanMLE) 7 | S3method(as_draws,CmdStanPathfinder) 8 | S3method(as_draws,CmdStanVB) 9 | S3method(process_init,"function") 10 | S3method(process_init,CmdStanLaplace) 11 | S3method(process_init,CmdStanMCMC) 12 | S3method(process_init,CmdStanMLE) 13 | S3method(process_init,CmdStanPathfinder) 14 | S3method(process_init,CmdStanVB) 15 | S3method(process_init,default) 16 | S3method(process_init,draws) 17 | S3method(process_init,list) 18 | export(as.CmdStanDiagnose) 19 | export(as.CmdStanGQ) 20 | export(as.CmdStanLaplace) 21 | export(as.CmdStanMCMC) 22 | export(as.CmdStanMLE) 23 | export(as.CmdStanPathfinder) 24 | export(as.CmdStanVB) 25 | export(as_cmdstan_fit) 26 | export(as_draws) 27 | export(as_mcmc.list) 28 | export(check_cmdstan_toolchain) 29 | export(cmdstan_default_install_path) 30 | export(cmdstan_default_path) 31 | export(cmdstan_make_local) 32 | export(cmdstan_model) 33 | export(cmdstan_path) 34 | export(cmdstan_version) 35 | export(cmdstanr_example) 36 | export(draws_to_csv) 37 | export(eng_cmdstan) 38 | export(install_cmdstan) 39 | export(num_threads) 40 | export(print_example_program) 41 | export(read_cmdstan_csv) 42 | export(read_sample_csv) 43 | export(rebuild_cmdstan) 44 | export(register_knitr_engine) 45 | export(set_cmdstan_path) 46 | export(set_num_threads) 47 | export(write_stan_file) 48 | export(write_stan_json) 49 | export(write_stan_tempfile) 50 | import(R6) 51 | importFrom(posterior,as_draws) 52 | importFrom(stats,aggregate) 53 | -------------------------------------------------------------------------------- /R/cmdstanr-package.R: -------------------------------------------------------------------------------- 1 | #' CmdStanR: the R interface to CmdStan 2 | #' 3 | #' @docType package 4 | #' @name cmdstanr-package 5 | #' @aliases cmdstanr CmdStanR 6 | #' 7 | #' @description 8 | #' \if{html}{ 9 | #' \figure{logo.png}{options: width="50" alt="https://mc-stan.org/about/logo/"} 10 | #' \emph{Stan Development Team} 11 | #' } 12 | #' 13 | #' \pkg{CmdStanR}: the \R interface to CmdStan. 14 | #' 15 | #' @details CmdStanR (\pkg{cmdstanr} package) is an interface to Stan 16 | #' ([mc-stan.org](https://mc-stan.org)) for \R users. It provides the 17 | #' necessary objects and functions to compile a Stan program and run Stan's 18 | #' algorithms from \R via CmdStan, the shell interface to Stan 19 | #' ([mc-stan.org/users/interfaces/cmdstan](https://mc-stan.org/users/interfaces/cmdstan)). 20 | #' 21 | #' @includeRmd vignettes/children/comparison-with-rstan.md 22 | #' 23 | #' @section Getting started: CmdStanR requires a working version of CmdStan. If 24 | #' you already have CmdStan installed see [cmdstan_model()] to get started, 25 | #' otherwise see [install_cmdstan()] to install CmdStan. The vignette 26 | #' [_Getting started with CmdStanR_](https://mc-stan.org/cmdstanr/articles/cmdstanr.html) 27 | #' demonstrates the basic functionality of the package. 28 | #' 29 | #' For a list of global [options][base::options()] see 30 | #' [cmdstanr_global_options]. 31 | #' 32 | #' @template seealso-docs 33 | #' @inherit cmdstan_model examples 34 | #' @import R6 35 | #' 36 | "_PACKAGE" 37 | 38 | if (getRversion() >= "2.15.1") utils::globalVariables(c("self", "private", "super")) 39 | -------------------------------------------------------------------------------- /R/generics.R: -------------------------------------------------------------------------------- 1 | #' Coercion methods for CmdStan objects 2 | #' 3 | #' These are generic functions intended to primarily be used by developers of 4 | #' packages that interface with on CmdStanR. Developers can define methods on 5 | #' top of these generics to coerce objects into CmdStanR's fitted model objects. 6 | #' 7 | #' @param object The object to be coerced. 8 | #' @param ... Additional arguments to pass to methods. 9 | #' 10 | #' @name cmdstan_coercion 11 | NULL 12 | 13 | #' @rdname cmdstan_coercion 14 | #' @export 15 | as.CmdStanMCMC <- function(object, ...) { 16 | UseMethod("as.CmdStanMCMC") 17 | } 18 | 19 | #' @rdname cmdstan_coercion 20 | #' @export 21 | as.CmdStanMLE <- function(object, ...) { 22 | UseMethod("as.CmdStanMLE") 23 | } 24 | 25 | #' @rdname cmdstan_coercion 26 | #' @export 27 | as.CmdStanLaplace <- function(object, ...) { 28 | UseMethod("as.CmdStanLaplace") 29 | } 30 | 31 | #' @rdname cmdstan_coercion 32 | #' @export 33 | as.CmdStanVB <- function(object, ...) { 34 | UseMethod("as.CmdStanVB") 35 | } 36 | 37 | #' @rdname cmdstan_coercion 38 | #' @export 39 | as.CmdStanPathfinder <- function(object, ...) { 40 | UseMethod("as.CmdStanPathfinder") 41 | } 42 | 43 | #' @rdname cmdstan_coercion 44 | #' @export 45 | as.CmdStanGQ <- function(object, ...) { 46 | UseMethod("as.CmdStanGQ") 47 | } 48 | 49 | #' @rdname cmdstan_coercion 50 | #' @export 51 | as.CmdStanDiagnose <- function(object, ...) { 52 | UseMethod("as.CmdStanDiagnose") 53 | } 54 | -------------------------------------------------------------------------------- /R/options.R: -------------------------------------------------------------------------------- 1 | #' CmdStanR global options 2 | #' 3 | #' These options can be set via [options()] for an entire \R session. 4 | #' 5 | #' @name cmdstanr_global_options 6 | #' 7 | #' @details 8 | #' 9 | #' * `cmdstanr_draws_format`: Which format provided by the \pkg{posterior} 10 | #' package should be used when returning the posterior or approximate posterior 11 | #' draws? The default depends on the model fitting method. See 12 | #' [draws][fit-method-draws] for more details. 13 | #' 14 | #' * `cmdstanr_force_recompile`: Should the default be to recompile models 15 | #' even if there were no Stan code changes since last compiled? See 16 | #' [compile][model-method-compile] for more details. The default is `FALSE`. 17 | #' 18 | #' * `cmdstanr_max_rows`: The maximum number of rows of output to print when 19 | #' using the [`$print()`][fit-method-summary] method. The default is 10. 20 | #' 21 | #' * `cmdstanr_print_line_numbers`: Should line numbers be included when 22 | #' printing a Stan program? The default is `FALSE`. 23 | #' 24 | #' * `cmdstanr_no_ver_check`: Should the check for a more recent version of 25 | #' CmdStan be disabled? The default is `FALSE`. 26 | #' 27 | #' * `cmdstanr_output_dir`: The directory where CmdStan should write its output 28 | #' CSV files when fitting models. The default is a temporary directory. Files in 29 | #' a temporary directory are removed as part of \R garbage collection, while 30 | #' files in an explicitly defined directory are not automatically deleted. 31 | #' 32 | #' * `cmdstanr_verbose`: Should more information be printed 33 | #' when compiling or running models, including showing how CmdStan was called 34 | #' internally? The default is `FALSE`. 35 | #' 36 | #' * `cmdstanr_warn_inits`: Should a warning be thrown if initial values are 37 | #' only provided for a subset of parameters? The default is `TRUE`. 38 | #' 39 | #' * `cmdstanr_write_stan_file_dir`: The directory where [write_stan_file()] 40 | #' should write Stan files. The default is a temporary directory. Files in 41 | #' a temporary directory are removed as part of \R garbage collection, while 42 | #' files in an explicitly defined directory are not automatically deleted. 43 | #' 44 | #' * `mc.cores`: The number of cores to use for various parallelization tasks 45 | #' (e.g. running MCMC chains, installing CmdStan). The default depends on the 46 | #' use case and is documented with the methods that make use of `mc.cores`. 47 | #' 48 | #' 49 | NULL 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CmdStanR 2 | 3 | 4 | [![CRAN status](https://www.r-pkg.org/badges/version/cmdstanr)](https://CRAN.R-project.org/package=cmdstanr) 5 | [![Unit tests](https://github.com/stan-dev/cmdstanr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/stan-dev/cmdstanr/actions/workflows/R-CMD-check.yaml) 6 | [![Codecov test coverage](https://codecov.io/gh/stan-dev/cmdstanr/branch/master/graph/badge.svg)](https://app.codecov.io/gh/stan-dev/cmdstanr?branch=master) 7 | 8 | 9 | ### Overview 10 | 11 | CmdStanR is a lightweight interface to [Stan](https://mc-stan.org) for R users 12 | (see [CmdStanPy](https://github.com/stan-dev/cmdstanpy) for Python). 13 | 14 | If you are new to CmdStanR we recommend starting with these vignettes: 15 | 16 | * [_Getting started with CmdStanR_](https://mc-stan.org/cmdstanr/articles/cmdstanr.html) 17 | 18 | * [_How does CmdStanR work?_](https://mc-stan.org/cmdstanr/articles/cmdstanr-internals.html) 19 | 20 | ### Goals 21 | 22 | * A clean interface to Stan services so that CmdStanR can keep up with Stan 23 | releases. 24 | 25 | * R code that doesn't interface directly with C++, only calls compiled executables. 26 | 27 | * Modularity: CmdStanR runs Stan's algorithms and lets downstream modules do the 28 | analysis. 29 | 30 | * Flexible [BSD-3 license](https://opensource.org/license/bsd-3-clause). 31 | 32 | 33 | ### Installation 34 | 35 | #### Installing the R package 36 | 37 | You can install the latest release of the **cmdstanr** R package with 38 | 39 | ```r 40 | # we recommend running this in a fresh R session or restarting your current session 41 | install.packages("cmdstanr", repos = c('https://stan-dev.r-universe.dev', getOption("repos"))) 42 | ``` 43 | This does not install the vignettes, which take a long time to build, but they are always available 44 | online at https://mc-stan.org/cmdstanr/articles/. 45 | 46 | To instead install the latest development version of the package from GitHub use 47 | 48 | ```r 49 | # install.packages("remotes") 50 | remotes::install_github("stan-dev/cmdstanr") 51 | ``` 52 | 53 | #### Installing CmdStan 54 | 55 | If you don't already have CmdStan installed then, in addition to installing the 56 | R package, it is also necessary to install CmdStan using CmdStanR's 57 | `install_cmdstan()` function. A suitable C++ toolchain is also required. 58 | Instructions are provided in the [_Getting started with 59 | CmdStanR_](https://mc-stan.org/cmdstanr/articles/cmdstanr.html) vignette. 60 | 61 | 62 | ### Contributing 63 | 64 | There is a lot of work still to be done, and we welcome contributions from 65 | anyone! If you are interested in contributing, please [comment on an open issue or 66 | open a new one](https://github.com/stan-dev/cmdstanr/issues) on GitHub. For more details, please check [CONTRIBUTING.md](https://github.com/stan-dev/cmdstanr/blob/fe126708f53d89ecd889d2f122f6bd69c7404836/.github/CONTRIBUTING.md). 67 | ### License 68 | 69 | CmdStanR, like CmdStan and the core Stan C++ code, is licensed under the 70 | following licenses: 71 | 72 | - Code: BSD 3-clause (https://opensource.org/license/bsd-3-clause/) 73 | - Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/) 74 | -------------------------------------------------------------------------------- /cmdstanr.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | ProjectId: b2cb035a-82d0-45fa-b984-ec75db1e1feb 3 | 4 | RestoreWorkspace: Default 5 | SaveWorkspace: Default 6 | AlwaysSaveHistory: Default 7 | 8 | EnableCodeIndexing: Yes 9 | UseSpacesForTab: Yes 10 | NumSpacesForTab: 2 11 | Encoding: UTF-8 12 | 13 | RnwWeave: Sweave 14 | LaTeX: pdfLaTeX 15 | 16 | AutoAppendNewline: Yes 17 | StripTrailingWhitespace: Yes 18 | 19 | BuildType: Package 20 | PackageUseDevtools: Yes 21 | PackageInstallArgs: --no-multiarch --with-keep.source 22 | PackageRoxygenize: rd,collate,namespace 23 | -------------------------------------------------------------------------------- /docs/apple-touch-icon-120x120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/apple-touch-icon-120x120.png -------------------------------------------------------------------------------- /docs/apple-touch-icon-152x152.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/apple-touch-icon-152x152.png -------------------------------------------------------------------------------- /docs/apple-touch-icon-180x180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/apple-touch-icon-180x180.png -------------------------------------------------------------------------------- /docs/apple-touch-icon-60x60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/apple-touch-icon-60x60.png -------------------------------------------------------------------------------- /docs/apple-touch-icon-76x76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/apple-touch-icon-76x76.png -------------------------------------------------------------------------------- /docs/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/apple-touch-icon.png -------------------------------------------------------------------------------- /docs/articles/articles-online-only/opencl_files/header-attrs-2.11/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/articles-online-only/opencl_files/header-attrs-2.13/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/articles-online-only/opencl_files/header-attrs-2.9/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/children/settings-knitr_files/header-attrs-2.9/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/cmdstanr-internals_files/accessible-code-block-0.0.1/empty-anchor.js: -------------------------------------------------------------------------------- 1 | // Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> 2 | // v0.0.1 3 | // Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. 4 | 5 | document.addEventListener('DOMContentLoaded', function() { 6 | const codeList = document.getElementsByClassName("sourceCode"); 7 | for (var i = 0; i < codeList.length; i++) { 8 | var linkList = codeList[i].getElementsByTagName('a'); 9 | for (var j = 0; j < linkList.length; j++) { 10 | if (linkList[j].innerHTML === "") { 11 | linkList[j].setAttribute('aria-hidden', 'true'); 12 | } 13 | } 14 | } 15 | }); 16 | -------------------------------------------------------------------------------- /docs/articles/cmdstanr-internals_files/header-attrs-2.3/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/cmdstanr-internals_files/header-attrs-2.6/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/cmdstanr-internals_files/header-attrs-2.8/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/cmdstanr-internals_files/header-attrs-2.9/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/accessible-code-block-0.0.1/empty-anchor.js: -------------------------------------------------------------------------------- 1 | // Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> 2 | // v0.0.1 3 | // Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. 4 | 5 | document.addEventListener('DOMContentLoaded', function() { 6 | const codeList = document.getElementsByClassName("sourceCode"); 7 | for (var i = 0; i < codeList.length; i++) { 8 | var linkList = codeList[i].getElementsByTagName('a'); 9 | for (var j = 0; j < linkList.length; j++) { 10 | if (linkList[j].innerHTML === "") { 11 | linkList[j].setAttribute('aria-hidden', 'true'); 12 | } 13 | } 14 | } 15 | }); 16 | -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/draws-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/draws-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/laplace-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/laplace-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/plot-compare-laplace-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/plot-compare-laplace-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/plot-compare-mcmc-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/plot-compare-mcmc-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/plot-compare-pf-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/plot-compare-pf-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/plot-compare-vb-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/plot-compare-vb-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/plot-mle-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/plot-mle-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/plot-variational-1-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/plot-variational-1-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/plot-variational-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/plot-variational-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/plot-variational-2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/plot-variational-2-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/plots-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/plots-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/unnamed-chunk-3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/unnamed-chunk-3-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/figure-html/variational-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/articles/cmdstanr_files/figure-html/variational-1.png -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/header-attrs-2.3/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/header-attrs-2.6/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/cmdstanr_files/header-attrs-2.9/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/deprecations_files/header-attrs-2.11/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/deprecations_files/header-attrs-2.9/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/posterior_files/header-attrs-2.18/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/profiling_files/accessible-code-block-0.0.1/empty-anchor.js: -------------------------------------------------------------------------------- 1 | // Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> 2 | // v0.0.1 3 | // Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. 4 | 5 | document.addEventListener('DOMContentLoaded', function() { 6 | const codeList = document.getElementsByClassName("sourceCode"); 7 | for (var i = 0; i < codeList.length; i++) { 8 | var linkList = codeList[i].getElementsByTagName('a'); 9 | for (var j = 0; j < linkList.length; j++) { 10 | if (linkList[j].innerHTML === "") { 11 | linkList[j].setAttribute('aria-hidden', 'true'); 12 | } 13 | } 14 | } 15 | }); 16 | -------------------------------------------------------------------------------- /docs/articles/profiling_files/anchor-sections-1.0/anchor-sections.css: -------------------------------------------------------------------------------- 1 | /* Styles for section anchors */ 2 | a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;} 3 | a.anchor-section::before {content: '#';} 4 | .hasAnchor:hover a.anchor-section {visibility: visible;} 5 | -------------------------------------------------------------------------------- /docs/articles/profiling_files/anchor-sections-1.0/anchor-sections.js: -------------------------------------------------------------------------------- 1 | // Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020. 2 | document.addEventListener('DOMContentLoaded', function() { 3 | // Do nothing if AnchorJS is used 4 | if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) { 5 | return; 6 | } 7 | 8 | const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6'); 9 | 10 | // Do nothing if sections are already anchored 11 | if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) { 12 | return null; 13 | } 14 | 15 | // Use section id when pandoc runs with --section-divs 16 | const section_id = function(x) { 17 | return ((x.classList.contains('section') || (x.tagName === 'SECTION')) 18 | ? x.id : ''); 19 | }; 20 | 21 | // Add anchors 22 | h.forEach(function(x) { 23 | const id = x.id || section_id(x.parentElement); 24 | if (id === '') { 25 | return null; 26 | } 27 | let anchor = document.createElement('a'); 28 | anchor.href = '#' + id; 29 | anchor.classList = ['anchor-section']; 30 | x.classList.add('hasAnchor'); 31 | x.appendChild(anchor); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /docs/articles/profiling_files/header-attrs-2.3/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/profiling_files/header-attrs-2.6/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/profiling_files/header-attrs-2.9/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/r-markdown_files/accessible-code-block-0.0.1/empty-anchor.js: -------------------------------------------------------------------------------- 1 | // Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> 2 | // v0.0.1 3 | // Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. 4 | 5 | document.addEventListener('DOMContentLoaded', function() { 6 | const codeList = document.getElementsByClassName("sourceCode"); 7 | for (var i = 0; i < codeList.length; i++) { 8 | var linkList = codeList[i].getElementsByTagName('a'); 9 | for (var j = 0; j < linkList.length; j++) { 10 | if (linkList[j].innerHTML === "") { 11 | linkList[j].setAttribute('aria-hidden', 'true'); 12 | } 13 | } 14 | } 15 | }); 16 | -------------------------------------------------------------------------------- /docs/articles/r-markdown_files/header-attrs-2.3/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/r-markdown_files/header-attrs-2.6/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/r-markdown_files/header-attrs-2.9/header-attrs.js: -------------------------------------------------------------------------------- 1 | // Pandoc 2.9 adds attributes on both header and div. We remove the former (to 2 | // be compatible with the behavior of Pandoc < 2.8). 3 | document.addEventListener('DOMContentLoaded', function(e) { 4 | var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); 5 | var i, h, a; 6 | for (i = 0; i < hs.length; i++) { 7 | h = hs[i]; 8 | if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 9 | a = h.attributes; 10 | while (a.length > 0) h.removeAttribute(a[0].name); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /docs/articles/r_markdown_files/accessible-code-block-0.0.1/empty-anchor.js: -------------------------------------------------------------------------------- 1 | // Hide empty tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> 2 | // v0.0.1 3 | // Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. 4 | 5 | document.addEventListener('DOMContentLoaded', function() { 6 | const codeList = document.getElementsByClassName("sourceCode"); 7 | for (var i = 0; i < codeList.length; i++) { 8 | var linkList = codeList[i].getElementsByTagName('a'); 9 | for (var j = 0; j < linkList.length; j++) { 10 | if (linkList[j].innerHTML === "") { 11 | linkList[j].setAttribute('aria-hidden', 'true'); 12 | } 13 | } 14 | } 15 | }); 16 | -------------------------------------------------------------------------------- /docs/bootstrap-toc.css: -------------------------------------------------------------------------------- 1 | /*! 2 | * Bootstrap Table of Contents v0.4.1 (http://afeld.github.io/bootstrap-toc/) 3 | * Copyright 2015 Aidan Feldman 4 | * Licensed under MIT (https://github.com/afeld/bootstrap-toc/blob/gh-pages/LICENSE.md) */ 5 | 6 | /* modified from https://github.com/twbs/bootstrap/blob/94b4076dd2efba9af71f0b18d4ee4b163aa9e0dd/docs/assets/css/src/docs.css#L548-L601 */ 7 | 8 | /* All levels of nav */ 9 | nav[data-toggle='toc'] .nav > li > a { 10 | display: block; 11 | padding: 4px 20px; 12 | font-size: 13px; 13 | font-weight: 500; 14 | color: #767676; 15 | } 16 | nav[data-toggle='toc'] .nav > li > a:hover, 17 | nav[data-toggle='toc'] .nav > li > a:focus { 18 | padding-left: 19px; 19 | color: #563d7c; 20 | text-decoration: none; 21 | background-color: transparent; 22 | border-left: 1px solid #563d7c; 23 | } 24 | nav[data-toggle='toc'] .nav > .active > a, 25 | nav[data-toggle='toc'] .nav > .active:hover > a, 26 | nav[data-toggle='toc'] .nav > .active:focus > a { 27 | padding-left: 18px; 28 | font-weight: bold; 29 | color: #563d7c; 30 | background-color: transparent; 31 | border-left: 2px solid #563d7c; 32 | } 33 | 34 | /* Nav: second level (shown on .active) */ 35 | nav[data-toggle='toc'] .nav .nav { 36 | display: none; /* Hide by default, but at >768px, show it */ 37 | padding-bottom: 10px; 38 | } 39 | nav[data-toggle='toc'] .nav .nav > li > a { 40 | padding-top: 1px; 41 | padding-bottom: 1px; 42 | padding-left: 30px; 43 | font-size: 12px; 44 | font-weight: normal; 45 | } 46 | nav[data-toggle='toc'] .nav .nav > li > a:hover, 47 | nav[data-toggle='toc'] .nav .nav > li > a:focus { 48 | padding-left: 29px; 49 | } 50 | nav[data-toggle='toc'] .nav .nav > .active > a, 51 | nav[data-toggle='toc'] .nav .nav > .active:hover > a, 52 | nav[data-toggle='toc'] .nav .nav > .active:focus > a { 53 | padding-left: 28px; 54 | font-weight: 500; 55 | } 56 | 57 | /* from https://github.com/twbs/bootstrap/blob/e38f066d8c203c3e032da0ff23cd2d6098ee2dd6/docs/assets/css/src/docs.css#L631-L634 */ 58 | nav[data-toggle='toc'] .nav > .active > ul { 59 | display: block; 60 | } 61 | -------------------------------------------------------------------------------- /docs/docsearch.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | 3 | // register a handler to move the focus to the search bar 4 | // upon pressing shift + "/" (i.e. "?") 5 | $(document).on('keydown', function(e) { 6 | if (e.shiftKey && e.keyCode == 191) { 7 | e.preventDefault(); 8 | $("#search-input").focus(); 9 | } 10 | }); 11 | 12 | $(document).ready(function() { 13 | // do keyword highlighting 14 | /* modified from https://jsfiddle.net/julmot/bL6bb5oo/ */ 15 | var mark = function() { 16 | 17 | var referrer = document.URL ; 18 | var paramKey = "q" ; 19 | 20 | if (referrer.indexOf("?") !== -1) { 21 | var qs = referrer.substr(referrer.indexOf('?') + 1); 22 | var qs_noanchor = qs.split('#')[0]; 23 | var qsa = qs_noanchor.split('&'); 24 | var keyword = ""; 25 | 26 | for (var i = 0; i < qsa.length; i++) { 27 | var currentParam = qsa[i].split('='); 28 | 29 | if (currentParam.length !== 2) { 30 | continue; 31 | } 32 | 33 | if (currentParam[0] == paramKey) { 34 | keyword = decodeURIComponent(currentParam[1].replace(/\+/g, "%20")); 35 | } 36 | } 37 | 38 | if (keyword !== "") { 39 | $(".contents").unmark({ 40 | done: function() { 41 | $(".contents").mark(keyword); 42 | } 43 | }); 44 | } 45 | } 46 | }; 47 | 48 | mark(); 49 | }); 50 | }); 51 | 52 | /* Search term highlighting ------------------------------*/ 53 | 54 | function matchedWords(hit) { 55 | var words = []; 56 | 57 | var hierarchy = hit._highlightResult.hierarchy; 58 | // loop to fetch from lvl0, lvl1, etc. 59 | for (var idx in hierarchy) { 60 | words = words.concat(hierarchy[idx].matchedWords); 61 | } 62 | 63 | var content = hit._highlightResult.content; 64 | if (content) { 65 | words = words.concat(content.matchedWords); 66 | } 67 | 68 | // return unique words 69 | var words_uniq = [...new Set(words)]; 70 | return words_uniq; 71 | } 72 | 73 | function updateHitURL(hit) { 74 | 75 | var words = matchedWords(hit); 76 | var url = ""; 77 | 78 | if (hit.anchor) { 79 | url = hit.url_without_anchor + '?q=' + escape(words.join(" ")) + '#' + hit.anchor; 80 | } else { 81 | url = hit.url + '?q=' + escape(words.join(" ")); 82 | } 83 | 84 | return url; 85 | } 86 | -------------------------------------------------------------------------------- /docs/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/favicon-16x16.png -------------------------------------------------------------------------------- /docs/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/favicon-32x32.png -------------------------------------------------------------------------------- /docs/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/favicon.ico -------------------------------------------------------------------------------- /docs/link.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /docs/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/logo.png -------------------------------------------------------------------------------- /docs/pkgdown.yml: -------------------------------------------------------------------------------- 1 | pandoc: '3.2' 2 | pkgdown: 2.0.9 3 | pkgdown_sha: ~ 4 | articles: 5 | cmdstanr-internals: cmdstanr-internals.html 6 | cmdstanr: cmdstanr.html 7 | opencl: articles-online-only/opencl.html 8 | posterior: posterior.html 9 | profiling: profiling.html 10 | r-markdown: r-markdown.html 11 | last_built: 2025-03-31T14:41Z 12 | urls: 13 | reference: https://mc-stan.org/cmdstanr/reference 14 | article: https://mc-stan.org/cmdstanr/articles 15 | 16 | -------------------------------------------------------------------------------- /docs/reference/CmdStanModel-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/CmdStanModel-1.png -------------------------------------------------------------------------------- /docs/reference/CmdStanModel-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/CmdStanModel-2.png -------------------------------------------------------------------------------- /docs/reference/CmdStanModel-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/CmdStanModel-3.png -------------------------------------------------------------------------------- /docs/reference/Rplot001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/Rplot001.png -------------------------------------------------------------------------------- /docs/reference/Rplot002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/Rplot002.png -------------------------------------------------------------------------------- /docs/reference/Rplot003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/Rplot003.png -------------------------------------------------------------------------------- /docs/reference/cmdstan_model-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/cmdstan_model-1.png -------------------------------------------------------------------------------- /docs/reference/cmdstan_model-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/cmdstan_model-2.png -------------------------------------------------------------------------------- /docs/reference/cmdstan_model-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/cmdstan_model-3.png -------------------------------------------------------------------------------- /docs/reference/cmdstanr-package-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/cmdstanr-package-1.png -------------------------------------------------------------------------------- /docs/reference/cmdstanr-package-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/cmdstanr-package-2.png -------------------------------------------------------------------------------- /docs/reference/cmdstanr-package-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/cmdstanr-package-3.png -------------------------------------------------------------------------------- /docs/reference/figures/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/figures/logo.png -------------------------------------------------------------------------------- /docs/reference/fit-method-draws-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/fit-method-draws-1.png -------------------------------------------------------------------------------- /docs/reference/fit-method-draws-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/fit-method-draws-2.png -------------------------------------------------------------------------------- /docs/reference/fit-method-lp-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/fit-method-lp-1.png -------------------------------------------------------------------------------- /docs/reference/model-method-laplace-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-laplace-1.png -------------------------------------------------------------------------------- /docs/reference/model-method-optimize-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-optimize-1.png -------------------------------------------------------------------------------- /docs/reference/model-method-optimize-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-optimize-2.png -------------------------------------------------------------------------------- /docs/reference/model-method-optimize-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-optimize-3.png -------------------------------------------------------------------------------- /docs/reference/model-method-pathfinder-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-pathfinder-1.png -------------------------------------------------------------------------------- /docs/reference/model-method-pathfinder-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-pathfinder-2.png -------------------------------------------------------------------------------- /docs/reference/model-method-pathfinder-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-pathfinder-3.png -------------------------------------------------------------------------------- /docs/reference/model-method-sample-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-sample-1.png -------------------------------------------------------------------------------- /docs/reference/model-method-sample-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-sample-2.png -------------------------------------------------------------------------------- /docs/reference/model-method-sample-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-sample-3.png -------------------------------------------------------------------------------- /docs/reference/model-method-variational-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-variational-1.png -------------------------------------------------------------------------------- /docs/reference/model-method-variational-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-variational-2.png -------------------------------------------------------------------------------- /docs/reference/model-method-variational-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/docs/reference/model-method-variational-3.png -------------------------------------------------------------------------------- /inst/include/base_rng.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // [[Rcpp::export]] 5 | SEXP base_rng(boost::uint32_t seed = 1) { 6 | Rcpp::XPtr rng_ptr(new stan::rng_t(seed)); 7 | return rng_ptr; 8 | } 9 | -------------------------------------------------------------------------------- /inst/include/rcpp_eigen_interop.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CMDSTANR_RCPP_EIGEN_INTEROP_HPP 2 | #define CMDSTANR_RCPP_EIGEN_INTEROP_HPP 3 | 4 | #include 5 | #include 6 | 7 | namespace Rcpp { 8 | namespace traits { 9 | template 10 | class Exporter> { 11 | private: 12 | SEXP object; 13 | public: 14 | Exporter(SEXP x) : object(x){} 15 | ~Exporter(){} 16 | 17 | // Adapted from Rcpp/internal/Exporter.h 18 | Eigen::Matrix get() { 19 | if (R == 1) { 20 | Eigen::Matrix result(Rf_length(object)); 21 | Rcpp::internal::export_indexing, T>(object, result); 22 | return result; 23 | } else if (C == 1) { 24 | Eigen::Matrix result(Rf_length(object)); 25 | Rcpp::internal::export_indexing, T>(object, result); 26 | return result; 27 | } else { 28 | Shield dims(Rf_getAttrib(object, R_DimSymbol)); 29 | if (Rf_isNull(dims) || Rf_length(dims) != 2) { 30 | throw Rcpp::not_a_matrix(); 31 | } 32 | int* dims_ = INTEGER(dims); 33 | Eigen::Matrix result(dims_[0], dims_[1]); 34 | T* result_data = result.data(); 35 | Rcpp::internal::export_indexing(object, result_data); 36 | return result; 37 | } 38 | } 39 | }; 40 | } // traits 41 | 42 | // Rcpp is hardcoded to assume that Eigen types will be wrapped using the 43 | // eigen_wrap function in the RcppEigen package 44 | namespace RcppEigen { 45 | template 46 | SEXP eigen_wrap(const T& x) { 47 | const static int RTYPE = Rcpp::traits::r_sexptype_traits>::rtype; 48 | Rcpp::Vector vec_rtn(Rcpp::wrap(stan::math::to_array_1d(x))); 49 | if (!stan::is_eigen_col_vector::value) { 50 | vec_rtn.attr("dim") = Rcpp::Dimension(x.rows(), x.cols()); 51 | } 52 | return vec_rtn; 53 | } 54 | } // RcppEigen 55 | } // Rcpp 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /inst/include/rcpp_tuple_interop.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CMDSTANR_RCPP_TUPLE_INTEROP_HPP 2 | #define CMDSTANR_RCPP_TUPLE_INTEROP_HPP 3 | 4 | #include 5 | #include 6 | 7 | namespace Rcpp { 8 | namespace traits { 9 | /** 10 | * The Rcpp::traits::Exporter class is the implementation used when calling 11 | * Rcpp::as() to convert an R object (SEXP) to the requested c++ type. 12 | */ 13 | template 14 | class Exporter> { 15 | private: 16 | Rcpp::List list_x; 17 | 18 | template 19 | auto get_impl(std::index_sequence i) { 20 | return std::make_tuple( 21 | Rcpp::as(list_x[I].get())... 22 | ); 23 | } 24 | 25 | public: 26 | Exporter(SEXP x) : list_x(x) { } 27 | std::tuple get() { 28 | return get_impl(std::index_sequence_for{}); 29 | } 30 | }; 31 | } 32 | 33 | /** 34 | * The Rcpp::wrap class is used to convert a C++ type to an R object type. 35 | * Rather than implement anything bespoke for tuples we simply return an R list. 36 | */ 37 | template 38 | SEXP wrap(const std::tuple& x) { 39 | return stan::math::apply([](const auto&... args) { 40 | return Rcpp::List::create(Rcpp::wrap(args)...); 41 | }, x); 42 | } 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /inst/include/stan_rng.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CMDSTANR_STAN_RNG_HPP 2 | #define CMDSTANR_STAN_RNG_HPP 3 | 4 | #include 5 | #include 6 | 7 | // A consistent rng_t is defined from 2.35 onwards 8 | // so add a fallback for older versions 9 | #if STAN_MAJOR == 2 && STAN_MINOR >= 35 10 | #include 11 | #else 12 | namespace stan { 13 | using rng_t = boost::ecuyer1988; 14 | } 15 | #endif 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /inst/logistic.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int K; 4 | array[N] int y; 5 | matrix[N, K] X; 6 | } 7 | parameters { 8 | real alpha; 9 | vector[K] beta; 10 | } 11 | model { 12 | target += normal_lpdf(alpha | 0, 1); 13 | target += normal_lpdf(beta | 0, 1); 14 | target += bernoulli_logit_glm_lpmf(y | X, alpha, beta); 15 | } 16 | generated quantities { 17 | vector[N] log_lik; 18 | for (n in 1 : N) { 19 | log_lik[n] = bernoulli_logit_lpmf(y[n] | alpha + X[n] * beta); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /inst/schools.data.json: -------------------------------------------------------------------------------- 1 | { 2 | "J": 8, 3 | "y": [28, 8, -3, 7, -1, 1, 18, 12], 4 | "sigma": [15, 10, 16, 11, 9, 11, 10, 18] 5 | } 6 | -------------------------------------------------------------------------------- /inst/schools.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int J; 3 | vector[J] sigma; 4 | vector[J] y; 5 | } 6 | parameters { 7 | real mu; 8 | real tau; 9 | vector[J] theta; 10 | } 11 | model { 12 | target += normal_lpdf(tau | 0, 10); 13 | target += normal_lpdf(mu | 0, 10); 14 | target += normal_lpdf(theta | mu, tau); 15 | target += normal_lpdf(y | theta, sigma); 16 | } 17 | -------------------------------------------------------------------------------- /inst/schools_ncp.data.json: -------------------------------------------------------------------------------- 1 | { 2 | "J": 8, 3 | "y": [28, 8, -3, 7, -1, 1, 18, 12], 4 | "sigma": [15, 10, 16, 11, 9, 11, 10, 18] 5 | } 6 | -------------------------------------------------------------------------------- /inst/schools_ncp.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int J; 3 | vector[J] sigma; 4 | vector[J] y; 5 | } 6 | parameters { 7 | real mu; 8 | real tau; 9 | vector[J] theta_raw; 10 | } 11 | transformed parameters { 12 | vector[J] theta = mu + tau * theta_raw; 13 | } 14 | model { 15 | target += normal_lpdf(tau | 0, 10); 16 | target += normal_lpdf(mu | 0, 10); 17 | target += normal_lpdf(theta_raw | 0, 1); 18 | target += normal_lpdf(y | theta, sigma); 19 | } 20 | -------------------------------------------------------------------------------- /man-roxygen/seealso-docs.R: -------------------------------------------------------------------------------- 1 | #' @seealso 2 | #' The CmdStanR website 3 | #' ([mc-stan.org/cmdstanr](https://mc-stan.org/cmdstanr/)) for online 4 | #' documentation and tutorials. 5 | #' 6 | #' The Stan and CmdStan documentation: 7 | #' * Stan documentation: [mc-stan.org/users/documentation](https://mc-stan.org/users/documentation/) 8 | #' * CmdStan User’s Guide: [mc-stan.org/docs/cmdstan-guide](https://mc-stan.org/docs/cmdstan-guide/) 9 | #' 10 | -------------------------------------------------------------------------------- /man/CmdStanDiagnose.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{CmdStanDiagnose} 4 | \alias{CmdStanDiagnose} 5 | \title{CmdStanDiagnose objects} 6 | \description{ 7 | A \code{CmdStanDiagnose} object is the object returned by the 8 | \code{\link[=model-method-diagnose]{$diagnose()}} method of a \code{\link{CmdStanModel}} object. 9 | } 10 | \section{Methods}{ 11 | \code{CmdStanDiagnose} objects have the following associated 12 | methods:\tabular{ll}{ 13 | \strong{Method} \tab \strong{Description} \cr 14 | \code{\link[=fit-method-gradients]{$gradients()}} \tab Return gradients from diagnostic mode. \cr 15 | \code{\link[=fit-method-lp]{$lp()}} \tab Return the total log probability density (\code{target}). \cr 16 | \code{\link[=fit-method-init]{$init()}} \tab Return user-specified initial values. \cr 17 | \code{\link[=fit-method-metadata]{$metadata()}} \tab Return a list of metadata gathered from the CmdStan CSV files. \cr 18 | \code{\link[=fit-method-save_output_files]{$save_output_files()}} \tab Save output CSV files to a specified location. \cr 19 | \code{\link[=fit-method-save_data_file]{$save_data_file()}} \tab Save JSON data file to a specified location. \cr 20 | } 21 | } 22 | 23 | \examples{ 24 | \dontrun{ 25 | test <- cmdstanr_example("logistic", method = "diagnose") 26 | 27 | # retrieve the gradients 28 | test$gradients() 29 | } 30 | 31 | } 32 | \seealso{ 33 | The CmdStanR website 34 | (\href{https://mc-stan.org/cmdstanr/}{mc-stan.org/cmdstanr}) for online 35 | documentation and tutorials. 36 | 37 | The Stan and CmdStan documentation: 38 | \itemize{ 39 | \item Stan documentation: \href{https://mc-stan.org/users/documentation/}{mc-stan.org/users/documentation} 40 | \item CmdStan User’s Guide: \href{https://mc-stan.org/docs/cmdstan-guide/}{mc-stan.org/docs/cmdstan-guide} 41 | } 42 | 43 | Other fitted model objects: 44 | \code{\link{CmdStanGQ}}, 45 | \code{\link{CmdStanLaplace}}, 46 | \code{\link{CmdStanMCMC}}, 47 | \code{\link{CmdStanMLE}}, 48 | \code{\link{CmdStanPathfinder}}, 49 | \code{\link{CmdStanVB}} 50 | } 51 | \concept{fitted model objects} 52 | -------------------------------------------------------------------------------- /man/as_draws.CmdStanMCMC.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{as_draws.CmdStanMCMC} 4 | \alias{as_draws.CmdStanMCMC} 5 | \alias{as_draws} 6 | \alias{as_draws.CmdStanMLE} 7 | \alias{as_draws.CmdStanLaplace} 8 | \alias{as_draws.CmdStanVB} 9 | \alias{as_draws.CmdStanGQ} 10 | \alias{as_draws.CmdStanPathfinder} 11 | \title{Create a \code{draws} object from a CmdStanR fitted model object} 12 | \usage{ 13 | \method{as_draws}{CmdStanMCMC}(x, ...) 14 | 15 | \method{as_draws}{CmdStanMLE}(x, ...) 16 | 17 | \method{as_draws}{CmdStanLaplace}(x, ...) 18 | 19 | \method{as_draws}{CmdStanVB}(x, ...) 20 | 21 | \method{as_draws}{CmdStanGQ}(x, ...) 22 | 23 | \method{as_draws}{CmdStanPathfinder}(x, ...) 24 | } 25 | \arguments{ 26 | \item{x}{A CmdStanR fitted model object.} 27 | 28 | \item{...}{Optional arguments passed to the \code{\link[=fit-method-draws]{$draws()}} 29 | method (e.g., \code{variables}, \code{inc_warmup}, etc.).} 30 | } 31 | \description{ 32 | Create a \code{draws} object supported by the \pkg{posterior} package. These 33 | methods are just wrappers around CmdStanR's \code{\link[=fit-method-draws]{$draws()}} 34 | method provided for convenience. 35 | } 36 | \details{ 37 | To subset iterations, chains, or draws, use the 38 | \code{\link[posterior:subset_draws]{posterior::subset_draws()}} method after creating the \code{draws} object. 39 | } 40 | \examples{ 41 | \dontrun{ 42 | fit <- cmdstanr_example() 43 | as_draws(fit) 44 | 45 | # posterior's as_draws_*() methods will also work 46 | posterior::as_draws_rvars(fit) 47 | posterior::as_draws_list(fit) 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /man/as_mcmc.list.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils.R 3 | \name{as_mcmc.list} 4 | \alias{as_mcmc.list} 5 | \title{Convert \code{CmdStanMCMC} to \code{mcmc.list}} 6 | \usage{ 7 | as_mcmc.list(x) 8 | } 9 | \arguments{ 10 | \item{x}{A \link{CmdStanMCMC} object.} 11 | } 12 | \value{ 13 | An \code{mcmc.list} object compatible with the \pkg{coda} package. 14 | } 15 | \description{ 16 | This function converts a \code{CmdStanMCMC} object to an \code{mcmc.list} object 17 | compatible with the \pkg{coda} package. This is primarily intended for users 18 | of Stan coming from BUGS/JAGS who are used to \pkg{coda} for plotting and 19 | diagnostics. In general we recommend the more recent MCMC diagnostics in 20 | \pkg{posterior} and the \pkg{ggplot2}-based plotting functions in 21 | \pkg{bayesplot}, but for users who prefer \pkg{coda} this function provides 22 | compatibility. 23 | } 24 | \examples{ 25 | \dontrun{ 26 | fit <- cmdstanr_example() 27 | x <- as_mcmc.list(fit) 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /man/cmdstan_coercion.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/generics.R 3 | \name{cmdstan_coercion} 4 | \alias{cmdstan_coercion} 5 | \alias{as.CmdStanMCMC} 6 | \alias{as.CmdStanMLE} 7 | \alias{as.CmdStanLaplace} 8 | \alias{as.CmdStanVB} 9 | \alias{as.CmdStanPathfinder} 10 | \alias{as.CmdStanGQ} 11 | \alias{as.CmdStanDiagnose} 12 | \title{Coercion methods for CmdStan objects} 13 | \usage{ 14 | as.CmdStanMCMC(object, ...) 15 | 16 | as.CmdStanMLE(object, ...) 17 | 18 | as.CmdStanLaplace(object, ...) 19 | 20 | as.CmdStanVB(object, ...) 21 | 22 | as.CmdStanPathfinder(object, ...) 23 | 24 | as.CmdStanGQ(object, ...) 25 | 26 | as.CmdStanDiagnose(object, ...) 27 | } 28 | \arguments{ 29 | \item{object}{The object to be coerced.} 30 | 31 | \item{...}{Additional arguments to pass to methods.} 32 | } 33 | \description{ 34 | These are generic functions intended to primarily be used by developers of 35 | packages that interface with on CmdStanR. Developers can define methods on 36 | top of these generics to coerce objects into CmdStanR's fitted model objects. 37 | } 38 | -------------------------------------------------------------------------------- /man/cmdstan_default_install_path.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/path.R 3 | \name{cmdstan_default_install_path} 4 | \alias{cmdstan_default_install_path} 5 | \title{cmdstan_default_install_path} 6 | \usage{ 7 | cmdstan_default_install_path(old = FALSE, wsl = FALSE) 8 | } 9 | \arguments{ 10 | \item{old}{Should the old default path (.cmdstanr) be used instead of the new 11 | one (.cmdstan)? Defaults to \code{FALSE} and may be removed in a future release.} 12 | 13 | \item{wsl}{Return the directory for WSL installations?} 14 | } 15 | \value{ 16 | The installation path. 17 | } 18 | \description{ 19 | Path to where \code{\link[=install_cmdstan]{install_cmdstan()}} with default settings installs CmdStan. 20 | } 21 | \keyword{internal} 22 | -------------------------------------------------------------------------------- /man/cmdstan_default_path.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/path.R 3 | \name{cmdstan_default_path} 4 | \alias{cmdstan_default_path} 5 | \title{cmdstan_default_path} 6 | \usage{ 7 | cmdstan_default_path(old = FALSE, dir = NULL) 8 | } 9 | \arguments{ 10 | \item{old}{See \code{\link[=cmdstan_default_install_path]{cmdstan_default_install_path()}}.} 11 | 12 | \item{dir}{Path to a custom install folder with CmdStan installations.} 13 | } 14 | \value{ 15 | Path to the CmdStan installation with the most recent release 16 | version, or \code{NULL} if no installation found. 17 | } 18 | \description{ 19 | Returns the path to the installation of CmdStan with the most recent release 20 | version. 21 | } 22 | \details{ 23 | For Windows systems with WSL CmdStan installs, if there are side-by-side WSL 24 | and native installs with the same version then the WSL is preferred. 25 | Otherwise, the most recent release is chosen, regardless of whether it is 26 | native or WSL. 27 | } 28 | \keyword{internal} 29 | -------------------------------------------------------------------------------- /man/cmdstanr_example.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/example.R 3 | \name{cmdstanr_example} 4 | \alias{cmdstanr_example} 5 | \alias{print_example_program} 6 | \title{Fit models for use in examples} 7 | \usage{ 8 | cmdstanr_example( 9 | example = c("logistic", "schools", "schools_ncp"), 10 | method = c("sample", "optimize", "laplace", "variational", "pathfinder", "diagnose"), 11 | ..., 12 | quiet = TRUE, 13 | force_recompile = getOption("cmdstanr_force_recompile", default = FALSE) 14 | ) 15 | 16 | print_example_program(example = c("logistic", "schools", "schools_ncp")) 17 | } 18 | \arguments{ 19 | \item{example}{(string) The name of the example. The currently available 20 | examples are 21 | \itemize{ 22 | \item \code{"logistic"}: logistic regression with intercept and 3 predictors. 23 | \item \code{"schools"}: the so-called "eight schools" model, a hierarchical 24 | meta-analysis. Fitting this model will result in warnings about 25 | divergences. 26 | \item \code{"schools_ncp"}: non-centered parameterization of the "eight schools" 27 | model that fixes the problem with divergences. 28 | } 29 | 30 | To print the Stan code for a given \code{example} use 31 | \code{print_example_program(example)}.} 32 | 33 | \item{method}{(string) Which fitting method should be used? The default is 34 | the \code{"sample"} method (MCMC).} 35 | 36 | \item{...}{Arguments passed to the chosen \code{method}. See the help pages for 37 | the individual methods for details.} 38 | 39 | \item{quiet}{(logical) If \code{TRUE} (the default) then fitting the model is 40 | wrapped in \code{\link[utils:capture.output]{utils::capture.output()}}.} 41 | 42 | \item{force_recompile}{Passed to the \link[=model-method-compile]{$compile()} method.} 43 | } 44 | \value{ 45 | The fitted model object returned by the selected \code{method}. 46 | } 47 | \description{ 48 | Fit models for use in examples 49 | } 50 | \examples{ 51 | \dontrun{ 52 | print_example_program("logistic") 53 | fit_logistic_mcmc <- cmdstanr_example("logistic", chains = 2) 54 | fit_logistic_mcmc$summary() 55 | 56 | fit_logistic_optim <- cmdstanr_example("logistic", method = "optimize") 57 | fit_logistic_optim$summary() 58 | 59 | fit_logistic_vb <- cmdstanr_example("logistic", method = "variational") 60 | fit_logistic_vb$summary() 61 | 62 | print_example_program("schools") 63 | fit_schools_mcmc <- cmdstanr_example("schools") 64 | fit_schools_mcmc$summary() 65 | 66 | print_example_program("schools_ncp") 67 | fit_schools_ncp_mcmc <- cmdstanr_example("schools_ncp") 68 | fit_schools_ncp_mcmc$summary() 69 | 70 | # optimization fails for hierarchical model 71 | cmdstanr_example("schools", "optimize", quiet = FALSE) 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /man/cmdstanr_global_options.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/options.R 3 | \name{cmdstanr_global_options} 4 | \alias{cmdstanr_global_options} 5 | \title{CmdStanR global options} 6 | \description{ 7 | These options can be set via \code{\link[=options]{options()}} for an entire \R session. 8 | } 9 | \details{ 10 | \itemize{ 11 | \item \code{cmdstanr_draws_format}: Which format provided by the \pkg{posterior} 12 | package should be used when returning the posterior or approximate posterior 13 | draws? The default depends on the model fitting method. See 14 | \link[=fit-method-draws]{draws} for more details. 15 | \item \code{cmdstanr_force_recompile}: Should the default be to recompile models 16 | even if there were no Stan code changes since last compiled? See 17 | \link[=model-method-compile]{compile} for more details. The default is \code{FALSE}. 18 | \item \code{cmdstanr_max_rows}: The maximum number of rows of output to print when 19 | using the \code{\link[=fit-method-summary]{$print()}} method. The default is 10. 20 | \item \code{cmdstanr_print_line_numbers}: Should line numbers be included when 21 | printing a Stan program? The default is \code{FALSE}. 22 | \item \code{cmdstanr_no_ver_check}: Should the check for a more recent version of 23 | CmdStan be disabled? The default is \code{FALSE}. 24 | \item \code{cmdstanr_output_dir}: The directory where CmdStan should write its output 25 | CSV files when fitting models. The default is a temporary directory. Files in 26 | a temporary directory are removed as part of \R garbage collection, while 27 | files in an explicitly defined directory are not automatically deleted. 28 | \item \code{cmdstanr_verbose}: Should more information be printed 29 | when compiling or running models, including showing how CmdStan was called 30 | internally? The default is \code{FALSE}. 31 | \item \code{cmdstanr_warn_inits}: Should a warning be thrown if initial values are 32 | only provided for a subset of parameters? The default is \code{TRUE}. 33 | \item \code{cmdstanr_write_stan_file_dir}: The directory where \code{\link[=write_stan_file]{write_stan_file()}} 34 | should write Stan files. The default is a temporary directory. Files in 35 | a temporary directory are removed as part of \R garbage collection, while 36 | files in an explicitly defined directory are not automatically deleted. 37 | \item \code{mc.cores}: The number of cores to use for various parallelization tasks 38 | (e.g. running MCMC chains, installing CmdStan). The default depends on the 39 | use case and is documented with the methods that make use of \code{mc.cores}. 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /man/draws_to_csv.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \name{draws_to_csv} 4 | \alias{draws_to_csv} 5 | \title{Write posterior draws objects to CSV files suitable for running standalone generated 6 | quantities with CmdStan.} 7 | \usage{ 8 | draws_to_csv( 9 | draws, 10 | sampler_diagnostics = NULL, 11 | dir = tempdir(), 12 | basename = "fittedParams" 13 | ) 14 | } 15 | \arguments{ 16 | \item{draws}{A \verb{posterior::draws_*} object.} 17 | 18 | \item{sampler_diagnostics}{Either \code{NULL} or a \verb{posterior::draws_*} object 19 | of sampler diagnostics.} 20 | 21 | \item{dir}{(string) An optional path to the directory where the CSV files will be 22 | written. If not set, \link[base:tempfile]{temporary directory} is used.} 23 | 24 | \item{basename}{(string) If \code{dir} is specified, `basename`` is used for naming 25 | the output CSV files. If not specified, the file names are randomly generated.} 26 | } 27 | \value{ 28 | Paths to CSV files (one per chain). 29 | } 30 | \description{ 31 | Write posterior draws objects to CSV files suitable for running standalone generated 32 | quantities with CmdStan. 33 | } 34 | \details{ 35 | \code{draws_to_csv()} generates a CSV suitable for running standalone generated 36 | quantities with CmdStan. The CSV file contains a single comment \verb{#num_samples}, 37 | which equals the number of iterations in the supplied draws object. 38 | 39 | The comment is followed by the column names. The first column is the \code{lp__} value, 40 | followed by sampler diagnostics and finnaly other variables of the draws object. 41 | #' If the draws object does not contain the \code{lp__} or sampler diagnostics variables, 42 | columns with zeros are created in order to conform with the requirements of the 43 | standalone generated quantities method of CmdStan. 44 | 45 | The column names line is finally followed by the values of the draws in the same 46 | order as the column names. 47 | } 48 | \examples{ 49 | \dontrun{ 50 | draws <- posterior::example_draws() 51 | 52 | draws_csv_files <- draws_to_csv(draws) 53 | print(draws_csv_files) 54 | 55 | # draws_csv_files <- draws_to_csv(draws, 56 | # sampler_diagnostic = sampler_diagnostics, 57 | # dir = "~/my_folder", 58 | # basename = "my-samples") 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /man/eng_cmdstan.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/knitr.R 3 | \name{eng_cmdstan} 4 | \alias{eng_cmdstan} 5 | \title{CmdStan knitr engine for Stan} 6 | \usage{ 7 | eng_cmdstan(options) 8 | } 9 | \arguments{ 10 | \item{options}{(named list) Chunk options, as provided by \code{knitr} during 11 | chunk execution.} 12 | } 13 | \description{ 14 | This provides a knitr engine for Stan, suitable for usage when attempting 15 | to render Stan chunks and compile the model code within to an executable with 16 | CmdStan. Use \code{\link[=register_knitr_engine]{register_knitr_engine()}} to make this the default engine for 17 | \code{stan} chunks. See the vignette 18 | \href{https://mc-stan.org/cmdstanr/articles/r-markdown.html}{R Markdown CmdStan Engine} 19 | for an example. 20 | } 21 | \examples{ 22 | \dontrun{ 23 | knitr::knit_engines$set(stan = cmdstanr::eng_cmdstan) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /man/figures/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/man/figures/logo.png -------------------------------------------------------------------------------- /man/fit-method-cmdstan_summary.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-cmdstan_summary} 4 | \alias{fit-method-cmdstan_summary} 5 | \alias{cmdstan_summary} 6 | \alias{fit-method-cmdstan_diagnose} 7 | \alias{cmdstan_diagnose} 8 | \title{Run CmdStan's \code{stansummary} and \code{diagnose} utilities} 9 | \usage{ 10 | cmdstan_summary(flags = NULL) 11 | 12 | cmdstan_diagnose() 13 | } 14 | \arguments{ 15 | \item{flags}{An optional character vector of flags (e.g. 16 | \code{flags = c("--sig_figs=1")}).} 17 | } 18 | \description{ 19 | Run CmdStan's \code{stansummary} and \code{diagnose} utilities. These are 20 | documented in the CmdStan Guide: 21 | \itemize{ 22 | \item https://mc-stan.org/docs/cmdstan-guide/stansummary.html 23 | \item https://mc-stan.org/docs/cmdstan-guide/diagnose.html 24 | } 25 | 26 | Although these methods can be used for models fit using the 27 | \code{\link[=model-method-variational]{$variational()}} method, much of the output is 28 | currently only relevant for models fit using the 29 | \code{\link[=model-method-sample]{$sample()}} method. 30 | 31 | See the \link[=fit-method-summary]{$summary()} for computing similar summaries in 32 | R rather than calling CmdStan's utilites. 33 | } 34 | \examples{ 35 | \dontrun{ 36 | fit <- cmdstanr_example("logistic") 37 | fit$cmdstan_diagnose() 38 | fit$cmdstan_summary() 39 | } 40 | 41 | } 42 | \seealso{ 43 | \code{\link{CmdStanMCMC}}, \link{fit-method-summary} 44 | } 45 | -------------------------------------------------------------------------------- /man/fit-method-code.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-code} 4 | \alias{fit-method-code} 5 | \alias{code} 6 | \title{Return Stan code} 7 | \usage{ 8 | code() 9 | } 10 | \value{ 11 | A character vector with one element per line of code. 12 | } 13 | \description{ 14 | Return Stan code 15 | } 16 | \examples{ 17 | 18 | \dontrun{ 19 | fit <- cmdstanr_example() 20 | fit$code() # character vector 21 | cat(fit$code(), sep = "\n") # pretty print 22 | } 23 | 24 | } 25 | \seealso{ 26 | \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, \code{\link{CmdStanVB}}, \code{\link{CmdStanGQ}} 27 | } 28 | -------------------------------------------------------------------------------- /man/fit-method-constrain_variables.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-constrain_variables} 4 | \alias{fit-method-constrain_variables} 5 | \alias{constrain_variables} 6 | \title{Transform a set of unconstrained parameter values to the constrained scale} 7 | \usage{ 8 | constrain_variables( 9 | unconstrained_variables, 10 | transformed_parameters = TRUE, 11 | generated_quantities = TRUE 12 | ) 13 | } 14 | \arguments{ 15 | \item{unconstrained_variables}{(numeric) A vector of unconstrained parameters 16 | to constrain.} 17 | 18 | \item{transformed_parameters}{(logical) Whether to return transformed 19 | parameters implied by newly-constrained parameters (defaults to TRUE).} 20 | 21 | \item{generated_quantities}{(logical) Whether to return generated quantities 22 | implied by newly-constrained parameters (defaults to TRUE).} 23 | } 24 | \description{ 25 | The \verb{$constrain_variables()} method transforms input parameters 26 | to the constrained scale. 27 | } 28 | \examples{ 29 | \dontrun{ 30 | fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE) 31 | fit_mcmc$constrain_variables(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2)) 32 | } 33 | 34 | } 35 | \seealso{ 36 | \code{\link[=log_prob]{log_prob()}}, \code{\link[=grad_log_prob]{grad_log_prob()}}, \code{\link[=constrain_variables]{constrain_variables()}}, 37 | \code{\link[=unconstrain_variables]{unconstrain_variables()}}, \code{\link[=unconstrain_draws]{unconstrain_draws()}}, \code{\link[=variable_skeleton]{variable_skeleton()}}, 38 | \code{\link[=hessian]{hessian()}} 39 | } 40 | -------------------------------------------------------------------------------- /man/fit-method-diagnostic_summary.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-diagnostic_summary} 4 | \alias{fit-method-diagnostic_summary} 5 | \alias{diagnostic_summary} 6 | \title{Sampler diagnostic summaries and warnings} 7 | \usage{ 8 | diagnostic_summary( 9 | diagnostics = c("divergences", "treedepth", "ebfmi"), 10 | quiet = FALSE 11 | ) 12 | } 13 | \arguments{ 14 | \item{diagnostics}{(character vector) One or more diagnostics to check. The 15 | currently supported diagnostics are \verb{"divergences}, \code{"treedepth"}, and 16 | \verb{"ebfmi}. The default is to check all of them.} 17 | 18 | \item{quiet}{(logical) Should warning messages about the diagnostics be 19 | suppressed? The default is \code{FALSE}, in which case warning messages are 20 | printed in addition to returning the values of the diagnostics.} 21 | } 22 | \value{ 23 | A list with as many named elements as \code{diagnostics} selected. The 24 | possible elements and their values are: 25 | \itemize{ 26 | \item \code{"num_divergent"}: A vector of the number of divergences per chain. 27 | \item \code{"num_max_treedepth"}: A vector of the number of times \code{max_treedepth} was hit per chain. 28 | \item \code{"ebfmi"}: A vector of E-BFMI values per chain. 29 | } 30 | } 31 | \description{ 32 | Warnings and summaries of sampler diagnostics. To instead get 33 | the underlying values of the sampler diagnostics for each iteration and 34 | chain use the \code{\link[=fit-method-sampler_diagnostics]{$sampler_diagnostics()}} 35 | method. 36 | 37 | Currently parameter-specific diagnostics like R-hat and effective sample 38 | size are \emph{not} handled by this method. Those diagnostics are provided via 39 | the \code{\link[=fit-method-summary]{$summary()}} method (using 40 | \code{\link[posterior:draws_summary]{posterior::summarize_draws()}}). 41 | } 42 | \examples{ 43 | \dontrun{ 44 | fit <- cmdstanr_example("schools") 45 | fit$diagnostic_summary() 46 | fit$diagnostic_summary(quiet = TRUE) 47 | } 48 | 49 | } 50 | \seealso{ 51 | \code{\link{CmdStanMCMC}} and the 52 | \code{\link[=fit-method-sampler_diagnostics]{$sampler_diagnostics()}} method 53 | } 54 | -------------------------------------------------------------------------------- /man/fit-method-grad_log_prob.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-grad_log_prob} 4 | \alias{fit-method-grad_log_prob} 5 | \alias{grad_log_prob} 6 | \title{Calculate the log-probability and the gradient w.r.t. each input for a 7 | given vector of unconstrained parameters} 8 | \usage{ 9 | grad_log_prob( 10 | unconstrained_variables, 11 | jacobian = TRUE, 12 | jacobian_adjustment = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{unconstrained_variables}{(numeric) A vector of unconstrained parameters.} 17 | 18 | \item{jacobian}{(logical) Whether to include the log-density adjustments from 19 | un/constraining variables.} 20 | 21 | \item{jacobian_adjustment}{Deprecated. Please use \code{jacobian} instead.} 22 | } 23 | \description{ 24 | The \verb{$grad_log_prob()} method provides access to the Stan 25 | model's \code{log_prob} function and its derivative. 26 | } 27 | \examples{ 28 | \dontrun{ 29 | fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE) 30 | fit_mcmc$grad_log_prob(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2)) 31 | } 32 | 33 | } 34 | \seealso{ 35 | \code{\link[=log_prob]{log_prob()}}, \code{\link[=grad_log_prob]{grad_log_prob()}}, \code{\link[=constrain_variables]{constrain_variables()}}, 36 | \code{\link[=unconstrain_variables]{unconstrain_variables()}}, \code{\link[=unconstrain_draws]{unconstrain_draws()}}, \code{\link[=variable_skeleton]{variable_skeleton()}}, 37 | \code{\link[=hessian]{hessian()}} 38 | } 39 | -------------------------------------------------------------------------------- /man/fit-method-gradients.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-gradients} 4 | \alias{fit-method-gradients} 5 | \alias{gradients} 6 | \title{Extract gradients after diagnostic mode} 7 | \usage{ 8 | gradients() 9 | } 10 | \value{ 11 | A list of lists. See \strong{Examples}. 12 | } 13 | \description{ 14 | Return the data frame containing the gradients for all 15 | parameters. 16 | } 17 | \examples{ 18 | \dontrun{ 19 | test <- cmdstanr_example("logistic", method = "diagnose") 20 | 21 | # retrieve the gradients 22 | test$gradients() 23 | } 24 | 25 | } 26 | \seealso{ 27 | \code{\link{CmdStanDiagnose}} 28 | } 29 | -------------------------------------------------------------------------------- /man/fit-method-hessian.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-hessian} 4 | \alias{fit-method-hessian} 5 | \alias{hessian} 6 | \title{Calculate the log-probability , the gradient w.r.t. each input, and the hessian 7 | for a given vector of unconstrained parameters} 8 | \usage{ 9 | hessian(unconstrained_variables, jacobian = TRUE, jacobian_adjustment = NULL) 10 | } 11 | \arguments{ 12 | \item{unconstrained_variables}{(numeric) A vector of unconstrained parameters.} 13 | 14 | \item{jacobian}{(logical) Whether to include the log-density adjustments from 15 | un/constraining variables.} 16 | 17 | \item{jacobian_adjustment}{Deprecated. Please use \code{jacobian} instead.} 18 | } 19 | \description{ 20 | The \verb{$hessian()} method provides access to the Stan model's 21 | \code{log_prob}, its derivative, and its hessian. 22 | } 23 | \examples{ 24 | \dontrun{ 25 | fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE) 26 | # fit_mcmc$init_model_methods(hessian = TRUE) 27 | # fit_mcmc$hessian(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2)) 28 | } 29 | 30 | } 31 | \seealso{ 32 | \code{\link[=log_prob]{log_prob()}}, \code{\link[=grad_log_prob]{grad_log_prob()}}, \code{\link[=constrain_variables]{constrain_variables()}}, 33 | \code{\link[=unconstrain_variables]{unconstrain_variables()}}, \code{\link[=unconstrain_draws]{unconstrain_draws()}}, \code{\link[=variable_skeleton]{variable_skeleton()}}, 34 | \code{\link[=hessian]{hessian()}} 35 | } 36 | -------------------------------------------------------------------------------- /man/fit-method-init.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-init} 4 | \alias{fit-method-init} 5 | \alias{init} 6 | \title{Extract user-specified initial values} 7 | \usage{ 8 | init() 9 | } 10 | \value{ 11 | A list of lists. See \strong{Examples}. 12 | } 13 | \description{ 14 | Return user-specified initial values. If the user provided 15 | initial values files or \R objects (list of lists or function) via the 16 | \code{init} argument when fitting the model then these are returned (always in 17 | the list of lists format). Currently it is not possible to extract initial 18 | values generated automatically by CmdStan, although CmdStan may support 19 | this in the future. 20 | } 21 | \examples{ 22 | \dontrun{ 23 | init_fun <- function() list(alpha = rnorm(1), beta = rnorm(3)) 24 | fit <- cmdstanr_example("logistic", init = init_fun, chains = 2) 25 | str(fit$init()) 26 | 27 | # partial inits (only specifying for a subset of parameters) 28 | init_list <- list( 29 | list(mu = 10, tau = 2), 30 | list(mu = -10, tau = 1) 31 | ) 32 | fit <- cmdstanr_example("schools_ncp", init = init_list, chains = 2, adapt_delta = 0.9) 33 | 34 | # only user-specified inits returned 35 | str(fit$init()) 36 | } 37 | 38 | } 39 | \seealso{ 40 | \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, \code{\link{CmdStanVB}} 41 | } 42 | -------------------------------------------------------------------------------- /man/fit-method-init_model_methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-init_model_methods} 4 | \alias{fit-method-init_model_methods} 5 | \alias{init_model_methods} 6 | \title{Compile additional methods for accessing the model log-probability function 7 | and parameter constraining and unconstraining.} 8 | \usage{ 9 | init_model_methods(seed = 1, verbose = FALSE, hessian = FALSE) 10 | } 11 | \arguments{ 12 | \item{seed}{(integer) The random seed to use when initializing the model.} 13 | 14 | \item{verbose}{(logical) Whether to show verbose logging during compilation.} 15 | 16 | \item{hessian}{(logical) Whether to expose the (experimental) hessian method.} 17 | } 18 | \description{ 19 | The \verb{$init_model_methods()} method compiles and initializes the 20 | \code{log_prob}, \code{grad_log_prob}, \code{constrain_variables}, \code{unconstrain_variables} 21 | and \code{unconstrain_draws} functions. These are then available as methods of 22 | the fitted model object. This requires the additional \code{Rcpp} package, 23 | which are not required for fitting models using 24 | CmdStanR. 25 | 26 | Note: there may be many compiler warnings emitted during compilation but 27 | these can be ignored so long as they are warnings and not errors. 28 | } 29 | \examples{ 30 | \dontrun{ 31 | fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE) 32 | } 33 | } 34 | \seealso{ 35 | \code{\link[=log_prob]{log_prob()}}, \code{\link[=grad_log_prob]{grad_log_prob()}}, \code{\link[=constrain_variables]{constrain_variables()}}, 36 | \code{\link[=unconstrain_variables]{unconstrain_variables()}}, \code{\link[=unconstrain_draws]{unconstrain_draws()}}, \code{\link[=variable_skeleton]{variable_skeleton()}}, 37 | \code{\link[=hessian]{hessian()}} 38 | } 39 | -------------------------------------------------------------------------------- /man/fit-method-inv_metric.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-inv_metric} 4 | \alias{fit-method-inv_metric} 5 | \alias{inv_metric} 6 | \title{Extract inverse metric (mass matrix) after MCMC} 7 | \usage{ 8 | inv_metric(matrix = TRUE) 9 | } 10 | \arguments{ 11 | \item{matrix}{(logical) If a diagonal metric was used, setting \code{matrix = FALSE} returns a list containing just the diagonals of the matrices instead 12 | of the full matrices. Setting \code{matrix = FALSE} has no effect for dense 13 | metrics.} 14 | } 15 | \value{ 16 | A list of length equal to the number of MCMC chains. See the \code{matrix} 17 | argument for details. 18 | } 19 | \description{ 20 | Extract the inverse metric (mass matrix) for each MCMC chain. 21 | } 22 | \examples{ 23 | \dontrun{ 24 | fit <- cmdstanr_example("logistic") 25 | fit$inv_metric() 26 | fit$inv_metric(matrix=FALSE) 27 | 28 | fit <- cmdstanr_example("logistic", metric = "dense_e") 29 | fit$inv_metric() 30 | } 31 | 32 | } 33 | \seealso{ 34 | \code{\link{CmdStanMCMC}} 35 | } 36 | -------------------------------------------------------------------------------- /man/fit-method-log_prob.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-log_prob} 4 | \alias{fit-method-log_prob} 5 | \alias{log_prob} 6 | \title{Calculate the log-probability given a provided vector of unconstrained parameters.} 7 | \usage{ 8 | log_prob(unconstrained_variables, jacobian = TRUE, jacobian_adjustment = NULL) 9 | } 10 | \arguments{ 11 | \item{unconstrained_variables}{(numeric) A vector of unconstrained parameters.} 12 | 13 | \item{jacobian}{(logical) Whether to include the log-density adjustments from 14 | un/constraining variables.} 15 | 16 | \item{jacobian_adjustment}{Deprecated. Please use \code{jacobian} instead.} 17 | } 18 | \description{ 19 | The \verb{$log_prob()} method provides access to the Stan model's 20 | \code{log_prob} function. 21 | } 22 | \examples{ 23 | \dontrun{ 24 | fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE) 25 | fit_mcmc$log_prob(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2)) 26 | } 27 | 28 | } 29 | \seealso{ 30 | \code{\link[=log_prob]{log_prob()}}, \code{\link[=grad_log_prob]{grad_log_prob()}}, \code{\link[=constrain_variables]{constrain_variables()}}, 31 | \code{\link[=unconstrain_variables]{unconstrain_variables()}}, \code{\link[=unconstrain_draws]{unconstrain_draws()}}, \code{\link[=variable_skeleton]{variable_skeleton()}}, 32 | \code{\link[=hessian]{hessian()}} 33 | } 34 | -------------------------------------------------------------------------------- /man/fit-method-loo.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-loo} 4 | \alias{fit-method-loo} 5 | \alias{loo} 6 | \title{Leave-one-out cross-validation (LOO-CV)} 7 | \usage{ 8 | loo(variables = "log_lik", r_eff = TRUE, moment_match = FALSE, ...) 9 | } 10 | \arguments{ 11 | \item{variables}{(string) The name of the variable in the Stan program 12 | containing the pointwise log-likelihood. The default is to look for 13 | \code{"log_lik"}. This argument is passed to the \code{\link[=fit-method-draws]{$draws()}} 14 | method.} 15 | 16 | \item{r_eff}{(multiple options) How to handle the \code{r_eff} argument for \code{loo()}: 17 | \itemize{ 18 | \item \code{TRUE} (the default) will automatically call \code{\link[loo:relative_eff]{loo::relative_eff.array()}} 19 | to compute the \code{r_eff} argument to pass to \code{\link[loo:loo]{loo::loo.array()}}. 20 | \item \code{FALSE} or \code{NULL} will avoid computing \code{r_eff} (which can sometimes be slow), 21 | but the reported ESS and MCSE estimates can be over-optimistic if the 22 | posterior draws are not (near) independent. 23 | \item If \code{r_eff} is anything else, that object will be passed as the \code{r_eff} 24 | argument to \code{\link[loo:loo]{loo::loo.array()}}. 25 | }} 26 | 27 | \item{moment_match}{(logical) Whether to use a 28 | \link[loo:loo_moment_match]{moment-matching} correction for problematic 29 | observations. The default is \code{FALSE}. Using \code{moment_match=TRUE} will result 30 | in compiling the additional methods described in 31 | \link{fit-method-init_model_methods}. This allows CmdStanR to automatically 32 | supply the functions for the \code{log_lik_i}, \code{unconstrain_pars}, 33 | \code{log_prob_upars}, and \code{log_lik_i_upars} arguments to 34 | \code{\link[loo:loo_moment_match]{loo::loo_moment_match()}}.} 35 | 36 | \item{...}{Other arguments (e.g., \code{cores}, \code{save_psis}, etc.) passed to 37 | \code{\link[loo:loo]{loo::loo.array()}} or \code{\link[loo:loo_moment_match]{loo::loo_moment_match.default()}} 38 | (if \code{moment_match} = \code{TRUE} is set).} 39 | } 40 | \value{ 41 | The object returned by \code{\link[loo:loo]{loo::loo.array()}} or 42 | \code{\link[loo:loo_moment_match]{loo::loo_moment_match.default()}}. 43 | } 44 | \description{ 45 | The \verb{$loo()} method computes approximate LOO-CV using the 46 | \pkg{loo} package. In order to use this method you must compute and save 47 | the pointwise log-likelihood in your Stan program. See \code{\link[loo:loo]{loo::loo.array()}} 48 | and the \pkg{loo} package \href{https://mc-stan.org/loo/articles/}{vignettes} 49 | for details. 50 | } 51 | \examples{ 52 | 53 | \dontrun{ 54 | # the "logistic" example model has "log_lik" in generated quantities 55 | fit <- cmdstanr_example("logistic") 56 | loo_result <- fit$loo(cores = 2) 57 | print(loo_result) 58 | } 59 | 60 | } 61 | \seealso{ 62 | The \pkg{loo} package website with 63 | \href{https://mc-stan.org/loo/reference/index.html}{documentation} and 64 | \href{https://mc-stan.org/loo/articles/}{vignettes}. 65 | } 66 | -------------------------------------------------------------------------------- /man/fit-method-lp.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-lp} 4 | \alias{fit-method-lp} 5 | \alias{lp} 6 | \alias{lp_approx} 7 | \title{Extract log probability (target)} 8 | \usage{ 9 | lp() 10 | 11 | lp_approx() 12 | 13 | lp_approx() 14 | } 15 | \value{ 16 | A numeric vector with length equal to the number of (post-warmup) 17 | draws or length equal to \code{1} for optimization. 18 | } 19 | \description{ 20 | The \verb{$lp()} method extracts \code{lp__}, the total log probability 21 | (\code{target}) accumulated in the model block of the Stan program. For 22 | variational inference the log density of the variational approximation to 23 | the posterior is available via the \verb{$lp_approx()} method. For 24 | Laplace approximation the unnormalized density of the approximation to 25 | the posterior is available via the \verb{$lp_approx()} method. 26 | 27 | See the \href{https://mc-stan.org/docs/reference-manual/statements.html}{Increment log density and Distribution Statements} 28 | sections of the Stan Reference Manual for details on when normalizing 29 | constants are dropped from log probability calculations. 30 | } 31 | \section{Details}{ 32 | 33 | \code{lp__} is the unnormalized log density on Stan's \href{https://mc-stan.org/docs/2_23/reference-manual/variable-transforms-chapter.html}{unconstrained space}. 34 | This will in general be different than the unnormalized model log density 35 | evaluated at a posterior draw (which is on the constrained space). \code{lp__} is 36 | intended to diagnose sampling efficiency and evaluate approximations. 37 | 38 | For variational inference \code{lp_approx__} is the log density of the variational 39 | approximation to \code{lp__} (also on the unconstrained space). It is exposed in 40 | the variational method for performing the checks described in Yao et al. 41 | (2018) and implemented in the \pkg{loo} package. 42 | 43 | For Laplace approximation \code{lp_approx__} is the unnormalized density of the 44 | Laplace approximation. It can be used to perform the same checks as in the 45 | case of the variational method described in Yao et al. (2018). 46 | } 47 | 48 | \examples{ 49 | \dontrun{ 50 | fit_mcmc <- cmdstanr_example("logistic") 51 | head(fit_mcmc$lp()) 52 | 53 | fit_mle <- cmdstanr_example("logistic", method = "optimize") 54 | fit_mle$lp() 55 | 56 | fit_vb <- cmdstanr_example("logistic", method = "variational") 57 | plot(fit_vb$lp(), fit_vb$lp_approx()) 58 | } 59 | 60 | } 61 | \references{ 62 | Yao, Y., Vehtari, A., Simpson, D., and Gelman, A. (2018). Yes, but did it 63 | work?: Evaluating variational inference. \emph{Proceedings of the 35th 64 | International Conference on Machine Learning}, PMLR 80:5581–5590. 65 | } 66 | \seealso{ 67 | \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, \code{\link{CmdStanLaplace}}, \code{\link{CmdStanVB}} 68 | } 69 | -------------------------------------------------------------------------------- /man/fit-method-metadata.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-metadata} 4 | \alias{fit-method-metadata} 5 | \alias{metadata} 6 | \title{Extract metadata from CmdStan CSV files} 7 | \usage{ 8 | metadata() 9 | } 10 | \description{ 11 | The \verb{$metadata()} method returns a list of information gathered 12 | from the CSV output files, including the CmdStan configuration used when 13 | fitting the model. See \strong{Examples} and \code{\link[=read_cmdstan_csv]{read_cmdstan_csv()}}. 14 | } 15 | \examples{ 16 | \dontrun{ 17 | fit_mcmc <- cmdstanr_example("logistic", method = "sample") 18 | str(fit_mcmc$metadata()) 19 | 20 | fit_mle <- cmdstanr_example("logistic", method = "optimize") 21 | str(fit_mle$metadata()) 22 | 23 | fit_vb <- cmdstanr_example("logistic", method = "variational") 24 | str(fit_vb$metadata()) 25 | } 26 | 27 | } 28 | \seealso{ 29 | \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, \code{\link{CmdStanVB}}, \code{\link{CmdStanGQ}} 30 | } 31 | -------------------------------------------------------------------------------- /man/fit-method-mle.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-mle} 4 | \alias{fit-method-mle} 5 | \alias{mle} 6 | \title{Extract point estimate after optimization} 7 | \usage{ 8 | mle(variables = NULL) 9 | } 10 | \arguments{ 11 | \item{variables}{(character vector) The variables (parameters, transformed 12 | parameters, and generated quantities) to include. If NULL (the default) 13 | then all variables are included.} 14 | } 15 | \value{ 16 | A numeric vector. See \strong{Examples}. 17 | } 18 | \description{ 19 | The \verb{$mle()} method is only available for \code{\link{CmdStanMLE}} 20 | objects. It returns the point estimate as a numeric vector with one element 21 | per variable. The returned vector does \emph{not} include \code{lp__}, the total log 22 | probability (\code{target}) accumulated in the model block of the Stan program, 23 | which is available via the \code{\link[=fit-method-lp]{$lp()}} method and also 24 | included in the \code{\link[=fit-method-draws]{$draws()}} method. 25 | 26 | For models with constrained parameters that are fit with \code{jacobian=TRUE}, 27 | the \verb{$mle()} method actually returns the maximum a posteriori (MAP) 28 | estimate (posterior mode) rather than the MLE. See 29 | \code{\link[=model-method-optimize]{$optimize()}} and the CmdStan User's Guide for 30 | more details. 31 | } 32 | \examples{ 33 | \dontrun{ 34 | fit <- cmdstanr_example("logistic", method = "optimize") 35 | fit$mle("alpha") 36 | fit$mle("beta") 37 | fit$mle("beta[2]") 38 | } 39 | 40 | } 41 | \seealso{ 42 | \code{\link{CmdStanMLE}} 43 | } 44 | -------------------------------------------------------------------------------- /man/fit-method-num_chains.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-num_chains} 4 | \alias{fit-method-num_chains} 5 | \alias{num_chains} 6 | \title{Extract number of chains after MCMC} 7 | \usage{ 8 | num_chains() 9 | } 10 | \value{ 11 | An integer. 12 | } 13 | \description{ 14 | The \verb{$num_chains()} method returns the number of MCMC chains. 15 | } 16 | \examples{ 17 | \dontrun{ 18 | fit_mcmc <- cmdstanr_example(chains = 2) 19 | fit_mcmc$num_chains() 20 | } 21 | 22 | } 23 | \seealso{ 24 | \code{\link{CmdStanMCMC}} 25 | } 26 | -------------------------------------------------------------------------------- /man/fit-method-output.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-output} 4 | \alias{fit-method-output} 5 | \alias{output} 6 | \title{Access console output} 7 | \usage{ 8 | output(id = NULL) 9 | } 10 | \arguments{ 11 | \item{id}{(integer) The chain id. Ignored if the model was not fit using 12 | MCMC.} 13 | } 14 | \description{ 15 | For MCMC, the \verb{$output()} method returns the stdout and stderr 16 | of all chains as a list of character vectors if \code{id=NULL}. If the \code{id} 17 | argument is specified it instead pretty prints the console output for a 18 | single chain. 19 | 20 | For optimization and variational inference \verb{$output()} just pretty prints 21 | the console output. 22 | } 23 | \examples{ 24 | \dontrun{ 25 | fit_mcmc <- cmdstanr_example("logistic", method = "sample") 26 | fit_mcmc$output(1) 27 | out <- fit_mcmc$output() 28 | str(out) 29 | 30 | fit_mle <- cmdstanr_example("logistic", method = "optimize") 31 | fit_mle$output() 32 | 33 | fit_vb <- cmdstanr_example("logistic", method = "variational") 34 | fit_vb$output() 35 | } 36 | 37 | } 38 | \seealso{ 39 | \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, \code{\link{CmdStanVB}}, \code{\link{CmdStanGQ}} 40 | } 41 | -------------------------------------------------------------------------------- /man/fit-method-profiles.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-profiles} 4 | \alias{fit-method-profiles} 5 | \alias{profiles} 6 | \title{Return profiling data} 7 | \usage{ 8 | profiles() 9 | } 10 | \value{ 11 | A list of data frames with profiling data if the profiling CSV files 12 | were created. 13 | } 14 | \description{ 15 | The \verb{$profiles()} method returns a list of data frames with 16 | profiling data if any profiling data was written to the profile CSV files. 17 | See \code{\link[=save_profile_files]{save_profile_files()}} to control where the files are saved. 18 | 19 | Support for profiling Stan programs is available with CmdStan >= 2.26 and 20 | requires adding profiling statements to the Stan program. 21 | } 22 | \examples{ 23 | 24 | \dontrun{ 25 | # first fit a model using MCMC 26 | mcmc_program <- write_stan_file( 27 | 'data { 28 | int N; 29 | array[N] int y; 30 | } 31 | parameters { 32 | real theta; 33 | } 34 | model { 35 | profile("likelihood") { 36 | y ~ bernoulli(theta); 37 | } 38 | } 39 | generated quantities { 40 | array[N] int y_rep; 41 | profile("gq") { 42 | y_rep = bernoulli_rng(rep_vector(theta, N)); 43 | } 44 | } 45 | ' 46 | ) 47 | mod_mcmc <- cmdstan_model(mcmc_program) 48 | 49 | data <- list(N = 10, y = c(1,1,0,0,0,1,0,1,0,0)) 50 | fit <- mod_mcmc$sample(data = data, seed = 123, refresh = 0) 51 | 52 | fit$profiles() 53 | } 54 | 55 | } 56 | \seealso{ 57 | \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, \code{\link{CmdStanVB}}, \code{\link{CmdStanGQ}} 58 | } 59 | -------------------------------------------------------------------------------- /man/fit-method-return_codes.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-return_codes} 4 | \alias{fit-method-return_codes} 5 | \alias{return_codes} 6 | \title{Extract return codes from CmdStan} 7 | \usage{ 8 | return_codes() 9 | } 10 | \value{ 11 | An integer vector of return codes with length equal to the number of 12 | CmdStan runs (number of chains for MCMC and one otherwise). 13 | } 14 | \description{ 15 | The \verb{$return_codes()} method returns a vector of return codes 16 | from the CmdStan run(s). A return code of 0 indicates a successful run. 17 | } 18 | \examples{ 19 | \dontrun{ 20 | # example with return codes all zero 21 | fit_mcmc <- cmdstanr_example("schools", method = "sample") 22 | fit_mcmc$return_codes() # should be all zero 23 | 24 | # example of non-zero return code (optimization fails for hierarchical model) 25 | fit_opt <- cmdstanr_example("schools", method = "optimize") 26 | fit_opt$return_codes() # should be non-zero 27 | } 28 | 29 | } 30 | \seealso{ 31 | \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, \code{\link{CmdStanVB}}, \code{\link{CmdStanGQ}} 32 | } 33 | -------------------------------------------------------------------------------- /man/fit-method-sampler_diagnostics.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-sampler_diagnostics} 4 | \alias{fit-method-sampler_diagnostics} 5 | \alias{sampler_diagnostics} 6 | \title{Extract sampler diagnostics after MCMC} 7 | \usage{ 8 | sampler_diagnostics( 9 | inc_warmup = FALSE, 10 | format = getOption("cmdstanr_draws_format", "draws_array") 11 | ) 12 | } 13 | \arguments{ 14 | \item{inc_warmup}{(logical) Should warmup draws be included? Defaults to \code{FALSE}.} 15 | 16 | \item{format}{(string) The draws format to return. See 17 | \link[=fit-method-draws]{draws} for details.} 18 | } 19 | \value{ 20 | Depends on \code{format}, but the default is a 3-D 21 | \code{\link[posterior:draws_array]{draws_array}} object (iteration x chain x 22 | variable). The variables for Stan's default MCMC algorithm are 23 | \code{"accept_stat__"}, \code{"stepsize__"}, \code{"treedepth__"}, \code{"n_leapfrog__"}, 24 | \code{"divergent__"}, \code{"energy__"}. 25 | } 26 | \description{ 27 | Extract the values of sampler diagnostics for each iteration and 28 | chain of MCMC. To instead get summaries of these diagnostics and associated 29 | warning messages use the 30 | \code{\link[=fit-method-diagnostic_summary]{$diagnostic_summary()}} method. 31 | } 32 | \examples{ 33 | \dontrun{ 34 | fit <- cmdstanr_example("logistic") 35 | sampler_diagnostics <- fit$sampler_diagnostics() 36 | str(sampler_diagnostics) 37 | 38 | library(posterior) 39 | as_draws_df(sampler_diagnostics) 40 | 41 | # or specify format to get a data frame instead of calling as_draws_df 42 | fit$sampler_diagnostics(format = "df") 43 | } 44 | 45 | } 46 | \seealso{ 47 | \code{\link{CmdStanMCMC}} 48 | } 49 | -------------------------------------------------------------------------------- /man/fit-method-save_object.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-save_object} 4 | \alias{fit-method-save_object} 5 | \alias{save_object} 6 | \title{Save fitted model object to a file} 7 | \usage{ 8 | save_object(file, ...) 9 | } 10 | \arguments{ 11 | \item{file}{(string) Path where the file should be saved.} 12 | 13 | \item{...}{Other arguments to pass to \code{\link[base:readRDS]{base::saveRDS()}} besides \code{object} and \code{file}.} 14 | } 15 | \description{ 16 | This method is a wrapper around \code{\link[base:readRDS]{base::saveRDS()}} that ensures 17 | that all posterior draws and diagnostics are saved when saving a fitted 18 | model object. Because the contents of the CmdStan output CSV files are only 19 | read into R lazily (i.e., as needed), the \verb{$save_object()} method is the 20 | safest way to guarantee that everything has been read in before saving. 21 | 22 | See the "Saving fitted model objects" section of the 23 | \href{https://mc-stan.org/cmdstanr/articles/cmdstanr.html}{\emph{Getting started with CmdStanR}} 24 | vignette for some suggestions on faster model saving for large models. 25 | } 26 | \examples{ 27 | \dontrun{ 28 | fit <- cmdstanr_example("logistic") 29 | 30 | temp_rds_file <- tempfile(fileext = ".RDS") 31 | fit$save_object(file = temp_rds_file) 32 | rm(fit) 33 | 34 | fit <- readRDS(temp_rds_file) 35 | fit$summary() 36 | } 37 | 38 | } 39 | \seealso{ 40 | \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, \code{\link{CmdStanVB}}, \code{\link{CmdStanGQ}} 41 | } 42 | -------------------------------------------------------------------------------- /man/fit-method-summary.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-summary} 4 | \alias{fit-method-summary} 5 | \alias{summary} 6 | \alias{fit-method-print} 7 | \alias{print.CmdStanMCMC} 8 | \alias{print.CmdStanMLE} 9 | \alias{print.CmdStanVB} 10 | \title{Compute a summary table of estimates and diagnostics} 11 | \usage{ 12 | summary(variables = NULL, ...) 13 | } 14 | \arguments{ 15 | \item{variables}{(character vector) The variables to include.} 16 | 17 | \item{...}{Optional arguments to pass to \code{\link[posterior:draws_summary]{posterior::summarise_draws()}}.} 18 | } 19 | \value{ 20 | The \verb{$summary()} method returns the tibble data frame created by 21 | \code{\link[posterior:draws_summary]{posterior::summarise_draws()}}. 22 | 23 | The \verb{$print()} method returns the fitted model object itself (invisibly), 24 | which is the standard behavior for print methods in \R. 25 | } 26 | \description{ 27 | The \verb{$summary()} method runs 28 | \code{\link[posterior:draws_summary]{summarise_draws()}} from the \pkg{posterior} 29 | package and returns the output. For MCMC, only post-warmup draws are 30 | included in the summary. 31 | 32 | There is also a \verb{$print()} method that prints the same summary stats but 33 | removes the extra formatting used for printing tibbles and returns the 34 | fitted model object itself. The \verb{$print()} method may also be faster than 35 | \verb{$summary()} because it is designed to only compute the summary statistics 36 | for the variables that will actually fit in the printed output whereas 37 | \verb{$summary()} will compute them for all of the specified variables in order 38 | to be able to return them to the user. See \strong{Examples}. 39 | } 40 | \examples{ 41 | \dontrun{ 42 | fit <- cmdstanr_example("logistic") 43 | fit$summary() 44 | fit$print() 45 | fit$print(max_rows = 2) # same as print(fit, max_rows = 2) 46 | 47 | # include only certain variables 48 | fit$summary("beta") 49 | fit$print(c("alpha", "beta[2]")) 50 | 51 | # include all variables but only certain summaries 52 | fit$summary(NULL, c("mean", "sd")) 53 | 54 | # can use functions created from formulas 55 | # for example, calculate Pr(beta > 0) 56 | fit$summary("beta", prob_gt_0 = ~ mean(. > 0)) 57 | 58 | # can combine user-specified functions with 59 | # the default summary functions 60 | fit$summary(variables = c("alpha", "beta"), 61 | posterior::default_summary_measures()[1:4], 62 | quantiles = ~ quantile2(., probs = c(0.025, 0.975)), 63 | posterior::default_convergence_measures() 64 | ) 65 | 66 | # the functions need to calculate the appropriate 67 | # value for a matrix input 68 | fit$summary(variables = "alpha", dim) 69 | 70 | # the usual [stats::var()] is therefore not directly suitable as it 71 | # will produce a covariance matrix unless the data is converted to a vector 72 | fit$print(c("alpha", "beta"), var2 = ~var(as.vector(.x))) 73 | 74 | } 75 | 76 | } 77 | \seealso{ 78 | \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, \code{\link{CmdStanLaplace}}, \code{\link{CmdStanVB}}, \code{\link{CmdStanGQ}} 79 | } 80 | -------------------------------------------------------------------------------- /man/fit-method-time.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-time} 4 | \alias{fit-method-time} 5 | \alias{time} 6 | \title{Report timing of CmdStan runs} 7 | \usage{ 8 | time() 9 | } 10 | \value{ 11 | A list with elements 12 | \itemize{ 13 | \item \code{total}: (scalar) The total run time. For MCMC this may be different than 14 | the sum of the chain run times if parallelization was used. 15 | \item \code{chains}: (data frame) For MCMC only, timing info for the individual 16 | chains. The data frame has columns \code{"chain_id"}, \code{"warmup"}, \code{"sampling"}, 17 | and \code{"total"}. 18 | } 19 | } 20 | \description{ 21 | Report the run time in seconds. For MCMC additional information 22 | is provided about the run times of individual chains and the warmup and 23 | sampling phases. For Laplace approximation the time only include the time 24 | for drawing the approximate sample and does not include the time 25 | taken to run the \verb{$optimize()} method. 26 | } 27 | \examples{ 28 | \dontrun{ 29 | fit_mcmc <- cmdstanr_example("logistic", method = "sample") 30 | fit_mcmc$time() 31 | 32 | fit_vb <- cmdstanr_example("logistic", method = "variational") 33 | fit_vb$time() 34 | 35 | fit_mle <- cmdstanr_example("logistic", method = "optimize", jacobian = TRUE) 36 | fit_mle$time() 37 | 38 | # use fit_mle to draw samples from laplace approximation 39 | fit_laplace <- cmdstanr_example("logistic", method = "laplace", mode = fit_mle) 40 | fit_laplace$time() # just time for drawing sample not for running optimize 41 | fit_laplace$time()$total + fit_mle$time()$total # total time 42 | } 43 | 44 | } 45 | \seealso{ 46 | \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, \code{\link{CmdStanVB}}, \code{\link{CmdStanGQ}} 47 | } 48 | -------------------------------------------------------------------------------- /man/fit-method-unconstrain_draws.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-unconstrain_draws} 4 | \alias{fit-method-unconstrain_draws} 5 | \alias{unconstrain_draws} 6 | \title{Transform all parameter draws to the unconstrained scale} 7 | \usage{ 8 | unconstrain_draws( 9 | files = NULL, 10 | draws = NULL, 11 | format = getOption("cmdstanr_draws_format", "draws_array"), 12 | inc_warmup = FALSE 13 | ) 14 | } 15 | \arguments{ 16 | \item{files}{(character vector) The paths to the CmdStan CSV files. These can 17 | be files generated by running CmdStanR or running CmdStan directly.} 18 | 19 | \item{draws}{A \verb{posterior::draws_*} object.} 20 | 21 | \item{format}{(string) The format of the returned draws. Must be a valid 22 | format from the \pkg{posterior} package.} 23 | 24 | \item{inc_warmup}{(logical) Should warmup draws be included? Defaults to 25 | \code{FALSE}.} 26 | } 27 | \description{ 28 | The \verb{$unconstrain_draws()} method transforms all parameter draws 29 | to the unconstrained scale. The method returns a list for each chain, 30 | containing the parameter values from each iteration on the unconstrained 31 | scale. If called with no arguments, then the draws within the fit object 32 | are unconstrained. Alternatively, either an existing draws object or a 33 | character vector of paths to CSV files can be passed. 34 | } 35 | \examples{ 36 | \dontrun{ 37 | fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE) 38 | 39 | # Unconstrain all internal draws 40 | unconstrained_internal_draws <- fit_mcmc$unconstrain_draws() 41 | 42 | # Unconstrain external CmdStan CSV files 43 | unconstrained_csv <- fit_mcmc$unconstrain_draws(files = fit_mcmc$output_files()) 44 | 45 | # Unconstrain existing draws object 46 | unconstrained_draws <- fit_mcmc$unconstrain_draws(draws = fit_mcmc$draws()) 47 | } 48 | 49 | } 50 | \seealso{ 51 | \code{\link[=log_prob]{log_prob()}}, \code{\link[=grad_log_prob]{grad_log_prob()}}, \code{\link[=constrain_variables]{constrain_variables()}}, 52 | \code{\link[=unconstrain_variables]{unconstrain_variables()}}, \code{\link[=unconstrain_draws]{unconstrain_draws()}}, \code{\link[=variable_skeleton]{variable_skeleton()}}, 53 | \code{\link[=hessian]{hessian()}} 54 | } 55 | -------------------------------------------------------------------------------- /man/fit-method-unconstrain_variables.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-unconstrain_variables} 4 | \alias{fit-method-unconstrain_variables} 5 | \alias{unconstrain_variables} 6 | \title{Transform a set of parameter values to the unconstrained scale} 7 | \usage{ 8 | unconstrain_variables(variables) 9 | } 10 | \arguments{ 11 | \item{variables}{(list) A list of parameter values to transform, in the same 12 | format as provided to the \code{init} argument of the \verb{$sample()} method.} 13 | } 14 | \description{ 15 | The \verb{$unconstrain_variables()} method transforms input 16 | parameters to the unconstrained scale. 17 | } 18 | \examples{ 19 | \dontrun{ 20 | fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE) 21 | fit_mcmc$unconstrain_variables(list(alpha = 0.5, beta = c(0.7, 1.1, 0.2))) 22 | } 23 | 24 | } 25 | \seealso{ 26 | \code{\link[=log_prob]{log_prob()}}, \code{\link[=grad_log_prob]{grad_log_prob()}}, \code{\link[=constrain_variables]{constrain_variables()}}, 27 | \code{\link[=unconstrain_variables]{unconstrain_variables()}}, \code{\link[=unconstrain_draws]{unconstrain_draws()}}, \code{\link[=variable_skeleton]{variable_skeleton()}}, 28 | \code{\link[=hessian]{hessian()}} 29 | } 30 | -------------------------------------------------------------------------------- /man/fit-method-variable_skeleton.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fit.R 3 | \name{fit-method-variable_skeleton} 4 | \alias{fit-method-variable_skeleton} 5 | \alias{variable_skeleton} 6 | \title{Return the variable skeleton for \code{relist}} 7 | \usage{ 8 | variable_skeleton(transformed_parameters = TRUE, generated_quantities = TRUE) 9 | } 10 | \arguments{ 11 | \item{transformed_parameters}{(logical) Whether to include transformed 12 | parameters in the skeleton (defaults to \code{TRUE}).} 13 | 14 | \item{generated_quantities}{(logical) Whether to include generated quantities 15 | in the skeleton (defaults to \code{TRUE}).} 16 | } 17 | \description{ 18 | The \verb{$variable_skeleton()} method returns the variable skeleton 19 | needed by \code{utils::relist()} to re-structure a vector of constrained 20 | parameter values to a named list. 21 | } 22 | \examples{ 23 | \dontrun{ 24 | fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE) 25 | fit_mcmc$variable_skeleton() 26 | } 27 | 28 | } 29 | \seealso{ 30 | \code{\link[=log_prob]{log_prob()}}, \code{\link[=grad_log_prob]{grad_log_prob()}}, \code{\link[=constrain_variables]{constrain_variables()}}, 31 | \code{\link[=unconstrain_variables]{unconstrain_variables()}}, \code{\link[=unconstrain_draws]{unconstrain_draws()}}, \code{\link[=variable_skeleton]{variable_skeleton()}}, 32 | \code{\link[=hessian]{hessian()}} 33 | } 34 | -------------------------------------------------------------------------------- /man/model-method-expose_functions.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/model.R 3 | \name{model-method-expose_functions} 4 | \alias{model-method-expose_functions} 5 | \alias{expose_functions} 6 | \alias{fit-method-expose_functions} 7 | \title{Expose Stan functions to R} 8 | \usage{ 9 | expose_functions(global = FALSE, verbose = FALSE) 10 | } 11 | \arguments{ 12 | \item{global}{(logical) Should the functions be added to the Global 13 | Environment? The default is \code{FALSE}, in which case the functions are 14 | available via the \code{functions} field of the R6 object.} 15 | 16 | \item{verbose}{(logical) Should detailed information about generated code be 17 | printed to the console? Defaults to \code{FALSE}.} 18 | } 19 | \description{ 20 | The \verb{$expose_functions()} method of a \code{\link{CmdStanModel}} object 21 | will compile the functions in the Stan program's \code{functions} block and 22 | expose them for use in \R. This can also be specified via the 23 | \code{compile_standalone} argument to the \code{\link[=model-method-compile]{$compile()}} 24 | method. 25 | 26 | This method is also available for fitted model objects (\code{\link{CmdStanMCMC}}, \code{\link{CmdStanVB}}, etc.). 27 | See \strong{Examples}. 28 | 29 | Note: there may be many compiler warnings emitted during compilation but 30 | these can be ignored so long as they are warnings and not errors. 31 | } 32 | \examples{ 33 | \dontrun{ 34 | stan_file <- write_stan_file( 35 | " 36 | functions { 37 | real a_plus_b(real a, real b) { 38 | return a + b; 39 | } 40 | } 41 | parameters { 42 | real x; 43 | } 44 | model { 45 | x ~ std_normal(); 46 | } 47 | " 48 | ) 49 | mod <- cmdstan_model(stan_file) 50 | mod$expose_functions() 51 | mod$functions$a_plus_b(1, 2) 52 | 53 | fit <- mod$sample(refresh = 0) 54 | fit$expose_functions() # already compiled because of above but this would compile them otherwise 55 | fit$functions$a_plus_b(1, 2) 56 | } 57 | 58 | 59 | } 60 | \seealso{ 61 | The CmdStanR website 62 | (\href{https://mc-stan.org/cmdstanr/}{mc-stan.org/cmdstanr}) for online 63 | documentation and tutorials. 64 | 65 | The Stan and CmdStan documentation: 66 | \itemize{ 67 | \item Stan documentation: \href{https://mc-stan.org/users/documentation/}{mc-stan.org/users/documentation} 68 | \item CmdStan User’s Guide: \href{https://mc-stan.org/docs/cmdstan-guide/}{mc-stan.org/docs/cmdstan-guide} 69 | } 70 | 71 | Other CmdStanModel methods: 72 | \code{\link{model-method-check_syntax}}, 73 | \code{\link{model-method-compile}}, 74 | \code{\link{model-method-diagnose}}, 75 | \code{\link{model-method-format}}, 76 | \code{\link{model-method-generate-quantities}}, 77 | \code{\link{model-method-laplace}}, 78 | \code{\link{model-method-optimize}}, 79 | \code{\link{model-method-pathfinder}}, 80 | \code{\link{model-method-sample}}, 81 | \code{\link{model-method-sample_mpi}}, 82 | \code{\link{model-method-variables}}, 83 | \code{\link{model-method-variational}} 84 | } 85 | \concept{CmdStanModel methods} 86 | -------------------------------------------------------------------------------- /man/model-method-variables.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/model.R 3 | \name{model-method-variables} 4 | \alias{model-method-variables} 5 | \alias{variables} 6 | \title{Input and output variables of a Stan program} 7 | \usage{ 8 | variables() 9 | } 10 | \value{ 11 | The \verb{$variables()} returns a list with information on input and 12 | output variables for each of the Stan model blocks. 13 | } 14 | \description{ 15 | The \verb{$variables()} method of a \code{\link{CmdStanModel}} object returns 16 | a list, each element representing a Stan model block: \code{data}, \code{parameters}, 17 | \code{transformed_parameters} and \code{generated_quantities}. 18 | 19 | Each element contains a list of variables, with each variables represented 20 | as a list with infromation on its scalar type (\code{real} or \code{int}) and 21 | number of dimensions. 22 | 23 | \verb{transformed data} is not included, as variables in that block are not 24 | part of the model's input or output. 25 | } 26 | \examples{ 27 | \dontrun{ 28 | file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.stan") 29 | 30 | # create a `CmdStanModel` object, compiling the model is not required 31 | mod <- cmdstan_model(file, compile = FALSE) 32 | 33 | mod$variables() 34 | 35 | } 36 | 37 | } 38 | \seealso{ 39 | Other CmdStanModel methods: 40 | \code{\link{model-method-check_syntax}}, 41 | \code{\link{model-method-compile}}, 42 | \code{\link{model-method-diagnose}}, 43 | \code{\link{model-method-expose_functions}}, 44 | \code{\link{model-method-format}}, 45 | \code{\link{model-method-generate-quantities}}, 46 | \code{\link{model-method-laplace}}, 47 | \code{\link{model-method-optimize}}, 48 | \code{\link{model-method-pathfinder}}, 49 | \code{\link{model-method-sample}}, 50 | \code{\link{model-method-sample_mpi}}, 51 | \code{\link{model-method-variational}} 52 | } 53 | \concept{CmdStanModel methods} 54 | -------------------------------------------------------------------------------- /man/read_sample_csv.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/csv.R 3 | \name{read_sample_csv} 4 | \alias{read_sample_csv} 5 | \title{Read CmdStan CSV files from sampling into \R} 6 | \usage{ 7 | read_sample_csv(files, variables = NULL, sampler_diagnostics = NULL) 8 | } 9 | \arguments{ 10 | \item{files, variables, sampler_diagnostics}{Deprecated. Use 11 | \code{\link[=read_cmdstan_csv]{read_cmdstan_csv()}} instead.} 12 | } 13 | \description{ 14 | Deprecated. Use \code{\link[=read_cmdstan_csv]{read_cmdstan_csv()}} instead. 15 | } 16 | \keyword{internal} 17 | -------------------------------------------------------------------------------- /man/register_knitr_engine.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/knitr.R 3 | \name{register_knitr_engine} 4 | \alias{register_knitr_engine} 5 | \title{Register CmdStanR's knitr engine for Stan} 6 | \usage{ 7 | register_knitr_engine(override = TRUE) 8 | } 9 | \arguments{ 10 | \item{override}{(logical) Override knitr's built-in, RStan-based engine for 11 | Stan? The default is \code{TRUE}. See \strong{Details}.} 12 | } 13 | \description{ 14 | Registers CmdStanR's knitr engine \code{\link[=eng_cmdstan]{eng_cmdstan()}} for processing Stan chunks. 15 | Refer to the vignette 16 | \href{https://mc-stan.org/cmdstanr/articles/r-markdown.html}{R Markdown CmdStan Engine} 17 | for a demonstration. 18 | } 19 | \details{ 20 | If \code{override = TRUE} (default), this registers CmdStanR's knitr engine as the 21 | engine for \code{stan} chunks, replacing knitr's built-in, RStan-based engine. If 22 | \code{override = FALSE}, this registers a \code{cmdstan} engine so that both engines 23 | may be used in the same R Markdown document. If the template supports syntax 24 | highlighting for the Stan language, the \code{cmdstan} chunks will have \code{stan} 25 | syntax highlighting applied to them. 26 | 27 | See the vignette 28 | \href{https://mc-stan.org/cmdstanr/articles/r-markdown.html}{R Markdown CmdStan Engine} 29 | for an example. 30 | 31 | \strong{Note:} When running chunks interactively in RStudio (e.g. when using 32 | \href{https://bookdown.org/yihui/rmarkdown/notebook.html}{R Notebooks}), it has 33 | been observed that the built-in, RStan-based engine is used for \code{stan} 34 | chunks even when CmdStanR's engine has been registered in the session. When 35 | the R Markdown document is knit/rendered, the correct engine is used. As a 36 | workaround, when running chunks interactively, it is recommended to use the 37 | \code{override = FALSE} option and change \code{stan} chunks to be \code{cmdstan} chunks. 38 | 39 | If you would like to keep \code{stan} chunks as \code{stan} chunks, it is possible to 40 | specify \code{engine = "cmdstan"} in the chunk options after registering the 41 | \code{cmdstan} engine with \code{override = FALSE}. 42 | } 43 | \references{ 44 | \itemize{ 45 | \item \href{https://bookdown.org/yihui/rmarkdown-cookbook/custom-engine.html}{Register a custom language engine for knitr} 46 | \item \href{https://bookdown.org/yihui/rmarkdown/language-engines.html#stan}{knitr's built-in Stan language engine} 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /man/set_cmdstan_path.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/path.R 3 | \name{set_cmdstan_path} 4 | \alias{set_cmdstan_path} 5 | \alias{cmdstan_path} 6 | \alias{cmdstan_version} 7 | \title{Get or set the file path to the CmdStan installation} 8 | \usage{ 9 | set_cmdstan_path(path = NULL) 10 | 11 | cmdstan_path() 12 | 13 | cmdstan_version(error_on_NA = TRUE) 14 | } 15 | \arguments{ 16 | \item{path}{(string) The full file path to the CmdStan installation. If 17 | \code{NULL} (the default) then the path is set to the default path used by 18 | \code{\link[=install_cmdstan]{install_cmdstan()}} if it exists.} 19 | 20 | \item{error_on_NA}{(logical) Should an error be thrown if CmdStan is not 21 | found. The default is \code{TRUE}. If \code{FALSE}, \code{cmdstan_version()} returns 22 | \code{NULL}.} 23 | } 24 | \value{ 25 | A string. Either the file path to the CmdStan installation or the 26 | CmdStan version number. 27 | 28 | CmdStan version string if available. If CmdStan is not found and 29 | \code{error_on_NA} is \code{FALSE}, \code{cmdstan_version()} returns \code{NULL}. 30 | } 31 | \description{ 32 | Use the \code{set_cmdstan_path()} function to tell CmdStanR where the 33 | CmdStan installation in located. Once the path has been set, 34 | \code{cmdstan_path()} will return the full path to the CmdStan installation and 35 | \code{cmdstan_version()} will return the CmdStan version number. See \strong{Details} 36 | for how to avoid manually setting the path in each \R session. 37 | } 38 | \details{ 39 | Before the package can be used it needs to know where the CmdStan 40 | installation is located. When the package is loaded it tries to help automate 41 | this to avoid having to manually set the path every session: 42 | \itemize{ 43 | \item If the \link[=Sys.setenv]{environment variable} \code{"CMDSTAN"} exists at load time 44 | then its value will be automatically set as the default path to CmdStan for 45 | the \R session. If the environment variable \code{"CMDSTAN"} is set, but a valid 46 | CmdStan is not found in the supplied path, the path is treated as a top 47 | folder that contains CmdStan installations. In that case, the CmdStan 48 | installation with the largest version number will be set as the path to 49 | CmdStan for the \R session. 50 | \item If no environment variable is found when loaded but any directory in the 51 | form \code{".cmdstan/cmdstan-[version]"} (e.g., \code{".cmdstan/cmdstan-2.23.0"}), 52 | exists in the user's home directory (\code{Sys.getenv("HOME")}, \emph{not} the current 53 | working directory) then the path to the cmdstan with the largest version 54 | number will be set as the path to CmdStan for the \R session. This is the 55 | same as the default directory that \code{\link[=install_cmdstan]{install_cmdstan()}} would use to install 56 | the latest version of CmdStan. 57 | } 58 | 59 | It is always possible to change the path after loading the package using 60 | \code{set_cmdstan_path(path)}. 61 | } 62 | -------------------------------------------------------------------------------- /man/stan_threads.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils.R 3 | \name{stan_threads} 4 | \alias{stan_threads} 5 | \alias{num_threads} 6 | \alias{set_num_threads} 7 | \title{Set or get the number of threads used to execute Stan models} 8 | \usage{ 9 | num_threads() 10 | 11 | set_num_threads(num_threads) 12 | } 13 | \arguments{ 14 | \item{num_threads}{(positive integer) The number of threads to set.} 15 | } 16 | \value{ 17 | The value of the environment variable \code{STAN_NUM_THREADS}. 18 | } 19 | \description{ 20 | DEPRECATED. Please use the \code{threads_per_chain} argument when fitting the model. 21 | } 22 | \keyword{internal} 23 | -------------------------------------------------------------------------------- /man/write_stan_file.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/example.R 3 | \name{write_stan_file} 4 | \alias{write_stan_file} 5 | \title{Write Stan code to a file} 6 | \usage{ 7 | write_stan_file( 8 | code, 9 | dir = getOption("cmdstanr_write_stan_file_dir", tempdir()), 10 | basename = NULL, 11 | force_overwrite = FALSE, 12 | hash_salt = "" 13 | ) 14 | } 15 | \arguments{ 16 | \item{code}{(character vector) The Stan code to write to the file. This can 17 | be a character vector of length one (a string) containing the entire Stan 18 | program or a character vector with each element containing one line of the 19 | Stan program.} 20 | 21 | \item{dir}{(string) An optional path to the directory where the file will be 22 | written. If omitted, a global option \code{cmdstanr_write_stan_file_dir} is 23 | used. If the global options is not set, \link[base:tempfile]{temporary directory} 24 | is used.} 25 | 26 | \item{basename}{(string) If \code{dir} is specified, optionally the basename to 27 | use for the file created. If not specified a file name is generated 28 | from \link[rlang:hash]{hashing} the code.} 29 | 30 | \item{force_overwrite}{(logical) If set to \code{TRUE} the file will always be 31 | overwritten and thus the resulting model will always be recompiled.} 32 | 33 | \item{hash_salt}{(string) Text to add to the model code prior to hashing to 34 | determine the file name if \code{basename} is not set.} 35 | } 36 | \value{ 37 | The path to the file. 38 | } 39 | \description{ 40 | Convenience function for writing Stan code to a (possibly 41 | \link[base:tempfile]{temporary}) file with a \code{.stan} extension. By default, the 42 | file name is chosen deterministically based on a \link[rlang:hash]{hash} 43 | of the Stan code, and the file is not overwritten if it already has correct 44 | contents. This means that calling this function multiple times with the same 45 | Stan code will reuse the compiled model. This also however means that the 46 | function is potentially not thread-safe. Using \code{hash_salt = Sys.getpid()} 47 | should ensure thread-safety in the rare cases when it is needed. 48 | } 49 | \examples{ 50 | # stan program as a single string 51 | stan_program <- " 52 | data { 53 | int N; 54 | array[N] int y; 55 | } 56 | parameters { 57 | real theta; 58 | } 59 | model { 60 | y ~ bernoulli(theta); 61 | } 62 | " 63 | 64 | f <- write_stan_file(stan_program) 65 | print(f) 66 | 67 | lines <- readLines(f) 68 | print(lines) 69 | cat(lines, sep = "\n") 70 | 71 | # stan program as character vector of lines 72 | f2 <- write_stan_file(lines) 73 | identical(readLines(f), readLines(f2)) 74 | 75 | } 76 | -------------------------------------------------------------------------------- /man/write_stan_json.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \name{write_stan_json} 4 | \alias{write_stan_json} 5 | \title{Write data to a JSON file readable by CmdStan} 6 | \usage{ 7 | write_stan_json(data, file, always_decimal = FALSE) 8 | } 9 | \arguments{ 10 | \item{data}{(list) A named list of \R objects.} 11 | 12 | \item{file}{(string) The path to where the data file should be written.} 13 | 14 | \item{always_decimal}{(logical) Force generate non-integers with decimal 15 | points to better distinguish between integers and floating point values. 16 | If \code{TRUE} all \R objects in \code{data} intended for integers must be of integer 17 | type.} 18 | } 19 | \description{ 20 | Write data to a JSON file readable by CmdStan 21 | } 22 | \details{ 23 | \code{write_stan_json()} performs several conversions before writing the JSON 24 | file: 25 | \itemize{ 26 | \item \code{logical} -> \code{integer} (\code{TRUE} -> \code{1}, \code{FALSE} -> \code{0}) 27 | \item \code{data.frame} -> \code{matrix} (via \code{\link[=data.matrix]{data.matrix()}}) 28 | \item \code{list} -> \code{array} 29 | \item \code{table} -> \code{vector}, \code{matrix}, or \code{array} (depending on dimensions of table) 30 | } 31 | 32 | The \code{list} to \code{array} conversion is intended to make it easier to prepare 33 | the data for certain Stan declarations involving arrays: 34 | \itemize{ 35 | \item \verb{vector[J] v[K]} (or equivalently \verb{array[K] vector[J] v } as of Stan 2.27) 36 | can be constructed in \R as a list with \code{K} elements where each element a 37 | vector of length \code{J} 38 | \item \verb{matrix[I,J] v[K]} (or equivalently \verb{array[K] matrix[I,J] m } as of Stan 39 | 2.27 ) can be constructed in \R as a list with \code{K} elements where each element 40 | an \code{IxJ} matrix 41 | } 42 | 43 | These can also be passed in from \R as arrays instead of lists but the list 44 | option is provided for convenience. Unfortunately for arrays with more than 45 | one dimension, e.g., \verb{vector[J] v[K,L]} (or equivalently 46 | \verb{array[K,L] vector[J] v } as of Stan 2.27) it is not possible to use an \R 47 | list and an array must be used instead. For this example the array in \R 48 | should have dimensions \code{KxLxJ}. 49 | } 50 | \examples{ 51 | x <- matrix(rnorm(10), 5, 2) 52 | y <- rpois(nrow(x), lambda = 10) 53 | z <- c(TRUE, FALSE) 54 | data <- list(N = nrow(x), K = ncol(x), x = x, y = y, z = z) 55 | 56 | # write data to json file 57 | file <- tempfile(fileext = ".json") 58 | write_stan_json(data, file) 59 | 60 | # check the contents of the file 61 | cat(readLines(file), sep = "\n") 62 | 63 | 64 | # demonstrating list to array conversion 65 | # suppose x is declared as `vector[3] x[2]` (or equivalently `array[2] vector[3] x`) 66 | # we can use a list of length 2 where each element is a vector of length 3 67 | data <- list(x = list(1:3, 4:6)) 68 | file <- tempfile(fileext = ".json") 69 | write_stan_json(data, file) 70 | cat(readLines(file), sep = "\n") 71 | 72 | } 73 | -------------------------------------------------------------------------------- /man/write_stan_tempfile.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/example.R 3 | \name{write_stan_tempfile} 4 | \alias{write_stan_tempfile} 5 | \title{Write Stan code to a temporary file} 6 | \usage{ 7 | write_stan_tempfile(code, dir = tempdir()) 8 | } 9 | \arguments{ 10 | \item{code}{(character vector) The Stan code to write to the file. This can 11 | be a character vector of length one (a string) containing the entire Stan 12 | program or a character vector with each element containing one line of the 13 | Stan program.} 14 | 15 | \item{dir}{(string) An optional path to the directory where the file will be 16 | written. If omitted, a global option \code{cmdstanr_write_stan_file_dir} is 17 | used. If the global options is not set, \link[base:tempfile]{temporary directory} 18 | is used.} 19 | } 20 | \description{ 21 | This function is deprecated. Please use \code{\link[=write_stan_file]{write_stan_file()}} instead. 22 | } 23 | \keyword{internal} 24 | -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-120x120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/pkgdown/favicon/apple-touch-icon-120x120.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-152x152.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/pkgdown/favicon/apple-touch-icon-152x152.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-180x180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/pkgdown/favicon/apple-touch-icon-180x180.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-60x60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/pkgdown/favicon/apple-touch-icon-60x60.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-76x76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/pkgdown/favicon/apple-touch-icon-76x76.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/pkgdown/favicon/apple-touch-icon.png -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/pkgdown/favicon/favicon-16x16.png -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/pkgdown/favicon/favicon-32x32.png -------------------------------------------------------------------------------- /pkgdown/favicon/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/pkgdown/favicon/favicon.ico -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(cmdstanr) 3 | 4 | if (identical(Sys.getenv("NOT_CRAN"), "true")) { 5 | test_check("cmdstanr") 6 | } 7 | -------------------------------------------------------------------------------- /tests/testthat/answers/json-boolean.json: -------------------------------------------------------------------------------- 1 | { 2 | "N": [1, 0, 1] 3 | } 4 | -------------------------------------------------------------------------------- /tests/testthat/answers/json-df-matrix.json: -------------------------------------------------------------------------------- 1 | { 2 | "X": [ 3 | [1, 0.2], 4 | [2, 0.3], 5 | [3, 0.4] 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /tests/testthat/answers/json-factor.json: -------------------------------------------------------------------------------- 1 | { 2 | "N": [1, 2, 3, 3, 2, 1] 3 | } 4 | -------------------------------------------------------------------------------- /tests/testthat/answers/json-integer.json: -------------------------------------------------------------------------------- 1 | { 2 | "N": [1, 2, 3, 4] 3 | } 4 | -------------------------------------------------------------------------------- /tests/testthat/answers/json-matrix-lists.json: -------------------------------------------------------------------------------- 1 | { 2 | "M": [ 3 | [ 4 | [1, 3], 5 | [2, 4] 6 | ], 7 | [ 8 | [5, 6], 9 | [7, 8] 10 | ] 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /tests/testthat/answers/json-table-array.json: -------------------------------------------------------------------------------- 1 | { 2 | "x": [ 3 | [ 4 | [5, 0, 0, 0], 5 | [0, 0, 0, 0], 6 | [0, 0, 0, 0], 7 | [0, 0, 0, 0] 8 | ], 9 | [ 10 | [0, 0, 0, 0], 11 | [0, 5, 0, 0], 12 | [0, 0, 0, 0], 13 | [0, 0, 0, 0] 14 | ], 15 | [ 16 | [0, 0, 0, 0], 17 | [0, 0, 0, 0], 18 | [0, 0, 5, 0], 19 | [0, 0, 0, 0] 20 | ], 21 | [ 22 | [0, 0, 0, 0], 23 | [0, 0, 0, 0], 24 | [0, 0, 0, 0], 25 | [0, 0, 0, 5] 26 | ] 27 | ] 28 | } 29 | -------------------------------------------------------------------------------- /tests/testthat/answers/json-table-matrix.json: -------------------------------------------------------------------------------- 1 | { 2 | "x": [ 3 | [5, 0, 0, 0], 4 | [0, 5, 0, 0], 5 | [0, 0, 5, 0], 6 | [0, 0, 0, 5] 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /tests/testthat/answers/json-table-vector.json: -------------------------------------------------------------------------------- 1 | { 2 | "x": [5, 5, 5, 5] 3 | } 4 | -------------------------------------------------------------------------------- /tests/testthat/answers/json-unboxing.json: -------------------------------------------------------------------------------- 1 | { 2 | "N": 10 3 | } 4 | -------------------------------------------------------------------------------- /tests/testthat/answers/json-vector-lists.json: -------------------------------------------------------------------------------- 1 | { 2 | "N": [ 3 | [1, 2, 3], 4 | [4, 5, 6] 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /tests/testthat/answers/model-code-output.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/tests/testthat/answers/model-code-output.rds -------------------------------------------------------------------------------- /tests/testthat/answers/model-print-output.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | array[N] int y; 4 | } 5 | parameters { 6 | real theta; 7 | } 8 | model { 9 | theta ~ beta(1, 1); // uniform prior on interval 0,1 10 | y ~ bernoulli(theta); 11 | } 12 | -------------------------------------------------------------------------------- /tests/testthat/helper-envvars-and-paths.R: -------------------------------------------------------------------------------- 1 | on_codecov <- function() { 2 | identical(Sys.getenv("R_COVR"), "true") 3 | } 4 | 5 | on_ci <- function() { 6 | isTRUE(as.logical(Sys.getenv("CI"))) 7 | } 8 | 9 | mpi_toolchain_present <- function() { 10 | tryCatch( 11 | wsl_compatible_run(command = "mpicxx", args = "--version")$status == 0 && 12 | wsl_compatible_run(command = "mpiexec", args = "--version")$status == 0, 13 | error=function(cond) { 14 | FALSE 15 | } 16 | ) 17 | } 18 | 19 | delete_extensions <- function() { 20 | if (os_is_windows()) { 21 | c(".exe", ".o", ".hpp") 22 | } else { 23 | c("", ".o",".hpp") 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/testthat/helper-mock-cli.R: -------------------------------------------------------------------------------- 1 | real_wcr <- wsl_compatible_run 2 | 3 | with_mocked_cli <- function(code, compile_ret, info_ret) { 4 | with_mocked_bindings( 5 | code, 6 | wsl_compatible_run = function(command, args, ...) { 7 | if ( 8 | !is.null(command) 9 | && command == "make" 10 | && !is.null(args) 11 | && startsWith(basename(args[1]), "model-") 12 | ) { 13 | message("mock-compile-was-called") 14 | compile_ret 15 | } else if (!is.null(args) && args[1] == "info") { 16 | info_ret 17 | } else { 18 | real_wcr(command = command, args = args, ...) 19 | } 20 | } 21 | ) 22 | } 23 | 24 | ######## Mock Compile Expectations ####### 25 | 26 | # These helpers mimic `assert_called` and `assert_not_called` in other languages. 27 | # 28 | # Logic 29 | # `expect_mock_compile` 30 | # passes if mock_compile is called (at all, doesn't matter how many times) 31 | # fails if mock_compile is never called 32 | # `expect_no_mock_compile` is the inverse. It 33 | # passes if mock_compile is *not* called at all 34 | # fails if mock_compile is called (even once) 35 | # 36 | # Implementation: 37 | # `with_mocked_cli` 38 | # if a compile is triggered 39 | # emits a message with the contents `mock-compile-was-called` 40 | # (defined as wsl_compatible_run being called with make model-*) 41 | # `expect_mock_compile` checks for this message: 42 | # passes if it detects such a message 43 | # fails if it does not 44 | # `expect_no_mock_compile` 45 | # fails if a message with exactly this text is detected 46 | # passes if no such message is detected 47 | # messages with any other text does not impact `expect_no_mock_compile` 48 | 49 | expect_mock_compile <- function(object, ...) { 50 | expect_message(object, regexp = "mock-compile-was-called", ...) 51 | } 52 | expect_no_mock_compile <- function(object, ...) { 53 | expect_no_message(object, message = "mock-compile-was-called", ...) 54 | } 55 | -------------------------------------------------------------------------------- /tests/testthat/helper-models.R: -------------------------------------------------------------------------------- 1 | testing_data <- function(name) { 2 | if (file.exists(test_path("resources", "data", paste0(name, ".data.rds")))) { 3 | readRDS(test_path("resources", "data", paste0(name, ".data.rds"))) 4 | } else { 5 | test_path("resources", "data", paste0(name, ".data.json")) 6 | } 7 | } 8 | testing_stan_file <- function(name) { 9 | test_path("resources", "stan", paste0(name, ".stan")) 10 | } 11 | 12 | cmdstan_example_file <- function() { 13 | # stan program in different directory from the others 14 | file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan") 15 | } 16 | 17 | testing_model <- function(name) { 18 | cmdstan_model(stan_file = testing_stan_file(name)) 19 | } 20 | 21 | testing_fit <- 22 | function(name, 23 | method = c("sample", 24 | "optimize", 25 | "laplace", 26 | "variational", 27 | "generate_quantities"), 28 | seed = 123, 29 | ...) { 30 | method <- match.arg(method) 31 | mod <- testing_model(name) 32 | utils::capture.output( 33 | fit <- mod[[method]](data = testing_data(name), seed = seed, ...) 34 | ) 35 | fit 36 | } 37 | -------------------------------------------------------------------------------- /tests/testthat/resources/csv/bernoulli-1-optimize.csv: -------------------------------------------------------------------------------- 1 | # stan_version_major = 2 2 | # stan_version_minor = 23 3 | # stan_version_patch = 0 4 | # model = bernoulli_model 5 | # method = optimize 6 | # optimize 7 | # algorithm = lbfgs (Default) 8 | # lbfgs 9 | # init_alpha = 0.001 (Default) 10 | # tol_obj = 9.9999999999999998e-13 (Default) 11 | # tol_rel_obj = 10000 (Default) 12 | # tol_grad = 1e-08 (Default) 13 | # tol_rel_grad = 10000000 (Default) 14 | # tol_param = 1e-08 (Default) 15 | # history_size = 5 (Default) 16 | # iter = 2000 (Default) 17 | # save_iterations = 0 (Default) 18 | # id = 1 19 | # data 20 | # file = /tmp/Rtmp61AV95/standata-730c65138d15.json 21 | # init = 2 (Default) 22 | # random 23 | # seed = 1393588279 24 | # output 25 | # file = /tmp/Rtmp61AV95/bernoulli-202006242136-1-ceffba.csv 26 | # diagnostic_file = (Default) 27 | # refresh = 100 (Default) 28 | lp__,theta 29 | -12.2173,0.300001 30 | -------------------------------------------------------------------------------- /tests/testthat/resources/csv/bernoulli-1-variational.csv: -------------------------------------------------------------------------------- 1 | # stan_version_major = 2 2 | # stan_version_minor = 23 3 | # stan_version_patch = 0 4 | # model = bernoulli_model 5 | # method = variational 6 | # variational 7 | # algorithm = meanfield (Default) 8 | # meanfield 9 | # iter = 250 10 | # grad_samples = 500 11 | # elbo_samples = 1000 12 | # eta = 1 (Default) 13 | # adapt 14 | # engaged = 1 (Default) 15 | # iter = 50 (Default) 16 | # tol_rel_obj = 0.01 (Default) 17 | # eval_elbo = 100 (Default) 18 | # output_samples = 50 19 | # id = 1 20 | # data 21 | # file = /tmp/Rtmp61AV95/standata-730c14c2b7b4.json 22 | # init = 2 (Default) 23 | # random 24 | # seed = 786427598 25 | # output 26 | # file = /tmp/Rtmp61AV95/bernoulli-202006242140-1-3fa36d.csv 27 | # diagnostic_file = (Default) 28 | # refresh = 100 (Default) 29 | lp__,log_p__,log_g__,theta 30 | # Stepsize adaptation complete. 31 | # eta = 1 32 | 0,0,0,0.309 33 | 0,-14.2,-0.366829,0.230751 34 | 0,-13.7684,-0.00044183,0.30605 35 | 0,-14.6201,-0.899997,0.455273 36 | 0,-14.0276,-0.316818,0.393235 37 | 0,-13.9857,-0.168601,0.254361 38 | 0,-14.6606,-0.827013,0.197133 39 | 0,-13.9496,-0.136854,0.25948 40 | 0,-16.045,-2.21953,0.54422 41 | 0,-13.7637,-0.0137135,0.325722 42 | 0,-15.453,-1.68673,0.159631 43 | 0,-13.8821,-0.161872,0.368289 44 | 0,-13.7627,-0.0116179,0.324375 45 | 0,-13.8481,-0.0524313,0.27773 46 | 0,-15.501,-1.74112,0.157802 47 | 0,-13.9918,-0.174026,0.253542 48 | 0,-13.9076,-0.189913,0.373435 49 | 0,-13.8224,-0.0329588,0.284046 50 | 0,-14.5375,-0.700503,0.204801 51 | 0,-13.8919,-0.172733,0.370328 52 | 0,-14.064,-0.35425,0.398331 53 | 0,-13.9335,-0.217819,0.378217 54 | 0,-13.7823,-0.0431681,0.338984 55 | 0,-15.1694,-1.37071,0.171275 56 | 0,-13.7679,-0.0216807,0.330101 57 | 0,-13.7785,-0.0379186,0.33706 58 | 0,-16.5717,-3.01184,0.124675 59 | 0,-14.8757,-1.142,0.474945 60 | 0,-13.7921,-0.0121432,0.293708 61 | 0,-13.9598,-0.245849,0.382742 62 | 0,-14.6684,-0.946071,0.459196 63 | 0,-15.3593,-1.58144,0.163307 64 | 0,-13.7783,-0.0375811,0.336932 65 | 0,-14.018,-0.306863,0.391834 66 | 0,-14.8553,-1.03124,0.186297 67 | 0,-13.8356,-0.109223,0.357341 68 | 0,-14.2181,-0.509532,0.417214 69 | 0,-13.7693,-0.024036,0.331238 70 | 0,-14.0539,-0.343901,0.396948 71 | 0,-14.8528,-1.12045,0.473279 72 | 0,-13.7944,-0.0593156,0.344289 73 | 0,-14.1121,-0.283807,0.239395 74 | 0,-13.8333,-0.106551,0.356725 75 | 0,-16.1883,-2.34967,0.551251 76 | 0,-14.3023,-0.465671,0.221894 77 | 0,-14.7446,-1.01833,0.46517 78 | 0,-14.2682,-0.432494,0.224725 79 | 0,-14.4072,-0.569223,0.2138 80 | 0,-13.9305,-0.214605,0.377681 81 | 0,-13.8634,-0.141071,0.364198 82 | 0,-13.9699,-0.256578,0.384411 83 | -------------------------------------------------------------------------------- /tests/testthat/resources/csv/bernoulli-3-diff_params.csv: -------------------------------------------------------------------------------- 1 | # stan_version_major = 2 2 | # stan_version_minor = 21 3 | # stan_version_patch = 0 4 | # model = bernoulli_model 5 | # method = sample (Default) 6 | # sample 7 | # num_samples = 1 8 | # num_warmup = 1000 (Default) 9 | # save_warmup = 0 10 | # thin = 1 (Default) 11 | # adapt 12 | # engaged = 1 (Default) 13 | # gamma = 0.050000000000000003 (Default) 14 | # delta = 0.80000000000000004 (Default) 15 | # kappa = 0.75 (Default) 16 | # t0 = 10 (Default) 17 | # init_buffer = 75 (Default) 18 | # term_buffer = 50 (Default) 19 | # window = 25 (Default) 20 | # algorithm = hmc (Default) 21 | # hmc 22 | # engine = nuts (Default) 23 | # nuts 24 | # max_depth = 10 25 | # metric = diag_e (Default) 26 | # metric_file = (Default) 27 | # stepsize = 1 (Default) 28 | # stepsize_jitter = 0 (Default) 29 | # id = 3 30 | # data 31 | # file = /tmp/RtmpcXhiID/standata-124764cd0999.json 32 | # init = 2 (Default) 33 | # random 34 | # seed = 1927315875 35 | # output 36 | # file = /tmp/RtmpcXhiID/tests-201911231643-1-3613bb.csv 37 | # diagnostic_file = (Default) 38 | # refresh = 100 (Default) 39 | lp__,accept_stat__,stepsize__,treedepth__,n_leapfrog__,divergent__,energy__,mu,theta 40 | # pi = 3.14 41 | # Adaptation terminated 42 | # Step size = 0.712907 43 | # Diagonal elements of inverse mass matrix: 44 | # 1.00098, 0.068748 45 | -19.4938,0.953779,0.712907,2,3,0,19.4971,8.11498,7.4563 46 | # 47 | # pi_square = 9.86 48 | # Elapsed Time: 0.038029 seconds (Warm-up) 49 | # 0.030711 seconds (Sampling) 50 | # 0.06874 seconds (Total) 51 | # 52 | -------------------------------------------------------------------------------- /tests/testthat/resources/csv/bernoulli_ppc-1-gq.csv: -------------------------------------------------------------------------------- 1 | # stan_version_major = 2 2 | # stan_version_minor = 23 3 | # stan_version_patch = 0 4 | # model = bernoulli_ppc_model 5 | # method = generate_quantities 6 | # generate_quantities 7 | # fitted_params = /tmp/RtmpCvOIQ1/fittedParams-202006271227-1-b85b52.csv 8 | # id = 1 9 | # data 10 | # file = /home/rok/.cmdstanr/cmdstan-2.23.0/examples/bernoulli/bernoulli.data.json 11 | # init = 2 (Default) 12 | # random 13 | # seed = 123 14 | # output 15 | # file = /tmp/RtmpCvOIQ1/bernoulli_ppc-202006271227-1-986540.csv 16 | # diagnostic_file = (Default) 17 | # refresh = 250 18 | y_rep.1,y_rep.2,y_rep.3,y_rep.4,y_rep.5,y_rep.6,y_rep.7,y_rep.8,y_rep.9,y_rep.10 19 | 0,0,0,0,0,0,0,0,0,0 20 | 0,1,1,0,1,0,1,1,0,0 21 | 0,0,0,0,0,0,0,0,0,0 22 | 0,0,0,0,0,1,0,0,0,1 23 | 1,0,0,0,0,0,0,0,0,0 -------------------------------------------------------------------------------- /tests/testthat/resources/csv/logistic-diagnose.csv: -------------------------------------------------------------------------------- 1 | # stan_version_major = 2 2 | # stan_version_minor = 26 3 | # stan_version_patch = 1 4 | # model = logistic_model 5 | # method = diagnose 6 | # diagnose 7 | # test = gradient (Default) 8 | # gradient 9 | # epsilon = 9.9999999999999995e-07 (Default) 10 | # error = 9.9999999999999995e-07 (Default) 11 | # id = 1 12 | # data 13 | # file = /home/rok/R/x86_64-pc-linux-gnu-library/4.0/cmdstanr/logistic.data.json 14 | # init = 2 (Default) 15 | # random 16 | # seed = 343476408 17 | # output 18 | # file = /tmp/RtmpMYURXb/logistic-202105051949-1-7b84f0.csv 19 | # diagnostic_file = (Default) 20 | # refresh = 100 (Default) 21 | # sig_figs = -1 (Default) 22 | # profile_file = /tmp/RtmpMYURXb/logistic-profile-202105051949-1-7b7eca.csv 23 | # stanc_version = stanc3 v2.26.1 24 | # stancflags = --name=logistic_model 25 | # 26 | # Log probability=-88.1497 27 | # 28 | # param idx value model finite diff error 29 | # 0 0.037817 8.83081 8.83081 9.919e-09 30 | # 1 -1.26198 4.07931 4.07931 3.13568e-08 31 | # 2 1.16792 -25.7167 -25.7167 -5.31186e-09 32 | # 3 0.933592 -4.11423 -4.11423 5.87693e-09 33 | -------------------------------------------------------------------------------- /tests/testthat/resources/csv/model1-1-dense_e_metric.csv: -------------------------------------------------------------------------------- 1 | # stan_version_major = 2 2 | # stan_version_minor = 21 3 | # stan_version_patch = 0 4 | # model = warning_model 5 | # method = sample (Default) 6 | # sample 7 | # num_samples = 1 8 | # num_warmup = 1000 (Default) 9 | # save_warmup = 0 (Default) 10 | # thin = 1 (Default) 11 | # adapt 12 | # engaged = 1 (Default) 13 | # gamma = 0.050000000000000003 (Default) 14 | # delta = 0.80000000000000004 (Default) 15 | # kappa = 0.75 (Default) 16 | # t0 = 10 (Default) 17 | # init_buffer = 75 (Default) 18 | # term_buffer = 50 (Default) 19 | # window = 25 (Default) 20 | # algorithm = hmc (Default) 21 | # hmc 22 | # engine = nuts (Default) 23 | # nuts 24 | # max_depth = 5 25 | # metric = dense_e 26 | # metric_file = (Default) 27 | # stepsize = 1 (Default) 28 | # stepsize_jitter = 0 (Default) 29 | # id = 1 30 | # data 31 | # file = /tmp/RtmpwuXj7q/standata-626e1446a4cd.dat 32 | # init = 2 (Default) 33 | # random 34 | # seed = 832246860 35 | # output 36 | # file = /tmp/RtmpwuXj7q/warning-201911291346-1-563d96.csv 37 | # diagnostic_file = (Default) 38 | # refresh = 100 (Default) 39 | lp__,accept_stat__,stepsize__,treedepth__,n_leapfrog__,divergent__,energy__,mu,tau,theta.1,theta.2,theta.3,theta.4,theta.5,theta.6,theta.7,theta.8 40 | # Adaptation terminated 41 | # Step size = 0.11757 42 | # Elements of inverse mass matrix: 43 | # 10.2742, -0.189148, 5.92065, 8.2658, 10.9931, 8.67196, 9.75007, 8.30008, 6.3396, 8.75422 44 | # -0.189148, 0.552614, 2.28054, 0.587285, -0.557112, 0.0689745, -1.06614, -0.502288, 1.49863, 0.450733 45 | # 5.92065, 2.28054, 52.7011, 7.89278, 3.9639, 5.71556, 2.16445, 1.88834, 13.8962, 15.1166 46 | # 8.2658, 0.587285, 7.89278, 30.7924, 8.39762, 8.51489, 8.27253, 9.96521, 7.58758, 7.8403 47 | # 10.9931, -0.557112, 3.9639, 8.39762, 39.6164, 9.88103, 9.62453, 9.80744, 6.28594, 6.77034 48 | # 8.67196, 0.0689745, 5.71556, 8.51489, 9.88103, 29.6702, 8.5937, 8.3289, 8.76294, 5.63637 49 | # 9.75007, -1.06614, 2.16445, 8.27253, 9.62453, 8.5937, 26.3294, 10.1908, 2.76266, 4.56938 50 | # 8.30008, -0.502288, 1.88834, 9.96521, 9.80744, 8.3289, 10.1908, 26.5846, 3.7247, 4.26521 51 | # 6.3396, 1.49863, 13.8962, 7.58758, 6.28594, 8.76294, 2.76266, 3.7247, 28.8872, 8.43035 52 | # 8.75422, 0.450733, 15.1166, 7.8403, 6.77034, 5.63637, 4.56938, 4.26521, 8.43035, 42.5438 53 | -6.22318,0.789257,0.11757,3,7,0,10.0181,3.451,0.646028,3.6284,4.4898,4.54992,3.50833,2.93186,3.77877,4.01059,1.97369 54 | -------------------------------------------------------------------------------- /tests/testthat/resources/csv/model1-2-dense_e_metric.csv: -------------------------------------------------------------------------------- 1 | # stan_version_major = 2 2 | # stan_version_minor = 21 3 | # stan_version_patch = 0 4 | # model = warning_model 5 | # method = sample (Default) 6 | # sample 7 | # num_samples = 1 8 | # num_warmup = 1000 (Default) 9 | # save_warmup = 0 (Default) 10 | # thin = 1 (Default) 11 | # adapt 12 | # engaged = 1 (Default) 13 | # gamma = 0.050000000000000003 (Default) 14 | # delta = 0.80000000000000004 (Default) 15 | # kappa = 0.75 (Default) 16 | # t0 = 10 (Default) 17 | # init_buffer = 75 (Default) 18 | # term_buffer = 50 (Default) 19 | # window = 25 (Default) 20 | # algorithm = hmc (Default) 21 | # hmc 22 | # engine = nuts (Default) 23 | # nuts 24 | # max_depth = 5 25 | # metric = dense_e 26 | # metric_file = (Default) 27 | # stepsize = 1 (Default) 28 | # stepsize_jitter = 0 (Default) 29 | # id = 2 30 | # data 31 | # file = /tmp/RtmpwuXj7q/standata-626e2e1de93.dat 32 | # init = 2 (Default) 33 | # random 34 | # seed = 1742584894 35 | # output 36 | # file = /tmp/RtmpwuXj7q/warning-201911291802-1-b9bf01.csv 37 | # diagnostic_file = (Default) 38 | # refresh = 100 (Default) 39 | lp__,accept_stat__,stepsize__,treedepth__,n_leapfrog__,divergent__,energy__,mu,tau,theta.1,theta.2,theta.3,theta.4,theta.5,theta.6,theta.7,theta.8 40 | # Adaptation terminated 41 | # Step size = 0.232778 42 | # Elements of inverse mass matrix: 43 | # 11.08, -0.305763, 5.27013, 7.33046, 7.31263, 6.93229, 10.1923, 7.46852, 7.51557, 7.78791 44 | # -0.305763, 0.678461, 1.70598, 0.337143, -0.69887, 0.423236, -0.974023, -0.605539, 1.83794, 0.0780934 45 | # 5.27013, 1.70598, 36.2726, 7.9386, 3.88642, 12.0214, 4.2487, 3.84886, 12.9738, 4.34037 46 | # 7.33046, 0.337143, 7.9386, 23.9878, 4.93047, 5.76139, 7.34008, 7.78631, 6.79063, 8.13132 47 | # 7.31263, -0.69887, 3.88642, 4.93047, 25.3662, 7.2269, 7.5408, 8.18066, 5.77239, 7.53072 48 | # 6.93229, 0.423236, 12.0214, 5.76139, 7.2269, 24.0619, 7.24315, 7.194, 10.1647, 5.61617 49 | # 10.1923, -0.974023, 4.2487, 7.34008, 7.5408, 7.24315, 26.2403, 6.91029, 1.26095, 4.72335 50 | # 7.46852, -0.605539, 3.84886, 7.78631, 8.18066, 7.194, 6.91029, 30.2862, 5.61914, 8.10162 51 | # 7.51557, 1.83794, 12.9738, 6.79063, 5.77239, 10.1647, 1.26095, 5.61914, 34.5498, 7.75486 52 | # 7.78791, 0.0780934, 4.34037, 8.13132, 7.53072, 5.61617, 4.72335, 8.10162, 7.75486, 35.6602 53 | -12.1669,0.771978,0.232778,3,7,0,19.0882,5.46421,2.63041,5.84162,5.91101,3.42499,6.52557,7.03691,8.51128,5.55333,2.67202 54 | -------------------------------------------------------------------------------- /tests/testthat/resources/csv/model1-3-diff_args.csv: -------------------------------------------------------------------------------- 1 | # stan_version_major = 2 2 | # stan_version_minor = 21 3 | # stan_version_patch = 0 4 | # model = model1 5 | # method = sample (Default) 6 | # sample 7 | # num_samples = 1 8 | # num_warmup = 1 9 | # save_warmup = 0 10 | # thin = 1 (Default) 11 | # adapt 12 | # engaged = 1 (Default) 13 | # gamma = 0.050000000000000003 (Default) 14 | # delta = 0.90000000000000004 (Default) 15 | # kappa = 0.75 (Default) 16 | # t0 = 10 (Default) 17 | # init_buffer = 75 (Default) 18 | # term_buffer = 50 (Default) 19 | # window = 25 (Default) 20 | # algorithm = hmc (Default) 21 | # hmc 22 | # engine = nuts (Default) 23 | # nuts 24 | # max_depth = 5 25 | # metric = diag_e (Default) 26 | # metric_file = (Default) 27 | # stepsize = 1 (Default) 28 | # stepsize_jitter = 0 (Default) 29 | # id = 1 30 | # data 31 | # file = /tmp/RtmpcXhiID/standata-124764cd0999.json 32 | # init = 2 (Default) 33 | # random 34 | # seed = 1927315875 35 | # output 36 | # file = /tmp/RtmpcXhiID/tests-201911231643-1-3613bb.csv 37 | # diagnostic_file = (Default) 38 | # refresh = 100 (Default) 39 | lp__,accept_stat__,stepsize__,treedepth__,n_leapfrog__,divergent__,energy__,mu,sigma 40 | -32.3122,1,0.125,2,3,0,43.1912,-0.780945,2.9559 41 | # Adaptation terminated 42 | # Step size = 0.712907 43 | # Diagonal elements of inverse mass matrix: 44 | # 1.00098, 0.068748 45 | -19.4938,0.953779,0.712907,2,3,0,19.4971,8.11498,7.4563 46 | # 47 | # Elapsed Time: 0.038029 seconds (Warm-up) 48 | # 0.030711 seconds (Sampling) 49 | # 0.06874 seconds (Total) 50 | # 51 | -------------------------------------------------------------------------------- /tests/testthat/resources/csv/model1-3-no-params.csv: -------------------------------------------------------------------------------- 1 | # stan_version_major = 2 2 | # stan_version_minor = 21 3 | # stan_version_patch = 0 4 | # model = model1 5 | # method = sample (Default) 6 | # sample 7 | # num_samples = 100 (Default) 8 | # num_warmup = 100 (Default) 9 | # save_warmup = 0 10 | # thin = 1 (Default) 11 | # adapt 12 | # engaged = 1 (Default) 13 | # gamma = 0.050000000000000003 (Default) 14 | # delta = 0.80000000000000004 (Default) 15 | # kappa = 0.75 (Default) 16 | # t0 = 10 (Default) 17 | # init_buffer = 75 (Default) 18 | # term_buffer = 50 (Default) 19 | # window = 25 (Default) 20 | # algorithm = hmc (Default) 21 | # hmc 22 | # engine = nuts (Default) 23 | # nuts 24 | # max_depth = 5 25 | # metric = diag_e (Default) 26 | # metric_file = (Default) 27 | # stepsize = 1 (Default) 28 | # stepsize_jitter = 0 (Default) 29 | # id = 2 30 | # data 31 | # file = /tmp/RtmpcXhiID/standata-124764cd0999.json 32 | # init = 2 (Default) 33 | # random 34 | # seed = 935257156 35 | # output 36 | # file = /tmp/RtmpcXhiID/tests-201911231643-2-3613bb.csv 37 | # diagnostic_file = (Default) 38 | # refresh = 100 (Default) 39 | -------------------------------------------------------------------------------- /tests/testthat/resources/csv/model1-3-no-samples.csv: -------------------------------------------------------------------------------- 1 | # stan_version_major = 2 2 | # stan_version_minor = 21 3 | # stan_version_patch = 0 4 | # model = model1 5 | # method = sample (Default) 6 | # sample 7 | # num_samples = 100 (Default) 8 | # num_warmup = 100 (Default) 9 | # save_warmup = 0 10 | # thin = 1 (Default) 11 | # adapt 12 | # engaged = 1 (Default) 13 | # gamma = 0.050000000000000003 (Default) 14 | # delta = 0.80000000000000004 (Default) 15 | # kappa = 0.75 (Default) 16 | # t0 = 10 (Default) 17 | # init_buffer = 75 (Default) 18 | # term_buffer = 50 (Default) 19 | # window = 25 (Default) 20 | # algorithm = hmc (Default) 21 | # hmc 22 | # engine = nuts (Default) 23 | # nuts 24 | # max_depth = 5 25 | # metric = diag_e (Default) 26 | # metric_file = (Default) 27 | # stepsize = 1 (Default) 28 | # stepsize_jitter = 0 (Default) 29 | # id = 2 30 | # data 31 | # file = /tmp/RtmpcXhiID/standata-124764cd0999.json 32 | # init = 2 (Default) 33 | # random 34 | # seed = 935257156 35 | # output 36 | # file = /tmp/RtmpcXhiID/tests-201911231643-2-3613bb.csv 37 | # diagnostic_file = (Default) 38 | # refresh = 100 (Default) 39 | lp__,accept_stat__,stepsize__,treedepth__,n_leapfrog__,divergent__,energy__,mu,sigma 40 | -------------------------------------------------------------------------------- /tests/testthat/resources/data/bernoulli.data.R: -------------------------------------------------------------------------------- 1 | y <- c(0, 1, 0, 0, 0, 0, 0, 0, 0, 1) 2 | N <- 10 3 | -------------------------------------------------------------------------------- /tests/testthat/resources/data/bernoulli.data.json: -------------------------------------------------------------------------------- 1 | { 2 | "N" : 10, 3 | "y" : [0,1,0,0,0,0,0,0,0,1] 4 | } 5 | -------------------------------------------------------------------------------- /tests/testthat/resources/data/bernoulli.data.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/tests/testthat/resources/data/bernoulli.data.rds -------------------------------------------------------------------------------- /tests/testthat/resources/data/bernoulli_log_lik.data.json: -------------------------------------------------------------------------------- 1 | { 2 | "N" : 10, 3 | "y" : [0,1,0,0,0,0,0,0,0,1] 4 | } 5 | -------------------------------------------------------------------------------- /tests/testthat/resources/data/bernoulli_ppc.data.R: -------------------------------------------------------------------------------- 1 | y <- c(0, 1, 0, 0, 0, 0, 0, 0, 0, 1) 2 | N <- 10 3 | -------------------------------------------------------------------------------- /tests/testthat/resources/data/bernoulli_ppc.data.json: -------------------------------------------------------------------------------- 1 | { 2 | "N" : 10, 3 | "y" : [0,1,0,0,0,0,0,0,0,1] 4 | } 5 | -------------------------------------------------------------------------------- /tests/testthat/resources/data/bernoulli_ppc.data.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/tests/testthat/resources/data/bernoulli_ppc.data.rds -------------------------------------------------------------------------------- /tests/testthat/resources/data/info_message.data.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/tests/testthat/resources/data/info_message.data.rds -------------------------------------------------------------------------------- /tests/testthat/resources/data/logistic.data.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/tests/testthat/resources/data/logistic.data.rds -------------------------------------------------------------------------------- /tests/testthat/resources/data/schools.data.json: -------------------------------------------------------------------------------- 1 | { 2 | "J": 8, 3 | "y": [28, 8, -3, 7, -1, 1, 18, 12], 4 | "sigma": [15, 10, 16, 11, 9, 11, 10, 18] 5 | } 6 | -------------------------------------------------------------------------------- /tests/testthat/resources/data/schools.data.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stan-dev/cmdstanr/c57888e301416bf0cade86ae0d5540349c093593/tests/testthat/resources/data/schools.data.rds -------------------------------------------------------------------------------- /tests/testthat/resources/init/bernoulli.init-1.json: -------------------------------------------------------------------------------- 1 | { 2 | "theta" : 0.5 3 | } 4 | -------------------------------------------------------------------------------- /tests/testthat/resources/init/bernoulli.init-2.json: -------------------------------------------------------------------------------- 1 | { 2 | "theta" : 0.55 3 | } 4 | -------------------------------------------------------------------------------- /tests/testthat/resources/metric/bernoulli.inv_metric.dense_e.data.R: -------------------------------------------------------------------------------- 1 | inv_metric <- 2 | structure(c(1), 3 | .Dim = c(1, 1)) 4 | -------------------------------------------------------------------------------- /tests/testthat/resources/metric/bernoulli.inv_metric.dense_e.json: -------------------------------------------------------------------------------- 1 | { 2 | "inv_metric" : [[1]] 3 | } 4 | -------------------------------------------------------------------------------- /tests/testthat/resources/metric/bernoulli.inv_metric.diag_e.data.R: -------------------------------------------------------------------------------- 1 | inv_metric <- c(1) 2 | -------------------------------------------------------------------------------- /tests/testthat/resources/metric/bernoulli.inv_metric.diag_e.json: -------------------------------------------------------------------------------- 1 | { 2 | "inv_metric" : [1] 3 | } 4 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/.gitignore: -------------------------------------------------------------------------------- 1 | /bernoulli 2 | /bernoulli_external 3 | /bernoulli_fp 4 | /bernoulli_include 5 | /bernoulli_log_lik 6 | /bernoulli_ppc 7 | /bernoulli_threads 8 | /chain_fails 9 | /divide_real_by_two 10 | /fail 11 | /info_message 12 | /init_warnings 13 | /logistic 14 | /logistic_profiling 15 | /schools 16 | 17 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/bernoulli.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | array[N] int y; 4 | } 5 | parameters { 6 | real theta; 7 | } 8 | model { 9 | theta ~ beta(1, 1); // uniform prior on interval 0,1 10 | y ~ bernoulli(theta); 11 | } 12 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/bernoulli_external.stan: -------------------------------------------------------------------------------- 1 | functions { 2 | real make_odds(real theta); 3 | } 4 | data { 5 | int N; 6 | array[N] int y; 7 | } 8 | parameters { 9 | real theta; 10 | } 11 | model { 12 | theta ~ beta(1, 1); 13 | y ~ bernoulli(theta); 14 | } 15 | generated quantities { 16 | real odds; 17 | odds = make_odds(theta); 18 | } 19 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/bernoulli_fp.stan: -------------------------------------------------------------------------------- 1 | transformed data { 2 | int N = 10; 3 | real theta = 0.35; 4 | } 5 | generated quantities { 6 | array[N] int y_sim; 7 | for (n in 1 : N) { 8 | y_sim[n] = bernoulli_rng(theta); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/bernoulli_include.stan: -------------------------------------------------------------------------------- 1 | functions { 2 | #include divide_real_by_two.stan 3 | } 4 | data { 5 | int N; 6 | array[N] int y; 7 | } 8 | parameters { 9 | real theta; 10 | } 11 | model { 12 | theta ~ beta(divide_real_by_two(2.0), 1); 13 | for (n in 1 : N) { 14 | y[n] ~ bernoulli(theta); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/bernoulli_log_lik.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | array[N] int y; 4 | } 5 | parameters { 6 | real theta; 7 | } 8 | model { 9 | theta ~ beta(1, 1); // uniform prior on interval 0,1 10 | y ~ bernoulli(theta); 11 | } 12 | generated quantities { 13 | vector[N] log_lik; 14 | for (n in 1 : N) { 15 | log_lik[n] = bernoulli_lpmf(y | theta); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/bernoulli_ppc.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | array[N] int y; 4 | } 5 | parameters { 6 | real theta; 7 | } 8 | model { 9 | theta ~ beta(1, 1); 10 | y ~ bernoulli(theta); 11 | } 12 | generated quantities { 13 | array[N] int y_rep; 14 | int sum_y; 15 | for (n in 1 : N) { 16 | y_rep[n] = bernoulli_rng(theta); 17 | } 18 | sum_y = sum(y_rep); 19 | } 20 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/chain_fails.stan: -------------------------------------------------------------------------------- 1 | data { 2 | real pr_fail; 3 | } 4 | transformed data { 5 | int fail = bernoulli_rng(pr_fail); 6 | } 7 | parameters { 8 | real theta; 9 | } 10 | model { 11 | if (fail) { 12 | // this should result in a failed chain 13 | theta ~ normal(1 / 0.0, 1); 14 | } else { 15 | theta ~ normal(0, 1); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/divide_real_by_two.stan: -------------------------------------------------------------------------------- 1 | real divide_real_by_two(real x){ 2 | return x / 2; 3 | } 4 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/fail.stan: -------------------------------------------------------------------------------- 1 | parameters { 2 | row_vector[3] a; 3 | vector[3] b; 4 | } 5 | 6 | model { 7 | matrix[1, 1] c = a * b; 8 | } 9 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/info_message.stan: -------------------------------------------------------------------------------- 1 | parameters { 2 | corr_matrix[10] Omega; 3 | } 4 | model { 5 | Omega ~ lkj_corr(1); 6 | } 7 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/init_warnings.stan: -------------------------------------------------------------------------------- 1 | parameters { 2 | real mu; 3 | } 4 | model { 5 | target += negative_infinity(); 6 | } 7 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/logistic.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int K; 4 | array[N] int y; 5 | matrix[N, K] X; 6 | } 7 | parameters { 8 | real alpha; 9 | vector[K] beta; 10 | } 11 | model { 12 | target += normal_lpdf(alpha | 0, 1); 13 | target += normal_lpdf(beta | 0, 1); 14 | target += bernoulli_logit_glm_lpmf(y | X, alpha, beta); 15 | } 16 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/logistic_profiling.stan: -------------------------------------------------------------------------------- 1 | functions { 2 | real glm(array[] int y, matrix X, real alpha, vector beta) { 3 | profile("glm") { 4 | return bernoulli_logit_glm_lpmf(y | X, alpha, beta); 5 | } 6 | } 7 | } 8 | data { 9 | int N; 10 | int K; 11 | array[N] int y; 12 | matrix[N, K] X; 13 | } 14 | parameters { 15 | real alpha; 16 | vector[K] beta; 17 | } 18 | model { 19 | profile("priors") { 20 | target += normal_lpdf(alpha | 0, 1); 21 | target += normal_lpdf(beta | 0, 1); 22 | } 23 | profile("udf") { 24 | target += glm(y, X, alpha, beta); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/logistic_simple.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | array[N] int y; 4 | } 5 | parameters { 6 | real alpha; 7 | } 8 | model { 9 | target += normal_lpdf(alpha | 0, 1); 10 | target += bernoulli_logit_lpmf(y | alpha); 11 | } 12 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/loo_moment_match.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int K; 3 | int N; 4 | matrix[N,K] x; 5 | array[N] int y; 6 | vector[N] outcome_offset; 7 | 8 | real beta_prior_scale; 9 | real alpha_prior_scale; 10 | } 11 | parameters { 12 | vector[K] beta; 13 | real intercept; 14 | } 15 | model { 16 | y ~ poisson(exp(x * beta + intercept + outcome_offset)); 17 | beta ~ normal(0,beta_prior_scale); 18 | intercept ~ normal(0,alpha_prior_scale); 19 | } 20 | generated quantities { 21 | vector[N] log_lik; 22 | for (n in 1:N) 23 | log_lik[n] = poisson_lpmf(y[n] | exp(x[n] * beta + intercept + outcome_offset[n])); 24 | } 25 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/old_array_syntax.stan: -------------------------------------------------------------------------------- 1 | parameters { 2 | real x[3]; 3 | } 4 | model { 5 | x ~ normal(0, 1); 6 | } 7 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/parameter_types.stan: -------------------------------------------------------------------------------- 1 | parameters { 2 | real real_p; 3 | vector[2] vector_p; 4 | matrix[2, 2] matrix_p; 5 | array[2] matrix[2, 2] array_matrix_p; 6 | corr_matrix[2] corr_p; 7 | array[2, 2] real array_array_real_p; 8 | array[2, 2] vector[3] array_array_vector_p; 9 | array[2, 2] matrix[3, 3] array_array_matrix_p; 10 | // complex complex_p; 11 | // complex_matrix[2, 2] complex_matrix_p; 12 | // complex_vector[4] complex_vector_p; 13 | // tuple(real, vector[3], array[2] matrix[2, 2], complex) tuple_int_vector_arraymatrix_complex_p; 14 | // array[2] tuple(real, tuple(vector[2], array[2] tuple(real, complex, matrix[2, 2]))) arraytuple_big_p; 15 | } 16 | -------------------------------------------------------------------------------- /tests/testthat/resources/stan/schools.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int J; 3 | vector[J] sigma; 4 | vector[J] y; 5 | } 6 | parameters { 7 | real mu; 8 | real tau; 9 | vector[J] theta; 10 | } 11 | model { 12 | target += normal_lpdf(tau | 0, 10); 13 | target += normal_lpdf(mu | 0, 10); 14 | target += normal_lpdf(theta | mu, tau); 15 | target += normal_lpdf(y | theta, sigma); 16 | } 17 | -------------------------------------------------------------------------------- /tests/testthat/teardown-remove-files.R: -------------------------------------------------------------------------------- 1 | # remove any files that aren't .stan files from resources/stan, 2 | # e.g. files created by $compile() 3 | all_files_in_stan <- 4 | list.files(test_path("resources", "stan"), 5 | full.names = TRUE, 6 | recursive = TRUE) 7 | not_stan_programs <- !grepl(".stan$", all_files_in_stan) 8 | file.remove(all_files_in_stan[not_stan_programs]) 9 | -------------------------------------------------------------------------------- /tests/testthat/test-cpp_opts.R: -------------------------------------------------------------------------------- 1 | test_that("parse_exe_info_string works", { 2 | expect_equal_ignore_order( 3 | parse_exe_info_string(" 4 | stan_version_major = 2 5 | stan_version_minor = 38 6 | stan_version_patch = 0 7 | STAN_THREADS=false 8 | STAN_MPI=false 9 | STAN_OPENCL=true 10 | STAN_NO_RANGE_CHECKS=false 11 | STAN_CPP_OPTIMS=false 12 | "), 13 | list( 14 | stan_version = "2.38.0", 15 | stan_threads = FALSE, 16 | stan_mpi = FALSE, 17 | stan_opencl = TRUE, 18 | stan_no_range_checks = FALSE, 19 | stan_cpp_optims = FALSE 20 | ) 21 | ) 22 | }) 23 | 24 | test_that("validate_cpp_options works", { 25 | expect_equal_ignore_order( 26 | validate_cpp_options(list( 27 | Stan_Threads = TRUE, 28 | STAN_OPENCL = NULL, 29 | aBc = FALSE 30 | )), 31 | list( 32 | stan_threads = TRUE, 33 | stan_opencl = NULL, 34 | abc = FALSE 35 | ) 36 | ) 37 | expect_warning(validate_cpp_options(list(STAN_OPENCL = FALSE))) 38 | }) 39 | 40 | 41 | test_that("exe_info cpp_options comparison works", { 42 | exe_info_all_flags_off <- exe_info_style_cpp_options(list()) 43 | exe_info_all_flags_off[["stan_version"]] <- "35.0.0" 44 | 45 | expect_true(exe_info_reflects_cpp_options( 46 | exe_info_all_flags_off, 47 | list() 48 | )) 49 | expect_true(exe_info_reflects_cpp_options( 50 | list(stan_opencl = FALSE), 51 | list(stan_opencl = NULL) 52 | )) 53 | expect_not_true(exe_info_reflects_cpp_options( 54 | list(stan_opencl = FALSE), 55 | list(stan_opencl = FALSE) 56 | )) 57 | expect_not_true(exe_info_reflects_cpp_options( 58 | list(stan_opencl = FALSE, stan_threads = FALSE), 59 | list(stan_opencl = NULL, stan_threads = TRUE) 60 | )) 61 | expect_not_true(exe_info_reflects_cpp_options( 62 | list(stan_opencl = FALSE, stan_threads = FALSE), 63 | list(stan_opencl = NULL, stan_threads = TRUE, EXTRA_ARG = TRUE) 64 | )) 65 | 66 | # no exe_info -> no recompile based on cpp info 67 | expect_warning( 68 | expect_true(exe_info_reflects_cpp_options(list(), list())), 69 | "Recompiling is recommended" 70 | ) 71 | }) 72 | -------------------------------------------------------------------------------- /tests/testthat/test-fit-laplace.R: -------------------------------------------------------------------------------- 1 | context("fitted-laplace") 2 | 3 | set_cmdstan_path() 4 | fit_laplace <- testing_fit("logistic", method = "laplace", seed = 100) 5 | PARAM_NAMES <- c("alpha", "beta[1]", "beta[2]", "beta[3]") 6 | 7 | 8 | test_that("summary() and print() methods works after laplace", { 9 | x <- fit_laplace$summary() 10 | expect_s3_class(x, "draws_summary") 11 | expect_equal(x$variable, c("lp__", "lp_approx__", PARAM_NAMES)) 12 | 13 | x <- fit_laplace$summary(variables = NULL, c("mean", "sd")) 14 | expect_s3_class(x, "draws_summary") 15 | expect_equal(x$variable, c("lp__", "lp_approx__", PARAM_NAMES)) 16 | expect_equal(colnames(x), c("variable", "mean", "sd")) 17 | 18 | expect_output(expect_s3_class(fit_laplace$print(), "CmdStanLaplace"), "variable") 19 | expect_output(fit_laplace$print(max_rows = 1), "# showing 1 of 6 rows") 20 | }) 21 | 22 | test_that("draws() method returns posterior sample (reading csv works)", { 23 | draws <- fit_laplace$draws() 24 | expect_type(draws, "double") 25 | expect_s3_class(draws, "draws_matrix") 26 | expect_equal(posterior::variables(draws), c("lp__", "lp_approx__", PARAM_NAMES)) 27 | }) 28 | 29 | test_that("lp(), lp_approx() methods return vectors (reading csv works)", { 30 | lp <- fit_laplace$lp() 31 | lg <- fit_laplace$lp_approx() 32 | expect_type(lp, "double") 33 | expect_type(lg, "double") 34 | expect_equal(length(lp), nrow(fit_laplace$draws())) 35 | expect_equal(length(lg), length(lp)) 36 | }) 37 | 38 | test_that("time() method works after laplace", { 39 | run_times <- fit_laplace$time() 40 | checkmate::expect_list(run_times, names = "strict", any.missing = FALSE) 41 | testthat::expect_named(run_times, c("total")) 42 | checkmate::expect_number(run_times$total, finite = TRUE) 43 | }) 44 | 45 | test_that("output() works for laplace", { 46 | expect_output(fit_laplace$output(), "method = laplace") 47 | }) 48 | 49 | test_that("draws() works for different formats", { 50 | a <- fit_laplace$draws() 51 | expect_true(posterior::is_draws_matrix(a)) 52 | a <- fit_laplace$draws(format = "list") 53 | expect_true(posterior::is_draws_list(a)) 54 | a <- fit_laplace$draws(format = "array") 55 | expect_true(posterior::is_draws_array(a)) 56 | a <- fit_laplace$draws(format = "df") 57 | expect_true(posterior::is_draws_df(a)) 58 | }) 59 | 60 | test_that("draws() errors if invalid format", { 61 | expect_error( 62 | fit_laplace$draws(format = "bad_format"), 63 | "The supplied draws format is not valid" 64 | ) 65 | }) 66 | 67 | -------------------------------------------------------------------------------- /tests/testthat/test-fit-mle.R: -------------------------------------------------------------------------------- 1 | context("fitted-mle") 2 | 3 | set_cmdstan_path() 4 | fit_mle <- testing_fit("logistic", method = "optimize", seed = 123) 5 | mod <- testing_model("bernoulli") 6 | data_list <- testing_data("bernoulli") 7 | PARAM_NAMES <- c("alpha", "beta[1]", "beta[2]", "beta[3]") 8 | 9 | 10 | test_that("mle and lp methods work after optimization", { 11 | expect_named(fit_mle$mle(), PARAM_NAMES) 12 | checkmate::expect_numeric(fit_mle$lp(), len = 1) 13 | }) 14 | 15 | test_that("summary method works after optimization", { 16 | x <- fit_mle$summary() 17 | expect_s3_class(x, "draws_summary") 18 | expect_equal(colnames(x), c("variable", "estimate")) 19 | expect_equal(x$variable, c("lp__", PARAM_NAMES)) 20 | 21 | x <- fit_mle$summary("lp__") 22 | expect_equal(colnames(x), c("variable", "estimate")) 23 | expect_equal(x$variable, "lp__") 24 | }) 25 | 26 | test_that("print() method works after optimization", { 27 | expect_output(expect_s3_class(fit_mle$print(), "CmdStanMLE"), "estimate") 28 | expect_output(fit_mle$print(max_rows = 1), "# showing 1 of 5 rows") 29 | expect_error( 30 | fit_mle$print(variable = "unknown", max_rows = 20), 31 | "Can't find the following variable(s): unknown", 32 | fixed = TRUE 33 | ) # unknown parameter 34 | }) 35 | 36 | test_that("time() method works after optimization", { 37 | run_times <- fit_mle$time() 38 | checkmate::expect_list(run_times, names = "strict", any.missing = FALSE) 39 | testthat::expect_named(run_times, c("total")) 40 | checkmate::expect_number(run_times$total, finite = TRUE) 41 | }) 42 | 43 | 44 | test_that("output() works for optimization", { 45 | expect_output(fit_mle$output(), 46 | "method = optimize") 47 | }) 48 | 49 | test_that("time is reported after optimization", { 50 | expect_output(mod$optimize(data = data_list, seed = 123), 51 | "Finished in") 52 | }) 53 | 54 | test_that("no error when checking estimates after failure", { 55 | expect_warning( 56 | fit <- cmdstanr_example("schools", method = "optimize", seed = 123), # optim ålways fails for this 57 | "Fitting finished unexpectedly" 58 | ) 59 | expect_error(fit$summary(), "Fitting failed. Unable to retrieve the draws.") 60 | }) 61 | 62 | test_that("draws() works for different formats", { 63 | a <- fit_mle$draws() 64 | expect_true(posterior::is_draws_matrix(a)) 65 | a <- fit_mle$draws(format = "list") 66 | expect_true(posterior::is_draws_list(a)) 67 | a <- fit_mle$draws(format = "array") 68 | expect_true(posterior::is_draws_array(a)) 69 | a <- fit_mle$draws(format = "df") 70 | expect_true(posterior::is_draws_df(a)) 71 | }) 72 | 73 | test_that("draws() errors if invalid format", { 74 | expect_error( 75 | fit_mle$draws(format = "bad_format"), 76 | "The supplied draws format is not valid" 77 | ) 78 | }) 79 | -------------------------------------------------------------------------------- /tests/testthat/test-knitr.R: -------------------------------------------------------------------------------- 1 | context("knitr engine") 2 | 3 | test_that("eng_cmdstan throws correct errors", { 4 | skip_if_not_installed("knitr") 5 | expect_error(eng_cmdstan(list(output.var = 1)), "must be a character string") 6 | expect_error(eng_cmdstan(list(output.var = c("A", "B"))), "must be a character string") 7 | }) 8 | 9 | test_that("eng_cmdstan works", { 10 | skip_if_not_installed("knitr") 11 | code <- " 12 | parameters { 13 | real y; 14 | } 15 | model { 16 | y ~ std_normal(); 17 | } 18 | " 19 | opts <- knitr::opts_chunk$merge(list( 20 | output.var = "ABC", 21 | code = code, 22 | cache = TRUE, 23 | cache.path = tempdir() 24 | )) 25 | expect_interactive_message(eng_cmdstan(opts), "Compiling Stan program") 26 | opts$eval <- FALSE 27 | expect_silent(eng_cmdstan(opts)) 28 | }) 29 | 30 | test_that("register_knitr_engine works with and without override", { 31 | skip_if_not_installed("knitr") 32 | 33 | knitr::knit_engines$delete(keys = "stan") 34 | expect_false("stan" %in% names(knitr::knit_engines$get())) 35 | 36 | register_knitr_engine(override = TRUE) 37 | expect_false("cmdstan" %in% names(knitr::knit_engines$get())) 38 | expect_true("stan" %in% names(knitr::knit_engines$get())) 39 | 40 | register_knitr_engine(override = FALSE) 41 | expect_true("cmdstan" %in% names(knitr::knit_engines$get())) 42 | 43 | knitr::knit_engines$restore() 44 | }) 45 | -------------------------------------------------------------------------------- /tests/testthat/test-model-code-print.R: -------------------------------------------------------------------------------- 1 | context("model-code-print") 2 | 3 | set_cmdstan_path() 4 | stan_program <- testing_stan_file("bernoulli") 5 | mod <- testing_model("bernoulli") 6 | 7 | 8 | test_that("code() and print() methods work", { 9 | expect_known_output(mod$print(), file = test_path("answers", "model-print-output.stan")) 10 | expect_known_value(mod$code(), file = test_path("answers", "model-code-output.rds")) 11 | }) 12 | 13 | test_that("code() and print() still work if file is removed", { 14 | code <- " 15 | parameters { 16 | real y; 17 | } 18 | model { 19 | y ~ std_normal(); 20 | } 21 | " 22 | stan_file_tmp <- write_stan_file(code) 23 | code_answer <- readLines(stan_file_tmp) 24 | mod_removed_stan_file <- cmdstan_model(stan_file_tmp, compile = FALSE) 25 | file.remove(stan_file_tmp) 26 | expect_identical(mod_removed_stan_file$code(), code_answer) 27 | }) 28 | 29 | test_that("code() doesn't change when file changes (unless model is recreated)", { 30 | code_1 <- " 31 | parameters { 32 | real y; 33 | } 34 | model { 35 | y ~ std_normal(); 36 | } 37 | " 38 | code_2 <- " 39 | parameters { 40 | real x; 41 | } 42 | model { 43 | x ~ std_normal(); 44 | } 45 | " 46 | stan_file_1 <- write_stan_file(code_1) 47 | stan_file_2 <- write_stan_file(code_2) 48 | code_1_answer <- readLines(stan_file_1) 49 | code_2_answer <- readLines(stan_file_2) 50 | 51 | mod <- cmdstan_model(stan_file_1, compile = FALSE) 52 | expect_identical(mod$code(), code_1_answer) 53 | expect_identical(utils::capture.output(mod$print()), code_1_answer) 54 | 55 | # overwrite with new code, but mod$code() shouldn't change 56 | file.copy(stan_file_2, stan_file_1, overwrite = TRUE) 57 | expect_identical(mod$code(), code_1_answer) 58 | 59 | # recreate CmdStanModel object, now mod$code() should change 60 | mod <- cmdstan_model(stan_file_1, compile = FALSE) 61 | expect_identical(mod$code(), code_2_answer) 62 | expect_identical(utils::capture.output(mod$print()), code_2_answer) 63 | }) 64 | 65 | test_that("code() warns and print() errors if only exe and no Stan file", { 66 | mod_exe <- cmdstan_model(exe_file = mod$exe_file()) 67 | expect_warning( 68 | expect_null(mod_exe$code()), 69 | "'$code()' will return NULL because the 'CmdStanModel' was not created with a Stan file", 70 | fixed = TRUE 71 | ) 72 | expect_error( 73 | mod_exe$print(), 74 | "'$print()' cannot be used because the 'CmdStanModel' was not created with a Stan file.", 75 | fixed = TRUE 76 | ) 77 | }) 78 | 79 | test_that("check_syntax() errors if only exe and no Stan file", { 80 | mod_exe <- cmdstan_model(exe_file = mod$exe_file()) 81 | expect_error( 82 | mod_exe$check_syntax(), 83 | "'$check_syntax()' cannot be used because the 'CmdStanModel' was not created with a Stan file.", 84 | fixed = TRUE 85 | ) 86 | }) 87 | -------------------------------------------------------------------------------- /tests/testthat/test-model-data.R: -------------------------------------------------------------------------------- 1 | context("model-data") 2 | # see separate test-json for testing writing data to JSON 3 | 4 | set_cmdstan_path() 5 | mod <- testing_model("logistic") 6 | data_list <- testing_data("logistic") 7 | 8 | 9 | test_that("error if CmdStan < 2.22 and any 0-dimensional data objects", { 10 | ver <- cmdstan_version() 11 | .cmdstanr$VERSION <- "2.21.0" 12 | expect_sample_output( 13 | fit <- mod$sample(data = data_list) 14 | ) 15 | 16 | data_list$X <- array(1, dim = 0) 17 | expect_error( 18 | fit <- mod$sample(data = data_list), 19 | "Data includes 0-dimensional data structures." 20 | ) 21 | 22 | data_list$X <- array(1, dim = c(100, 0)) 23 | expect_error( 24 | fit <- mod$sample(data = data_list), 25 | "Data includes 0-dimensional data structures." 26 | ) 27 | 28 | data_list$X <- array(1, dim = c(100, 3, 0)) 29 | expect_error( 30 | fit <- mod$sample(data = data_list), 31 | "Data includes 0-dimensional data structures" 32 | ) 33 | .cmdstanr$VERSION <- ver 34 | }) 35 | 36 | test_that("error if data contains NA elements", { 37 | # some different ways data can contain NAs 38 | data_list1 <- data_list2 <- data_list3 <- data_list 39 | data_list1$N <- NA 40 | data_list2$y[3] <- NA 41 | data_list3$X[3, 2] <- NA 42 | 43 | expect_error(mod$sample(data = data_list1), "Variable 'N' has NA values") 44 | expect_error(mod$sample(data = data_list2), "Variable 'y' has NA values") 45 | expect_error(mod$sample(data = data_list3), "Variable 'X' has NA values") 46 | }) 47 | 48 | test_that("empty data list doesn't error if no data block", { 49 | mod <- cmdstan_model(write_stan_file(" 50 | parameters { 51 | real x; 52 | } 53 | model { 54 | x ~ normal(0, 1); 55 | } 56 | ")) 57 | 58 | expect_sample_output( 59 | fit <- mod$sample(data = list(), chains = 1) 60 | ) 61 | 62 | # would error if fitting failed 63 | expect_silent(fit$draws()) 64 | }) 65 | -------------------------------------------------------------------------------- /tests/testthat/test-model-recompile-logic.R: -------------------------------------------------------------------------------- 1 | stan_program <- cmdstan_example_file() 2 | file_that_doesnt_exist <- "placeholder_doesnt_exist" 3 | file_that_exists <- "placeholder_exists" 4 | file.create(file_that_exists) 5 | withr::defer( 6 | if (file.exists(file_that_exists)) file.remove(file_that_exists), 7 | teardown_env() 8 | ) 9 | 10 | skip_message <- "To be fixed in a later version." 11 | 12 | test_that("warning when no recompile and no info", { 13 | skip(skip_message) 14 | with_mocked_cli( 15 | compile_ret = list(), 16 | info_ret = list(status = 1), 17 | code = expect_warning({ 18 | mod <- cmdstan_model( 19 | stan_file = stan_program, 20 | exe_file = file_that_exists, 21 | compile = FALSE 22 | ) 23 | }, "Recompiling is recommended.") 24 | ) 25 | }) 26 | 27 | test_that("recompiles when force_recompile flag set", 28 | with_mocked_cli( 29 | compile_ret = list(status = 0), 30 | info_ret = list(), 31 | code = expect_mock_compile({ 32 | mod <- cmdstan_model(stan_file = stan_program, force_recompile = TRUE) 33 | }) 34 | ) 35 | ) 36 | 37 | test_that("no mismatch results in no recompile", with_mocked_cli( 38 | compile_ret = list(status = 0), 39 | info_ret = list( 40 | status = 0, 41 | stdout = " 42 | stan_version_major = 2 43 | stan_version_minor = 35 44 | stan_version_patch = 0 45 | STAN_THREADS=false 46 | STAN_MPI=false 47 | STAN_OPENCL=false 48 | STAN_NO_RANGE_CHECKS=false 49 | STAN_CPP_OPTIMS=false 50 | " 51 | ), 52 | code = expect_no_mock_compile({ 53 | mod <- cmdstan_model(stan_file = stan_program, exe_file = file_that_exists) 54 | }) 55 | )) 56 | 57 | test_that("mismatch results in recompile.", { 58 | skip(skip_message) 59 | with_mocked_cli( 60 | compile_ret = list(status = 0), 61 | info_ret = list( 62 | status = 0, 63 | stdout = " 64 | stan_version_major = 2 65 | stan_version_minor = 35 66 | stan_version_patch = 0 67 | STAN_THREADS=false 68 | STAN_MPI=false 69 | STAN_OPENCL=false 70 | STAN_NO_RANGE_CHECKS=false 71 | STAN_CPP_OPTIMS=false 72 | " 73 | ), 74 | code = expect_mock_compile({ 75 | mod <- cmdstan_model( 76 | stan_file = stan_program, 77 | exe_file = file_that_exists, 78 | cpp_options = list(stan_threads = TRUE) 79 | ) 80 | }) 81 | ) 82 | }) 83 | 84 | test_that("recompile when cpp args don't match binary", { 85 | skip(skip_message) 86 | with_mocked_cli( 87 | compile_ret = list(status = 0), 88 | info_ret = list( 89 | status = 0, 90 | stdout = " 91 | stan_version_major = 2 92 | stan_version_minor = 38 93 | stan_version_patch = 0 94 | STAN_THREADS=false 95 | STAN_MPI=false 96 | STAN_OPENCL=true 97 | STAN_NO_RANGE_CHECKS=false 98 | STAN_CPP_OPTIMS=false 99 | " 100 | ), 101 | expect_mock_compile({ 102 | mod_gq <- cmdstan_model( 103 | testing_stan_file("bernoulli_ppc"), 104 | exe_file = file_that_exists, 105 | cpp_options = list(stan_threads = TRUE) 106 | ) 107 | }) 108 | ) 109 | }) -------------------------------------------------------------------------------- /tests/testthat/test-model-sample_mpi.R: -------------------------------------------------------------------------------- 1 | context("model-sample_mpi") 2 | 3 | test_that("sample_mpi() works", { 4 | skip_if(!mpi_toolchain_present()) 5 | mpi_file <- write_stan_file(" 6 | functions { 7 | vector test(vector beta, vector theta, array[] real x, array[] int y) { 8 | return theta; 9 | } 10 | } 11 | transformed data { 12 | vector[4] a; 13 | array[4] vector[5] b = {[1,1,1,1,1]', [2,2,2,2,2]', [3,3,3,3,3]', [4,4,4,4,4]'}; 14 | array[4,4] real x; 15 | array[4,4] int y; 16 | } 17 | parameters { 18 | real beta; 19 | } 20 | model { 21 | beta ~ std_normal(); 22 | } 23 | generated quantities { 24 | vector[20] c = map_rect(test, a, b, x, y); 25 | } 26 | ") 27 | if (os_is_macos()) { 28 | tbb_cxx_type <- "clang" 29 | } else { 30 | tbb_cxx_type <- "gcc" 31 | } 32 | cpp_options = list(cxx="mpicxx", stan_mpi = TRUE, tbb_cxx_type=tbb_cxx_type) 33 | mod_mpi <- cmdstan_model(mpi_file, cpp_options = cpp_options) 34 | 35 | if (os_is_wsl()) { 36 | # Default GHA WSL install runs as root, which MPI discourages 37 | # Specify that this is safe to ignore for this test 38 | Sys.setenv("OMPI_ALLOW_RUN_AS_ROOT"=1) 39 | Sys.setenv("OMPI_ALLOW_RUN_AS_ROOT_CONFIRM"=1) 40 | Sys.setenv("WSLENV"="OMPI_ALLOW_RUN_AS_ROOT/u:OMPI_ALLOW_RUN_AS_ROOT_CONFIRM/u") 41 | } 42 | 43 | utils::capture.output( 44 | f <- mod_mpi$sample_mpi(chains = 1, mpi_args = list("n" = 1)) 45 | ) 46 | expect_equal(f$metadata()$mpi_enable, 1) 47 | expect_equal( 48 | as.numeric(posterior::subset_draws(f$draws("c"), iteration = 1)), 49 | c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4) 50 | ) 51 | }) 52 | -------------------------------------------------------------------------------- /tests/testthat/test-model-variational.R: -------------------------------------------------------------------------------- 1 | context("model-variational") 2 | 3 | set_cmdstan_path() 4 | mod <- testing_model("bernoulli") 5 | data_list <- testing_data("bernoulli") 6 | 7 | # these are all valid for variational() 8 | ok_arg_values <- list( 9 | data = data_list, 10 | refresh = 5, 11 | init = 0, 12 | seed = 123, 13 | algorithm = "meanfield", 14 | iter = 10000, 15 | grad_samples = 2, 16 | elbo_samples = 101, 17 | eta = 1.5, 18 | adapt_engaged = TRUE, 19 | adapt_iter = 51, 20 | tol_rel_obj = 0.011, 21 | eval_elbo = 101, 22 | draws = 10, 23 | save_latent_dynamics = FALSE 24 | ) 25 | 26 | # using any one of these should cause variational() to error 27 | bad_arg_values <- list( 28 | data = "NOT_A_FILE", 29 | refresh = -10, 30 | init = -10, 31 | seed = "NOT_A_SEED", 32 | algorithm = "NOT_AN_ALGORITHM", 33 | iter = -10, 34 | grad_samples = -10, 35 | elbo_samples = -10, 36 | eta = -1.5, 37 | adapt_engaged = "NOT_VALID", 38 | adapt_iter = -10, 39 | tol_rel_obj = -0.5, 40 | eval_elbo = -10, 41 | draws = -10, 42 | save_latent_dynamics = "NOT_LOGICAL" 43 | ) 44 | 45 | 46 | test_that("variational() method runs when all arguments specified validly", { 47 | # specifying all arguments validly 48 | expect_vb_output(fit1 <- do.call(mod$variational, ok_arg_values)) 49 | expect_is(fit1, "CmdStanVB") 50 | 51 | # leaving all at default (except data and seed) 52 | expect_vb_output(fit2 <- mod$variational(data = data_list, seed = 123)) 53 | expect_is(fit2, "CmdStanVB") 54 | }) 55 | 56 | test_that("variational() warns if threads specified but not enabled", { 57 | expect_warning( 58 | expect_vb_output(fit <- mod$variational(data = data_list, threads = 2, seed = 123)), 59 | "'threads' will have no effect" 60 | ) 61 | }) 62 | 63 | test_that("variational() method errors for any invalid argument before calling cmdstan", { 64 | for (nm in names(bad_arg_values)) { 65 | args <- ok_arg_values 66 | args[[nm]] <- bad_arg_values[[nm]] 67 | expect_error(do.call(mod$variational, args), regexp = nm) 68 | } 69 | }) 70 | 71 | test_that("variational() method runs when the stan file is removed", { 72 | stan_file_tmp <- tempfile(pattern = "tmp", fileext = ".stan") 73 | file.copy(testing_stan_file("bernoulli"), stan_file_tmp) 74 | mod_tmp <- cmdstan_model(stan_file_tmp) 75 | file.remove(stan_file_tmp) 76 | expect_vb_output( 77 | mod_tmp$variational(data = data_list, seed = 123) 78 | ) 79 | }) 80 | -------------------------------------------------------------------------------- /tests/testthat/test-profiling.R: -------------------------------------------------------------------------------- 1 | context("profiling") 2 | 3 | set_cmdstan_path() 4 | 5 | 6 | test_that("profiling works if profiling data is present", { 7 | mod <- testing_model("logistic_profiling") 8 | utils::capture.output( 9 | fit <- mod$sample(data = testing_data("logistic"), refresh = 0, seed = 123) 10 | ) 11 | expect_equal(length(fit$profile_files()), 4) 12 | profiles <- fit$profiles() 13 | expect_equal(length(profiles), 4) 14 | expect_equal(dim(profiles[[1]]), c(3,9)) 15 | for (name in profiles[[1]][,"name"]) { 16 | expect_true(name %in% c("udf", "priors", "glm")) 17 | } 18 | 19 | file.remove(fit$profile_files()) 20 | expect_error( 21 | fit$profile_files(), 22 | "No profile files found. The model that produced the fit did not use any profiling.", 23 | fixed = TRUE 24 | ) 25 | 26 | profiles_no_csv <- fit$profiles() 27 | expect_equal(length(profiles_no_csv), 4) 28 | expect_equal(dim(profiles_no_csv[[1]]), c(3,9)) 29 | for (name in profiles_no_csv[[1]][,"name"]) { 30 | expect_true(name %in% c("udf", "priors", "glm")) 31 | } 32 | }) 33 | 34 | test_that("profiling errors if no profiling files are present", { 35 | mod <- testing_model("logistic") 36 | suppressWarnings( 37 | utils::capture.output( 38 | fit <- mod$sample(data = testing_data("logistic"), refresh = 0, seed = 123) 39 | ) 40 | ) 41 | expect_error( 42 | fit$profile_files(), 43 | "No profile files found. The model that produced the fit did not use any profiling.", 44 | fixed = TRUE 45 | ) 46 | expect_error(fit$profiles(), 47 | "No profile files found. The model that produced the fit did not use any profiling.") 48 | expect_error(fit$save_profile_files(), 49 | "No profile files found. The model that produced the fit did not use any profiling.") 50 | }) 51 | 52 | test_that("saving profile csv output works", { 53 | mod <- testing_model("logistic_profiling") 54 | utils::capture.output( 55 | fit <- mod$sample(data = testing_data("logistic"), refresh = 0, seed = 123) 56 | ) 57 | old_paths <- fit$profile_files() 58 | checkmate::expect_file_exists(old_paths, extension = "csv") 59 | 60 | expect_message( 61 | paths <- fit$save_profile_files(tempdir(), basename = "testing-output"), 62 | paste("Moved", fit$num_procs(), "files and set internal paths") 63 | ) 64 | checkmate::expect_file_exists(paths, extension = "csv") 65 | expect_true(all(file.size(paths) > 0)) 66 | 67 | expect_false(any(file.exists(old_paths))) 68 | }) 69 | -------------------------------------------------------------------------------- /tests/testthat/testdata/Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: 3 | @$(R_HOME)/bin/Rscript --vanilla -e 'cat(cmdstanr:::get_cmdstan_flags("STANCFLAGS"))' 4 | -------------------------------------------------------------------------------- /vignettes/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | *.R 3 | articles-online-only/opencl-files/bernoulli_logit_glm_opencl 4 | articles-online-only/opencl-files/bernoulli_logit_glm 5 | -------------------------------------------------------------------------------- /vignettes/articles-online-only/opencl-files/bernoulli_logit_glm.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int k; 3 | int n; 4 | matrix[n, k] X; 5 | array[n] int y; 6 | } 7 | parameters { 8 | vector[k] beta; 9 | real alpha; 10 | } 11 | model { 12 | target += std_normal_lpdf(beta); 13 | target += std_normal_lpdf(alpha); 14 | target += bernoulli_logit_glm_lpmf(y | X, alpha, beta); 15 | } 16 | -------------------------------------------------------------------------------- /vignettes/children/_settings-knitr.Rmd: -------------------------------------------------------------------------------- 1 | ```{r settings-knitr, include=FALSE} 2 | stopifnot(require(knitr)) 3 | opts_chunk$set( 4 | # collapse = TRUE, 5 | dev = "png", 6 | dpi = 150, 7 | fig.asp = 0.618, 8 | fig.width = 5, 9 | out.width = "60%", 10 | fig.align = "center", 11 | comment = NA, 12 | eval = if (isTRUE(exists("params"))) params$EVAL else FALSE 13 | ) 14 | ``` 15 | -------------------------------------------------------------------------------- /vignettes/children/comparison-with-rstan.md: -------------------------------------------------------------------------------- 1 | ### Different ways of interfacing with Stan's C++ 2 | 3 | The RStan interface ([**rstan**](https://mc-stan.org/rstan/) package) is an 4 | in-memory interface to Stan and relies on R packages like **Rcpp** and 5 | **inline** to call C++ code from R. On the other hand, the CmdStanR interface 6 | does not directly call any C++ code from R, instead relying on the CmdStan 7 | interface behind the scenes for compilation, running algorithms, and writing 8 | results to output files. 9 | 10 | ### Advantages of RStan 11 | 12 | * Allows other developers to distribute R packages with 13 | _pre-compiled_ Stan programs (like **rstanarm**) on CRAN. (Note: As of 2023, 14 | this can mostly be achieved with CmdStanR as well. See 15 | [Developing using CmdStanR](https://mc-stan.org/cmdstanr/articles/cmdstanr-internals.html#developing-using-cmdstanr).) 16 | 17 | * Avoids use of R6 classes, which may result in more familiar syntax for many R users. 18 | 19 | * CRAN binaries available for Mac and Windows. 20 | 21 | ### Advantages of CmdStanR 22 | 23 | * Compatible with latest versions of Stan. Keeping up with Stan releases is 24 | complicated for RStan, often requiring non-trivial changes to the **rstan** 25 | package and new CRAN releases of both **rstan** and **StanHeaders**. With 26 | CmdStanR the latest improvements in Stan will be available from R immediately 27 | after updating CmdStan using `cmdstanr::install_cmdstan()`. 28 | 29 | * Running Stan via external processes results in fewer unexpected crashes, 30 | especially in RStudio. 31 | 32 | * Less memory overhead. 33 | 34 | * More permissive license. RStan uses the GPL-3 license while the license for 35 | CmdStanR is BSD-3, which is a bit more permissive and is the same license used 36 | for CmdStan and the Stan C++ source code. 37 | --------------------------------------------------------------------------------