diff --git a/R/generics.R b/R/generics.R index 656cf9a1..4377dcce 100644 --- a/R/generics.R +++ b/R/generics.R @@ -4,8 +4,8 @@ #' packages that interface with on CmdStanR. Developers can define methods on #' top of these generics to coerce objects into CmdStanR's fitted model objects. #' -#' @param object to be coerced -#' @param ... additional arguments +#' @param object The object to be coerced. +#' @param ... Additional arguments to pass to methods. #' #' @name cmdstan_coercion NULL diff --git a/_pkgdown.yml b/_pkgdown.yml index b9f89cf3..8e28d1fc 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -76,12 +76,13 @@ articles: - articles-online-only/opencl reference: - - title: "Package description" + - title: "Package overview and global options" desc: > An overview of the package and how it differs from [RStan](https://mc-stan.org/rstan). contents: - cmdstanr-package + - cmdstanr_global_options - title: "Installing and setting the path to CmdStan" desc: > Install CmdStan, assuming the necessary C++ toolchain. @@ -106,7 +107,7 @@ reference: - CmdStanGQ - CmdStanDiagnose - starts_with("fit-method") - - title: "Other tools for working with CmdStan" + - title: "Other tools" contents: - read_cmdstan_csv - write_stan_json @@ -114,6 +115,7 @@ reference: - draws_to_csv - as_mcmc.list - as_draws.CmdStanMCMC + - cmdstan_coercion - title: "Using CmdStanR with knitr and R Markdown" contents: - register_knitr_engine diff --git a/docs/404.html b/docs/404.html index 4499f2e6..becc3d45 100644 --- a/docs/404.html +++ b/docs/404.html @@ -12,7 +12,7 @@ - + @@ -39,7 +39,7 @@
@@ -147,12 +147,12 @@Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson.
+Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson, Steve Bronder.
To build the model with OpenCL support, add
cpp_options = list(stan_opencl = TRUE)
at the compilation
step.
Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson.
+Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson, Steve Bronder.
data {
int<lower=0> N;
- array[N] int<lower=0,upper=1> y;
+ array[N] int<lower=0, upper=1> y;
}
parameters {
- real<lower=0,upper=1> theta;
+ real<lower=0, upper=1> theta;
}
model {
- theta ~ beta(1,1); // uniform prior on interval 0,1
+ theta ~ beta(1, 1); // uniform prior on interval 0,1
y ~ bernoulli(theta);
}
mod$stan_file()
[1] "/Users/jgabry/.cmdstan/cmdstan-2.33.1/examples/bernoulli/bernoulli.stan"
+[1] "/Users/jgabry/.cmdstan/cmdstan-2.35.0/examples/bernoulli/bernoulli.stan"
mod$exe_file()
[1] "/Users/jgabry/.cmdstan/cmdstan-2.33.1/examples/bernoulli/bernoulli"
+[1] "/Users/jgabry/.cmdstan/cmdstan-2.35.0/examples/bernoulli/bernoulli"
Subsequently, if you create a CmdStanModel
object from
the same Stan file then compilation will be skipped (assuming the file
hasn’t changed).
mod$compile()
mod$exe_file()
-[1] "/Users/jgabry/.cmdstan/cmdstan-2.33.1/examples/bernoulli/bernoulli"
+[1] "/Users/jgabry/.cmdstan/cmdstan-2.35.0/examples/bernoulli/bernoulli"
mod_pedantic <- cmdstan_model(stan_file_pedantic, pedantic = TRUE) -Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-74281eb75594.stan', line 11, column 14: A +Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/model-9a1418059d6f.stan', line 11, column 14: A poisson distribution is given parameter lambda as a rate parameter (argument 1), but lambda was not constrained to be strictly positive. Warning: The parameter lambda has no priors. This means either no prior is @@ -288,7 +288,7 @@
argument to thePedantic checkpedantic
$check_syntax()
method.+mod_pedantic$check_syntax(pedantic = TRUE) -Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model_febb1e69c7387a0e64cf13583e078104.stan', line 11, column 14: A +Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/model_febb1e69c7387a0e64cf13583e078104.stan', line 11, column 14: A poisson distribution is given parameter lambda as a rate parameter (argument 1), but lambda was not constrained to be strictly positive. Warning: The parameter lambda has no priors. This means either no prior is @@ -300,18 +300,18 @@
Pedantic check
+[1] TRUEfile.remove(mod_pedantic$exe_file()) # delete compiled executable -[1] TRUE -rm(mod_pedantic) - -mod_pedantic <- cmdstan_model(stan_file_pedantic, compile = FALSE) -mod_pedantic$check_syntax(pedantic = TRUE) -Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model_febb1e69c7387a0e64cf13583e078104.stan', line 11, column 14: A - poisson distribution is given parameter lambda as a rate parameter - (argument 1), but lambda was not constrained to be strictly positive. -Warning: The parameter lambda has no priors. This means either no prior is - provided, or the prior(s) depend on data variables. In the later case, - this may be a false positive. -Stan program is syntactically correct
rm(mod_pedantic) + +mod_pedantic <- cmdstan_model(stan_file_pedantic, compile = FALSE) +mod_pedantic$check_syntax(pedantic = TRUE) +Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/model_febb1e69c7387a0e64cf13583e078104.stan', line 11, column 14: A + poisson distribution is given parameter lambda as a rate parameter + (argument 1), but lambda was not constrained to be strictly positive. +Warning: The parameter lambda has no priors. This means either no prior is + provided, or the prior(s) depend on data variables. In the later case, + this may be a false positive. +Stan program is syntactically correct
+diff --git a/docs/reference/fit-method-init_model_methods.html b/docs/reference/fit-method-init_model_methods.html index d27c2170..83a00fb4 100644 --- a/docs/reference/fit-method-init_model_methods.html +++ b/docs/reference/fit-method-init_model_methods.html @@ -1,11 +1,9 @@ -stan_file_variables <- write_stan_file(" data { int<lower=1> J; @@ -351,47 +351,47 @@
Stan model variables
+names(variables)
-[1] "parameters" "included_files" "data" [4] "transformed_parameters" "generated_quantities"
@@ -140,11 +140,11 @@+names(variables$data)
-[1] "J" "sigma" "y"
diff --git a/docs/reference/fit-method-grad_log_prob.html b/docs/reference/fit-method-grad_log_prob.html index e6dae257..93ab582f 100644 --- a/docs/reference/fit-method-grad_log_prob.html +++ b/docs/reference/fit-method-grad_log_prob.html @@ -1,7 +1,5 @@ -+names(variables$parameters)
-[1] "mu" "tau" "theta_raw"
@@ -203,7 +203,7 @@+names(variables$transformed_parameters)
-[1] "theta"
@@ -161,29 +161,33 @@+names(variables$generated_quantities)
character(0)
Each variable is represented as a list containing the type information (currently limited to
-real
orint
) and the number of dimensions.diff --git a/docs/reference/fit-method-constrain_variables.html b/docs/reference/fit-method-constrain_variables.html index 122d2d4b..ed77fc8a 100644 --- a/docs/reference/fit-method-constrain_variables.html +++ b/docs/reference/fit-method-constrain_variables.html @@ -1,5 +1,5 @@ -+variables$data$J
-$type [1] "int" $dimensions [1] 0
@@ -183,11 +183,11 @@+variables$data$sigma
-$type [1] "real" $dimensions [1] 1
@@ -427,20 +427,20 @@+variables$parameters$tau
-$type [1] "real" $dimensions [1] 0
+variables$transformed_parameters$theta
$type [1] "real" @@ -405,7 +405,7 @@
Executable location
+mod <- cmdstan_model(stan_file, dir = "path/to/directory/for/executable")
Named list of R objectsN, the number of data points, and
y
an integer array of observations. -@@ -149,7 +149,7 @@+mod$print()
-data { int<lower=0> N; - array[N] int<lower=0,upper=1> y; + array[N] int<lower=0, upper=1> y; } parameters { - real<lower=0,upper=1> theta; + real<lower=0, upper=1> theta; } model { - theta ~ beta(1,1); // uniform prior on interval 0,1 + theta ~ beta(1, 1); // uniform prior on interval 0,1 y ~ bernoulli(theta); }
diff --git a/docs/reference/fit-method-cmdstan_summary.html b/docs/reference/fit-method-cmdstan_summary.html index 47d7f8f7..6bbc2727 100644 --- a/docs/reference/fit-method-cmdstan_summary.html +++ b/docs/reference/fit-method-cmdstan_summary.html @@ -1,5 +1,5 @@ -+@@ -448,7 +448,7 @@# data block has 'N' and 'y' data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1)) fit <- mod$sample(data = data_list)
Named list of R objectswrite_stan_json(). This happens internally, but it is also possible to call
write_stan_json()
directly. -@@ -144,11 +144,11 @@+diff --git a/docs/reference/cmdstanr_global_options.html b/docs/reference/cmdstanr_global_options.html new file mode 100644 index 00000000..879820a9 --- /dev/null +++ b/docs/reference/cmdstanr_global_options.html @@ -0,0 +1,166 @@ + +data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1)) json_file <- tempfile(fileext = ".json") write_stan_json(data_list, json_file) @@ -465,7 +465,7 @@
: -JSON filewrite_stan_json()
+fit <- mod$sample(data = json_file)
@@ -475,7 +475,7 @@@@ -189,16 +189,16 @@R dump filerstan::stan_rdump(): -
diff --git a/docs/reference/cmdstanr-package-1.png b/docs/reference/cmdstanr-package-1.png index 0aa41c0e..aa185638 100644 Binary files a/docs/reference/cmdstanr-package-1.png and b/docs/reference/cmdstanr-package-1.png differ diff --git a/docs/reference/cmdstanr-package-2.png b/docs/reference/cmdstanr-package-2.png index 835a8a66..4cf81fa8 100644 Binary files a/docs/reference/cmdstanr-package-2.png and b/docs/reference/cmdstanr-package-2.png differ diff --git a/docs/reference/cmdstanr-package-3.png b/docs/reference/cmdstanr-package-3.png index 8d58780f..c67b37ed 100644 Binary files a/docs/reference/cmdstanr-package-3.png and b/docs/reference/cmdstanr-package-3.png differ diff --git a/docs/reference/cmdstanr-package.html b/docs/reference/cmdstanr-package.html index 072311bb..497703b7 100644 --- a/docs/reference/cmdstanr-package.html +++ b/docs/reference/cmdstanr-package.html @@ -1,5 +1,5 @@ -+@@ -185,7 +185,7 @@rdump_file <- tempfile(fileext = ".data.R") rstan::stan_rdump(names(data_list), file = rdump_file, envir = list2env(data_list)) cat(readLines(rdump_file), sep = "\n") @@ -488,31 +488,31 @@
Writing CmdStan output to CSV
Default temporary files
-diff --git a/docs/reference/cmdstan_default_path.html b/docs/reference/cmdstan_default_path.html index 01f43b73..826c04c6 100644 --- a/docs/reference/cmdstan_default_path.html +++ b/docs/reference/cmdstan_default_path.html @@ -1,5 +1,5 @@ -+When fitting a model, the default behavior is to write the output from CmdStan to CSV files in a temporary directory.
-@@ -137,11 +137,11 @@+-fit$output_files()
+[1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-202312131009-1-37e810.csv" -[2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-202312131009-2-37e810.csv" -[3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-202312131009-3-37e810.csv" -[4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-202312131009-4-37e810.csv"
[1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/bernoulli-202407021546-1-2808db.csv" +[2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/bernoulli-202407021546-2-2808db.csv" +[3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/bernoulli-202407021546-3-2808db.csv" +[4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/bernoulli-202407021546-4-2808db.csv"
These files will be lost if you end your R session or if you remove the
-fit
object and force (or wait for) garbage collection.diff --git a/docs/reference/cmdstan_coercion.html b/docs/reference/cmdstan_coercion.html new file mode 100644 index 00000000..f0de9503 --- /dev/null +++ b/docs/reference/cmdstan_coercion.html @@ -0,0 +1,164 @@ + ++files <- fit$output_files() file.exists(files)
-[1] TRUE TRUE TRUE TRUE
++ --used (Mb) gc trigger (Mb) limit (Mb) max used (Mb) -Ncells 1260600 67.4 2436292 130.2 NA 2436292 130.2 -Vcells 2212085 16.9 8388608 64.0 32768 3459642 26.4
@@ -523,10 +523,10 @@++used (Mb) gc trigger (Mb) limit (Mb) max used (Mb) +Ncells 1155803 61.8 2350794 125.6 NA 1542121 82.4 +Vcells 2048173 15.7 8388608 64.0 32768 2851724 21.8
file.exists(files)
[1] FALSE FALSE FALSE FALSE
Non-temporary files
+-# see ?save_output_files for info on optional arguments fit$save_output_files(dir = "path/to/directory")
diff --git a/docs/index.html b/docs/index.html index 54947fb0..de070a6d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -12,18 +12,13 @@ - + - + - +++[1] 1.11 0.80 1.05 0.95fit <- mod$sample( data = data_list, output_dir = "path/to/directory" @@ -546,7 +546,7 @@
object, notice how theLazy CSV readingfit
Private
slotdraws_
isNULL
, indicating that the CSV files haven’t yet been read into R. -+str(fit)
Classes 'CmdStanMCMC', 'CmdStanFit', 'R6' <CmdStanMCMC> Inherits from: <CmdStanFit> @@ -555,6 +555,7 @@
Lazy CSV readingLazy CSV readingLazy CSV readingLazy CSV readingLazy CSV reading
After we call a method that requires the draws then if we reexamine the structure of the object we will see that the
-draws_
slot inPrivate
is no longer empty.+draws <- fit$draws() # force CSVs to be read into R str(fit)
Classes 'CmdStanMCMC', 'CmdStanFit', 'R6' <CmdStanMCMC> @@ -617,6 +621,7 @@
Lazy CSV readingLazy CSV readingLazy CSV readingLazy CSV readingLazy CSV reading
For models with many parameters, transformed parameters, or generated @@ -678,19 +686,19 @@
read_cmdstan_csv()read_cmdstan_csv() function is used to read the CmdStan CSV files into R. This function is exposed to users, so you can also call it directly. -
+# see ?read_cmdstan_csv for info on optional arguments controlling # what information is read in csv_contents <- read_cmdstan_csv(fit$output_files()) str(csv_contents)
List of 8 - $ metadata :List of 40 + $ metadata :List of 42 ..$ stan_version_major : num 2 - ..$ stan_version_minor : num 33 + ..$ stan_version_minor : num 35 ..$ stan_version_patch : num 0 - ..$ start_datetime : chr "2023-12-13 17:09:13 UTC" + ..$ start_datetime : chr "2024-07-02 21:46:33 UTC" ..$ method : chr "sample" - ..$ save_warmup : num 0 + ..$ save_warmup : int 0 ..$ thin : num 1 ..$ gamma : num 0.05 ..$ kappa : num 0.75 @@ -698,6 +706,7 @@
read_cmdstan_csv()read_cmdstan_csv()read_cmdstan_csv()read_cmdstan_csv()as_cmdstan_fit()
If you need to manually create fitted model objects from CmdStan CSV files use
-as_cmdstan_fit()
.+fit2 <- as_cmdstan_fit(fit$output_files())
This is pointless in our case since we have the original
fit
object, but this function can be used to create fitted @@ -783,32 +793,32 @@Saving and
CmdStanR does not yet provide a special method for processing these files but they can be read into R using R’s standard CSV reading functions.
--fit <- mod$sample(data = data_list, save_latent_dynamics = TRUE)
++fit <- mod$sample(data = data_list, save_latent_dynamics = TRUE)
-fit$latent_dynamics_files()
-[1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-diagnostic-202312131009-1-425883.csv" -[2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-diagnostic-202312131009-2-425883.csv" -[3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-diagnostic-202312131009-3-425883.csv" -[4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-diagnostic-202312131009-4-425883.csv"
-++[1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/bernoulli-diagnostic-202407021546-1-54a823.csv" +[2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/bernoulli-diagnostic-202407021546-2-54a823.csv" +[3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/bernoulli-diagnostic-202407021546-3-54a823.csv" +[4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/bernoulli-diagnostic-202407021546-4-54a823.csv"
# read one of the files in x <- utils::read.csv(fit$latent_dynamics_files()[1], comment.char = "#") head(x)
+1 -6.77025 0.987691 0.980824 2 3 0 +2 -7.20991 0.932461 0.980824 2 3 0 +3 -7.66830 0.933065 0.980824 1 1 0 +4 -7.37078 1.000000 0.980824 1 1 0 +5 -7.59961 0.948892 0.980824 2 3 0 +6 -7.95801 0.940559 0.980824 2 3 0 + energy__ theta p_theta g_theta +1 7.08007 -0.959636 -1.160790 0.323414 +2 7.21726 -1.779880 0.178790 -1.268180 +3 7.67261 -2.089350 -0.136971 -1.678370 +4 7.66302 -1.898700 -1.127370 -1.436940 +5 8.20617 -2.047820 -1.624180 -1.628720 +6 8.67205 -2.252980 1.762220 -1.858880lp__ accept_stat__ stepsize__ treedepth__ n_leapfrog__ divergent__ -1 -9.73111 1.000000 0.932232 2 3 0 -2 -6.75346 1.000000 0.932232 1 3 0 -3 -8.48107 0.665681 0.932232 1 3 0 -4 -7.74234 1.000000 0.932232 1 1 0 -5 -6.82333 1.000000 0.932232 2 3 0 -6 -6.82333 0.784555 0.932232 1 3 0 - energy__ theta p_theta g_theta -1 10.97150 0.3939970 -2.155610 4.166930 -2 9.05119 -1.0295100 -2.933880 0.158161 -3 8.61284 0.0530292 0.702591 3.159050 -4 8.45793 -0.2148440 1.637290 2.357940 -5 7.76204 -1.3633100 1.875250 -0.555575 -6 8.04989 -1.3633100 -2.143570 -0.555575
The column
lp__
is also provided viafit$draws()
, and the columnsaccept_stat__
,stepsize__
,treedepth__
, @@ -816,15 +826,15 @@Saving and
energy__
are also provided byfit$sampler_diagnostics()
, but there are several columns unique to the latent dynamics file. -+ -+theta p_theta g_theta -1 0.3939970 -2.155610 4.166930 -2 -1.0295100 -2.933880 0.158161 -3 0.0530292 0.702591 3.159050 -4 -0.2148440 1.637290 2.357940 -5 -1.3633100 1.875250 -0.555575 -6 -1.3633100 -2.143570 -0.555575
theta p_theta g_theta +1 -0.959636 -1.160790 0.323414 +2 -1.779880 0.178790 -1.268180 +3 -2.089350 -0.136971 -1.678370 +4 -1.898700 -1.127370 -1.436940 +5 -2.047820 -1.624180 -1.628720 +6 -2.252980 1.762220 -1.858880
Our model has a single parameter
theta
and the three columns above correspond totheta
in the unconstrained space (theta
on the constrained @@ -867,22 +877,24 @@Troubleshooting and debugging to
TRUE
. -+options("cmdstanr_verbose"=TRUE) mod <- cmdstan_model(stan_file, force_recompile = TRUE)
-Running make \ - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467 \ + /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/model-9a14560da326 \ "STANCFLAGS += --name='bernoulli_model'" --- Translating Stan model to C++ code --- -bin/stanc --name='bernoulli_model' --o=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467.hpp /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467.stan +bin/stanc --name='bernoulli_model' --o=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/model-9a14560da326.hpp /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/model-9a14560da326.stan + +--- Compiling C++ code --- +clang++ -Wno-deprecated-declarations -Wno-deprecated-declarations -std=c++17 -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.84.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DBOOST_DISABLE_ASSERTS -c -include-pch stan/src/stan/model/model_header.hpp.gch/model_header_15_0.hpp.gch -x c++ -o /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/model-9a14560da326.o /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/model-9a14560da326.hpp ---- Compiling, linking C++ code --- -clang++ -Wno-deprecated-declarations -Wno-deprecated-declarations -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DBOOST_DISABLE_ASSERTS -c -include-pch stan/src/stan/model/model_header_13_0.hpp.gch -x c++ -o /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467.o /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467.hpp -clang++ -Wno-deprecated-declarations -Wno-deprecated-declarations -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DBOOST_DISABLE_ASSERTS -Wl,-L,"/Users/jgabry/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/Users/jgabry/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/lib/tbb" /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467.o src/cmdstan/main.o -Wl,-L,"/Users/jgabry/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/Users/jgabry/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/lib/tbb" stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_kinsol.a stan/lib/stan_math/lib/tbb/libtbb.dylib stan/lib/stan_math/lib/tbb/libtbbmalloc.dylib stan/lib/stan_math/lib/tbb/libtbbmalloc_proxy.dylib -o /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467 -rm -f /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/model-742871ce8467.o
diff --git a/docs/articles/cmdstanr.html b/docs/articles/cmdstanr.html index cac1ca1b..06f9ad3c 100644 --- a/docs/articles/cmdstanr.html +++ b/docs/articles/cmdstanr.html @@ -12,7 +12,7 @@ - + @@ -26,7 +26,7 @@ - ++--- Linking model --- +clang++ -Wno-deprecated-declarations -Wno-deprecated-declarations -std=c++17 -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.84.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DBOOST_DISABLE_ASSERTS -Wl,-L,"/Users/jgabry/.cmdstan/cmdstan-2.35.0/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/Users/jgabry/.cmdstan/cmdstan-2.35.0/stan/lib/stan_math/lib/tbb" /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/model-9a14560da326.o src/cmdstan/main.o -ltbb stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_kinsol.a stan/lib/stan_math/lib/tbb/libtbb.dylib stan/lib/stan_math/lib/tbb/libtbbmalloc.dylib stan/lib/stan_math/lib/tbb/libtbbmalloc_proxy.dylib -o /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/model-9a14560da326 +rm /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/model-9a14560da326.hpp /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/model-9a14560da326.o+fit <- mod$sample( data = data_list, chains = 1, @@ -891,28 +903,29 @@
Troubleshooting and debugging)
Running MCMC with 1 chain... -Running ./bernoulli 'id=1' random 'seed=90958316' data \ - 'file=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/standata-742874baeb02.json' \ +Running ./bernoulli 'id=1' random 'seed=707272264' data \ + 'file=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/standata-9a144d3dc244.json' \ output \ - 'file=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-202312131009-1-4db120.csv' \ - 'profile_file=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpXOnqh8/bernoulli-profile-202312131009-1-1c1f83.csv' \ + 'file=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/bernoulli-202407021546-1-38ac27.csv' \ + 'profile_file=/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmp2hQnhs/bernoulli-profile-202407021546-1-910909.csv' \ 'method=sample' 'num_samples=100' 'num_warmup=100' 'save_warmup=0' \ 'algorithm=hmc' 'engine=nuts' adapt 'engaged=1' Chain 1 method = sample (Default) Chain 1 sample Chain 1 num_samples = 100 Chain 1 num_warmup = 100 -Chain 1 save_warmup = 0 (Default) +Chain 1 save_warmup = false (Default) Chain 1 thin = 1 (Default) Chain 1 adapt -Chain 1 engaged = 1 (Default) -Chain 1 gamma = 0.050000000000000003 (Default) -Chain 1 delta = 0.80000000000000004 (Default) +Chain 1 engaged = true (Default) +Chain 1 gamma = 0.05 (Default) +Chain 1 delta = 0.8 (Default) Chain 1 kappa = 0.75 (Default) Chain 1 t0 = 10 (Default) Chain 1 init_buffer = 75 (Default) Chain 1 term_buffer = 50 (Default) Chain 1 window = 25 (Default) +Chain 1 save_metric = false (Default) Chain 1 algorithm = hmc (Default) Chain 1 hmc Chain 1 engine = nuts (Default) @@ -925,19 +938,20 @@
Troubleshooting and debuggingTroubleshooting and debuggingTroubleshooting and debugging -
Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson.
+Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson, Steve Bronder.
- +@@ -49,7 +49,7 @@ - + @@ -64,7 +64,7 @@ Other Packages - + @@ -125,7 +125,7 @@ @@ -133,15 +133,15 @@- - + +Getting started with CmdStanR
Jonah Gabry, Rok Češnovar, and Andrew Johnson
- - + + Source:vignettes/cmdstanr.Rmd
cmdstanr.Rmd
+install.packages("cmdstanr", repos = c('https://stan-dev.r-universe.dev', getOption("repos")))Introduction
@@ -154,7 +154,7 @@Introduction
# we recommend running this is a fresh R session or restarting your current session -install.packages("cmdstanr", repos = c("https://stan-dev.r-universe.dev", getOption("repos")))
We can now load the package like any other R package. We’ll also load the bayesplot and posterior packages to use later in examples.
@@ -214,10 +214,10 @@Installing CmdStancmdstan_version(): -
+[1] "/Users/jgabry/.cmdstan/cmdstan-2.33.1"
-[1] "/Users/jgabry/.cmdstan/cmdstan-2.35.0"
+[1] "2.33.1"
[1] "2.35.0"
Compiling a model @@ -241,20 +241,20 @@
Compiling a modelmod$print()
data { int<lower=0> N; - array[N] int<lower=0,upper=1> y; + array[N] int<lower=0, upper=1> y; } parameters { - real<lower=0,upper=1> theta; + real<lower=0, upper=1> theta; } model { - theta ~ beta(1,1); // uniform prior on interval 0,1 + theta ~ beta(1, 1); // uniform prior on interval 0,1 y ~ bernoulli(theta); }
The path to the compiled executable is returned by the
$exe_file()
method:-mod$exe_file()
+[1] "/Users/jgabry/.cmdstan/cmdstan-2.33.1/examples/bernoulli/bernoulli"
[1] "/Users/jgabry/.cmdstan/cmdstan-2.35.0/examples/bernoulli/bernoulli"
Running MCMC @@ -277,30 +277,30 @@
Running MCMC)
Running MCMC with 4 parallel chains... -Chain 1 Iteration: 1 / 2000 [ 0%] (Warmup) -Chain 1 Iteration: 500 / 2000 [ 25%] (Warmup) -Chain 1 Iteration: 1000 / 2000 [ 50%] (Warmup) -Chain 1 Iteration: 1001 / 2000 [ 50%] (Sampling) -Chain 1 Iteration: 1500 / 2000 [ 75%] (Sampling) -Chain 1 Iteration: 2000 / 2000 [100%] (Sampling) -Chain 2 Iteration: 1 / 2000 [ 0%] (Warmup) -Chain 2 Iteration: 500 / 2000 [ 25%] (Warmup) -Chain 2 Iteration: 1000 / 2000 [ 50%] (Warmup) -Chain 2 Iteration: 1001 / 2000 [ 50%] (Sampling) -Chain 2 Iteration: 1500 / 2000 [ 75%] (Sampling) -Chain 2 Iteration: 2000 / 2000 [100%] (Sampling) -Chain 3 Iteration: 1 / 2000 [ 0%] (Warmup) -Chain 3 Iteration: 500 / 2000 [ 25%] (Warmup) -Chain 3 Iteration: 1000 / 2000 [ 50%] (Warmup) -Chain 3 Iteration: 1001 / 2000 [ 50%] (Sampling) -Chain 3 Iteration: 1500 / 2000 [ 75%] (Sampling) -Chain 3 Iteration: 2000 / 2000 [100%] (Sampling) -Chain 4 Iteration: 1 / 2000 [ 0%] (Warmup) -Chain 4 Iteration: 500 / 2000 [ 25%] (Warmup) -Chain 4 Iteration: 1000 / 2000 [ 50%] (Warmup) -Chain 4 Iteration: 1001 / 2000 [ 50%] (Sampling) -Chain 4 Iteration: 1500 / 2000 [ 75%] (Sampling) -Chain 4 Iteration: 2000 / 2000 [100%] (Sampling) +Chain 1 Iteration: 1 / 2000 [ 0%] (Warmup) +Chain 1 Iteration: 500 / 2000 [ 25%] (Warmup) +Chain 1 Iteration: 1000 / 2000 [ 50%] (Warmup) +Chain 1 Iteration: 1001 / 2000 [ 50%] (Sampling) +Chain 1 Iteration: 1500 / 2000 [ 75%] (Sampling) +Chain 1 Iteration: 2000 / 2000 [100%] (Sampling) +Chain 2 Iteration: 1 / 2000 [ 0%] (Warmup) +Chain 2 Iteration: 500 / 2000 [ 25%] (Warmup) +Chain 2 Iteration: 1000 / 2000 [ 50%] (Warmup) +Chain 2 Iteration: 1001 / 2000 [ 50%] (Sampling) +Chain 2 Iteration: 1500 / 2000 [ 75%] (Sampling) +Chain 2 Iteration: 2000 / 2000 [100%] (Sampling) +Chain 3 Iteration: 1 / 2000 [ 0%] (Warmup) +Chain 3 Iteration: 500 / 2000 [ 25%] (Warmup) +Chain 3 Iteration: 1000 / 2000 [ 50%] (Warmup) +Chain 3 Iteration: 1001 / 2000 [ 50%] (Sampling) +Chain 3 Iteration: 1500 / 2000 [ 75%] (Sampling) +Chain 3 Iteration: 2000 / 2000 [100%] (Sampling) +Chain 4 Iteration: 1 / 2000 [ 0%] (Warmup) +Chain 4 Iteration: 500 / 2000 [ 25%] (Warmup) +Chain 4 Iteration: 1000 / 2000 [ 50%] (Warmup) +Chain 4 Iteration: 1001 / 2000 [ 50%] (Sampling) +Chain 4 Iteration: 1500 / 2000 [ 75%] (Sampling) +Chain 4 Iteration: 2000 / 2000 [100%] (Sampling) Chain 1 finished in 0.0 seconds. Chain 2 finished in 0.0 seconds. Chain 3 finished in 0.0 seconds. @@ -347,17 +347,17 @@
Summaries from the posterior packa posterior::default_summary_measures(), extra_quantiles = ~posterior::quantile2(., probs = c(.0275, .975)) )
+variable mean median sd mad q5 q95 rhat ess_bulk ess_tail -1 lp__ -7.27 -7.00 0.71 0.34 -8.70 -6.75 1 1852 2114 -2 theta 0.25 0.23 0.12 0.12 0.08 0.47 1 1611 1678
variable mean median sd mad q5 q95 rhat ess_bulk ess_tail +1 lp__ -7.31 -7.01 0.81 0.34 -8.871 -6.75 1 1404 1631 +2 theta 0.26 0.24 0.13 0.13 0.079 0.49 1 1130 1314
+1 theta 0.26 0.13 +2 lp__ -7.31 0.81variable mean sd -1 theta 0.25 0.12 -2 lp__ -7.27 0.71
-variable pr_lt_half -1 theta 0.97
+1 theta 0.96 +variable mean median sd mad q5 q95 q2.75 q97.5 -1 lp__ -7.27 -7.00 0.71 0.34 -8.70 -6.75 -9.165 -6.75 -2 theta 0.25 0.23 0.12 0.12 0.08 0.47 0.065 0.52
variable mean median sd mad q5 q95 q2.75 q97.5 +1 lp__ -7.31 -7.01 0.81 0.34 -8.871 -6.75 -9.443 -6.75 +2 theta 0.26 0.24 0.13 0.13 0.079 0.49 0.063 0.54
-CmdStan’s stansummary utility @@ -384,7 +384,7 @@
Extracting draws# iterations x chains x variables draws_arr <- fit$draws() # or format="array" str(draws_arr)
'draws_array' num [1:1000, 1:4, 1:2] -6.78 -6.9 -7.05 -6.85 -6.75 ... +
'draws_array' num [1:1000, 1:4, 1:2] -7.01 -7.89 -7.41 -6.75 -6.91 ... - attr(*, "dimnames")=List of 3 ..$ iteration: chr [1:1000] "1" "2" "3" "4" ... ..$ chain : chr [1:4] "1" "2" "3" "4" @@ -394,8 +394,8 @@
Extracting drawsdraws_df <- fit$draws(format = "df") str(draws_df)
@@ -403,16 +403,16 @@draws_df [4,000 × 5] (S3: draws_df/draws/tbl_df/tbl/data.frame) - $ lp__ : num [1:4000] -6.78 -6.9 -7.05 -6.85 -6.75 ... - $ theta : num [1:4000] 0.284 0.186 0.162 0.196 0.252 ... + $ lp__ : num [1:4000] -7.01 -7.89 -7.41 -6.75 -6.91 ... + $ theta : num [1:4000] 0.168 0.461 0.409 0.249 0.185 ... $ .chain : int [1:4000] 1 1 1 1 1 1 1 1 1 1 ... $ .iteration: int [1:4000] 1 2 3 4 5 6 7 8 9 10 ... $ .draw : int [1:4000] 1 2 3 4 5 6 7 8 9 10 ...
Extracting drawsprint(draws_df)
# A draws_df: 1000 iterations, 4 chains, and 2 variables lp__ theta -1 -6.8 0.28 -2 -6.9 0.19 -3 -7.0 0.16 -4 -6.9 0.20 -5 -6.7 0.25 -6 -7.1 0.36 -7 -9.0 0.55 -8 -7.2 0.15 -9 -6.8 0.23 -10 -7.5 0.42 +1 -7.0 0.17 +2 -7.9 0.46 +3 -7.4 0.41 +4 -6.7 0.25 +5 -6.9 0.18 +6 -6.9 0.33 +7 -7.2 0.15 +8 -6.8 0.29 +9 -6.8 0.24 +10 -6.8 0.24 # ... with 3990 more draws # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
To convert an existing draws object to a different format use the @@ -426,6 +426,10 @@
Extracting draws$draws() method, but in most cases the speed difference will be minor. +
The vignette Working with +Posteriors has more details on posterior draws, including how to +reproduce the structured output RStan users are accustomed to getting +from
rstan::extract()
.Plotting draws @@ -453,7 +457,7 @@
Extracting di
-# this is a draws_array object from the posterior package str(fit$sampler_diagnostics())
'draws_array' num [1:1000, 1:4, 1:6] 1 2 2 2 2 1 1 1 1 2 ... +
'draws_array' num [1:1000, 1:4, 1:6] 2 1 2 2 2 1 2 1 2 1 ... - attr(*, "dimnames")=List of 3 ..$ iteration: chr [1:1000] "1" "2" "3" "4" ... ..$ chain : chr [1:4] "1" "2" "3" "4" @@ -462,12 +466,12 @@
Extracting di
# this is a draws_df object from the posterior package str(fit$sampler_diagnostics(format = "df"))
@@ -487,7 +491,7 @@draws_df [4,000 × 9] (S3: draws_df/draws/tbl_df/tbl/data.frame) - $ treedepth__ : num [1:4000] 1 2 2 2 2 1 1 1 1 2 ... + $ treedepth__ : num [1:4000] 2 1 2 2 2 1 2 1 2 1 ... $ divergent__ : num [1:4000] 0 0 0 0 0 0 0 0 0 0 ... - $ energy__ : num [1:4000] 6.79 6.9 7.06 7 7.9 ... - $ accept_stat__: num [1:4000] 0.996 0.984 0.988 1 0.835 ... - $ stepsize__ : num [1:4000] 1.03 1.03 1.03 1.03 1.03 ... - $ n_leapfrog__ : num [1:4000] 1 3 3 3 3 3 1 3 3 3 ... + $ energy__ : num [1:4000] 8.95 8.77 7.87 7.64 6.93 ... + $ accept_stat__: num [1:4000] 0.688 0.811 1 0.966 0.976 ... + $ stepsize__ : num [1:4000] 0.905 0.905 0.905 0.905 0.905 ... + $ n_leapfrog__ : num [1:4000] 3 3 3 3 3 3 3 3 3 3 ... $ .chain : int [1:4000] 1 1 1 1 1 1 1 1 1 1 ... $ .iteration : int [1:4000] 1 2 3 4 5 6 7 8 9 10 ... $ .draw : int [1:4000] 1 2 3 4 5 6 7 8 9 10 ...
Sampler diagnostic warnings a [1] 0 0 0 0 $ebfmi -[1] 1.0 1.3 1.1 1.2
We see the number of divergences for each of the four chains, the number of times the maximum treedepth was hit for each chain, and the E-BFMI for each chain.
@@ -496,34 +500,33 @@Sampler diagnostic warnings a suffers from divergences.
-fit_with_warning <- cmdstanr_example("schools")
-Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
-Chain 4 Exception: normal_lpdf: Scale parameter is 0, but must be positive! (in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmpl3ZPjU/model-76ac71f3c1fe.stan', line 14, column 2 to column 41)
-Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
-Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
-Chain 4
Warning: 149 of 4000 (4.0%) transitions ended with a divergence. +
+Warning: 374 of 4000 (9.0%) transitions ended with a divergence. +See https://mc-stan.org/misc/warnings for details.
Warning: 1 of 4 chains had an E-BFMI less than 0.3. See https://mc-stan.org/misc/warnings for details.
After fitting there is a warning about divergences. We can also regenerate this warning message later using
-fit$diagnostic_summary()
.+-diagnostics <- fit_with_warning$diagnostic_summary()
Warning: 149 of 4000 (4.0%) transitions ended with a divergence. +
+Warning: 374 of 4000 (9.0%) transitions ended with a divergence. +See https://mc-stan.org/misc/warnings for details.
-Warning: 1 of 4 chains had an E-BFMI less than 0.3. See https://mc-stan.org/misc/warnings for details.
@@ -562,26 +565,26 @@+print(diagnostics)
-$num_divergent -[1] 11 54 32 52 +[1] 269 29 11 65 $num_max_treedepth [1] 0 0 0 0 $ebfmi -[1] 0.40 0.36 0.37 0.30
+[1] 0.15 0.34 0.38 0.37+-# number of divergences reported in warning is the sum of the per chain values sum(diagnostics$num_divergent)
+[1] 149
[1] 374
CmdStan’s diagnose utility @@ -544,7 +547,7 @@
Create a
stanfit
object+stanfit <- rstan::read_stan_csv(fit$output_files())
Optimization
$optimize()
. -++-fit_mle <- mod$optimize(data = data_list, seed = 123)
Initial log joint probability = -9.51104 - Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes - 6 -5.00402 0.000103557 2.55661e-07 1 1 9 -Optimization terminated normally: - Convergence detected: relative gradient magnitude is below tolerance +
-Initial log joint probability = -16.144 + Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes + 6 -5.00402 0.000246518 8.73164e-07 1 1 9 +Optimization terminated normally: + Convergence detected: relative gradient magnitude is below tolerance Finished in 0.2 seconds.
+fit_mle$print() # includes lp__ (log prob calculated by Stan program)
-variable estimate lp__ -5.00 theta 0.20
-+fit_mle$mle("theta")
theta 0.2
Here’s a plot comparing the penalized MLE to the posterior distribution of
-theta
.@@ -324,7 +324,7 @@+ @@ -592,18 +595,18 @@OptimizationMaximum Likelihood Estimation section of the CmdStan User’s Guide for more details. -
+-fit_map <- mod$optimize( data = data_list, jacobian = TRUE, seed = 123 )
+Initial log joint probability = -11.0088 - Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes - 5 -6.74802 0.000938344 1.39149e-05 1 1 8 -Optimization terminated normally: - Convergence detected: relative gradient magnitude is below tolerance -Finished in 0.1 seconds.
Initial log joint probability = -18.2733 + Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes + 5 -6.74802 0.000708195 1.43227e-05 1 1 8 +Optimization terminated normally: + Convergence detected: relative gradient magnitude is below tolerance +Finished in 0.2 seconds.
@@ -249,12 +249,12 @@Laplace Approximation @@ -621,7 +624,7 @@
Laplace Approximationmode argument. If
mode
is omitted then optimization will be run internally before taking draws from the normal approximation. -@@ -364,12 +390,12 @@+-fit_laplace <- mod$laplace( mode = fit_map, draws = 4000, @@ -629,19 +632,19 @@
Laplace Approximation seed = 123, refresh = 1000 )
Calculating Hessian -Calculating inverse of Cholesky factor -Generating draws -iteration: 0 -iteration: 1000 -iteration: 2000 -iteration: 3000 +
-Calculating Hessian +Calculating inverse of Cholesky factor +Generating draws +iteration: 0 +iteration: 1000 +iteration: 2000 +iteration: 3000 Finished in 0.1 seconds.
+fit_laplace$print("theta")
-variable mean median sd mad q5 q95 - theta 0.27 0.25 0.12 0.12 0.10 0.50
@@ -652,40 +655,40 @@+ theta 0.27 0.25 0.12 0.12 0.10 0.51+mcmc_hist(fit_laplace$draws("theta"), binwidth = 0.025)
Variational (ADVI)
$variational()
method. For details on the ADVI algorithm see the CmdStan User’s Guide. -+-fit_vb <- mod$variational( data = data_list, seed = 123, draws = 4000 )
------------------------------------------------------------ -EXPERIMENTAL ALGORITHM: - This procedure has not been thoroughly tested and may be unstable - or buggy. The interface is subject to change. ------------------------------------------------------------- -Gradient evaluation took 5e-06 seconds -1000 transitions using 10 leapfrog steps per transition would take 0.05 seconds. -Adjust your expectations accordingly! -Begin eta adaptation. -Iteration: 1 / 250 [ 0%] (Adaptation) -Iteration: 50 / 250 [ 20%] (Adaptation) -Iteration: 100 / 250 [ 40%] (Adaptation) -Iteration: 150 / 250 [ 60%] (Adaptation) -Iteration: 200 / 250 [ 80%] (Adaptation) -Success! Found best value [eta = 1] earlier than expected. -Begin stochastic gradient ascent. - iter ELBO delta_ELBO_mean delta_ELBO_med notes - 100 -6.262 1.000 1.000 - 200 -6.263 0.500 1.000 - 300 -6.307 0.336 0.007 MEDIAN ELBO CONVERGED -Drawing a sample of size 4000 from the approximate posterior... -COMPLETED. +
------------------------------------------------------------- +EXPERIMENTAL ALGORITHM: + This procedure has not been thoroughly tested and may be unstable + or buggy. The interface is subject to change. +------------------------------------------------------------ +Gradient evaluation took 1.3e-05 seconds +1000 transitions using 10 leapfrog steps per transition would take 0.13 seconds. +Adjust your expectations accordingly! +Begin eta adaptation. +Iteration: 1 / 250 [ 0%] (Adaptation) +Iteration: 50 / 250 [ 20%] (Adaptation) +Iteration: 100 / 250 [ 40%] (Adaptation) +Iteration: 150 / 250 [ 60%] (Adaptation) +Iteration: 200 / 250 [ 80%] (Adaptation) +Success! Found best value [eta = 1] earlier than expected. +Begin stochastic gradient ascent. + iter ELBO delta_ELBO_mean delta_ELBO_med notes + 100 -6.164 1.000 1.000 + 200 -6.225 0.505 1.000 + 300 -6.186 0.339 0.010 MEDIAN ELBO CONVERGED +Drawing a sample of size 4000 from the approximate posterior... +COMPLETED. Finished in 0.1 seconds.
+fit_vb$print("theta")
-variable mean median sd mad q5 q95 - theta 0.27 0.25 0.12 0.12 0.10 0.49
@@ -697,31 +700,31 @@+ theta 0.26 0.24 0.11 0.11 0.11 0.46+mcmc_hist(fit_vb$draws("theta"), binwidth = 0.025)
Variational (Pathfinder)CmdStan User’s Guide. Pathfinder is run using the
$pathfinder()
method. -+-fit_pf <- mod$pathfinder( data = data_list, seed = 123, draws = 4000 )
Path [1] :Initial log joint density = -11.008832 -Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes - 5 -6.748e+00 9.383e-04 1.391e-05 1.000e+00 1.000e+00 126 -6.264e+00 -6.264e+00 -Path [1] :Best Iter: [3] ELBO (-6.195408) evaluations: (126) -Path [2] :Initial log joint density = -7.318450 -Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes - 4 -6.748e+00 5.414e-03 1.618e-04 1.000e+00 1.000e+00 101 -6.251e+00 -6.251e+00 -Path [2] :Best Iter: [3] ELBO (-6.229174) evaluations: (101) -Path [3] :Initial log joint density = -12.374612 -Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes - 5 -6.748e+00 1.419e-03 2.837e-05 1.000e+00 1.000e+00 126 -6.199e+00 -6.199e+00 -Path [3] :Best Iter: [5] ELBO (-6.199185) evaluations: (126) -Path [4] :Initial log joint density = -13.009824 -Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes - 5 -6.748e+00 1.677e-03 3.885e-05 1.000e+00 1.000e+00 126 -6.173e+00 -6.173e+00 -Path [4] :Best Iter: [5] ELBO (-6.172860) evaluations: (126) -Total log probability function evaluations:4379 +
-Path [1] :Initial log joint density = -18.273334 +Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes + 5 -6.748e+00 7.082e-04 1.432e-05 1.000e+00 1.000e+00 126 -6.145e+00 -6.145e+00 +Path [1] :Best Iter: [5] ELBO (-6.145070) evaluations: (126) +Path [2] :Initial log joint density = -19.192715 +Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes + 5 -6.748e+00 2.015e-04 2.228e-06 1.000e+00 1.000e+00 126 -6.223e+00 -6.223e+00 +Path [2] :Best Iter: [2] ELBO (-6.170358) evaluations: (126) +Path [3] :Initial log joint density = -6.774820 +Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes + 4 -6.748e+00 1.137e-04 2.596e-07 1.000e+00 1.000e+00 101 -6.178e+00 -6.178e+00 +Path [3] :Best Iter: [4] ELBO (-6.177909) evaluations: (101) +Path [4] :Initial log joint density = -7.949193 +Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes + 5 -6.748e+00 2.145e-04 1.301e-06 1.000e+00 1.000e+00 126 -6.197e+00 -6.197e+00 +Path [4] :Best Iter: [5] ELBO (-6.197118) evaluations: (126) +Total log probability function evaluations:4379 Finished in 0.1 seconds.
-+fit_pf$print("theta")
@@ -729,22 +732,22 @@variable mean median sd mad q5 q95 theta 0.25 0.24 0.12 0.12 0.08 0.47
Variational (Pathfinder) -
+-mcmc_hist(fit_pf$draws("theta"), binwidth = 0.025) + ggplot2::labs(subtitle = "Approximate posterior from pathfinder") + ggplot2::xlim(0, 1)
-+-mcmc_hist(fit_vb$draws("theta"), binwidth = 0.025) + ggplot2::labs(subtitle = "Approximate posterior from variational") + ggplot2::xlim(0, 1)
-+-mcmc_hist(fit_laplace$draws("theta"), binwidth = 0.025) + ggplot2::labs(subtitle = "Approximate posterior from Laplace") + ggplot2::xlim(0, 1)
@@ -147,36 +147,36 @@+@@ -768,7 +771,7 @@mcmc_hist(fit$draws("theta"), binwidth = 0.025) + ggplot2::labs(subtitle = "Posterior from MCMC") + ggplot2::xlim(0, 1)
Saving fitted model objects
+diff --git a/docs/articles/posterior.html b/docs/articles/posterior.html index bf1156f8..a614e889 100644 --- a/docs/articles/posterior.html +++ b/docs/articles/posterior.html @@ -12,7 +12,7 @@ - + @@ -40,7 +40,7 @@fit$save_object(file = "fit.RDS") # can be read back in using readRDS @@ -780,7 +783,7 @@
Saving fitted model objects
$save_object()
and replacesaveRDS
with the much fasterqsave()
function from theqs
package. -@@ -128,11 +128,11 @@++ - - + diff --git a/docs/articles/cmdstanr_files/figure-html/laplace-1.png b/docs/articles/cmdstanr_files/figure-html/laplace-1.png index ed19be52..975e63a2 100644 Binary files a/docs/articles/cmdstanr_files/figure-html/laplace-1.png and b/docs/articles/cmdstanr_files/figure-html/laplace-1.png differ diff --git a/docs/articles/cmdstanr_files/figure-html/plot-compare-laplace-1.png b/docs/articles/cmdstanr_files/figure-html/plot-compare-laplace-1.png index 087e11fd..a0eca79f 100644 Binary files a/docs/articles/cmdstanr_files/figure-html/plot-compare-laplace-1.png and b/docs/articles/cmdstanr_files/figure-html/plot-compare-laplace-1.png differ diff --git a/docs/articles/cmdstanr_files/figure-html/plot-compare-mcmc-1.png b/docs/articles/cmdstanr_files/figure-html/plot-compare-mcmc-1.png index f5c674ee..6b4765df 100644 Binary files a/docs/articles/cmdstanr_files/figure-html/plot-compare-mcmc-1.png and b/docs/articles/cmdstanr_files/figure-html/plot-compare-mcmc-1.png differ diff --git a/docs/articles/cmdstanr_files/figure-html/plot-compare-pf-1.png b/docs/articles/cmdstanr_files/figure-html/plot-compare-pf-1.png index f72f9f29..d3495d11 100644 Binary files a/docs/articles/cmdstanr_files/figure-html/plot-compare-pf-1.png and b/docs/articles/cmdstanr_files/figure-html/plot-compare-pf-1.png differ diff --git a/docs/articles/cmdstanr_files/figure-html/plot-compare-vb-1.png b/docs/articles/cmdstanr_files/figure-html/plot-compare-vb-1.png index dd9d803e..d23f5c95 100644 Binary files a/docs/articles/cmdstanr_files/figure-html/plot-compare-vb-1.png and b/docs/articles/cmdstanr_files/figure-html/plot-compare-vb-1.png differ diff --git a/docs/articles/cmdstanr_files/figure-html/plot-mle-1.png b/docs/articles/cmdstanr_files/figure-html/plot-mle-1.png index 1998cf9a..8a373b96 100644 Binary files a/docs/articles/cmdstanr_files/figure-html/plot-mle-1.png and b/docs/articles/cmdstanr_files/figure-html/plot-mle-1.png differ diff --git a/docs/articles/cmdstanr_files/figure-html/plots-1.png b/docs/articles/cmdstanr_files/figure-html/plots-1.png index 4d847586..03122c05 100644 Binary files a/docs/articles/cmdstanr_files/figure-html/plots-1.png and b/docs/articles/cmdstanr_files/figure-html/plots-1.png differ diff --git a/docs/articles/cmdstanr_files/figure-html/variational-1.png b/docs/articles/cmdstanr_files/figure-html/variational-1.png index 7d8aa624..1fe52698 100644 Binary files a/docs/articles/cmdstanr_files/figure-html/variational-1.png and b/docs/articles/cmdstanr_files/figure-html/variational-1.png differ diff --git a/docs/articles/index.html b/docs/articles/index.html index c68f0512..c7286099 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -1,5 +1,5 @@ -# Load CmdStan output files into the fitted model object. fit$draws() # Load posterior draws into the object. try(fit$sampler_diagnostics(), silent = TRUE) # Load sampler diagnostics. @@ -796,7 +799,7 @@
Saving fitted model objects
+# Load posterior draws into the fitted model object and omit other output. fit$draws() @@ -891,21 +894,21 @@
Additional resources -
Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson.
+Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson, Steve Bronder.
Articles • cmdstanr Articles • cmdstanr @@ -17,7 +17,7 @@More details
Summary statistics
fit <- cmdstanr::cmdstanr_example("schools", method = "sample")
Warning: 411 of 4000 (10.0%) transitions ended with a divergence. +
-Warning: 124 of 4000 (3.0%) transitions ended with a divergence. See https://mc-stan.org/misc/warnings for details.
Warning: 1 of 4 chains had an E-BFMI less than 0.2. +
Warning: 1 of 4 chains had an E-BFMI less than 0.3. See https://mc-stan.org/misc/warnings for details.
-fit$summary()
+variable mean median sd mad q5 q95 -1 lp__ -55.581147 -56.536200 7.135791 7.809299 -66.108490 -44.17410 -2 mu 6.053657 5.516030 3.777403 3.597633 -0.119312 12.18247 -3 tau 4.456447 3.456865 3.641807 3.645828 0.778481 11.91315 -4 theta[1] 8.436941 7.287125 6.350694 4.628344 0.249281 20.38983 -5 theta[2] 6.522999 5.993620 5.308671 4.494072 -1.760367 15.64359 -6 theta[3] 5.147676 4.349235 6.014958 5.082679 -5.803791 14.17618 -7 theta[4] 6.189408 5.541310 5.578086 4.501730 -2.557880 15.63053 -8 theta[5] 4.669689 4.952550 5.338198 4.271882 -5.029484 12.96141 -9 theta[6] 5.123046 4.853110 5.623562 4.579796 -4.797084 13.70336 -10 theta[7] 8.283195 7.324155 5.703079 4.912514 1.112880 18.94623 -11 theta[8] 6.499894 5.594240 6.114406 4.839688 -2.747390 16.75831 - rhat ess_bulk ess_tail -1 1.348190 9.585696 NA -2 1.094243 190.870204 910.104440 -3 1.305410 10.562073 4.833756 -4 1.037209 205.819798 1097.512010 -5 1.234963 615.265251 1158.714996 -6 1.270209 429.358164 1118.125260 -7 1.060915 288.566093 1331.398737 -8 1.265317 356.169676 1352.544578 -9 1.281493 482.412281 1311.140711 -10 1.041368 75.113061 938.699910 -11 1.069623 436.937773 1068.232198
variable mean median sd mad q5 q95 +1 lp__ -58.697829 -58.911800 5.056327 5.134318 -66.6875850 -49.89660 +2 mu 6.353763 6.597480 4.381424 4.372565 -1.2281605 13.23484 +3 tau 5.604807 4.871790 3.537844 3.371217 1.4006485 12.28454 +4 theta[1] 9.416434 8.789030 7.469694 6.297677 -1.7534945 22.77244 +5 theta[2] 6.708629 6.756395 5.775008 5.428570 -2.7675325 16.09375 +6 theta[3] 5.013658 5.633410 7.082039 6.055554 -7.5991140 15.69620 +7 theta[4] 6.479275 6.629210 6.113995 5.602212 -3.7474250 16.47548 +8 theta[5] 4.415845 4.751705 5.837104 5.557445 -5.7668200 13.46467 +9 theta[6] 5.316444 5.776025 6.132648 5.695771 -5.1632545 14.89951 +10 theta[7] 9.159755 8.682990 6.276413 5.676549 -0.3210604 20.38977 +11 theta[8] 7.114358 7.154305 6.994382 6.108230 -4.2959180 18.25591 + rhat ess_bulk ess_tail +1 1.018582 172.4200 135.9782 +2 1.008425 635.3354 1168.1366 +3 1.021625 165.4672 120.1282 +4 1.003830 1073.3788 1319.6507 +5 1.004990 1050.8445 1693.6799 +6 1.007712 871.0003 1537.3018 +7 1.003560 1027.3246 1869.0154 +8 1.007430 780.1467 1587.7245 +9 1.007162 917.8167 1453.6511 +10 1.003741 956.3136 1718.3496 +11 1.002369 1338.3238 1986.1357
By default all variables are summaries with the follow functions:
@@ -184,18 +184,18 @@posterior::default_summary_measures()
Summary statistics
fit$summary(variables = c("mu", "tau"))
+variable mean median sd mad q5 q95 rhat -1 mu 6.053657 5.516030 3.777403 3.597633 -0.119312 12.18247 1.094243 -2 tau 4.456447 3.456865 3.641807 3.645828 0.778481 11.91315 1.305410 - ess_bulk ess_tail -1 190.87020 910.104440 -2 10.56207 4.833756
variable mean median sd mad q5 q95 rhat +1 mu 6.353763 6.59748 4.381424 4.372565 -1.228160 13.23484 1.008425 +2 tau 5.604807 4.87179 3.537844 3.371217 1.400649 12.28454 1.021625 + ess_bulk ess_tail +1 635.3354 1168.1366 +2 165.4672 120.1282
We can additionally change which functions are used
fit$summary(variables = c("mu", "tau"), mean, sd)
+1 mu 6.353763 4.381424 +2 tau 5.604807 3.537844variable mean sd -1 mu 6.053657 3.777403 -2 tau 4.456447 3.641807
To summarize all variables with non-default functions, it is necessary to set explicitly set the variables argument, either to
@@ -206,17 +206,17 @@NULL
or the full vector of variable names.Summary statistics
fit$summary(variables = NULL, "mean", "median")
+1 lp__ -58.697829 -58.911800 +2 mu 6.353763 6.597480 +3 tau 5.604807 4.871790 +4 theta[1] 9.416434 8.789030 +5 theta[2] 6.708629 6.756395 +6 theta[3] 5.013658 5.633410 +7 theta[4] 6.479275 6.629210 +8 theta[5] 4.415845 4.751705 +9 theta[6] 5.316444 5.776025 +10 theta[7] 9.159755 8.682990 +11 theta[8] 7.114358 7.154305variable mean median -1 lp__ -55.581147 -56.536200 -2 mu 6.053657 5.516030 -3 tau 4.456447 3.456865 -4 theta[1] 8.436941 7.287125 -5 theta[2] 6.522999 5.993620 -6 theta[3] 5.147676 4.349235 -7 theta[4] 6.189408 5.541310 -8 theta[5] 4.669689 4.952550 -9 theta[6] 5.123046 4.853110 -10 theta[7] 8.283195 7.324155 -11 theta[8] 6.499894 5.594240
Summary functions can be specified by character string, function, or using a formula (or anything else supported by
rlang::as_function()
). If these arguments are named, those @@ -232,32 +232,32 @@Summary statistics~quantile(.x, probs = c(0.1, 0.9)), Minimum = function(x) min(x) )
+variable MEAN median My_SD 10% 90% Minimum -1 mu 6.053657 5.516030 3.777403 1.565533 10.863700 -8.554010 -2 tau 4.456447 3.456865 3.641807 0.778481 9.666494 0.774844
variable MEAN median My_SD 10% 90% Minimum +1 mu 6.353763 6.59748 4.381424 0.756247 11.71914 -9.340540 +2 tau 5.604807 4.87179 3.537844 1.723530 10.46083 0.749924
Arguments to all summary functions can also be specified with
.args
.+1 mu -2.926103 -1.228160 13.23484 14.96275 +2 tau 1.106000 1.400649 12.28454 14.09220variable 2.5% 5% 95% 97.5% -1 mu -1.614829 -0.119312 12.18247 13.54298 -2 tau 0.778481 0.778481 11.91315 13.43694
The summary functions are applied to the array of sample values, with dimension
iter_sampling
xchains
.fit$summary(variables = NULL, dim, colMeans)
+1 lp__ 1000 4 -57.746884 -58.396713 -59.488281 -59.159440 +2 mu 1000 4 6.886773 6.098293 6.275602 6.154385 +3 tau 1000 4 4.969696 5.504146 6.035791 5.909594 +4 theta[1] 1000 4 9.584919 8.855289 9.788069 9.437459 +5 theta[2] 1000 4 7.218764 6.441512 6.701321 6.472920 +6 theta[3] 1000 4 6.039431 4.880572 4.532884 4.601744 +7 theta[4] 1000 4 6.923818 6.048362 6.708068 6.236851 +8 theta[5] 1000 4 5.339809 4.243510 3.959413 4.120650 +9 theta[6] 1000 4 5.971513 4.851822 5.284337 5.158102 +10 theta[7] 1000 4 9.310560 8.904282 9.350879 9.073298 +11 theta[8] 1000 4 7.380020 7.069916 7.245772 6.761726variable dim.1 dim.2 1 2 3 4 -1 lp__ 1000 4 -56.698590 -58.592226 -56.911658 -50.122112 -2 mu 1000 4 6.836467 6.338743 6.214918 4.824501 -3 tau 1000 4 4.751132 5.540196 4.617587 2.916872 -4 theta[1] 1000 4 9.139180 9.470688 8.631066 6.506831 -5 theta[2] 1000 4 7.088238 6.756565 6.691301 5.555891 -6 theta[3] 1000 4 6.087235 5.430155 5.208329 3.864985 -7 theta[4] 1000 4 6.963234 6.883517 6.322756 4.588127 -8 theta[5] 1000 4 5.312623 4.530775 4.623091 4.212267 -9 theta[6] 1000 4 5.778286 4.999767 5.497782 4.216348 -10 theta[7] 1000 4 9.354323 9.367796 8.483796 5.926863 -11 theta[8] 1000 4 7.269928 7.165762 6.670007 4.893880
For this reason users may have unexpected results if they use
stats::var()
directly, as it will return a covariance matrix. An alternative is thedistributional::variance()
@@ -266,8 +266,8 @@Summary statistics
fit$summary(c("mu", "tau"), posterior::variance, ~var(as.vector(.x)))
+1 mu 19.19687 19.19687 +2 tau 12.51634 12.51634variable posterior::variance ~var(as.vector(.x)) -1 mu 14.26877 14.26877 -2 tau 13.26276 13.26276
Summary functions need not be numeric, but these won’t work with
$print()
.-@@ -304,7 +304,7 @@Extracting posterior draws/samples# iterations x chains x variables draws_arr <- fit$draws() # or format="array" str(draws_arr)
'draws_array' num [1:1000, 1:4, 1:11] -61.7 -60.5 -61.8 -60.5 -66.8 ... +
'draws_array' num [1:1000, 1:4, 1:11] -66.1 -61.6 -58.3 -57.5 -55.3 ... - attr(*, "dimnames")=List of 3 ..$ iteration: chr [1:1000] "1" "2" "3" "4" ... ..$ chain : chr [1:4] "1" "2" "3" "4" @@ -314,17 +314,17 @@
Extracting posterior draws/samplesdraws_df <- fit$draws(format = "df") str(draws_df)
@@ -332,23 +332,49 @@draws_df [4,000 × 14] (S3: draws_df/draws/tbl_df/tbl/data.frame) - $ lp__ : num [1:4000] -61.7 -60.5 -61.8 -60.5 -66.8 ... - $ mu : num [1:4000] 8.86 6.18 9.7 4.97 5.16 ... - $ tau : num [1:4000] 5.6 8.77 7.58 7.37 11.97 ... - $ theta[1] : num [1:4000] 16.716 14.062 6.67 6.25 -0.404 ... - $ theta[2] : num [1:4000] 4.377 6.173 15.608 0.638 10.449 ... - $ theta[3] : num [1:4000] 11.59 9.27 11.11 3.74 13.42 ... - $ theta[4] : num [1:4000] 13.68 12.51 7.77 6.48 10.16 ... - $ theta[5] : num [1:4000] -0.877 1.959 7.197 2.097 3.229 ... - $ theta[6] : num [1:4000] -5.02 -4.95 5.66 5.24 14.26 ... - $ theta[7] : num [1:4000] 13.38 17.44 13.69 6.65 13.77 ... - $ theta[8] : num [1:4000] 10.46 5.48 -6.15 20.92 31.79 ... + $ lp__ : num [1:4000] -66.1 -61.6 -58.3 -57.5 -55.3 ... + $ mu : num [1:4000] 1.93 8.57 4.05 9.53 9.94 ... + $ tau : num [1:4000] 10.73 6.28 6.1 4.65 3.2 ... + $ theta[1] : num [1:4000] 9.1 21.74 11.18 13.36 9.68 ... + $ theta[2] : num [1:4000] 11.006 5.842 -0.815 17.943 11.148 ... + $ theta[3] : num [1:4000] 0.4 12.17 7.94 6.17 3.94 ... + $ theta[4] : num [1:4000] -3.74 14.86 6.59 9.74 7.95 ... + $ theta[5] : num [1:4000] 12.97 5.5 1.32 8.77 12.83 ... + $ theta[6] : num [1:4000] -11.04 1.81 6.18 6.33 6.56 ... + $ theta[7] : num [1:4000] 22.21 10.53 7.75 14.3 11.54 ... + $ theta[8] : num [1:4000] 14.77 -2.98 11.19 7.25 10.37 ... $ .chain : int [1:4000] 1 1 1 1 1 1 1 1 1 1 ... $ .iteration: int [1:4000] 1 2 3 4 5 6 7 8 9 10 ... $ .draw : int [1:4000] 1 2 3 4 5 6 7 8 9 10 ...
Extracting posterior draws/samplesprint(draws_df)
# A draws_df: 1000 iterations, 4 chains, and 11 variables lp__ mu tau theta[1] theta[2] theta[3] theta[4] theta[5] -1 -62 8.9 5.6 16.7 4.38 11.6 13.676 -0.88 -2 -61 6.2 8.8 14.1 6.17 9.3 12.515 1.96 -3 -62 9.7 7.6 6.7 15.61 11.1 7.770 7.20 -4 -60 5.0 7.4 6.3 0.64 3.7 6.477 2.10 -5 -67 5.2 12.0 -0.4 10.45 13.4 10.162 3.23 -6 -63 11.1 7.9 24.6 11.68 18.8 -1.606 3.79 -7 -61 14.7 6.1 28.9 11.15 15.3 6.780 9.95 -8 -63 13.4 5.7 13.7 21.03 7.2 20.051 12.99 -9 -64 16.0 8.3 20.2 2.65 11.3 19.000 3.51 -10 -59 2.9 4.5 4.2 11.37 2.5 -0.057 6.30 +1 -66 1.9 10.7 9.1 11.01 0.4 -3.7 13.0 +2 -62 8.6 6.3 21.7 5.84 12.2 14.9 5.5 +3 -58 4.1 6.1 11.2 -0.82 7.9 6.6 1.3 +4 -58 9.5 4.6 13.4 17.94 6.2 9.7 8.8 +5 -55 9.9 3.2 9.7 11.15 3.9 7.9 12.8 +6 -55 9.9 4.0 8.5 8.81 6.6 9.8 12.0 +7 -54 9.7 2.7 14.5 10.87 5.8 6.8 10.2 +8 -52 8.7 2.2 10.5 10.42 9.4 9.8 9.4 +9 -57 10.1 5.0 13.4 10.29 11.4 5.8 12.0 +10 -58 10.5 5.5 14.8 9.45 13.9 5.6 12.0 # ... with 3990 more draws, and 3 more variables # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
To convert an existing draws object to a different format use the
posterior::as_draws_*()
functions.To manipulate the
+draws
objects use the various methods -described in the posterior package vignettes +described in the posterior package vignettes and documentation.+Structured draws similar to
+rstan::extract()
+The posterior package’s
+rvar
format +provides a multidimensional, sample-based representation of random +variables. See https://mc-stan.org/posterior/articles/rvar.html for +details. In addition to being useful in its own right, this format also +allows CmdStanR users to obtain draws in a similar format to +rstan::extract()
.Suppose we have a parameter
+matrix[2,3] x
. The +rvar
format lets you interact withx
as if +it’s a2 x 3
matrix and automatically applies operations +over the many posterior draws ofx
. To instead directly +access the draws ofx
while maintaining the structure of +the matrix useposterior::draws_of()
. For example:++draws <- posterior::as_draws_rvars(fit$draws()) +x_rvar <- draws$x +x_array <- posterior::draws_of(draws$x)
The object
+x_rvar
will be anrvar
that can +be used like a2 x 3
matrix, with the draws handled behind +the scenes. The objectx_array
will be a +4000 x 2 x 3
array (assuming4000
posterior +draws), which is the same as it would be after being extracted from the +list returned byrstan::extract()
.Extracting posterior draws/samples
-diff --git a/docs/articles/profiling.html b/docs/articles/profiling.html index dfbed9b1..2336b31c 100644 --- a/docs/articles/profiling.html +++ b/docs/articles/profiling.html @@ -12,7 +12,7 @@ - + @@ -40,7 +40,7 @@Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson.
+Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson, Steve Bronder.
Accessing the profiling info
fit$profiles()
+1 34634 17317 1 +2 34651317 17317 1[[1]] - name thread_id total_time forward_time reverse_time chain_stack -1 likelihood 0x118759e00 0.69970800 0.57529800 0.12441000 51468 -2 priors 0x118759e00 0.00446571 0.00270154 0.00176417 34312 + name thread_id total_time forward_time reverse_time chain_stack +1 priors 0x7ff858f85100 0.00636262 0.0041022 0.00226042 34634 +2 likelihood 0x7ff858f85100 0.92346400 0.7530890 0.17037500 51951 no_chain_stack autodiff_calls no_autodiff_calls -1 34329156 17156 1 -2 34312 17156 1
The
@@ -296,12 +296,12 @@total_time
column is the total time spent inside a given profile statement. It is clear that the vast majority of time is spent in the likelihood function.Comparing to a faster versio
fit_glm$profiles()
+1 34894 17447 1 +2 17447 17447 1[[1]] - name thread_id total_time forward_time reverse_time chain_stack -1 likelihood 0x106ce4e00 0.38400000 0.38264800 0.00135175 52203 -2 priors 0x106ce4e00 0.00356564 0.00237053 0.00119511 34802 + name thread_id total_time forward_time reverse_time chain_stack +1 priors 0x7ff858f85100 0.00547866 0.00378008 0.00169859 34894 +2 likelihood 0x7ff858f85100 0.55301400 0.55111300 0.00190093 52341 no_chain_stack autodiff_calls no_autodiff_calls -1 17401 17401 1 -2 34802 17401 1
We can see from the
total_time
column that this is much faster than the previous model.Per-gradient timings, and memory
profile_chain_1 <- fit$profiles()[[1]] per_gradient_timing <- profile_chain_1$total_time/profile_chain_1$autodiff_calls print(per_gradient_timing) # two elements for the two profile statements in the model
+[1] 4.078503e-05 2.603002e-07
[1] 3.674205e-07 5.332702e-05
@@ -229,13 +229,14 @@Accessing and saving the profile files @@ -335,7 +335,7 @@
Accessing and saving the profile
$profile_files()
.-fit$profile_files()
+[1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpJRtaoq/model_6580008f67848265f3dfd0e7ae3b0600-profile-202312131010-1-95e407.csv"
[1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpEbNhyd/model_6580008f67848265f3dfd0e7ae3b0600-profile-202407021547-1-96d3d3.csv"
These can be saved to a more permanent location with the
$save_profile_files()
method.diff --git a/docs/articles/r-markdown.html b/docs/articles/r-markdown.html index 23c674be..2f9ac3f1 100644 --- a/docs/articles/r-markdown.html +++ b/docs/articles/r-markdown.html @@ -12,7 +12,7 @@ - + @@ -40,7 +40,7 @@@@ -363,12 +363,12 @@References -
Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson.
+Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson, Steve Bronder.
Example #> #> All 4 chains finished successfully. #> Mean chain execution time: 0.0 seconds. -#> Total execution time: 0.7 seconds. - +#> Total execution time: 0.7 seconds.
+#> lp__ -1.55 -1.24 1.24 1.04 -4.07 -0.18 1.00 1509 1671 +#> y[1] -0.03 -0.03 1.01 0.99 -1.69 1.68 1.00 1852 2195 +#> y[2] -0.13 -0.07 2.92 2.14 -4.92 4.65 1.00 1855 1666+print(fit) #> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail -#> lp__ -1.50 -1.17 1.24 0.96 -3.94 -0.18 1.00 1304 1536 -#> y[1] -0.01 -0.01 0.99 0.99 -1.67 1.60 1.00 1993 2262 -#> y[2] -0.07 -0.04 2.90 2.05 -4.79 4.54 1.00 2050 1420
@@ -174,11 +179,11 @@Option 3: Using both RStan and CmdStanR in the same R Markdown @@ -247,26 +248,26 @@
project, the option to use both exists. When registering CmdStanR’s knitr engine, set
override = FALSE
to register the engine as acmdstan
engine: -+register_knitr_engine(override = FALSE)
This will cause
- +stan
chunks to be processed by knitr’s built-in, RStan-based engine and only use CmdStanR’s knitr engine forcmdstan
chunks:@@ -99,12 +99,12 @@Caching chunks @@ -304,12 +305,12 @@
Running interactively
-diff --git a/docs/authors.html b/docs/authors.html index 8c3fd645..fb693ea9 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -1,5 +1,5 @@ -Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson.
+Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson, Steve Bronder.
Authors and Citation • cmdstanr Authors and Citation • cmdstanr @@ -17,7 +17,7 @@- @@ -156,15 +160,16 @@Citation
Gabry J, Češnovar R, Johnson A (2023). +
Gabry J, Češnovar R, Johnson A, Bronder S (2024). cmdstanr: R Interface to 'CmdStan'. -https://mc-stan.org/cmdstanr/, https://discourse.mc-stan.org. +R package version 0.8.1, https://discourse.mc-stan.org, https://mc-stan.org/cmdstanr/.
@Manual{, title = {cmdstanr: R Interface to 'CmdStan'}, - author = {Jonah Gabry and Rok Češnovar and Andrew Johnson}, - year = {2023}, - note = {https://mc-stan.org/cmdstanr/, https://discourse.mc-stan.org}, + author = {Jonah Gabry and Rok Češnovar and Andrew Johnson and Steve Bronder}, + year = {2024}, + note = {R package version 0.8.1, https://discourse.mc-stan.org}, + url = {https://mc-stan.org/cmdstanr/}, }Citation
- +@@ -54,7 +49,7 @@ - + @@ -69,7 +64,7 @@ Other Packages - + @@ -130,7 +125,7 @@ @@ -102,6 +102,61 @@@@ -156,7 +151,7 @@Goals
A clean interface to Stan services so that CmdStanR can keep up with Stan releases.
R code that doesn’t interface directly with C++, only calls compiled executables.
- Modularity: CmdStanR runs Stan’s algorithms and lets downstream modules do the analysis.
+ Flexible BSD-3 license.
Flexible BSD-3 license.
@@ -167,8 +162,8 @@Installing the R package
You can install the latest beta release of the cmdstanr R package with
+-# we recommend running this is a fresh R session or restarting your current session -install.packages("cmdstanr", repos = c("https://stan-dev.r-universe.dev", getOption("repos")))
# we recommend running this in a fresh R session or restarting your current session +install.packages("cmdstanr", repos = c('https://stan-dev.r-universe.dev', getOption("repos")))
This does not install the vignettes, which take a long time to build, but they are always available online at https://mc-stan.org/cmdstanr/articles/.
To instead install the latest development version of the package from GitHub use
+ - - + diff --git a/docs/news/index.html b/docs/news/index.html index f702a044..b66cb1ab 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -1,5 +1,5 @@ -@@ -233,9 +228,10 @@Citation
@@ -248,21 +244,21 @@Developers
-
- Jonah Gabry
+
Author, maintainer- Jonah Gabry
Author- Rok Češnovar
-
Author- Andrew Johnson
+
Author- Andrew Johnson
+
Author, maintainer- Steve Bronder
Author- More about authors...
Developers
Changelog • cmdstanr Changelog • cmdstanr @@ -17,7 +17,7 @@Changelog
Source:NEWS.md
++cmdstanr 0.8.1
+++Minor changes
+
- Added
+CMDSTANR_USE_RTOOLS
environment variable to force stock RTools on Windows by @andrjohns in #980- Added support for Windows ARM64 by @andrjohns in #990
+- Automatically initialise model methods when called, add
+inc_warmup
argument to$unconstrain_draws()
by @andrjohns in #985++Bugfixes
+
- Fix errors when using pathfinder object as initial values by @avehtari in #984
+- Fix error with
+$unconstrain_draws()
returning incorrect assumptions in some cases by @andrjohns in #983- Fix spurious errors about missing CmdStan config files by @andrjohns in #981
+- Fix linking error when exposing SUNDIALS/KINSOL functions or model methods by @andrjohns in #977
+- Fix long-standing error with OneDrive paths on Windows by @andrjohns in #990
++cmdstanr 0.8.0
+++Major new features
+
- Add functionality for passing
+CmdStanFit
objects as initial values by @SteveBronder in #937++Other improvements
+
- Add compatibility with CmdStan 2.35 by @andrjohns in #972
+- Add
+show_messages
andshow_exceptions
arguments to all methods for controlling output by @andrjohns in #897- Drop RcppEigen dependency, implement basic Eigen -> C++ interop by @andrjohns in #899
+- Add compatibility with CmdStan 2.34 by @andrjohns in #905 #910
+- Add a format argument to the
+unconstrain_draws()
method to specify draws format of return by @andrjohns in #886- Align
+cmdstanr
EBFMI diagnostic threshold with CmdStan by @andrjohns in #892- Add global option
+cmdstanr_print_line_numbers
to add line number to model printing by @sbfnk in #967- Add new CmdStan arguments
+save_metric
andsave_cmdstan_config
by @venpopov in #932- Add documentation for CmdStanR global options by @jgabry in #951
+- Add documentation for how to obtain structured output similar to
+rstan::extract()
using a combination ofcmdstanr
andposterior
by @jgabry in #955- Added coercion generics for CmdStanFit objects by @gowerc in #943
+- +
+psis_resample
andcalculate_lp
arguments added to Pathfinder method by @SteveBronder in #903- Documentation and tests for LOO method updated by @jgabry in #923
+- Global option
+cmdstanr_warn_inits
added to disable warnings about partially specified initial values by @jgabry in #913- Updates to MCMC
+output_dir
documentation by @jgabry in #929++Bugfixes
+
- Fix broken link in OpenCL documentation by @eipi10 in #908
+- Fix a minor typo in the README by @jgabry in #911
+- Make exported RNG functions respect changes to R’s seed by @andrjohns in #973
+- Optimisations for model methods functions by @andrjohns in #960
+- Bugfix for passing function for initial values with Pathfinder method and default
+num_paths
by @andrjohns in #964- Continue with compilation if
+compile_stanalone=TRUE
but no functions are found by @jgabry in #956- Update tests and CI for compatibility with MacOS ARM64 by @andrjohns in #958
+- Fix handling of
+inv_metric
argument with only 1 parameter by @venpopov in #935- Fixes for compatibility with RTools44 by @andrjohns in #952 #959
+@@ -153,11 +153,11 @@cmdstanr 0.7.0
@@ -350,11 +405,11 @@diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index 14ff6a17..427640f5 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -1,14 +1,14 @@ -pandoc: 3.1.1 -pkgdown: 2.0.7 +pandoc: 3.1.11 +pkgdown: 2.0.9 pkgdown_sha: ~ articles: - opencl: articles-online-only/opencl.html cmdstanr-internals: cmdstanr-internals.html cmdstanr: cmdstanr.html + opencl: articles-online-only/opencl.html posterior: posterior.html profiling: profiling.html r-markdown: r-markdown.html -last_built: 2023-12-13T17:00Z +last_built: 2024-07-02T21:30Z urls: reference: https://mc-stan.org/cmdstanr/reference article: https://mc-stan.org/cmdstanr/articles diff --git a/docs/reference/CmdStanDiagnose.html b/docs/reference/CmdStanDiagnose.html index e0d66d56..8a2a8930 100644 --- a/docs/reference/CmdStanDiagnose.html +++ b/docs/reference/CmdStanDiagnose.html @@ -1,5 +1,5 @@ -cmdstanr 0.1.0
-Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson.
+Developed by Jonah Gabry, Rok Češnovar, Andrew Johnson, Steve Bronder.
CmdStanDiagnose objects — CmdStanDiagnose • cmdstanr CmdStanDiagnose objects — CmdStanDiagnose • cmdstanr CmdStanGQ objects — CmdStanGQ • cmdstanr CmdStanGQ objects — CmdStanGQ • cmdstanr CmdStanLaplace objects — CmdStanLaplace • cmdstanr CmdStanLaplace objects — CmdStanLaplace • cmdstanr CmdStanMCMC objects — CmdStanMCMC • cmdstanr CmdStanMCMC objects — CmdStanMCMC • cmdstanr CmdStanMLE objects — CmdStanMLE • cmdstanr CmdStanMLE objects — CmdStanMLE • cmdstanr CmdStanModel objects — CmdStanModel • cmdstanr CmdStanModel objects — CmdStanModel • cmdstanr CmdStanPathfinder objects — CmdStanPathfinder • cmdstanr CmdStanPathfinder objects — CmdStanPathfinder • cmdstanr CmdStanVB objects — CmdStanVB • cmdstanr CmdStanVB objects — CmdStanVB • cmdstanr Create a draws object from a CmdStanR fitted model object — as_draws.CmdStanMCMC • cmdstanr Create a draws object from a CmdStanR fitted model object — as_draws.CmdStanMCMC • cmdstanr Convert CmdStanMCMC to mcmc.list — as_mcmc.list • cmdstanr Convert CmdStanMCMC to mcmc.list — as_mcmc.list • cmdstanr cmdstanr - 0.7.0 + 0.8.1Examples
Coercion methods for CmdStan objects — cmdstan_coercion • cmdstanr + + +++ + + + + + + + diff --git a/docs/reference/cmdstan_default_install_path.html b/docs/reference/cmdstan_default_install_path.html index e2ca8ac8..d8ecd6d6 100644 --- a/docs/reference/cmdstan_default_install_path.html +++ b/docs/reference/cmdstan_default_install_path.html @@ -1,5 +1,5 @@ -+ + + + ++ + ++ + ++ +++ +These are generic functions intended to primarily be used by developers of +packages that interface with on CmdStanR. Developers can define methods on +top of these generics to coerce objects into CmdStanR's fitted model objects.
+++ ++as.CmdStanMCMC(object, ...) + +as.CmdStanMLE(object, ...) + +as.CmdStanLaplace(object, ...) + +as.CmdStanVB(object, ...) + +as.CmdStanPathfinder(object, ...) + +as.CmdStanGQ(object, ...) + +as.CmdStanDiagnose(object, ...)
++ +Arguments
+
- object
+- + + +
The object to be coerced.
- ...
+- + +
Additional arguments to pass to methods.
cmdstan_default_install_path — cmdstan_default_install_path • cmdstanr cmdstan_default_install_path — cmdstan_default_install_path • cmdstanr @@ -17,7 +17,7 @@Value
cmdstan_default_path — cmdstan_default_path • cmdstanr cmdstan_default_path — cmdstan_default_path • cmdstanr Create a new CmdStanModel object — cmdstan_model • cmdstanr Create a new CmdStanModel object — cmdstan_model • cmdstanr cmdstanr - 0.7.0 + 0.8.1Examples
# Otherwise use the `path` argument to specify the location of your # CmdStan installation.) set_cmdstan_path(path = NULL) -#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.33.1 +#> CmdStan path set to: /Users/jgabry/.cmdstan/cmdstan-2.35.0 # Create a CmdStanModel object from a Stan program, # here using the example model that comes with CmdStan @@ -194,15 +194,29 @@Examples
mod$print() #> data { #> int<lower=0> N; -#> array[N] int<lower=0,upper=1> y; +#> array[N] int<lower=0, upper=1> y; #> } #> parameters { -#> real<lower=0,upper=1> theta; +#> real<lower=0, upper=1> theta; #> } #> model { -#> theta ~ beta(1,1); // uniform prior on interval 0,1 +#> theta ~ beta(1, 1); // uniform prior on interval 0,1 #> y ~ bernoulli(theta); #> } +# Print with line numbers. This can be set globally using the +# `cmdstanr_print_line_numbers` option. +mod$print(line_numbers = TRUE) +#> 1: data { +#> 2: int<lower=0> N; +#> 3: array[N] int<lower=0, upper=1> y; +#> 4: } +#> 5: parameters { +#> 6: real<lower=0, upper=1> theta; +#> 7: } +#> 8: model { +#> 9: theta ~ beta(1, 1); // uniform prior on interval 0,1 +#> 10: y ~ bernoulli(theta); +#> 11: } # Data as a named list (like RStan) stan_data <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1)) @@ -273,8 +287,8 @@Examples
#> # A tibble: 2 × 10 #> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail #> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 lp__ -7.30 -7.03 0.721 0.380 -8.82 -6.75 1.00 902. 1006. -#> 2 theta 0.247 0.233 0.122 0.129 0.0786 0.470 1.00 762. 712. +#> 1 lp__ -7.30 -7.00 0.811 0.344 -8.83 -6.75 1.00 702. 776. +#> 2 theta 0.254 0.238 0.125 0.124 0.0807 0.483 1.00 634. 580. # Check sampling diagnostics fit_mcmc$diagnostic_summary() @@ -285,7 +299,7 @@Examples
#> [1] 0 0 #> #> $ebfmi -#> [1] 1.017555 1.250490 +#> [1] 1.1148699 0.8012192 #> # Get posterior draws @@ -295,22 +309,22 @@Examples
#> , , variable = lp__ #> #> chain -#> iteration 1 2 -#> 1 -6.8 -6.8 -#> 2 -6.9 -6.8 -#> 3 -7.0 -7.0 -#> 4 -6.9 -7.1 -#> 5 -6.7 -7.0 +#> iteration 1 2 +#> 1 -7.0 -6.8 +#> 2 -7.9 -6.9 +#> 3 -7.4 -6.8 +#> 4 -6.7 -6.8 +#> 5 -6.9 -10.2 #> #> , , variable = theta #> #> chain #> iteration 1 2 -#> 1 0.28 0.21 -#> 2 0.19 0.20 -#> 3 0.16 0.17 -#> 4 0.20 0.36 -#> 5 0.25 0.34 +#> 1 0.17 0.23 +#> 2 0.46 0.18 +#> 3 0.41 0.28 +#> 4 0.25 0.23 +#> 5 0.18 0.62 #> #> # ... with 995 more iterations @@ -318,16 +332,16 @@Examples
as_draws_df(draws) #> # A draws_df: 1000 iterations, 2 chains, and 2 variables #> lp__ theta -#> 1 -6.8 0.28 -#> 2 -6.9 0.19 -#> 3 -7.0 0.16 -#> 4 -6.9 0.20 -#> 5 -6.7 0.25 -#> 6 -7.1 0.36 -#> 7 -9.0 0.55 -#> 8 -7.2 0.15 -#> 9 -6.8 0.23 -#> 10 -7.5 0.42 +#> 1 -7.0 0.17 +#> 2 -7.9 0.46 +#> 3 -7.4 0.41 +#> 4 -6.7 0.25 +#> 5 -6.9 0.18 +#> 6 -6.9 0.33 +#> 7 -7.2 0.15 +#> 8 -6.8 0.29 +#> 9 -6.8 0.24 +#> 10 -6.8 0.24 #> # ... with 1990 more draws #> # ... hidden reserved variables {'.chain', '.iteration', '.draw'} @@ -345,12 +359,12 @@Examples
# and also demonstrate specifying data as a path to a file instead of a list my_data_file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.data.json") fit_optim <- mod$optimize(data = my_data_file, seed = 123) -#> Initial log joint probability = -9.51104 +#> Initial log joint probability = -16.144 #> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes -#> 6 -5.00402 0.000103557 2.55661e-07 1 1 9 +#> 6 -5.00402 0.000246518 8.73164e-07 1 1 9 #> Optimization terminated normally: #> Convergence detected: relative gradient magnitude is below tolerance -#> Finished in 0.1 seconds. +#> Finished in 0.2 seconds. fit_optim$summary() #> # A tibble: 2 × 2 #> variable estimate @@ -361,12 +375,12 @@Examples
# Run 'optimize' again with 'jacobian=TRUE' and then draw from Laplace approximation # to the posterior fit_optim <- mod$optimize(data = my_data_file, jacobian = TRUE) -#> Initial log joint probability = -17.5452 +#> Initial log joint probability = -13.9671 #> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes -#> 5 -6.74802 0.00115356 2.90728e-05 1 1 8 +#> 5 -6.74802 0.00195337 5.29486e-05 1 1 8 #> Optimization terminated normally: #> Convergence detected: relative gradient magnitude is below tolerance -#> Finished in 0.1 seconds. +#> Finished in 0.2 seconds. fit_laplace <- mod$laplace(data = my_data_file, mode = fit_optim, draws = 2000) #> Calculating Hessian #> Calculating inverse of Cholesky factor @@ -391,14 +405,14 @@Examples
#> iteration: 1700 #> iteration: 1800 #> iteration: 1900 -#> Finished in 0.1 seconds. +#> Finished in 0.2 seconds. fit_laplace$summary() #> # A tibble: 3 × 7 -#> variable mean median sd mad q5 q95 -#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 lp__ -7.27 -7.00 0.705 0.338 -8.72 -6.75 -#> 2 lp_approx__ -0.527 -0.251 0.714 0.340 -2.02 -0.00142 -#> 3 theta 0.270 0.251 0.126 0.125 0.0959 0.509 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp__ -7.21 -6.97 0.646 0.293 -8.55 -6.75 +#> 2 lp_approx__ -0.466 -0.220 0.655 0.297 -1.78 -0.00205 +#> 3 theta 0.270 0.250 0.120 0.121 0.109 0.497 # Run 'variational' method to use ADVI to approximate posterior fit_vb <- mod$variational(data = stan_data, seed = 123) @@ -407,8 +421,8 @@Examples
#> This procedure has not been thoroughly tested and may be unstable #> or buggy. The interface is subject to change. #> ------------------------------------------------------------ -#> Gradient evaluation took 5e-06 seconds -#> 1000 transitions using 10 leapfrog steps per transition would take 0.05 seconds. +#> Gradient evaluation took 2.3e-05 seconds +#> 1000 transitions using 10 leapfrog steps per transition would take 0.23 seconds. #> Adjust your expectations accordingly! #> Begin eta adaptation. #> Iteration: 1 / 250 [ 0%] (Adaptation) @@ -419,50 +433,50 @@Examples
#> Success! Found best value [eta = 1] earlier than expected. #> Begin stochastic gradient ascent. #> iter ELBO delta_ELBO_mean delta_ELBO_med notes -#> 100 -6.262 1.000 1.000 -#> 200 -6.263 0.500 1.000 -#> 300 -6.307 0.336 0.007 MEDIAN ELBO CONVERGED +#> 100 -6.164 1.000 1.000 +#> 200 -6.225 0.505 1.000 +#> 300 -6.186 0.339 0.010 MEDIAN ELBO CONVERGED #> Drawing a sample of size 1000 from the approximate posterior... #> COMPLETED. -#> Finished in 0.1 seconds. +#> Finished in 0.2 seconds. fit_vb$summary() #> # A tibble: 3 × 7 #> variable mean median sd mad q5 q95 #> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 lp__ -7.18 -6.94 0.588 0.259 -8.36 -6.75 -#> 2 lp_approx__ -0.515 -0.221 0.692 0.303 -2.06 -0.00257 -#> 3 theta 0.263 0.246 0.115 0.113 0.106 0.481 +#> 1 lp__ -7.14 -6.93 0.528 0.247 -8.21 -6.75 +#> 2 lp_approx__ -0.520 -0.244 0.740 0.326 -1.90 -0.00227 +#> 3 theta 0.251 0.236 0.107 0.108 0.100 0.446 mcmc_hist(fit_vb$draws("theta")) #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. # Run 'pathfinder' method, a new alternative to the variational method fit_pf <- mod$pathfinder(data = stan_data, seed = 123) -#> Path [1] :Initial log joint density = -11.008832 +#> Path [1] :Initial log joint density = -18.273334 #> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 5 -6.748e+00 9.383e-04 1.391e-05 1.000e+00 1.000e+00 126 -6.264e+00 -6.264e+00 -#> Path [1] :Best Iter: [3] ELBO (-6.195408) evaluations: (126) -#> Path [2] :Initial log joint density = -7.318450 +#> 5 -6.748e+00 7.082e-04 1.432e-05 1.000e+00 1.000e+00 126 -6.145e+00 -6.145e+00 +#> Path [1] :Best Iter: [5] ELBO (-6.145070) evaluations: (126) +#> Path [2] :Initial log joint density = -19.192715 #> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 4 -6.748e+00 5.414e-03 1.618e-04 1.000e+00 1.000e+00 101 -6.251e+00 -6.251e+00 -#> Path [2] :Best Iter: [3] ELBO (-6.229174) evaluations: (101) -#> Path [3] :Initial log joint density = -12.374612 +#> 5 -6.748e+00 2.015e-04 2.228e-06 1.000e+00 1.000e+00 126 -6.223e+00 -6.223e+00 +#> Path [2] :Best Iter: [2] ELBO (-6.170358) evaluations: (126) +#> Path [3] :Initial log joint density = -6.774820 #> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 5 -6.748e+00 1.419e-03 2.837e-05 1.000e+00 1.000e+00 126 -6.199e+00 -6.199e+00 -#> Path [3] :Best Iter: [5] ELBO (-6.199185) evaluations: (126) -#> Path [4] :Initial log joint density = -13.009824 +#> 4 -6.748e+00 1.137e-04 2.596e-07 1.000e+00 1.000e+00 101 -6.178e+00 -6.178e+00 +#> Path [3] :Best Iter: [4] ELBO (-6.177909) evaluations: (101) +#> Path [4] :Initial log joint density = -7.949193 #> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 5 -6.748e+00 1.677e-03 3.885e-05 1.000e+00 1.000e+00 126 -6.173e+00 -6.173e+00 -#> Path [4] :Best Iter: [5] ELBO (-6.172860) evaluations: (126) +#> 5 -6.748e+00 2.145e-04 1.301e-06 1.000e+00 1.000e+00 126 -6.197e+00 -6.197e+00 +#> Path [4] :Best Iter: [5] ELBO (-6.197118) evaluations: (126) #> Total log probability function evaluations:4379 -#> Finished in 0.1 seconds. +#> Finished in 0.2 seconds. fit_pf$summary() #> # A tibble: 3 × 7 #> variable mean median sd mad q5 q95 #> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 lp_approx__ -1.08 -0.728 0.886 0.304 -2.71 -0.511 -#> 2 lp__ -7.26 -6.96 0.738 0.297 -8.72 -6.75 -#> 3 theta 0.249 0.230 0.120 0.121 0.0854 0.471 +#> 1 lp_approx__ -1.07 -0.727 0.945 0.311 -2.91 -0.450 +#> 2 lp__ -7.25 -6.97 0.753 0.308 -8.78 -6.75 +#> 3 theta 0.256 0.245 0.119 0.123 0.0824 0.462 mcmc_hist(fit_pf$draws("theta")) #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. @@ -471,48 +485,49 @@Examples
# better covariance approximation, and fewer LBFGSs iterations fit_pf <- mod$pathfinder(data = stan_data, num_paths=10, single_path_draws=40, history_size=50, max_lbfgs_iters=100) -#> Path [1] :Initial log joint density = -11.515672 +#> Warning: Number of PSIS draws is larger than the total number of draws returned by the single Pathfinders. This is likely unintentional and leads to re-sampling from the same draws. +#> Path [1] :Initial log joint density = -7.405417 #> Path [1] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 5 -6.748e+00 1.064e-03 1.692e-05 1.000e+00 1.000e+00 126 -6.254e+00 -6.254e+00 -#> Path [1] :Best Iter: [3] ELBO (-6.213658) evaluations: (126) -#> Path [2] :Initial log joint density = -9.189304 +#> 5 -6.748e+00 9.081e-05 3.233e-07 1.000e+00 1.000e+00 126 -6.231e+00 -6.231e+00 +#> Path [1] :Best Iter: [3] ELBO (-6.194155) evaluations: (126) +#> Path [2] :Initial log joint density = -7.706381 #> Path [2] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 5 -6.748e+00 3.749e-04 3.327e-06 1.000e+00 1.000e+00 126 -6.289e+00 -6.289e+00 -#> Path [2] :Best Iter: [4] ELBO (-6.214534) evaluations: (126) -#> Path [3] :Initial log joint density = -13.270061 +#> 5 -6.748e+00 1.603e-04 8.100e-07 1.000e+00 1.000e+00 126 -6.209e+00 -6.209e+00 +#> Path [2] :Best Iter: [5] ELBO (-6.209214) evaluations: (126) +#> Path [3] :Initial log joint density = -8.140103 #> Path [3] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 5 -6.748e+00 1.767e-03 4.305e-05 1.000e+00 1.000e+00 126 -6.276e+00 -6.276e+00 -#> Path [3] :Best Iter: [3] ELBO (-6.225363) evaluations: (126) -#> Path [4] :Initial log joint density = -6.748213 +#> 5 -6.748e+00 2.528e-04 1.703e-06 1.000e+00 1.000e+00 126 -6.231e+00 -6.231e+00 +#> Path [3] :Best Iter: [3] ELBO (-6.213740) evaluations: (126) +#> Path [4] :Initial log joint density = -6.779208 #> Path [4] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 2 -6.748e+00 1.004e-02 7.315e-05 1.000e+00 1.000e+00 51 -6.200e+00 -6.200e+00 -#> Path [4] :Best Iter: [2] ELBO (-6.200114) evaluations: (51) -#> Path [5] :Initial log joint density = -7.368682 +#> 4 -6.748e+00 1.406e-04 3.696e-07 1.000e+00 1.000e+00 101 -6.183e+00 -6.183e+00 +#> Path [4] :Best Iter: [4] ELBO (-6.183257) evaluations: (101) +#> Path [5] :Initial log joint density = -6.762413 #> Path [5] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 5 -6.748e+00 5.377e-04 9.870e-07 1.000e+00 1.000e+00 126 -6.276e+00 -6.276e+00 -#> Path [5] :Best Iter: [3] ELBO (-6.167297) evaluations: (126) -#> Path [6] :Initial log joint density = -9.465368 +#> 3 -6.748e+00 2.209e-03 1.061e-04 1.000e+00 1.000e+00 76 -6.247e+00 -6.247e+00 +#> Path [5] :Best Iter: [2] ELBO (-6.191386) evaluations: (76) +#> Path [6] :Initial log joint density = -7.106649 #> Path [6] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 5 -6.748e+00 4.613e-04 4.599e-06 1.000e+00 1.000e+00 126 -6.238e+00 -6.238e+00 -#> Path [6] :Best Iter: [3] ELBO (-6.185454) evaluations: (126) -#> Path [7] :Initial log joint density = -6.856712 +#> 4 -6.748e+00 3.282e-03 7.049e-05 1.000e+00 1.000e+00 101 -6.179e+00 -6.179e+00 +#> Path [6] :Best Iter: [3] ELBO (-6.136787) evaluations: (101) +#> Path [7] :Initial log joint density = -7.037942 #> Path [7] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 4 -6.748e+00 1.028e-04 1.386e-05 9.452e-01 9.452e-01 101 -6.242e+00 -6.242e+00 -#> Path [7] :Best Iter: [3] ELBO (-6.159687) evaluations: (101) -#> Path [8] :Initial log joint density = -7.085030 +#> 4 -6.748e+00 2.570e-03 4.694e-05 1.000e+00 1.000e+00 101 -6.238e+00 -6.238e+00 +#> Path [7] :Best Iter: [3] ELBO (-6.227629) evaluations: (101) +#> Path [8] :Initial log joint density = -6.772444 #> Path [8] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 4 -6.748e+00 3.058e-03 6.268e-05 1.000e+00 1.000e+00 101 -6.221e+00 -6.221e+00 -#> Path [8] :Best Iter: [4] ELBO (-6.221460) evaluations: (101) -#> Path [9] :Initial log joint density = -7.443992 +#> 4 -6.748e+00 9.980e-05 2.088e-07 1.000e+00 1.000e+00 101 -6.208e+00 -6.208e+00 +#> Path [8] :Best Iter: [2] ELBO (-6.208417) evaluations: (101) +#> Path [9] :Initial log joint density = -17.488355 #> Path [9] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 5 -6.748e+00 9.951e-05 3.747e-07 1.000e+00 1.000e+00 126 -6.228e+00 -6.228e+00 -#> Path [9] :Best Iter: [2] ELBO (-6.206717) evaluations: (126) -#> Path [10] :Initial log joint density = -6.751684 +#> 5 -6.748e+00 1.187e-03 3.028e-05 1.000e+00 1.000e+00 126 -6.283e+00 -6.283e+00 +#> Path [9] :Best Iter: [2] ELBO (-6.205015) evaluations: (126) +#> Path [10] :Initial log joint density = -7.447696 #> Path [10] : Iter log prob ||dx|| ||grad|| alpha alpha0 # evals ELBO Best ELBO Notes -#> 3 -6.748e+00 5.950e-04 1.461e-05 1.000e+00 1.000e+00 76 -6.209e+00 -6.209e+00 -#> Path [10] :Best Iter: [3] ELBO (-6.209430) evaluations: (76) -#> Total log probability function evaluations:1235 -#> Finished in 0.1 seconds. +#> 5 -6.748e+00 1.004e-04 3.798e-07 1.000e+00 1.000e+00 126 -6.264e+00 -6.264e+00 +#> Path [10] :Best Iter: [3] ELBO (-6.226969) evaluations: (126) +#> Total log probability function evaluations:1260 +#> Finished in 0.2 seconds. # Specifying initial values as a function fit_mcmc_w_init_fun <- mod$sample( @@ -594,7 +609,7 @@Examples
#> 6 -5.00402 0.000237915 9.55309e-07 1 1 9 #> Optimization terminated normally: #> Convergence detected: relative gradient magnitude is below tolerance -#> Finished in 0.1 seconds. +#> Finished in 0.2 seconds. fit_optim_w_init_list$init() #> [[1]] #> [[1]]$theta @@ -613,11 +628,11 @@Examples
CmdStanR: the R interface to CmdStan — cmdstanr-package • cmdstanr CmdStanR: the R interface to CmdStan — cmdstanr-package • cmdstanr Fit models for use in examples — cmdstanr_example • cmdstanr Fit models for use in examples — cmdstanr_example • cmdstanr @@ -17,7 +17,7 @@Examples
#> # A tibble: 105 × 10 #> variable mean median sd mad q5 q95 rhat ess_bulk #> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 lp__ -66.0 -65.6 1.45 1.20 -68.7 -64.3 1.00 934. -#> 2 alpha 0.384 0.386 0.218 0.218 0.0341 0.748 1.00 1959. -#> 3 beta[1] -0.667 -0.661 0.247 0.248 -1.08 -0.273 1.00 1840. -#> 4 beta[2] -0.275 -0.273 0.231 0.231 -0.662 0.114 1.00 1954. -#> 5 beta[3] 0.687 0.685 0.271 0.269 0.254 1.13 1.00 2212. -#> 6 log_lik[1] -0.515 -0.509 0.101 0.0967 -0.698 -0.360 1.00 2038. -#> 7 log_lik[2] -0.402 -0.380 0.150 0.140 -0.673 -0.195 1.00 2565. -#> 8 log_lik[3] -0.498 -0.467 0.223 0.212 -0.922 -0.201 1.00 2174. -#> 9 log_lik[4] -0.449 -0.430 0.154 0.148 -0.731 -0.232 0.999 1785. -#> 10 log_lik[5] -1.19 -1.17 0.283 0.281 -1.69 -0.772 1.00 2119. +#> 1 lp__ -65.9 -65.6 1.43 1.23 -68.7 -64.3 1.00 890. +#> 2 alpha 0.377 0.375 0.220 0.220 0.0153 0.743 1.00 2007. +#> 3 beta[1] -0.670 -0.663 0.247 0.244 -1.10 -0.273 1.00 1880. +#> 4 beta[2] -0.268 -0.269 0.223 0.218 -0.637 0.106 1.00 1834. +#> 5 beta[3] 0.684 0.681 0.271 0.271 0.243 1.14 1.00 2163. +#> 6 log_lik[1] -0.516 -0.511 0.101 0.0973 -0.694 -0.363 1.00 2030. +#> 7 log_lik[2] -0.398 -0.38 0.143 0.141 -0.661 -0.204 1.00 2280. +#> 8 log_lik[3] -0.493 -0.463 0.216 0.196 -0.883 -0.197 1.00 1914. +#> 9 log_lik[4] -0.450 -0.430 0.153 0.148 -0.730 -0.229 1.00 2150. +#> 10 log_lik[5] -1.19 -1.16 0.288 0.283 -1.70 -0.769 1.00 2194. #> # ℹ 95 more rows #> # ℹ 1 more variable: ess_tail <dbl> @@ -211,7 +211,7 @@Examples
#> 2 alpha 0.364 #> 3 beta[1] -0.632 #> 4 beta[2] -0.259 -#> 5 beta[3] 0.648 +#> 5 beta[3] 0.649 #> 6 log_lik[1] -0.515 #> 7 log_lik[2] -0.394 #> 8 log_lik[3] -0.469 @@ -222,18 +222,18 @@Examples
fit_logistic_vb <- cmdstanr_example("logistic", method = "variational") fit_logistic_vb$summary() #> # A tibble: 106 × 7 -#> variable mean median sd mad q5 q95 -#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 lp__ -66.5 -66.0 1.82 1.62 -69.9 -64.3 -#> 2 lp_approx__ -1.96 -1.62 1.41 1.19 -4.62 -0.318 -#> 3 alpha 0.503 0.492 0.219 0.222 0.144 0.876 -#> 4 beta[1] -0.582 -0.568 0.301 0.288 -1.10 -0.0916 -#> 5 beta[2] -0.278 -0.275 0.185 0.183 -0.592 0.0118 -#> 6 beta[3] 0.688 0.696 0.301 0.307 0.211 1.18 -#> 7 log_lik[1] -0.480 -0.475 0.0981 0.0994 -0.648 -0.332 -#> 8 log_lik[2] -0.466 -0.443 0.190 0.185 -0.836 -0.197 -#> 9 log_lik[3] -0.470 -0.443 0.195 0.188 -0.838 -0.209 -#> 10 log_lik[4] -0.495 -0.480 0.152 0.148 -0.776 -0.277 +#> variable mean median sd mad q5 q95 +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp__ -66.2 -65.8 1.56 1.32 -69.2 -64.3 +#> 2 lp_approx__ -1.94 -1.61 1.34 1.15 -4.56 -0.362 +#> 3 alpha 0.448 0.454 0.184 0.186 0.144 0.745 +#> 4 beta[1] -0.710 -0.711 0.297 0.296 -1.20 -0.230 +#> 5 beta[2] -0.211 -0.214 0.213 0.218 -0.546 0.131 +#> 6 beta[3] 0.719 0.717 0.272 0.278 0.285 1.18 +#> 7 log_lik[1] -0.478 -0.474 0.0839 0.0865 -0.622 -0.351 +#> 8 log_lik[2] -0.391 -0.364 0.154 0.141 -0.698 -0.180 +#> 9 log_lik[3] -0.408 -0.382 0.181 0.171 -0.755 -0.162 +#> 10 log_lik[4] -0.483 -0.471 0.145 0.142 -0.745 -0.272 #> # ℹ 96 more rows print_example_program("schools") @@ -254,23 +254,25 @@Examples
#> target += normal_lpdf(y | theta, sigma); #> } fit_schools_mcmc <- cmdstanr_example("schools") -#> Warning: 146 of 4000 (4.0%) transitions ended with a divergence. +#> Warning: 679 of 4000 (17.0%) transitions ended with a divergence. +#> See https://mc-stan.org/misc/warnings for details. +#> Warning: 2 of 4 chains had an E-BFMI less than 0.3. #> See https://mc-stan.org/misc/warnings for details. fit_schools_mcmc$summary() #> # A tibble: 11 × 10 -#> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail -#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 lp__ -58.4 -58.5 5.06 5.42 -66.4 -50.2 1.02 139. 71.0 -#> 2 mu 6.36 6.39 4.24 4.27 -0.684 13.0 1.01 487. 345. -#> 3 tau 5.42 4.61 3.53 3.33 1.25 12.3 1.02 136. 67.5 -#> 4 theta[1] 9.11 8.41 7.20 6.29 -1.35 21.8 1.01 774. 601. -#> 5 theta[2] 6.65 6.69 5.88 5.59 -2.86 16.2 1.01 1011. 2093. -#> 6 theta[3] 5.38 5.61 6.74 6.18 -5.83 15.9 1.01 863. 1953. -#> 7 theta[4] 6.59 6.59 6.01 5.73 -2.94 16.6 1.01 946. 2109. -#> 8 theta[5] 4.37 4.72 5.66 5.51 -5.63 12.8 1.01 745. 2276. -#> 9 theta[6] 5.28 5.49 5.96 5.60 -4.99 14.6 1.01 879. 1751. -#> 10 theta[7] 9.06 8.65 6.17 5.72 -0.151 20.1 1.01 678. 1072. -#> 11 theta[8] 6.91 6.84 6.86 6.09 -4.10 17.8 1.01 939. 2286. +#> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp__ -55.2 -56.2 7.20 8.44 -65.7 -44.5 1.35 9.26 502. +#> 2 mu 5.92 4.83 3.74 2.79 0.0739 12.5 1.14 269. 979. +#> 3 tau 4.29 3.34 3.72 3.44 0.650 11.7 1.35 9.24 5.53 +#> 4 theta[1] 8.28 6.13 6.22 3.70 0.148 20.3 1.19 199. 1448. +#> 5 theta[2] 6.41 5.49 5.11 3.45 -1.72 15.4 1.10 460. 1260. +#> 6 theta[3] 5.14 4.33 5.96 3.94 -5.01 15.0 1.23 782. 1447. +#> 7 theta[4] 5.87 4.59 5.16 3.49 -2.48 15.2 1.31 730. 948. +#> 8 theta[5] 4.56 4.37 5.04 4.04 -4.72 12.5 1.28 643. 1270. +#> 9 theta[6] 5.08 4.56 5.20 3.35 -3.77 13.6 1.12 659. 1302. +#> 10 theta[7] 8.06 6.46 5.45 3.53 0.928 18.2 1.05 73.8 1255. +#> 11 theta[8] 6.38 5.50 6.15 3.54 -2.77 17.5 1.11 260. 1699. print_example_program("schools_ncp") #> data { @@ -295,38 +297,40 @@Examples
fit_schools_ncp_mcmc <- cmdstanr_example("schools_ncp") fit_schools_ncp_mcmc$summary() #> # A tibble: 19 × 10 -#> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail -#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 lp__ -46.9 -4.66e+1 2.49 2.36 -51.6 -43.6 1.00 1229. 1844. -#> 2 mu 6.43 6.46e+0 4.08 3.93 -0.238 13.1 1.00 2985. 2197. -#> 3 tau 4.74 4.03e+0 3.70 3.41 0.334 11.9 1.00 1724. 1496. -#> 4 theta_r… 0.351 3.53e-1 0.979 0.955 -1.26 1.96 1.00 3404. 2385. -#> 5 theta_r… 0.0484 6.67e-2 0.924 0.913 -1.49 1.53 1.00 3680. 2435. -#> 6 theta_r… -0.131 -1.50e-1 0.962 0.938 -1.71 1.50 1.00 3067. 2557. -#> 7 theta_r… 0.0221 5.18e-3 0.919 0.905 -1.49 1.59 1.00 3748. 2583. -#> 8 theta_r… -0.273 -3.14e-1 0.931 0.892 -1.77 1.34 1.00 4139. 2891. -#> 9 theta_r… -0.157 -1.57e-1 0.930 0.929 -1.69 1.36 1.00 3560. 2797. -#> 10 theta_r… 0.363 4.01e-1 0.924 0.911 -1.15 1.87 1.00 3687. 2941. -#> 11 theta_r… 0.0558 7.25e-2 0.951 0.924 -1.55 1.65 1.00 4077. 2555. -#> 12 theta[1] 8.85 8.09e+0 6.85 5.77 -0.638 21.2 1.00 2994. 2531. -#> 13 theta[2] 6.79 6.75e+0 5.59 5.01 -2.46 15.9 1.00 4593. 3367. -#> 14 theta[3] 5.40 5.88e+0 6.64 5.63 -5.94 15.5 1.00 3448. 3181. -#> 15 theta[4] 6.65 6.62e+0 5.69 5.09 -2.25 16.0 1.00 4537. 3155. -#> 16 theta[5] 4.78 5.06e+0 5.54 5.26 -4.69 13.2 1.00 3996. 3191. -#> 17 theta[6] 5.44 5.63e+0 5.97 5.29 -4.66 14.8 1.00 3934. 2877. -#> 18 theta[7] 8.83 8.37e+0 5.98 5.56 0.141 19.4 1.00 4104. 3336. -#> 19 theta[8] 6.89 6.73e+0 6.34 5.33 -3.03 17.1 1.00 3988. 3025. +#> variable mean median sd mad q5 q95 rhat ess_bulk +#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 lp__ -46.9 -46.6 2.41 2.29 -51.3 -43.5 1.00 1454. +#> 2 mu 6.51 6.48 4.11 4.18 -0.0368 13.3 1.00 3392. +#> 3 tau 4.77 3.96 3.63 3.48 0.449 11.8 1.00 2260. +#> 4 theta_raw[1] 0.377 0.379 0.975 0.954 -1.26 1.95 1.00 4065. +#> 5 theta_raw[2] 0.0526 0.0555 0.895 0.884 -1.43 1.53 1.00 3436. +#> 6 theta_raw[3] -0.143 -0.156 0.970 0.957 -1.73 1.48 1.00 3686. +#> 7 theta_raw[4] 0.0219 0.0261 0.906 0.930 -1.48 1.50 1.00 3671. +#> 8 theta_raw[5] -0.263 -0.285 0.920 0.904 -1.76 1.29 1.00 4086. +#> 9 theta_raw[6] -0.130 -0.144 0.930 0.917 -1.64 1.42 1.00 4353. +#> 10 theta_raw[7] 0.369 0.376 0.918 0.911 -1.15 1.87 1.00 3860. +#> 11 theta_raw[8] 0.0659 0.0592 0.971 0.988 -1.51 1.69 1.00 3781. +#> 12 theta[1] 9.01 8.30 6.74 5.76 -0.246 21.2 1.00 3647. +#> 13 theta[2] 6.88 6.83 5.35 4.89 -1.89 15.7 1.00 4377. +#> 14 theta[3] 5.45 5.79 6.51 5.55 -5.90 15.5 1.00 3662. +#> 15 theta[4] 6.73 6.60 5.73 5.22 -2.58 16.1 1.00 3999. +#> 16 theta[5] 4.91 5.16 5.52 4.90 -4.78 13.6 1.00 4517. +#> 17 theta[6] 5.64 5.91 6.01 5.33 -4.77 15.0 1.00 4197. +#> 18 theta[7] 8.87 8.36 5.95 5.37 0.312 19.6 1.00 3846. +#> 19 theta[8] 7.06 6.77 6.80 5.70 -3.70 18.3 1.00 3866. +#> # ℹ 1 more variable: ess_tail <dbl> # optimization fails for hierarchical model cmdstanr_example("schools", "optimize", quiet = FALSE) -#> Initial log joint probability = -56.2449 +#> Initial log joint probability = -53.966 #> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes -#> 99 121.706 0.109668 2.48002e+09 1 1 174 +#> 99 123.351 0.058381 2.09576e+09 0.5049 0.5049 181 #> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes -#> 187 244.785 0.253817 8.00296 1e-12 0.001 400 LS failed, Hessian reset -#> Line search failed to achieve a sufficient decrease, no more progress can be made +#> 184 248.826 0.4674 3.92933e+16 1e-12 0.001 384 LS failed, Hessian reset #> Chain 1 Optimization terminated with error: +#> Chain 1 Line search failed to achieve a sufficient decrease, no more progress can be made #> Warning: Fitting finished unexpectedly! Use the $output() method for more information. +#> Finished in 0.2 seconds. #> Error: Fitting failed. Unable to print. # } @@ -340,11 +344,11 @@Examples
CmdStanR global options — cmdstanr_global_options • cmdstanr + + +++ + + + + + + + diff --git a/docs/reference/draws_to_csv.html b/docs/reference/draws_to_csv.html index 355224d7..2ce97f97 100644 --- a/docs/reference/draws_to_csv.html +++ b/docs/reference/draws_to_csv.html @@ -1,7 +1,5 @@ -+ + + + ++ + ++ + ++ +++ + +These options can be set via
+options()
for an entire R session.++ +Details
+ +
- +
cmdstanr_draws_format
: Which format provided by the posterior +package should be used when returning the posterior or approximate posterior +draws? The default depends on the model fitting method. See +draws for more details.- +
cmdstanr_force_recompile
: Should the default be to recompile models +even if there were no Stan code changes since last compiled? See +compile for more details. The default isFALSE
.- +
cmdstanr_max_rows
: The maximum number of rows of output to print when +using the$print()
method. The default is 10.- +
cmdstanr_no_ver_check
: Should the check for a more recent version of +CmdStan be disabled? The default isFALSE
.- +
cmdstanr_output_dir
: The directory where CmdStan should write its output +CSV files when fitting models. The default is a temporary directory. Files in +a temporary directory are removed as part of R garbage collection, while +files in an explicitly defined directory are not automatically deleted.- +
cmdstanr_verbose
: Should more information be printed +when compiling or running models, including showing how CmdStan was called +internally? The default isFALSE
.- +
cmdstanr_warn_inits
: Should a warning be thrown if initial values are +only provided for a subset of parameters? The default isTRUE
.- +
cmdstanr_write_stan_file_dir
: The directory wherewrite_stan_file()
+should write Stan files. The default is a temporary directory. Files in +a temporary directory are removed as part of R garbage collection, while +files in an explicitly defined directory are not automatically deleted.- +
mc.cores
: The number of cores to use for various parallelization tasks +(e.g. running MCMC chains, installing CmdStan). The default depends on the +use case and is documented with the methods that make use ofmc.cores
.Write posterior draws objects to CSV files suitable for running standalone generated -quantities with CmdStan. — draws_to_csv • cmdstanr Write posterior draws objects to CSV files suitable for running standalone generated quantities with CmdStan. — draws_to_csv • cmdstanr CmdStan knitr engine for Stan — eng_cmdstan • cmdstanr CmdStan knitr engine for Stan — eng_cmdstan • cmdstanr cmdstanr - 0.7.0 + 0.8.1Examples
Run CmdStan's stansummary and diagnose utilities — fit-method-cmdstan_summary • cmdstanr Run CmdStan's stansummary and diagnose utilities — fit-method-cmdstan_summary • cmdstanr cmdstanr - 0.7.0 + 0.8.1Examples
diff --git a/docs/reference/fit-method-code.html b/docs/reference/fit-method-code.html index e2080e07..cdf0e851 100644 --- a/docs/reference/fit-method-code.html +++ b/docs/reference/fit-method-code.html @@ -1,5 +1,5 @@ -# \dontrun{ fit <- cmdstanr_example("logistic") fit$cmdstan_diagnose() -#> Processing csv files: /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131001-1-2718e9.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131001-2-2718e9.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131001-3-2718e9.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131001-4-2718e9.csv +#> Processing csv files: /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/logistic-202407021533-1-42dde2.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/logistic-202407021533-2-42dde2.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/logistic-202407021533-3-42dde2.csv, /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/logistic-202407021533-4-42dde2.csv #> #> Checking sampler transitions treedepth. #> Treedepth satisfactory for all transitions. @@ -169,123 +169,123 @@
Examples
#> Inference for Stan model: logistic_model #> 4 chains: each with iter=(1000,1000,1000,1000); warmup=(0,0,0,0); thin=(1,1,1,1); 4000 iterations saved. #> -#> Warmup took (0.022, 0.023, 0.023, 0.023) seconds, 0.091 seconds total -#> Sampling took (0.071, 0.070, 0.070, 0.070) seconds, 0.28 seconds total +#> Warmup took (0.043, 0.046, 0.042, 0.041) seconds, 0.17 seconds total +#> Sampling took (0.15, 0.15, 0.16, 0.15) seconds, 0.61 seconds total #> #> Mean MCSE StdDev 5% 50% 95% N_Eff N_Eff/s R_hat #> -#> lp__ -6.6e+01 3.1e-02 1.4 -69 -6.6e+01 -6.4e+01 2120 7545 1.0 -#> accept_stat__ 0.91 1.4e-03 0.10 0.69 0.95 1.0 5437 19350 1.0e+00 -#> stepsize__ 0.74 2.1e-02 0.030 0.70 0.75 0.78 2.0 7.1 1.1e+13 -#> treedepth__ 2.4 8.8e-03 0.53 2.0 2.0 3.0 3601 12815 1.0e+00 -#> n_leapfrog__ 5.3 3.3e-02 2.0 3.0 7.0 7.0 3773 13428 1.0e+00 +#> lp__ -6.6e+01 3.3e-02 1.4 -69 -6.6e+01 -6.4e+01 1837 3007 1.0 +#> accept_stat__ 0.90 0.011 0.12 0.65 0.94 1.0 123 202 1.0e+00 +#> stepsize__ 0.77 0.038 0.054 0.73 0.77 0.86 2.0 3.3 1.9e+13 +#> treedepth__ 2.3 0.10 0.51 2.0 2.0 3.0 26 42 1.1e+00 +#> n_leapfrog__ 4.9 0.35 2.0 3.0 3.0 7.0 34 55 1.0e+00 #> divergent__ 0.00 nan 0.00 0.00 0.00 0.00 nan nan nan -#> energy__ 68 4.9e-02 2.0 65 68 72 1750 6227 1.0e+00 +#> energy__ 68 0.049 2.0 65 68 72 1652 2704 1.0e+00 #> -#> alpha 3.8e-01 3.5e-03 0.22 0.026 3.8e-01 7.5e-01 3978 14156 1.00 -#> beta[1] -6.7e-01 4.1e-03 0.26 -1.1 -6.6e-01 -2.6e-01 3779 13447 1.0 -#> beta[2] -2.7e-01 3.5e-03 0.23 -0.64 -2.7e-01 1.0e-01 4061 14450 1.0 -#> beta[3] 6.7e-01 4.3e-03 0.27 0.24 6.7e-01 1.1e+00 3879 13804 1.00 -#> log_lik[1] -5.1e-01 1.5e-03 0.097 -0.68 -5.1e-01 -3.7e-01 3951 14060 1.00 -#> log_lik[2] -4.1e-01 2.3e-03 0.15 -0.69 -3.9e-01 -1.9e-01 4167 14830 1.00 -#> log_lik[3] -4.9e-01 3.3e-03 0.21 -0.88 -4.6e-01 -2.1e-01 4010 14272 1.00 -#> log_lik[4] -4.5e-01 2.5e-03 0.15 -0.72 -4.4e-01 -2.4e-01 3751 13350 1.0 -#> log_lik[5] -1.2e+00 4.4e-03 0.28 -1.7 -1.2e+00 -7.6e-01 4068 14476 1.0 -#> log_lik[6] -5.9e-01 3.0e-03 0.19 -0.96 -5.7e-01 -3.2e-01 4275 15213 1.0 -#> log_lik[7] -6.4e-01 2.0e-03 0.13 -0.86 -6.4e-01 -4.5e-01 4183 14885 1.0 -#> log_lik[8] -2.8e-01 2.3e-03 0.14 -0.55 -2.5e-01 -1.1e-01 3659 13021 1.0 -#> log_lik[9] -6.9e-01 2.7e-03 0.17 -1.0 -6.7e-01 -4.4e-01 4128 14690 1.0 -#> log_lik[10] -7.3e-01 3.6e-03 0.22 -1.1 -7.1e-01 -4.1e-01 3894 13857 1.00 -#> log_lik[11] -2.8e-01 2.1e-03 0.13 -0.54 -2.6e-01 -1.1e-01 3822 13601 1.00 -#> log_lik[12] -5.0e-01 3.8e-03 0.24 -0.95 -4.6e-01 -1.9e-01 3952 14065 1.00 -#> log_lik[13] -6.6e-01 3.3e-03 0.21 -1.0 -6.4e-01 -3.5e-01 4181 14878 1.0 -#> log_lik[14] -3.6e-01 2.6e-03 0.17 -0.67 -3.2e-01 -1.4e-01 4021 14308 1.00 -#> log_lik[15] -2.8e-01 1.7e-03 0.10 -0.47 -2.6e-01 -1.3e-01 3858 13730 1.00 -#> log_lik[16] -2.8e-01 1.5e-03 0.091 -0.44 -2.7e-01 -1.5e-01 3706 13189 1.00 -#> log_lik[17] -1.6e+00 5.0e-03 0.30 -2.1 -1.6e+00 -1.1e+00 3612 12854 1.0 -#> log_lik[18] -4.8e-01 1.8e-03 0.11 -0.68 -4.7e-01 -3.1e-01 4081 14523 1.0 -#> log_lik[19] -2.3e-01 1.3e-03 0.079 -0.37 -2.2e-01 -1.2e-01 3751 13347 1.0 -#> log_lik[20] -1.1e-01 1.3e-03 0.078 -0.26 -9.4e-02 -2.8e-02 3798 13516 1.00 -#> log_lik[21] -2.1e-01 1.5e-03 0.091 -0.39 -2.0e-01 -9.3e-02 3626 12906 1.00 -#> log_lik[22] -5.7e-01 2.4e-03 0.15 -0.86 -5.5e-01 -3.5e-01 4061 14452 1.0 -#> log_lik[23] -3.3e-01 2.3e-03 0.15 -0.61 -3.1e-01 -1.4e-01 3872 13780 1.0 -#> log_lik[24] -1.4e-01 1.1e-03 0.069 -0.27 -1.2e-01 -5.1e-02 3743 13321 1.00 -#> log_lik[25] -4.6e-01 1.9e-03 0.12 -0.68 -4.5e-01 -2.8e-01 3975 14145 1.0 -#> log_lik[26] -1.5e+00 5.8e-03 0.35 -2.1 -1.5e+00 -1.0e+00 3699 13164 1.0 -#> log_lik[27] -3.1e-01 2.0e-03 0.13 -0.55 -2.9e-01 -1.4e-01 3831 13633 1.00 -#> log_lik[28] -4.4e-01 1.4e-03 0.086 -0.59 -4.4e-01 -3.1e-01 3970 14130 1.0 -#> log_lik[29] -7.3e-01 3.4e-03 0.22 -1.1 -7.1e-01 -4.0e-01 4258 15153 1.0 -#> log_lik[30] -6.9e-01 3.1e-03 0.20 -1.1 -6.8e-01 -4.0e-01 4121 14666 1.00 -#> log_lik[31] -4.9e-01 2.6e-03 0.17 -0.81 -4.7e-01 -2.5e-01 4046 14398 1.0 -#> log_lik[32] -4.2e-01 1.7e-03 0.11 -0.62 -4.1e-01 -2.6e-01 3986 14185 1.0 -#> log_lik[33] -4.1e-01 2.0e-03 0.13 -0.65 -4.0e-01 -2.2e-01 4122 14669 1.0 -#> log_lik[34] -6.5e-02 9.1e-04 0.054 -0.17 -5.0e-02 -1.3e-02 3544 12612 1.0 -#> log_lik[35] -5.9e-01 2.8e-03 0.18 -0.92 -5.7e-01 -3.2e-01 4411 15696 1.0 -#> log_lik[36] -3.3e-01 2.2e-03 0.14 -0.58 -3.0e-01 -1.4e-01 4001 14239 1.0 -#> log_lik[37] -6.9e-01 3.4e-03 0.22 -1.1 -6.7e-01 -3.7e-01 4045 14394 1.0 -#> log_lik[38] -3.2e-01 2.4e-03 0.15 -0.60 -2.9e-01 -1.2e-01 3854 13717 1.0 -#> log_lik[39] -1.8e-01 1.7e-03 0.11 -0.38 -1.5e-01 -5.2e-02 3915 13934 1.00 -#> log_lik[40] -6.8e-01 2.1e-03 0.13 -0.92 -6.7e-01 -4.8e-01 4145 14752 1.0 -#> log_lik[41] -1.1e+00 4.2e-03 0.26 -1.6 -1.1e+00 -7.3e-01 3929 13983 1.0 -#> log_lik[42] -9.3e-01 3.2e-03 0.20 -1.3 -9.1e-01 -6.3e-01 3967 14116 1.0 -#> log_lik[43] -4.1e-01 4.0e-03 0.26 -0.91 -3.4e-01 -1.0e-01 4191 14913 1.0 -#> log_lik[44] -1.2e+00 3.1e-03 0.19 -1.5 -1.2e+00 -9.0e-01 3709 13201 1.00 -#> log_lik[45] -3.6e-01 1.8e-03 0.11 -0.56 -3.4e-01 -1.9e-01 4009 14267 1.00 -#> log_lik[46] -5.9e-01 2.0e-03 0.13 -0.82 -5.8e-01 -3.8e-01 4248 15119 1.00 -#> log_lik[47] -3.1e-01 2.2e-03 0.13 -0.55 -2.9e-01 -1.3e-01 3696 13153 1.0 -#> log_lik[48] -3.2e-01 1.4e-03 0.085 -0.47 -3.2e-01 -2.0e-01 3847 13691 1.0 -#> log_lik[49] -3.2e-01 1.3e-03 0.082 -0.46 -3.1e-01 -2.0e-01 3716 13223 1.00 -#> log_lik[50] -1.3e+00 5.4e-03 0.33 -1.9 -1.3e+00 -8.1e-01 3859 13734 1.0 -#> log_lik[51] -2.9e-01 1.6e-03 0.096 -0.46 -2.8e-01 -1.5e-01 3714 13218 1.0 -#> log_lik[52] -8.4e-01 2.2e-03 0.14 -1.1 -8.3e-01 -6.2e-01 3985 14182 1.00 -#> log_lik[53] -4.0e-01 2.1e-03 0.13 -0.66 -3.9e-01 -2.1e-01 3970 14127 1.00 -#> log_lik[54] -3.7e-01 2.2e-03 0.14 -0.62 -3.5e-01 -1.8e-01 4008 14263 1.00 -#> log_lik[55] -3.9e-01 2.2e-03 0.14 -0.65 -3.7e-01 -1.9e-01 4107 14614 1.00 -#> log_lik[56] -3.2e-01 3.2e-03 0.20 -0.70 -2.8e-01 -9.3e-02 3745 13326 1.0 -#> log_lik[57] -6.5e-01 1.8e-03 0.12 -0.85 -6.5e-01 -4.8e-01 3918 13943 1.0 -#> log_lik[58] -9.4e-01 5.3e-03 0.35 -1.6 -9.0e-01 -4.5e-01 4317 15364 1.0 -#> log_lik[59] -1.4e+00 5.7e-03 0.35 -2.0 -1.3e+00 -8.4e-01 3801 13527 1.0 -#> log_lik[60] -9.8e-01 2.5e-03 0.16 -1.3 -9.7e-01 -7.4e-01 3902 13886 1.00 -#> log_lik[61] -5.4e-01 1.5e-03 0.097 -0.70 -5.3e-01 -3.9e-01 4078 14513 1.00 -#> log_lik[62] -8.9e-01 5.0e-03 0.31 -1.5 -8.5e-01 -4.4e-01 3852 13708 1.00 -#> log_lik[63] -1.2e-01 1.3e-03 0.077 -0.27 -9.9e-02 -3.2e-02 3330 11849 1.00 -#> log_lik[64] -9.0e-01 3.8e-03 0.25 -1.4 -8.8e-01 -5.3e-01 4445 15817 1.00 -#> log_lik[65] -2.0e+00 9.8e-03 0.61 -3.1 -2.0e+00 -1.1e+00 3783 13462 1.00 -#> log_lik[66] -5.1e-01 2.1e-03 0.13 -0.74 -4.9e-01 -3.1e-01 4154 14782 1.0 -#> log_lik[67] -2.8e-01 1.4e-03 0.085 -0.43 -2.7e-01 -1.6e-01 3827 13618 1.0 -#> log_lik[68] -1.1e+00 3.8e-03 0.24 -1.5 -1.0e+00 -6.8e-01 4102 14597 1.00 -#> log_lik[69] -4.3e-01 1.4e-03 0.085 -0.58 -4.3e-01 -3.0e-01 3842 13673 1.0 -#> log_lik[70] -6.4e-01 3.6e-03 0.24 -1.1 -6.1e-01 -3.1e-01 4371 15555 1.00 -#> log_lik[71] -6.1e-01 3.2e-03 0.21 -0.99 -5.9e-01 -3.1e-01 4401 15663 1.0 -#> log_lik[72] -4.6e-01 2.7e-03 0.17 -0.79 -4.5e-01 -2.2e-01 4214 14996 1.0 -#> log_lik[73] -1.5e+00 6.1e-03 0.37 -2.1 -1.4e+00 -9.2e-01 3671 13065 1.0 -#> log_lik[74] -9.5e-01 3.0e-03 0.19 -1.3 -9.4e-01 -6.7e-01 3944 14035 1.00 -#> log_lik[75] -1.2e+00 6.4e-03 0.39 -1.8 -1.1e+00 -5.9e-01 3678 13088 1.00 -#> log_lik[76] -3.7e-01 2.1e-03 0.13 -0.62 -3.5e-01 -1.8e-01 3976 14151 1.00 -#> log_lik[77] -8.8e-01 2.2e-03 0.14 -1.1 -8.8e-01 -6.6e-01 4034 14357 1.0 -#> log_lik[78] -4.8e-01 2.7e-03 0.17 -0.79 -4.6e-01 -2.4e-01 4097 14581 1.00 -#> log_lik[79] -7.6e-01 2.9e-03 0.18 -1.1 -7.4e-01 -4.8e-01 3889 13840 1.0 -#> log_lik[80] -5.5e-01 3.0e-03 0.20 -0.91 -5.2e-01 -2.7e-01 4407 15683 1.00 -#> log_lik[81] -1.7e-01 1.7e-03 0.10 -0.37 -1.4e-01 -4.8e-02 3780 13450 1.0 -#> log_lik[82] -2.3e-01 2.3e-03 0.14 -0.51 -2.0e-01 -6.3e-02 3943 14031 1.00 -#> log_lik[83] -3.4e-01 1.3e-03 0.081 -0.48 -3.4e-01 -2.2e-01 3766 13404 1.00 -#> log_lik[84] -2.7e-01 1.5e-03 0.090 -0.44 -2.6e-01 -1.4e-01 3767 13405 1.00 -#> log_lik[85] -1.3e-01 1.2e-03 0.075 -0.28 -1.1e-01 -4.1e-02 3780 13453 1.00 -#> log_lik[86] -1.1e+00 5.0e-03 0.32 -1.7 -1.1e+00 -6.4e-01 4173 14850 1.00 -#> log_lik[87] -8.3e-01 2.0e-03 0.13 -1.1 -8.2e-01 -6.2e-01 4183 14887 1.00 -#> log_lik[88] -7.8e-01 3.8e-03 0.24 -1.2 -7.5e-01 -4.2e-01 4157 14794 1.0 -#> log_lik[89] -1.3e+00 5.1e-03 0.32 -1.9 -1.2e+00 -7.9e-01 3879 13805 1.00 -#> log_lik[90] -2.6e-01 2.2e-03 0.14 -0.52 -2.4e-01 -9.4e-02 4008 14262 1.0 -#> log_lik[91] -3.9e-01 2.1e-03 0.13 -0.63 -3.8e-01 -2.1e-01 4144 14746 1.0 -#> log_lik[92] -1.5e+00 5.6e-03 0.34 -2.1 -1.5e+00 -9.7e-01 3829 13625 1.00 -#> log_lik[93] -7.5e-01 3.4e-03 0.21 -1.1 -7.2e-01 -4.4e-01 3888 13835 1.00 -#> log_lik[94] -3.2e-01 1.4e-03 0.087 -0.47 -3.1e-01 -1.9e-01 3803 13533 1.00 -#> log_lik[95] -3.9e-01 1.9e-03 0.12 -0.60 -3.8e-01 -2.2e-01 3889 13841 1.0 -#> log_lik[96] -1.6e+00 4.8e-03 0.29 -2.1 -1.6e+00 -1.1e+00 3601 12815 1.00 -#> log_lik[97] -4.3e-01 1.6e-03 0.10 -0.61 -4.2e-01 -2.7e-01 4067 14472 1.0 -#> log_lik[98] -1.0e+00 5.6e-03 0.37 -1.7 -1.0e+00 -5.2e-01 4342 15452 1.0 -#> log_lik[99] -7.0e-01 2.2e-03 0.15 -0.96 -6.9e-01 -4.7e-01 4351 15484 1.00 -#> log_lik[100] -3.9e-01 1.5e-03 0.097 -0.55 -3.8e-01 -2.4e-01 4019 14301 1.00 +#> alpha 3.8e-01 3.5e-03 0.22 0.023 3.8e-01 7.4e-01 3919 6414 1.0 +#> beta[1] -6.7e-01 3.8e-03 0.25 -1.1 -6.7e-01 -2.6e-01 4387 7180 1.0 +#> beta[2] -2.7e-01 3.5e-03 0.23 -0.64 -2.7e-01 1.1e-01 4209 6889 1.00 +#> beta[3] 6.8e-01 4.3e-03 0.26 0.25 6.7e-01 1.1e+00 3682 6026 1.00 +#> log_lik[1] -5.1e-01 1.5e-03 0.099 -0.69 -5.1e-01 -3.6e-01 4109 6725 1.0 +#> log_lik[2] -4.0e-01 2.2e-03 0.15 -0.67 -3.8e-01 -2.0e-01 4736 7751 1.0 +#> log_lik[3] -5.0e-01 3.4e-03 0.22 -0.90 -4.6e-01 -2.0e-01 4043 6617 1.0 +#> log_lik[4] -4.5e-01 2.4e-03 0.15 -0.73 -4.3e-01 -2.4e-01 3941 6450 1.0 +#> log_lik[5] -1.2e+00 4.5e-03 0.28 -1.7 -1.2e+00 -7.7e-01 3844 6291 1.00 +#> log_lik[6] -5.9e-01 2.9e-03 0.19 -0.93 -5.7e-01 -3.2e-01 4123 6748 1.00 +#> log_lik[7] -6.4e-01 1.9e-03 0.13 -0.87 -6.3e-01 -4.5e-01 4625 7569 1.0 +#> log_lik[8] -2.8e-01 2.1e-03 0.13 -0.53 -2.5e-01 -1.1e-01 3984 6520 1.00 +#> log_lik[9] -6.9e-01 2.6e-03 0.17 -0.99 -6.8e-01 -4.4e-01 4294 7028 1.0 +#> log_lik[10] -7.4e-01 3.7e-03 0.23 -1.2 -7.1e-01 -4.0e-01 3964 6488 1.0 +#> log_lik[11] -2.8e-01 2.1e-03 0.12 -0.51 -2.6e-01 -1.2e-01 3501 5729 1.00 +#> log_lik[12] -5.0e-01 3.5e-03 0.23 -0.93 -4.6e-01 -1.9e-01 4348 7116 1.0 +#> log_lik[13] -6.6e-01 3.2e-03 0.21 -1.0 -6.3e-01 -3.5e-01 4224 6914 1.00 +#> log_lik[14] -3.6e-01 2.7e-03 0.17 -0.68 -3.3e-01 -1.3e-01 4013 6568 1.0 +#> log_lik[15] -2.8e-01 1.8e-03 0.11 -0.47 -2.6e-01 -1.3e-01 3538 5790 1.00 +#> log_lik[16] -2.8e-01 1.4e-03 0.086 -0.43 -2.7e-01 -1.5e-01 3558 5822 1.00 +#> log_lik[17] -1.6e+00 4.8e-03 0.29 -2.1 -1.6e+00 -1.2e+00 3654 5981 1.0 +#> log_lik[18] -4.8e-01 1.6e-03 0.11 -0.67 -4.7e-01 -3.1e-01 4457 7294 1.00 +#> log_lik[19] -2.3e-01 1.2e-03 0.075 -0.37 -2.3e-01 -1.2e-01 3731 6107 1.00 +#> log_lik[20] -1.1e-01 1.4e-03 0.080 -0.26 -9.3e-02 -2.7e-02 3471 5681 1.0 +#> log_lik[21] -2.1e-01 1.5e-03 0.086 -0.37 -2.0e-01 -9.3e-02 3393 5554 1.00 +#> log_lik[22] -5.7e-01 2.3e-03 0.15 -0.83 -5.6e-01 -3.5e-01 4157 6803 1.00 +#> log_lik[23] -3.3e-01 2.1e-03 0.14 -0.60 -3.1e-01 -1.4e-01 4351 7120 1.00 +#> log_lik[24] -1.4e-01 1.1e-03 0.066 -0.26 -1.2e-01 -5.2e-02 3635 5949 1.00 +#> log_lik[25] -4.6e-01 1.8e-03 0.12 -0.68 -4.5e-01 -2.8e-01 4638 7591 1.0 +#> log_lik[26] -1.5e+00 5.1e-03 0.34 -2.1 -1.5e+00 -1.0e+00 4383 7173 1.00 +#> log_lik[27] -3.1e-01 2.0e-03 0.12 -0.52 -2.9e-01 -1.4e-01 3553 5815 1.00 +#> log_lik[28] -4.4e-01 1.3e-03 0.083 -0.59 -4.4e-01 -3.2e-01 4086 6688 1.0 +#> log_lik[29] -7.3e-01 3.4e-03 0.23 -1.1 -7.0e-01 -3.9e-01 4476 7326 1.00 +#> log_lik[30] -6.9e-01 2.9e-03 0.19 -1.0 -6.8e-01 -4.1e-01 4511 7383 1.0 +#> log_lik[31] -4.9e-01 2.6e-03 0.16 -0.78 -4.7e-01 -2.6e-01 3899 6381 1.00 +#> log_lik[32] -4.2e-01 1.8e-03 0.11 -0.61 -4.1e-01 -2.7e-01 3671 6009 1.00 +#> log_lik[33] -4.1e-01 1.9e-03 0.13 -0.65 -4.0e-01 -2.3e-01 4622 7564 1.0 +#> log_lik[34] -6.4e-02 8.5e-04 0.050 -0.16 -5.0e-02 -1.3e-02 3474 5686 1.00 +#> log_lik[35] -5.9e-01 2.6e-03 0.18 -0.92 -5.7e-01 -3.2e-01 4937 8080 1.00 +#> log_lik[36] -3.2e-01 1.9e-03 0.13 -0.57 -3.1e-01 -1.5e-01 4737 7753 1.00 +#> log_lik[37] -7.0e-01 3.5e-03 0.23 -1.1 -6.7e-01 -3.7e-01 4158 6805 1.0 +#> log_lik[38] -3.1e-01 2.4e-03 0.15 -0.60 -2.9e-01 -1.2e-01 4035 6604 1.00 +#> log_lik[39] -1.8e-01 1.8e-03 0.11 -0.39 -1.5e-01 -5.2e-02 3644 5964 1.0 +#> log_lik[40] -6.8e-01 1.9e-03 0.13 -0.91 -6.7e-01 -4.8e-01 4470 7316 1.0 +#> log_lik[41] -1.1e+00 4.1e-03 0.25 -1.6 -1.1e+00 -7.6e-01 3765 6162 1.00 +#> log_lik[42] -9.3e-01 3.0e-03 0.20 -1.3 -9.1e-01 -6.3e-01 4354 7127 1.0 +#> log_lik[43] -4.1e-01 3.7e-03 0.26 -0.91 -3.5e-01 -1.0e-01 4788 7836 1.00 +#> log_lik[44] -1.2e+00 3.1e-03 0.19 -1.5 -1.2e+00 -8.9e-01 3623 5929 1.0 +#> log_lik[45] -3.6e-01 1.8e-03 0.12 -0.57 -3.4e-01 -1.9e-01 4021 6581 1.0 +#> log_lik[46] -5.8e-01 1.9e-03 0.13 -0.81 -5.7e-01 -3.9e-01 4786 7833 1.0 +#> log_lik[47] -3.1e-01 2.0e-03 0.13 -0.55 -2.9e-01 -1.4e-01 4246 6949 1.0 +#> log_lik[48] -3.2e-01 1.3e-03 0.082 -0.47 -3.2e-01 -2.0e-01 4166 6819 1.00 +#> log_lik[49] -3.2e-01 1.3e-03 0.079 -0.46 -3.1e-01 -2.0e-01 3546 5804 1.00 +#> log_lik[50] -1.3e+00 4.9e-03 0.33 -1.9 -1.3e+00 -8.0e-01 4492 7352 1.00 +#> log_lik[51] -2.9e-01 1.4e-03 0.093 -0.46 -2.8e-01 -1.6e-01 4451 7284 1.00 +#> log_lik[52] -8.4e-01 2.2e-03 0.14 -1.1 -8.3e-01 -6.2e-01 4245 6948 1.0 +#> log_lik[53] -4.0e-01 2.1e-03 0.13 -0.63 -3.9e-01 -2.2e-01 3748 6134 1.00 +#> log_lik[54] -3.7e-01 2.2e-03 0.14 -0.63 -3.5e-01 -1.7e-01 4130 6759 1.0 +#> log_lik[55] -3.9e-01 2.0e-03 0.14 -0.64 -3.7e-01 -2.0e-01 4649 7608 1.0 +#> log_lik[56] -3.2e-01 2.8e-03 0.19 -0.69 -2.8e-01 -9.5e-02 4610 7546 1.00 +#> log_lik[57] -6.6e-01 1.8e-03 0.12 -0.86 -6.5e-01 -4.7e-01 4203 6879 1.0 +#> log_lik[58] -9.5e-01 5.3e-03 0.36 -1.6 -9.0e-01 -4.4e-01 4498 7362 1.00 +#> log_lik[59] -1.4e+00 5.4e-03 0.34 -2.0 -1.3e+00 -8.4e-01 4097 6706 1.0 +#> log_lik[60] -9.8e-01 2.5e-03 0.16 -1.3 -9.7e-01 -7.3e-01 4050 6628 1.0 +#> log_lik[61] -5.4e-01 1.5e-03 0.098 -0.71 -5.3e-01 -3.9e-01 4382 7171 1.0 +#> log_lik[62] -8.8e-01 4.7e-03 0.31 -1.4 -8.4e-01 -4.4e-01 4166 6818 1.00 +#> log_lik[63] -1.2e-01 1.2e-03 0.072 -0.25 -1.0e-01 -3.2e-02 3346 5477 1.00 +#> log_lik[64] -9.0e-01 3.7e-03 0.25 -1.4 -8.8e-01 -5.3e-01 4389 7183 1.00 +#> log_lik[65] -2.0e+00 9.9e-03 0.58 -3.0 -2.0e+00 -1.1e+00 3440 5629 1.00 +#> log_lik[66] -5.1e-01 2.2e-03 0.14 -0.75 -5.0e-01 -3.1e-01 3804 6226 1.00 +#> log_lik[67] -2.8e-01 1.3e-03 0.081 -0.42 -2.7e-01 -1.6e-01 4007 6558 1.00 +#> log_lik[68] -1.1e+00 3.7e-03 0.24 -1.5 -1.0e+00 -7.0e-01 4155 6800 1.00 +#> log_lik[69] -4.3e-01 1.4e-03 0.084 -0.58 -4.3e-01 -3.1e-01 3753 6142 1.00 +#> log_lik[70] -6.4e-01 3.5e-03 0.24 -1.1 -6.1e-01 -3.1e-01 4500 7365 1.00 +#> log_lik[71] -6.1e-01 3.0e-03 0.21 -0.99 -5.8e-01 -3.1e-01 4571 7481 1.00 +#> log_lik[72] -4.6e-01 2.6e-03 0.17 -0.78 -4.4e-01 -2.2e-01 4261 6974 1.00 +#> log_lik[73] -1.5e+00 5.8e-03 0.37 -2.1 -1.5e+00 -9.2e-01 4023 6585 1.0 +#> log_lik[74] -9.5e-01 3.1e-03 0.20 -1.3 -9.4e-01 -6.5e-01 4106 6721 1.00 +#> log_lik[75] -1.1e+00 5.9e-03 0.38 -1.9 -1.1e+00 -5.9e-01 4198 6870 1.00 +#> log_lik[76] -3.7e-01 2.2e-03 0.14 -0.63 -3.5e-01 -1.8e-01 3982 6517 1.0 +#> log_lik[77] -8.8e-01 2.2e-03 0.14 -1.1 -8.7e-01 -6.6e-01 4043 6618 1.0 +#> log_lik[78] -4.8e-01 2.5e-03 0.17 -0.79 -4.6e-01 -2.5e-01 4533 7418 1.00 +#> log_lik[79] -7.6e-01 3.0e-03 0.19 -1.1 -7.5e-01 -4.8e-01 4054 6635 1.0 +#> log_lik[80] -5.4e-01 2.8e-03 0.19 -0.89 -5.2e-01 -2.7e-01 4782 7827 1.0 +#> log_lik[81] -1.6e-01 1.6e-03 0.10 -0.37 -1.4e-01 -4.9e-02 4161 6810 1.0 +#> log_lik[82] -2.2e-01 2.1e-03 0.14 -0.49 -1.9e-01 -6.5e-02 4340 7103 1.0 +#> log_lik[83] -3.4e-01 1.3e-03 0.081 -0.48 -3.4e-01 -2.2e-01 3663 5996 1.0 +#> log_lik[84] -2.7e-01 1.5e-03 0.092 -0.44 -2.6e-01 -1.5e-01 3524 5767 1.00 +#> log_lik[85] -1.3e-01 1.3e-03 0.076 -0.28 -1.1e-01 -4.1e-02 3532 5780 1.0 +#> log_lik[86] -1.1e+00 4.8e-03 0.32 -1.7 -1.1e+00 -6.6e-01 4283 7011 1.0 +#> log_lik[87] -8.2e-01 1.9e-03 0.13 -1.0 -8.2e-01 -6.3e-01 4347 7115 1.0 +#> log_lik[88] -7.8e-01 3.7e-03 0.24 -1.2 -7.5e-01 -4.3e-01 4170 6825 1.00 +#> log_lik[89] -1.3e+00 5.0e-03 0.32 -1.8 -1.2e+00 -7.9e-01 4012 6566 1.0 +#> log_lik[90] -2.6e-01 2.1e-03 0.14 -0.53 -2.4e-01 -9.3e-02 4313 7058 1.00 +#> log_lik[91] -3.9e-01 1.9e-03 0.13 -0.63 -3.7e-01 -2.1e-01 4620 7562 1.0 +#> log_lik[92] -1.5e+00 5.7e-03 0.34 -2.1 -1.5e+00 -9.8e-01 3518 5758 1.00 +#> log_lik[93] -7.4e-01 3.5e-03 0.22 -1.1 -7.2e-01 -4.2e-01 3871 6336 1.00 +#> log_lik[94] -3.2e-01 1.4e-03 0.088 -0.48 -3.1e-01 -1.9e-01 3732 6107 1.0 +#> log_lik[95] -3.9e-01 1.8e-03 0.11 -0.58 -3.8e-01 -2.3e-01 3830 6269 1.00 +#> log_lik[96] -1.6e+00 4.8e-03 0.28 -2.1 -1.6e+00 -1.1e+00 3457 5657 1.0 +#> log_lik[97] -4.3e-01 1.5e-03 0.10 -0.61 -4.2e-01 -2.8e-01 4740 7758 1.00 +#> log_lik[98] -1.0e+00 5.5e-03 0.37 -1.7 -1.0e+00 -5.1e-01 4582 7499 1.00 +#> log_lik[99] -6.9e-01 2.1e-03 0.14 -0.95 -6.8e-01 -4.8e-01 4565 7472 1.0 +#> log_lik[100] -3.9e-01 1.5e-03 0.096 -0.56 -3.8e-01 -2.5e-01 4243 6945 1.0 #> #> Samples were drawn using hmc with nuts. #> For each parameter, N_Eff is a crude measure of effective sample size, @@ -303,11 +303,11 @@Examples
Return Stan code — fit-method-code • cmdstanr Return Stan code — fit-method-code • cmdstanr @@ -17,7 +17,7 @@Examples
Transform a set of unconstrained parameter values to the constrained scale — fit-method-constrain_variables • cmdstanr Transform a set of unconstrained parameter values to the constrained scale — fit-method-constrain_variables • cmdstanr Sampler diagnostic summaries and warnings — fit-method-diagnostic_summary • cmdstanr Sampler diagnostic summaries and warnings — fit-method-diagnostic_summary • cmdstanr cmdstanr - 0.7.0 + 0.8.1See also
Examples
diff --git a/docs/reference/fit-method-draws-1.png b/docs/reference/fit-method-draws-1.png index a6899fc4..b241904b 100644 Binary files a/docs/reference/fit-method-draws-1.png and b/docs/reference/fit-method-draws-1.png differ diff --git a/docs/reference/fit-method-draws-2.png b/docs/reference/fit-method-draws-2.png index fb93238c..b9abe398 100644 Binary files a/docs/reference/fit-method-draws-2.png and b/docs/reference/fit-method-draws-2.png differ diff --git a/docs/reference/fit-method-draws.html b/docs/reference/fit-method-draws.html index b257cb5d..c7816907 100644 --- a/docs/reference/fit-method-draws.html +++ b/docs/reference/fit-method-draws.html @@ -1,5 +1,5 @@ -# \dontrun{ fit <- cmdstanr_example("schools") -#> Warning: 91 of 4000 (2.0%) transitions ended with a divergence. +#> Warning: 235 of 4000 (6.0%) transitions ended with a divergence. +#> See https://mc-stan.org/misc/warnings for details. +#> Warning: 2 of 4 chains had an E-BFMI less than 0.3. #> See https://mc-stan.org/misc/warnings for details. fit$diagnostic_summary() -#> Warning: 91 of 4000 (2.0%) transitions ended with a divergence. +#> Warning: 235 of 4000 (6.0%) transitions ended with a divergence. +#> See https://mc-stan.org/misc/warnings for details. +#> Warning: 2 of 4 chains had an E-BFMI less than 0.3. #> See https://mc-stan.org/misc/warnings for details. #> $num_divergent -#> [1] 12 18 11 50 +#> [1] 104 30 98 3 #> #> $num_max_treedepth #> [1] 0 0 0 0 #> #> $ebfmi -#> [1] 0.3003970 0.2974182 0.3438194 0.4920025 +#> [1] 0.4564907 0.4041931 0.2577905 0.2642813 #> fit$diagnostic_summary(quiet = TRUE) #> $num_divergent -#> [1] 12 18 11 50 +#> [1] 104 30 98 3 #> #> $num_max_treedepth #> [1] 0 0 0 0 #> #> $ebfmi -#> [1] 0.3003970 0.2974182 0.3438194 0.4920025 +#> [1] 0.4564907 0.4041931 0.2577905 0.2642813 #> # } @@ -197,11 +201,11 @@
Examples
Extract posterior draws — fit-method-draws • cmdstanr Extract posterior draws — fit-method-draws • cmdstanr cmdstanr - 0.7.0 + 0.8.1Examples
dim(draws) #> [1] 1000 4 105 str(draws) -#> 'draws_array' num [1:1000, 1:4, 1:105] -64.2 -65.3 -65.3 -64.7 -64.8 ... +#> 'draws_array' num [1:1000, 1:4, 1:105] -65 -66.8 -64.9 -66.9 -66.5 ... #> - attr(*, "dimnames")=List of 3 #> ..$ iteration: chr [1:1000] "1" "2" "3" "4" ... #> ..$ chain : chr [1:4] "1" "2" "3" "4" @@ -214,13 +214,13 @@Examples
head(posterior::as_draws_matrix(draws)) #> # A draws_matrix: 6 iterations, 1 chains, and 105 variables #> variable -#> draw lp__ alpha beta[1] beta[2] beta[3] log_lik[1] log_lik[2] log_lik[3] -#> 1 -64 0.28 -0.73 -0.16 0.73 -0.53 -0.30 -0.39 -#> 2 -65 0.19 -0.42 -0.13 0.37 -0.59 -0.48 -0.53 -#> 3 -65 0.61 -0.93 -0.25 0.83 -0.40 -0.34 -0.30 -#> 4 -65 0.42 -0.81 -0.32 0.94 -0.50 -0.28 -0.41 -#> 5 -65 0.36 -0.41 -0.20 0.39 -0.52 -0.56 -0.52 -#> 6 -67 0.55 -0.24 -0.43 0.24 -0.49 -0.87 -0.75 +#> draw lp__ alpha beta[1] beta[2] beta[3] log_lik[1] log_lik[2] log_lik[3] +#> 1 -65 0.363 -0.70 -0.26 1.04 -0.53 -0.24 -0.40 +#> 2 -67 0.522 -0.55 -0.61 1.18 -0.54 -0.30 -0.65 +#> 3 -65 0.607 -0.57 -0.41 0.82 -0.46 -0.44 -0.49 +#> 4 -67 -0.012 -0.52 -0.11 0.25 -0.65 -0.44 -0.59 +#> 5 -67 0.715 -0.77 -0.34 1.14 -0.41 -0.29 -0.32 +#> 6 -65 0.473 -0.42 -0.41 0.76 -0.52 -0.45 -0.60 #> # ... with 97 more variables # or can specify 'format' argument to avoid manual conversion @@ -229,13 +229,13 @@Examples
head(draws) #> # A draws_matrix: 6 iterations, 1 chains, and 105 variables #> variable -#> draw lp__ alpha beta[1] beta[2] beta[3] log_lik[1] log_lik[2] log_lik[3] -#> 1 -64 0.28 -0.73 -0.16 0.73 -0.53 -0.30 -0.39 -#> 2 -65 0.19 -0.42 -0.13 0.37 -0.59 -0.48 -0.53 -#> 3 -65 0.61 -0.93 -0.25 0.83 -0.40 -0.34 -0.30 -#> 4 -65 0.42 -0.81 -0.32 0.94 -0.50 -0.28 -0.41 -#> 5 -65 0.36 -0.41 -0.20 0.39 -0.52 -0.56 -0.52 -#> 6 -67 0.55 -0.24 -0.43 0.24 -0.49 -0.87 -0.75 +#> draw lp__ alpha beta[1] beta[2] beta[3] log_lik[1] log_lik[2] log_lik[3] +#> 1 -65 0.363 -0.70 -0.26 1.04 -0.53 -0.24 -0.40 +#> 2 -67 0.522 -0.55 -0.61 1.18 -0.54 -0.30 -0.65 +#> 3 -65 0.607 -0.57 -0.41 0.82 -0.46 -0.44 -0.49 +#> 4 -67 -0.012 -0.52 -0.11 0.25 -0.65 -0.44 -0.59 +#> 5 -67 0.715 -0.77 -0.34 1.14 -0.41 -0.29 -0.32 +#> 6 -65 0.473 -0.42 -0.41 0.76 -0.52 -0.45 -0.60 #> # ... with 97 more variables # can select specific parameters @@ -244,12 +244,12 @@Examples
#> , , variable = alpha #> #> chain -#> iteration 1 2 3 4 -#> 1 0.28 0.27 0.62 0.10 -#> 2 0.19 0.70 0.49 0.71 -#> 3 0.61 0.26 0.89 0.14 -#> 4 0.42 0.39 0.58 0.39 -#> 5 0.36 0.39 0.58 0.48 +#> iteration 1 2 3 4 +#> 1 0.363 0.52 0.50 -0.0071 +#> 2 0.522 0.50 0.46 0.3258 +#> 3 0.607 0.19 0.25 0.2788 +#> 4 -0.012 0.13 0.41 0.2299 +#> 5 0.715 0.48 0.40 0.6308 #> #> # ... with 995 more iterations fit$draws("beta") # selects entire vector beta @@ -258,31 +258,31 @@Examples
#> #> chain #> iteration 1 2 3 4 -#> 1 -0.73 -0.46 -0.82 -0.55 -#> 2 -0.42 -0.92 -1.18 -1.08 -#> 3 -0.93 -0.52 -0.63 -0.42 -#> 4 -0.81 -0.86 -0.30 -0.72 -#> 5 -0.41 -0.86 -0.30 -0.86 +#> 1 -0.70 -0.74 -0.87 -0.37 +#> 2 -0.55 -0.71 -0.58 -0.84 +#> 3 -0.57 -0.85 -0.59 -0.37 +#> 4 -0.52 -0.66 -0.60 -0.53 +#> 5 -0.77 -0.40 -0.55 -0.68 #> #> , , variable = beta[2] #> #> chain -#> iteration 1 2 3 4 -#> 1 -0.16 -0.31 -0.88 -0.18 -#> 2 -0.13 -0.27 -0.69 -0.21 -#> 3 -0.25 -0.39 -0.38 -0.37 -#> 4 -0.32 -0.43 -0.52 -0.36 -#> 5 -0.20 -0.43 -0.52 -0.42 +#> iteration 1 2 3 4 +#> 1 -0.26 -0.12 -0.28 -0.474 +#> 2 -0.61 -0.12 -0.51 -0.487 +#> 3 -0.41 -0.10 -0.22 -0.069 +#> 4 -0.11 -0.14 -0.13 -0.094 +#> 5 -0.34 -0.08 -0.58 -0.328 #> #> , , variable = beta[3] #> #> chain #> iteration 1 2 3 4 -#> 1 0.73 0.28 0.87 0.93 -#> 2 0.37 1.26 0.75 0.45 -#> 3 0.83 0.28 0.88 0.83 -#> 4 0.94 0.64 0.71 0.48 -#> 5 0.39 0.64 0.71 0.41 +#> 1 1.04 0.96 0.78 0.75 +#> 2 1.18 0.90 0.40 0.81 +#> 3 0.82 0.29 1.27 0.64 +#> 4 0.25 0.45 0.97 0.64 +#> 5 1.14 0.18 0.54 0.66 #> #> # ... with 995 more iterations fit$draws(c("alpha", "beta[2]")) @@ -290,22 +290,22 @@Examples
#> , , variable = alpha #> #> chain -#> iteration 1 2 3 4 -#> 1 0.28 0.27 0.62 0.10 -#> 2 0.19 0.70 0.49 0.71 -#> 3 0.61 0.26 0.89 0.14 -#> 4 0.42 0.39 0.58 0.39 -#> 5 0.36 0.39 0.58 0.48 +#> iteration 1 2 3 4 +#> 1 0.363 0.52 0.50 -0.0071 +#> 2 0.522 0.50 0.46 0.3258 +#> 3 0.607 0.19 0.25 0.2788 +#> 4 -0.012 0.13 0.41 0.2299 +#> 5 0.715 0.48 0.40 0.6308 #> #> , , variable = beta[2] #> #> chain -#> iteration 1 2 3 4 -#> 1 -0.16 -0.31 -0.88 -0.18 -#> 2 -0.13 -0.27 -0.69 -0.21 -#> 3 -0.25 -0.39 -0.38 -0.37 -#> 4 -0.32 -0.43 -0.52 -0.36 -#> 5 -0.20 -0.43 -0.52 -0.42 +#> iteration 1 2 3 4 +#> 1 -0.26 -0.12 -0.28 -0.474 +#> 2 -0.61 -0.12 -0.51 -0.487 +#> 3 -0.41 -0.10 -0.22 -0.069 +#> 4 -0.11 -0.14 -0.13 -0.094 +#> 5 -0.34 -0.08 -0.58 -0.328 #> #> # ... with 995 more iterations @@ -323,21 +323,21 @@Examples
#> # A draws_matrix: 6 iterations, 1 chains, and 3 variables #> variable #> draw beta[1] beta[2] beta[3] -#> 1 -0.62 -0.485 0.63 -#> 2 -0.34 0.188 0.77 -#> 3 -0.63 -0.236 0.90 -#> 4 -0.65 -0.392 0.76 -#> 5 -0.57 0.028 0.44 -#> 6 -0.58 -0.299 0.69 +#> 1 -0.80 -0.25 0.98 +#> 2 -0.56 -0.31 0.57 +#> 3 -0.98 -0.35 0.66 +#> 4 -0.67 -0.78 0.49 +#> 5 -0.98 -0.38 0.75 +#> 6 -0.52 -0.52 0.37 head(fit$draws("beta", format = "df")) #> # A draws_df: 6 iterations, 1 chains, and 3 variables #> beta[1] beta[2] beta[3] -#> 1 -0.62 -0.485 0.63 -#> 2 -0.34 0.188 0.77 -#> 3 -0.63 -0.236 0.90 -#> 4 -0.65 -0.392 0.76 -#> 5 -0.57 0.028 0.44 -#> 6 -0.58 -0.299 0.69 +#> 1 -0.80 -0.25 0.98 +#> 2 -0.56 -0.31 0.57 +#> 3 -0.98 -0.35 0.66 +#> 4 -0.67 -0.78 0.49 +#> 5 -0.98 -0.38 0.75 +#> 6 -0.52 -0.52 0.37 #> # ... hidden reserved variables {'.chain', '.iteration', '.draw'} # } @@ -351,11 +351,11 @@Examples
Calculate the log-probability and the gradient w.r.t. each input for a -given vector of unconstrained parameters — fit-method-grad_log_prob • cmdstanr Calculate the log-probability and the gradient w.r.t. each input for a given vector of unconstrained parameters — fit-method-grad_log_prob • cmdstanr Extract gradients after diagnostic mode — fit-method-gradients • cmdstanr Extract gradients after diagnostic mode — fit-method-gradients • cmdstanr Calculate the log-probability , the gradient w.r.t. each input, and the hessian -for a given vector of unconstrained parameters — fit-method-hessian • cmdstanr Calculate the log-probability , the gradient w.r.t. each input, and the hessian for a given vector of unconstrained parameters — fit-method-hessian • cmdstanr Extract user-specified initial values — fit-method-init • cmdstanr Extract user-specified initial values — fit-method-init • cmdstanr cmdstanr - 0.7.0 + 0.8.1Examples
str(fit$init()) #> List of 2 #> $ :List of 2 -#> ..$ alpha: num -0.538 -#> ..$ beta : num [1:3] -0.0289 1.8267 1.6042 +#> ..$ alpha: num -1.12 +#> ..$ beta : num [1:3] 0.398 0.421 -0.425 #> $ :List of 2 -#> ..$ alpha: num -1.4 -#> ..$ beta : num [1:3] -1.313 -1.04 0.224 +#> ..$ alpha: num -0.877 +#> ..$ beta : num [1:3] 1.3 -1.34 1.43 # partial inits (only specifying for a subset of parameters) init_list <- list( @@ -156,6 +156,8 @@Examples
#> Missing init values for the following parameters: #> - chain 1: theta_raw #> - chain 2: theta_raw +#> +#> To disable this message use options(cmdstanr_warn_inits = FALSE). # only user-specified inits returned str(fit$init()) @@ -178,11 +180,11 @@Examples
Compile additional methods for accessing the model log-probability function -and parameter constraining and unconstraining. — fit-method-init_model_methods • cmdstanr Compile additional methods for accessing the model log-probability function and parameter constraining and unconstraining. — fit-method-init_model_methods • cmdstanr Extract inverse metric (mass matrix) after MCMC — fit-method-inv_metric • cmdstanr Extract inverse metric (mass matrix) after MCMC — fit-method-inv_metric • cmdstanr @@ -17,7 +17,7 @@
(multiple options) How to handle the r_eff
argument for loo()
:
TRUE
(the default) will automatically call loo::relative_eff.array()
to compute the r_eff
argument to pass to loo::loo.array()
.
FALSE
or NULL
will avoid computing r_eff
(which can sometimes be slow)
-but will result in a warning from the loo package.
FALSE
or NULL
will avoid computing r_eff
(which can sometimes be slow),
+but the reported ESS and MCSE estimates can be over-optimistic if the
+posterior draws are not (near) independent.
If r_eff
is anything else, that object will be passed as the r_eff
argument to loo::loo.array()
.
# \dontrun{
fit_mcmc <- cmdstanr_example("logistic")
head(fit_mcmc$lp())
-#> [1] -65.6405 -64.4734 -65.4431 -64.9703 -65.8088 -65.6905
+#> [1] -64.8724 -65.7859 -65.2974 -66.5566 -66.7795 -67.0780
fit_mle <- cmdstanr_example("logistic", method = "optimize")
fit_mle$lp()
@@ -191,11 +191,11 @@ Examples
For $save_profile_files()
everything is the same as for
$save_output_files()
except "-profile-"
is included in the new
file name after basename
.
For $save_metric_files()
everything is the same as for
+$save_output_files()
except "-metric-"
is included in the new
+file name after basename
.
For $save_config_files()
everything is the same as for
+$save_output_files()
except "-config-"
is included in the new
+file name after basename
.
For $save_data_file()
no id
is included in the file name because even
with multiple MCMC chains the data file is the same.
# \dontrun{
fit <- cmdstanr_example()
fit$output_files()
-#> [1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131005-1-212881.csv"
-#> [2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131005-2-212881.csv"
-#> [3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131005-3-212881.csv"
-#> [4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/logistic-202312131005-4-212881.csv"
+#> [1] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/logistic-202407021539-1-615e63.csv"
+#> [2] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/logistic-202407021539-2-615e63.csv"
+#> [3] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/logistic-202407021539-3-615e63.csv"
+#> [4] "/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/logistic-202407021539-4-615e63.csv"
fit$data_file()
-#> [1] "/private/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/Rtmps9TW4l/temp_libpath5800f91909/cmdstanr/logistic.data.json"
+#> [1] "/private/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpBGNQRs/temp_libpath45223306cea5/cmdstanr/logistic.data.json"
# just using tempdir for the example
my_dir <- tempdir()
fit$save_output_files(dir = my_dir, basename = "banana")
#> Moved 4 files and set internal paths to new locations:
-#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/banana-202312131005-1-932dd0.csv
-#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/banana-202312131005-2-932dd0.csv
-#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/banana-202312131005-3-932dd0.csv
-#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/banana-202312131005-4-932dd0.csv
+#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/banana-202407021539-1-897d5b.csv
+#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/banana-202407021539-2-897d5b.csv
+#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/banana-202407021539-3-897d5b.csv
+#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/banana-202407021539-4-897d5b.csv
fit$save_output_files(dir = my_dir, basename = "tomato", timestamp = FALSE)
#> Moved 4 files and set internal paths to new locations:
-#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/tomato-1-1f5444.csv
-#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/tomato-2-1f5444.csv
-#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/tomato-3-1f5444.csv
-#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/tomato-4-1f5444.csv
+#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/tomato-1-4291b8.csv
+#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/tomato-2-4291b8.csv
+#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/tomato-3-4291b8.csv
+#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/tomato-4-4291b8.csv
fit$save_output_files(dir = my_dir, basename = "lettuce", timestamp = FALSE, random = FALSE)
#> Moved 4 files and set internal paths to new locations:
-#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/lettuce-1.csv
-#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/lettuce-2.csv
-#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/lettuce-3.csv
-#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpAMFHpW/lettuce-4.csv
+#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/lettuce-1.csv
+#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/lettuce-2.csv
+#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/lettuce-3.csv
+#> - /var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/lettuce-4.csv
# }
unconstrain_draws(files = NULL, draws = NULL)
unconstrain_draws(
+ files = NULL,
+ draws = NULL,
+ format = getOption("cmdstanr_draws_format", "draws_array"),
+ inc_warmup = FALSE
+)
A posterior::draws_*
object.
(string) The format of the returned draws. Must be a valid +format from the posterior package.
(logical) Should warmup draws be included? Defaults to
+FALSE
.
# \dontrun{
fit_mcmc <- cmdstanr_example("logistic", method = "sample", force_recompile = TRUE)
-fit_mcmc$init_model_methods()
-#> Compiling additional model methods...
# Unconstrain all internal draws
unconstrained_internal_draws <- fit_mcmc$unconstrain_draws()
@@ -166,11 +179,11 @@ Examples
- Package description+Package overview and global optionsAn overview of the package and how it differs from RStan. |
||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CmdStanR: the R interface to CmdStan |
+ ||||||||||||||||||||||
+ + | +CmdStanR global options |
|||||||||||||||||||||
Installing and setting the path to CmdStanInstall CmdStan, assuming the necessary C++ toolchain. @@ -244,8 +248,7 @@Fitted model objects and methods
|
- |
+ Calculate the log-probability and the gradient w.r.t. each input for a -given vector of unconstrained parameters Calculate the log-probability and the gradient w.r.t. each input for a given vector of unconstrained parameters
|
@@ -253,8 +256,7 @@ Fitted model objects and methods
|
- |
+ Calculate the log-probability , the gradient w.r.t. each input, and the hessian -for a given vector of unconstrained parameters Calculate the log-probability , the gradient w.r.t. each input, and the hessian for a given vector of unconstrained parameters
|
@@ -262,8 +264,7 @@ Fitted model objects and methods
|
- |
+ Compile additional methods for accessing the model log-probability function -and parameter constraining and unconstraining. Compile additional methods for accessing the model log-probability function and parameter constraining and unconstraining.
|
@@ -277,7 +278,7 @@ Fitted model objects and methods
Leave-one-out cross-validation (LOO-CV)
-
+
|
Extract log probability (target)
@@ -313,7 +314,7 @@ | Fitted model objects and methods
Save fitted model object to a file
- |
Save output and data files
@@ -341,7 +342,7 @@ | Fitted model objects and methods
Expose Stan functions to R | |||||||||
- Other tools for working with CmdStan+Other tools |
||||||||||||||||||||||
@@ -359,8 +360,7 @@ Other tools for working with CmdSt | ||||||||||||||||||||||
- | Write posterior draws objects to CSV files suitable for running standalone generated -quantities with CmdStan. |
+ Write posterior draws objects to CSV files suitable for running standalone generated quantities with CmdStan. |
||||||||||||||||||||
@@ -369,6 +369,10 @@ | Create a |
+ |||||||||||||||||||||
+
|
+ Coercion methods for CmdStan objects |
|||||||||||||||||||||
Using CmdStanR with knitr and R Markdown@@ -390,11 +394,11 @@Using CmdStanR with knitr and
diff --git a/docs/reference/install_cmdstan.html b/docs/reference/install_cmdstan.html
index f63b9b78..8f4dbe2a 100644
--- a/docs/reference/install_cmdstan.html
+++ b/docs/reference/install_cmdstan.html
@@ -1,5 +1,5 @@
-(logical) Should the (experimental) (logical) Should functions in the Stan model be
compiled for use in R? If Argumentsbut skip the actual C++ compilation. Used to speedup tests.(logical) Should the (experimental) Deprecated and will be removed in a future release. Please
turn on threading via |