Skip to content

Commit ae46573

Browse files
committed
export stanfit getters
1 parent 98886fa commit ae46573

36 files changed

+518
-126
lines changed

NAMESPACE

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,41 @@ S3method(coef,dynamitefit)
99
S3method(confint,dynamitefit)
1010
S3method(fitted,dynamitefit)
1111
S3method(formula,dynamitefit)
12+
S3method(get_algorithm,CmdStanMCMC)
13+
S3method(get_algorithm,CmdStanMCMC_CSV)
14+
S3method(get_algorithm,stanfit)
1215
S3method(get_code,dynamitefit)
1316
S3method(get_code,dynamiteformula)
1417
S3method(get_data,dynamitefit)
1518
S3method(get_data,dynamiteformula)
19+
S3method(get_diagnostics,CmdStanMCMC)
20+
S3method(get_diagnostics,CmdStanMCMC_CSV)
21+
S3method(get_diagnostics,stanfit)
22+
S3method(get_draws,CMdStanMCMC_CSV)
23+
S3method(get_draws,CmdStanMCMC)
24+
S3method(get_draws,stanfit)
25+
S3method(get_elapsed_time,CmdStanMCMC)
26+
S3method(get_elapsed_time,CmdStanMCMC_CSV)
27+
S3method(get_elapsed_time,stanfit)
28+
S3method(get_max_treedepth,CmdStanMCMC)
29+
S3method(get_max_treedepth,CmdStanMCMC_CSV)
30+
S3method(get_max_treedepth,stanfit)
31+
S3method(get_model_code,CmdStanMCMC)
32+
S3method(get_model_code,CmdStanMCMC_CSV)
33+
S3method(get_model_code,stanfit)
34+
S3method(get_nchains,CmdStanMCMC)
35+
S3method(get_nchains,CmdStanMCMC_CSV)
36+
S3method(get_nchains,stanfit)
37+
S3method(get_ndraws,CmdStanMCMC)
38+
S3method(get_ndraws,CmdStanMCMC_CSV)
39+
S3method(get_ndraws,stanfit)
1640
S3method(get_parameter_dims,dynamitefit)
1741
S3method(get_parameter_dims,dynamiteformula)
1842
S3method(get_parameter_names,dynamitefit)
1943
S3method(get_parameter_types,dynamitefit)
44+
S3method(get_pars_oi,CmdStanMCMC)
45+
S3method(get_pars_oi,CmdStanMCMC_CSV)
46+
S3method(get_pars_oi,stanfit)
2047
S3method(get_priors,dynamitefit)
2148
S3method(get_priors,dynamiteformula)
2249
S3method(hmc_diagnostics,dynamitefit)
@@ -41,11 +68,20 @@ export(aux)
4168
export(dynamice)
4269
export(dynamite)
4370
export(dynamiteformula)
71+
export(get_algorithm)
4472
export(get_code)
4573
export(get_data)
74+
export(get_diagnostics)
75+
export(get_draws)
76+
export(get_elapsed_time)
77+
export(get_max_treedepth)
78+
export(get_model_code)
79+
export(get_nchains)
80+
export(get_ndraws)
4681
export(get_parameter_dims)
4782
export(get_parameter_names)
4883
export(get_parameter_types)
84+
export(get_pars_oi)
4985
export(get_priors)
5086
export(hmc_diagnostics)
5187
export(lags)

R/as_data_table.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ as_data_table_omega <- function(x, draws, n_draws, response, category, ...) {
561561
#' @describeIn as_data_table_default Data Table for a "omega_alpha" Parameter
562562
#' @noRd
563563
as_data_table_omega_alpha <- function(x, draws, n_draws, response,
564-
category, ...) {
564+
category, ...) {
565565
D <- x$stan$model_vars$D
566566
data.table::data.table(
567567
parameter = rep(

R/dynamite-package.R

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#' * The package vignettes
1212
#' * [dynamite::dynamiteformula()] for information on defining models.
1313
#' * [dynamite::dynamite()] for information on fitting models.
14-
#' * \url{https://github.com/ropensci/dynamite/issues/} to submit a bug report
14+
#' * <https://github.com/ropensci/dynamite/issues/> to submit a bug report
1515
#' or a feature request.
1616
#'
1717
#' # Authors
@@ -56,8 +56,8 @@
5656
#' coefficients vary according to a spline with 20 degrees of freedom.
5757
#'
5858
#' @family examples
59-
#' @source The data was generated according to a script in
60-
#' \url{https://github.com/ropensci/dynamite/blob/main/data-raw/gaussian_example.R}
59+
#' @source The data was generated via `gaussian_example.R` in
60+
#' <https://github.com/ropensci/dynamite/tree/main/data-raw/>
6161
#' @format A data frame with 3000 rows and 5 variables:
6262
#' \describe{
6363
#' \item{y}{The response variable.}
@@ -95,8 +95,8 @@
9595
#' }
9696
#' Note the very small number of samples due to size restrictions on CRAN.
9797
#' @family examples
98-
#' @source The data was generated according to a script in
99-
#' \url{https://github.com/ropensci/dynamite/blob/main/data-raw/gaussian_example_fit.R}
98+
#' @source The data was generated via `gaussian_example_fit.R` in
99+
#' <https://github.com/ropensci/dynamite/tree/main/data-raw/>
100100
#' @format A `dynamitefit` object.
101101
"gaussian_example_fit"
102102

@@ -120,8 +120,8 @@
120120
# #' )
121121
# #' }
122122
# #' @family examples
123-
# #' @source The data was generated according to a script in
124-
# #' \url{https://github.com/ropensci/dynamite/blob/main/data-raw/gaussian_simulation_fit.R}
123+
# #' @source The data was generated via to `gaussian_simulation_fit.R` in
124+
# #' <https://github.com/ropensci/dynamite/tree/main/data-raw/>
125125
# #' @format A `dynamitefit` object.
126126
# "gaussian_simulation_fit"
127127

@@ -131,8 +131,8 @@
131131
#' response variables of different distributions.
132132
#'
133133
#' @family examples
134-
#' @source The data was generated according to a script in
135-
#' \url{https://github.com/ropensci/dynamite/blob/main/data-raw/multichannel_example.R}
134+
#' @source The data was generated via `multichannel_example.R` in
135+
#' <https://github.com/ropensci/dynamite/tree/main/data-raw/>
136136
#' @format A data frame with 3000 rows and 5 variables:
137137
#' \describe{
138138
#' \item{id}{Variable defining individuals (1 to 50).}
@@ -171,8 +171,8 @@
171171
#' }
172172
#' Note the small number of samples due to size restrictions on CRAN.
173173
#' @family examples
174-
#' @source Script in
175-
#' \url{https://github.com/ropensci/dynamite/blob/main/data-raw/multichannel_example_fit.R}
174+
#' @source THe data was generated via `multichannel_example_fit.R` in
175+
#' <https://github.com/ropensci/dynamite/tree/main/data-raw/>
176176
#' @format A `dynamitefit` object.
177177
"multichannel_example_fit"
178178

@@ -182,8 +182,8 @@
182182
#' response variables.
183183
#'
184184
#' @family examples
185-
#' @source The data was generated according to a script in
186-
#' \url{https://github.com/ropensci/dynamite/blob/main/data-raw/categorical_example.R}
185+
#' @source The data was generated via `categorical_example.R` in
186+
#' <https://github.com/ropensci/dynamite/tree/main/data-raw/>
187187
#' @format A data frame with 2000 rows and 5 variables:
188188
#' \describe{
189189
#' \item{id}{Variable defining individuals (1 to 100).}
@@ -216,8 +216,8 @@
216216
#' }
217217
#' Note the small number of samples due to size restrictions on CRAN.
218218
#' @family examples
219-
#' @source Script in
220-
#' \url{https://github.com/ropensci/dynamite/blob/main/data-raw/categorical_example_fit.R}
219+
#' @source The data was generated via `categorical_example_fit.R` in
220+
#' <https://github.com/ropensci/dynamite/tree/main/data-raw/>
221221
#' @format A `dynamitefit` object.
222222
"categorical_example_fit"
223223

@@ -227,8 +227,8 @@
227227
# #' trajectories are defined by a latent factor and random intercept terms.
228228
# #'
229229
# #' @family examples
230-
# #' @source The data was generated according to a script in
231-
# #' \url{https://github.com/ropensci/dynamite/blob/main/data-raw/latent_factor_example.R}
230+
# #' @source The data was generated via `latent_factor_example.R` in
231+
# #' <https://github.com/ropensci/dynamite/blob/main/data-raw/>
232232
# #' @format A data frame with 2000 rows and 3 variables:
233233
# #' \describe{
234234
# #' \item{y}{A continuos variable.}
@@ -264,7 +264,7 @@
264264
# #' }
265265
# #' Note the very small number of samples due to size restrictions on CRAN.
266266
# #' @family examples
267-
# #' @source Script in
268-
# #' \url{https://github.com/ropensci/dynamite/blob/main/data-raw/latent_factor_example_fit.R}
267+
# #' @source The data was generated via `latent_factor_example_fit.R` in
268+
# #' <https://github.com/ropensci/dynamite/tree/main/data-raw/>
269269
# #' @format A `dynamitefit` object.
270270
# "latent_factor_example_fit"

R/dynamite.R

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#' Bayesian inference. The \pkg{dynamite} package supports a wide range of
55
#' distributions and allows the user to flexibly customize the priors for the
66
#' model parameters. The dynamite model is specified using standard \R formula
7-
#' syntax via [dynamite::dynamiteformula()]. For more information and examples,
7+
#' syntax via [dynamiteformula()]. For more information and examples,
88
#' see 'Details' and the package vignettes.
99
#'
1010
#' The best-case scalability of `dynamite` in terms of data size should be
@@ -17,7 +17,7 @@
1717
#' @family fitting
1818
#' @rdname dynamite
1919
#' @param dformula \[`dynamiteformula`]\cr The model formula.
20-
#' See [dynamite::dynamiteformula()] and 'Details'.
20+
#' See [dynamiteformula()] and 'Details'.
2121
#' @param data
2222
#' \[`data.frame`, `tibble::tibble`, or `data.table::data.table`]\cr
2323
#' The data that contains the variables in the model in long format.
@@ -38,22 +38,23 @@
3838
#' group. In case of name conflicts with `data`, see the `group_var` element
3939
#' of the return object to get the column name of the new variable.
4040
#' @param priors \[`data.frame`]\cr An optional data frame with prior
41-
#' definitions. See [dynamite::get_priors()] and 'Details'.
41+
#' definitions. See [get_priors()] and 'Details'.
4242
#' @param backend \[`character(1)`]\cr Defines the backend interface to Stan,
4343
#' should be either `"rstan"` (the default) or `"cmdstanr"`. Note that
4444
#' `cmdstanr` needs to be installed separately as it is not on CRAN. It also
45-
#' needs the actual `CmdStan` software. See https://mc-stan.org/cmdstanr/ for
46-
#' details.
45+
#' needs the actual `CmdStan` software. See <https://mc-stan.org/cmdstanr/>
46+
#' for details.
4747
#' @param verbose \[`logical(1)`]\cr All warnings and messages are suppressed
4848
#' if set to `FALSE`. Defaults to `TRUE`. Setting this to `FALSE` will also
4949
#' disable checks for perfect collinearity in the model matrix.
5050
#' @param verbose_stan \[`logical(1)`]\cr This is the `verbose` argument for
5151
#' [rstan::sampling()]. Defaults to `FALSE`.
5252
#' @param stanc_options \[`list()`]\cr This is the `stanc_options` argument
5353
#' passed to the compile method of a `CmdStanModel` object via
54-
#' [cmdstanr::cmdstan_model()] when `backend = "cmdstanr"`.
55-
#' Defaults to `list("O0")`. To enable level one compiler optimizations,
56-
#' use `list("O1")`.
54+
#' `cmdstan_model()` when `backend = "cmdstanr"`. Defaults to `list("O0")`.
55+
#' To enable level one compiler optimizations, use `list("O1")`.
56+
#' See <https://mc-stan.org/cmdstanr/reference/cmdstan_model.html>
57+
#' for details.
5758
#' @param threads_per_chain \[`integer(1)`]\cr A Positive integer defining the
5859
#' number of parallel threads to use within each chain. Default is `1`. See
5960
#' [rstan::rstan_options()] and [cmdstanr::sample()] for details.
@@ -76,11 +77,13 @@
7677
#' combined with `model_code = TRUE`, which adds the Stan model code to the
7778
#' return object.
7879
#' @param ... For `dynamite()`, additional arguments to [rstan::sampling()] or
79-
#' [cmdstanr::sample()], such as `chains` and `cores` (`chains` and
80-
#' `parallel_chains` in `cmdstanr`). For `summary()`, additional arguments to
81-
#' [dynamite::as.data.frame.dynamitefit()]. For `print()`, further arguments
82-
#' to the print method for tibbles (see [tibble::formatting]). Not used for
83-
#' `formula()`.
80+
#' the `$sample()` method of the `CmdStanModel` object
81+
#' (see <https://mc-stan.org/cmdstanr/reference/model-method-sample.html>),
82+
#' such as `chains` and `cores`
83+
#' (`chains` and `parallel_chains` in `cmdstanr`). For `summary()`,
84+
#' additional arguments to [as.data.frame.dynamitefit()]. For `print()`,
85+
#' further arguments to the print method for tibbles
86+
#' (see [tibble::formatting]). Not used for `formula()`.
8487
#' @return `dynamite` returns a `dynamitefit` object which is a list containing
8588
#' the following components:
8689
#'
@@ -321,7 +324,8 @@ dynamite_check <- function(dformula, data, time, group, priors, verbose,
321324
)
322325
stopifnot_(
323326
checkmate::test_string(x = custom_stan_model, null.ok = TRUE),
324-
"Argument {.arg custom_stan_model} must be a single {.cls character} string."
327+
"Argument {.arg custom_stan_model}
328+
must be a single {.cls character} string."
325329
)
326330
stopifnot_(
327331
!isTRUE(grepl("\\.stan$", custom_stan_model, perl = TRUE)) ||

R/families.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ dynamitefamily <- function(name, link) {
1818
)
1919
stopifnot_(
2020
is_supported_link(name, link),
21-
"{.val {link}} is not a supported link function for a {.val {name}} channel."
21+
"{.val {link}} is not a supported link function
22+
for a {.val {name}} channel."
2223
)
2324
structure(
2425
list(name = name, link = link),

R/lags.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ parse_lags <- function(dformula, data, group_var, time_var, verbose) {
363363
#' are stochastic.
364364
#' @param increment \[`logical()`]\cr A vector indicating whether to add
365365
#' the new lag term or not (e.g.,, whether it was already present or not).
366-
#' @param type \[`character(1)`]\cr Either `"fixed"`, `"varying"`, or `"random"`.
366+
#' @param type \[`character(1)`]\cr
367+
#' Either `"fixed"`, `"varying"`, or `"random"`.
367368
#' @param lhs \[`character()`]\cr A vector of the new lagged variable names.
368369
#' @noRd
369370
parse_new_lags <- function(dformula, channels_stoch, increment, type, lhs) {
@@ -594,5 +595,3 @@ prepare_lagged_response <- function(dformula, lag_map,
594595
}
595596
y
596597
}
597-
598-

R/lfo.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#' the LFO computations to the console.
1919
#' @param k_threshold \[`numeric(1)`]\cr Threshold for the Pareto k estimate
2020
#' triggering refit. Default is 0.7.
21-
#' @param ... Additional arguments passed to [rstan::sampling()] or
22-
#' [cmdstanr::sample()], such as `chains` and `cores` (`parallel_chains` in
23-
#' `cmdstanr`).
21+
#' @param ... Additional arguments passed to [rstan::sampling()] or the
22+
#' `$sample()` method of the `CmdStanModel` object, such as `chains` and
23+
#' `cores` (`parallel_chains` in `cmdstanr`).
2424
#' @return An `lfo` object which is a `list` with the following components:
2525
#'
2626
#' * `ELPD`\cr Expected log predictive density estimate.

R/prepare_stan_input.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ prepare_channel_default <- function(y, Y, channel, sampling,
517517
nzchar(category),
518518
priors[priors$response == y & priors$category == category, ],
519519
priors[priors$response == y, ]
520-
)
520+
)
521521
channel$prior_distr <- list()
522522
types <- priors$type
523523
loop_types <- intersect(

R/print.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,18 @@ print.dynamitefit <- function(x, full_diagnostics = FALSE, ...) {
7070
if (mcmc_algorithm) {
7171
min_ess <- which.min(sumr$ess_bulk)
7272
cat("\nSmallest bulk-ESS: ", round(sumr$ess_bulk[min_ess]), " (",
73-
sumr$variable[min_ess], ")",
74-
sep = ""
73+
sumr$variable[min_ess], ")",
74+
sep = ""
7575
)
7676
min_ess <- which.min(sumr$ess_tail)
7777
cat("\nSmallest tail-ESS: ", round(sumr$ess_tail[min_ess]), " (",
78-
sumr$variable[min_ess], ")",
79-
sep = ""
78+
sumr$variable[min_ess], ")",
79+
sep = ""
8080
)
8181
max_rhat <- which.max(sumr$rhat)
8282
cat("\nLargest Rhat: ", round(sumr$rhat[max_rhat], 3), " (",
83-
sumr$variable[max_rhat], ")",
84-
sep = ""
83+
sumr$variable[max_rhat], ")",
84+
sep = ""
8585
)
8686
runtimes <- get_elapsed_time(x$stanfit)
8787
if (nrow(runtimes) > 2L) {

0 commit comments

Comments
 (0)