From 9709208c62d7063dbe2f6b196aa9d9bbcbf42080 Mon Sep 17 00:00:00 2001 From: santikka Date: Sun, 14 Jul 2024 11:45:03 +0300 Subject: [PATCH] get_parameter_dims for cmdstanr, docs --- R/as_data_frame.R | 10 ++++----- R/as_data_table.R | 2 +- R/as_draws.R | 11 +++++---- R/ci.R | 2 +- R/coef.R | 2 +- R/deprecated.R | 4 ++-- R/deterministic.R | 2 +- R/dynamice.R | 7 +++--- R/dynamite-package.R | 25 +++++++++++---------- R/dynamite.R | 9 ++++---- R/dynamiteformula.R | 8 +++---- R/fitted.R | 8 +++---- R/getters.R | 35 +++++++++++++++++++---------- R/lags.R | 2 +- R/latent_factor.R | 4 ++-- R/lfo.R | 2 +- R/loo.R | 2 +- R/mcmc_diagnostics.R | 4 ++-- R/nobs.R | 2 +- R/plot.R | 15 ++++++------- R/predict.R | 4 ++-- R/predict_helpers.R | 4 ++-- R/print.R | 10 ++++----- R/summary.R | 4 ++-- R/update.R | 2 +- inst/CITATION | 2 +- man/as.data.frame.dynamitefit.Rd | 8 +++---- man/as.data.table.dynamitefit.Rd | 2 +- man/as_draws-dynamitefit.Rd | 10 ++++----- man/coef.dynamitefit.Rd | 4 ++-- man/confint.dynamitefit.Rd | 2 +- man/dynamice.Rd | 2 +- man/dynamite-deprecated.Rd | 2 +- man/dynamite-package.Rd | 9 ++++---- man/dynamite.Rd | 13 ++++++----- man/dynamiteformula.Rd | 6 ++--- man/fitted.dynamitefit.Rd | 8 +++---- man/gaussian_example.Rd | 10 ++++----- man/gaussian_example_fit.Rd | 2 +- man/get_code.Rd | 2 +- man/get_data.Rd | 2 +- man/get_parameter_dims.Rd | 2 +- man/get_parameter_names.Rd | 2 +- man/get_parameter_types.Rd | 2 +- man/get_priors.Rd | 6 ++--- man/hmc_diagnostics.Rd | 2 +- man/lags.Rd | 2 +- man/lfactor.Rd | 4 ++-- man/lfo.Rd | 2 +- man/loo.dynamitefit.Rd | 2 +- man/mcmc_diagnostics.Rd | 2 +- man/nobs.dynamitefit.Rd | 4 ++-- man/plot.dynamitefit.Rd | 7 +++--- man/predict.dynamitefit.Rd | 4 ++-- man/update.dynamitefit.Rd | 2 +- tests/testthat/test-cmdstanr.R | 38 +++++++++++++++++++++++++++++++- tests/testthat/test-output.R | 17 +++++++++++--- 57 files changed, 208 insertions(+), 154 deletions(-) diff --git a/R/as_data_frame.R b/R/as_data_frame.R index 20bca30..9fab276 100644 --- a/R/as_data_frame.R +++ b/R/as_data_frame.R @@ -31,8 +31,8 @@ #' factors. Samples of the full correlation matrix can be extracted #' manually as `rstan::extract(fit$stanfit, pars = "corr_matrix_psi")` if #' necessary. -#' * `sigma`\cr Standard deviations of gaussian responses. -#' * `corr`\cr Pairwise correlations of multivariate gaussian responses. +#' * `sigma`\cr Standard deviations of Gaussian responses. +#' * `corr`\cr Pairwise correlations of multivariate Gaussian responses. #' * `phi`\cr Describes various distributional parameters, such as: #' - Dispersion parameter of the Negative Binomial distribution. #' - Shape parameter of the Gamma distribution. @@ -64,10 +64,10 @@ #' should be extracted. Possible options are elements of #' `unique(x$priors$response)`, and the default is this entire vector. #' Ignored if the argument `parameters` is supplied. -#' `omega_alpha`, and `omega_psi`. See also [dynamite::get_parameter_types()]. +#' `omega_alpha`, and `omega_psi`. See also [get_parameter_types()]. #' @param times \[`double()`]\cr Time point(s) to keep. If `NULL` #' (the default), all time points are kept. -#' @param groups \[`character()`] Group name(s) to keep. If `NULL` +#' @param groups \[`character()`]\cr Group name(s) to keep. If `NULL` #' (the default), all groups are kept. #' @param summary \[`logical(1)`]\cr If `TRUE`, returns posterior #' mean, standard deviation, and posterior quantiles (as defined by the @@ -82,7 +82,7 @@ #' @param ... Ignored. #' @return A `tibble` containing either samples or summary statistics of the #' model parameters in a long format. For a wide format, see -#' [dynamite::as_draws()]. +#' [as_draws.dynamitefit()]. #' @examples #' data.table::setDTthreads(1) # For CRAN #' as.data.frame( diff --git a/R/as_data_table.R b/R/as_data_table.R index f6809fc..69092ea 100644 --- a/R/as_data_table.R +++ b/R/as_data_table.R @@ -1,7 +1,7 @@ #' Extract Samples From a `dynamitefit` Object as a Data Table #' #' Provides a `data.table` representation of the posterior samples of the model -#' parameters. See [dynamite::as.data.frame.dynamitefit()] for details. +#' parameters. See [as.data.frame.dynamitefit()] for details. #' #' @export #' @export as.data.table diff --git a/R/as_draws.R b/R/as_draws.R index d5ca6db..6295734 100644 --- a/R/as_draws.R +++ b/R/as_draws.R @@ -1,19 +1,18 @@ #' Convert `dynamite` Output to `draws_df` Format #' -#' Converts the output from a [dynamite::dynamite()] call to a +#' Converts the output from a [dynamite()] call to a #' `draws_df` format of the \pkg{posterior} package, enabling the use #' of diagnostics and plotting methods of \pkg{posterior} and \pkg{bayesplot} #' packages. Note that this function returns variables in a wide format, -#' whereas [dynamite::as.data.frame()] uses the long format. +#' whereas [as.data.frame.dynamitefit()] uses the long format. #' #' You can use the arguments `parameters`, `responses` and `types` to extract #' only a subset of the model parameters (i.e., only certain types of #' parameters related to a certain response variable). #' -#' See potential values for the types argument in -#' [dynamite::as.data.frame.dynamitefit()] and -#' [dynamite::get_parameter_names()] for potential values for `parameters` -#' argument. +#' See potential values for the types argument in [as.data.frame.dynamitefit()] +#' and [get_parameter_names()] for potential values for `parameters` argument. +#' #' @export #' @family output #' @aliases as_draws_df diff --git a/R/ci.R b/R/ci.R index 956c73f..1dc526c 100644 --- a/R/ci.R +++ b/R/ci.R @@ -1,4 +1,4 @@ -#' Credible Intervals for Dynamite Model Parameters +#' Credible Intervals for \pkg{dynamite} Model Parameters #' #' Extracts credible intervals from `dynamitefit` object. #' diff --git a/R/coef.R b/R/coef.R index e40f4c3..7824a81 100644 --- a/R/coef.R +++ b/R/coef.R @@ -1,4 +1,4 @@ -#' Extract Regression Coefficients of a Dynamite Model +#' Extract Regression Coefficients of a \pkg{dynamite} Model #' #' Extracts either time-varying or time-invariant parameters of the model. #' diff --git a/R/deprecated.R b/R/deprecated.R index a3d2c9d..fdd28c5 100644 --- a/R/deprecated.R +++ b/R/deprecated.R @@ -1,4 +1,4 @@ -#' Deprecated Functions in the dynamite Package +#' Deprecated Functions in the \pkg{dynamite} Package #' #' These functions are provided for compatibility with older versions of the #' package. They will eventually be completely removed. @@ -11,7 +11,7 @@ #' plot_lambdas(x, ...) #' plot_psis(x, ...) #' @return A `ggplot` object. -#' @seealso See [dynamite::plot.dynamitefit()] for documentation of the +#' @seealso See [plot.dynamitefit()] for documentation of the #' parameters of these functions #' @export plot_betas plot_deltas plot_nus plot_lambdas plot_psis #' @aliases plot_betas plot_deltas plot_nus plot_lambdas plot_psis diff --git a/R/deterministic.R b/R/deterministic.R index 9deda27..21ebcb7 100644 --- a/R/deterministic.R +++ b/R/deterministic.R @@ -195,7 +195,7 @@ assign_lags_init <- function(data, idx, ro, lhs, rhs, offset = 1L) { #' Evaluate Definitions of Deterministic Channels #' #' @inheritParams parse_data -#' @param dformulas \[list()]\cr The return object of [dynamite::parse_lags()]. +#' @param dformulas \[list()]\cr The return object of [parse_lags()]. #' @noRd evaluate_deterministic <- function(dformulas, data, group_var, time_var) { fixed <- as.integer(attr(dformulas$all, "max_lag")) diff --git a/R/dynamice.R b/R/dynamice.R index be116d0..802e81f 100644 --- a/R/dynamice.R +++ b/R/dynamice.R @@ -2,7 +2,7 @@ #' #' Applies multiple imputation using [mice::mice()] to the supplied `data` #' and fits a dynamic multivariate panel model to each imputed data set using -#' [dynamite::dynamite()]. Posterior samples from each imputation run are +#' [dynamite()]. Posterior samples from each imputation run are #' combined. When using wide format imputation, the long format `data` is #' automatically converted to a wide format before imputation to preserve the #' longitudinal structure, and then converted back to long format for @@ -19,11 +19,12 @@ #' kept in the return object? The default is `FALSE`. If `TRUE`, the #' imputations will be included in the `imputed` field in the return object #' that is otherwise `NULL`. -#' @param stan_csv_dir \[`character(1)`] A directory path to output the +#' @param stan_csv_dir \[`character(1)`]\cr A directory path to output the #' Stan .csv files when `backend` is `"cmdstanr"`. The files are saved here #' via `$save_output_files()` to avoid garbage collection between sampling #' runs with different imputed datasets. #' @export +#' dynamice <- function(dformula, data, time, group = NULL, priors = NULL, backend = "rstan", verbose = TRUE, verbose_stan = FALSE, @@ -150,9 +151,7 @@ dynamice <- function(dformula, data, time, group = NULL, stanfit <- rstan::sflist2stanfit(sf) } else { stanfit <- cmdstanr::as_cmdstan_fit(filenames, check_diagnostics = FALSE) - #stanfit@stanmodel <- methods::new("stanmodel", model_code = tmp$model_code) } - # TODO does this work in this case? n_draws <- ifelse_(is.null(stanfit), 0L, get_ndraws(stanfit)) # TODO return object? How is this going to work with update? structure( diff --git a/R/dynamite-package.R b/R/dynamite-package.R index 056703f..22cc355 100644 --- a/R/dynamite-package.R +++ b/R/dynamite-package.R @@ -1,16 +1,17 @@ -#' The `dynamite` package. +#' The \pkg{dynamite} Package #' #' @description Easy-to-use and efficient interface for Bayesian inference of #' complex panel data consisting of multiple individuals with multiple -#' measurements over time. Supports several observational distributions, -#' time-varying effects and realistic counterfactual predictions which take into -#' account the dynamic structure of the model. +#' measurements over time using dynamic multivariate panel models. +#' Supports several observational distributions, time-varying effects and +#' realistic counterfactual predictions which take into account the dynamic +#' structure of the model. #' #' # See Also #' #' * The package vignettes -#' * [dynamite::dynamiteformula()] for information on defining models. -#' * [dynamite::dynamite()] for information on fitting models. +#' * [dynamiteformula()] for information on defining models. +#' * [dynamite()] for information on fitting models. #' * to submit a bug report #' or a feature request. #' @@ -47,12 +48,12 @@ #' recovered. "_PACKAGE" -#' Simulated Data of Gaussian Responses +#' Simulated Data of a Gaussian Response #' -#' Simulated data containing gaussian response variables with two covariates. -#' The dataset was generated from a model with time-varying effects of -#' covariate x and the lagged value of the response variable, time-varying -#' intercept, and time-invariant effect of covariate z. The time-varying +#' Simulated data containing a Gaussian response variable `y` with two +#' covariates. The dataset was generated from a model with time-varying effects +#' of covariate `x` and the lagged value of the response variable, time-varying +#' intercept, and time-invariant effect of covariate `z`. The time-varying #' coefficients vary according to a spline with 20 degrees of freedom. #' #' @family examples @@ -68,7 +69,7 @@ #' } "gaussian_example" -#' Model Fit for the Simulated Data of Gaussian Responses +#' Model Fit for the Simulated Data of a Gaussian Response #' #' A `dynamitefit` object obtained by running `dynamite` on the #' `gaussian_example` dataset as diff --git a/R/dynamite.R b/R/dynamite.R index 4f6b00a..76c143f 100644 --- a/R/dynamite.R +++ b/R/dynamite.R @@ -98,6 +98,7 @@ #' * `priors`\cr Data frame containing the used priors. #' * `backend`\cr Either `"rstan"` or `"cmdstanr"` indicating which #' package was used in sampling. +#' * `permutation`\cr Randomized permutation of the posterior draws. #' * `call`\cr Original function call as an object of class `call`. #' #' @srrstats {G2.9} Potential loss of information is reported by `dynamite`. @@ -132,8 +133,8 @@ #' based on Stan, the scalability of the package depends directly on the #' scalability of Stan. #' @references -#' Santtu Tikka and Jouni Helske (2023). `dynamite`: An \R Package for Dynamic -#' Multivariate Panel Models. arXiv preprint, +#' Santtu Tikka and Jouni Helske (2023). \pkg{dynamite}: An \R Package for +#' Dynamic Multivariate Panel Models. arXiv preprint, #' . #' #' Jouni Helske and Santtu Tikka (2022). Estimating Causal Effects @@ -496,8 +497,6 @@ dynamite_sampling <- function(sampling, backend, model_code, model, out <- with(e, { do.call(model$sample, args) }) - #out <- rstan::read_stan_csv(sampling_out$output_files()) - #out@stanmodel <- methods::new("stanmodel", model_code = model_code) } } out @@ -700,7 +699,7 @@ remove_redundant_parameters <- function(stan_input, backend, dots } -#' Access the Model Formula of a Dynamite Model +#' Access the Model Formula of a \pkg{dynamite} Model #' #' The `formula` method returns the model definition as a quoted expression. #' diff --git a/R/dynamiteformula.R b/R/dynamiteformula.R index 3b09930..85a8bce 100644 --- a/R/dynamiteformula.R +++ b/R/dynamiteformula.R @@ -1,4 +1,4 @@ -#' Model formula for \pkg{dynamite} +#' Model Formula for \pkg{dynamite} #' #' Defines a new observational or a new auxiliary channel for the model using #' standard \R formula syntax. Formulas of individual response variables can be @@ -12,7 +12,7 @@ #' #' * Categorical: `categorical` (with a softmax link using the first category #' as reference). See the documentation of the `categorical_logit_glm` in the -#' Stan function reference manual (https://mc-stan.org/users/documentation/). +#' Stan function reference manual . #' * Multinomial: `multinomial` (softmax link, first category is reference). #' * Gaussian: `gaussian` (identity link, parameterized using mean and standard #' deviation). @@ -37,7 +37,7 @@ #' combined with `+`. For example a formula #' `obs(y ~ lag(x), family = "gaussian") + obs(x ~ z, family = "poisson")` #' defines a model with two channels; -#' first we declare that `y` is a gaussian variable depending on a previous +#' first we declare that `y` is a Gaussian variable depending on a previous #' value of `x` (`lag(x)`), and then we add a second channel declaring `x` as #' Poisson distributed depending on some exogenous variable `z` #' (for which we do not define any distribution). @@ -106,7 +106,7 @@ #' `random(~1)` leads to a model where in addition to the common intercept, #' each individual/group has their own intercept with zero-mean normal prior and #' unknown standard deviation analogously with the typical mixed models. An -#' additional model component [dynamite::random_spec()] can be used to define +#' additional model component [random_spec()] can be used to define #' whether the random effects are allowed to correlate within and across #' channels and whether to use centered or noncentered parameterization for #' the random effects. diff --git a/R/fitted.R b/R/fitted.R index 7ec10aa..da012e7 100644 --- a/R/fitted.R +++ b/R/fitted.R @@ -1,9 +1,9 @@ -#' Extract Fitted Values of a Dynamite Model +#' Extract Fitted Values of a \pkg{dynamite} Model #' #' Fitted values for a `dynamitefit` object, i.e., #' \eqn{E(y_t | newdata, \theta)} where \eqn{\theta} contains all the -#' model parameters. See also -#' [dynamite::predict.dynamitefit()] for multi-step predictions. +#' model parameters. See also [predict.dynamitefit()] for multi-step +#' predictions. #' #' @export #' @family prediction @@ -55,7 +55,7 @@ #' ggplot2::geom_line(ggplot2::aes(y = LakeHuron), colour = "tomato") + #' ggplot2::theme_bw() #' } -#' } +#' } #' } #' fitted.dynamitefit <- function(object, newdata = NULL, n_draws = NULL, thin = 1, diff --git a/R/getters.R b/R/getters.R index 9944edf..3b05854 100644 --- a/R/getters.R +++ b/R/getters.R @@ -1,12 +1,12 @@ -#' Get Prior Definitions of a Dynamite Model +#' Get Prior Definitions of a \pkg{dynamite} Model #' -#' Extracts the priors used in the dynamite model as a data frame. You +#' Extracts the priors used in the `dynamite` model as a data frame. You #' can then alter the priors by changing the contents of the `prior` column and #' supplying this data frame to `dynamite` function using the argument #' `priors`. See vignettes for details. #' #' @note Only the `prior` column of the output should be altered when defining -#' the user-defined priors for the `dynamite`. +#' the user-defined priors for `dynamite`. #' #' @export #' @family fitting @@ -48,7 +48,7 @@ get_priors.dynamitefit <- function(x, ...) { x$priors } -#' Extract the Stan Code of the Dynamite Model +#' Extract the Stan Code of the \pkg{dynamite} Model #' #' Returns the Stan code of the model. Mostly useful for debugging or for #' building a customized version of the model. @@ -156,7 +156,7 @@ get_code_ <- function(x, blocks = NULL) { paste_rows(out, .parse = FALSE) } -#' Extract the Model Data of the Dynamite Model +#' Extract the Model Data of the \pkg{dynamite} Model #' #' Returns the input data to the Stan model. Mostly useful for debugging. #' @@ -206,11 +206,11 @@ get_data.dynamitefit <- function(x, ...) { out$stan_input$sampling_vars } -#' Get Parameter Dimensions of the Dynamite Model +#' Get Parameter Dimensions of the \pkg{dynamite} Model #' #' Extracts the names and dimensions of all parameters used in the -#' `dynamite` model. See also [dynamite::get_parameter_types()] and -#' [dynamite::get_parameter_names()]. The returned dimensions match those of +#' `dynamite` model. See also [get_parameter_types()] and +#' [get_parameter_names()]. The returned dimensions match those of #' the `stanfit` element of the `dynamitefit` object. When applied to #' `dynamiteformula` objects, the model is compiled and sampled for 1 iteration #' to get the parameter dimensions. @@ -267,6 +267,17 @@ get_parameter_dims.dynamitefit <- function(x, ...) { !is.null(x$stanfit), "No Stan model fit is available." ) + if (x$backend == "cmdstanr") { + return( + get_parameter_dims.dynamiteformula( + x = eval(formula(x)), + data = x$data, + time = x$time_var, + group = x$group_var, + ... + ) + ) + } pars_text <- get_code(x, blocks = "parameters") pars <- get_parameters(pars_text) # TODO no inits @@ -298,10 +309,10 @@ get_parameters <- function(x) { vapply(par_matches, "[[", character(1L), 2L) } -#' Get Parameter Types of the Dynamite Model +#' Get Parameter Types of the \pkg{dynamite} Model #' #' Extracts all parameter types of used in the `dynamitefit` object. See -#' [dynamite::as.data.frame.dynamitefit()] for explanations of different types. +#' [as.data.frame.dynamitefit()] for explanations of different types. #' #' @param x \[`dynamitefit`]\cr The model fit object. #' @param ... Ignored. @@ -323,7 +334,7 @@ get_parameter_types.dynamitefit <- function(x, ...) { unique(d$type) } -#' Get Parameter Names of the Dynamite Model +#' Get Parameter Names of the \pkg{dynamite} Model #' #' Extracts all parameter names of used in the `dynamitefit` object. #' @@ -336,7 +347,7 @@ get_parameter_types.dynamitefit <- function(x, ...) { #' #' @param x \[`dynamitefit`]\cr The model fit object. #' @param types \[`character()`]\cr Extract only names of parameter of a -#' certain type. See [dynamite::get_parameter_types()]. +#' certain type. See [get_parameter_types()]. #' @param ... Ignored. #' @return A `character` vector with parameter names of the input model. #' @export diff --git a/R/lags.R b/R/lags.R index 23c61b6..aa929cf 100644 --- a/R/lags.R +++ b/R/lags.R @@ -1,4 +1,4 @@ -#' Add Lagged Responses as Predictors to Each Channel of a Dynamite Model +#' Add Lagged Responses as Predictors to Each Channel of a \pkg{dynamite} Model #' #' Adds the lagged value of the response of each channel specified via #' [dynamiteformula()] as a predictor to each channel. The added predictors diff --git a/R/latent_factor.R b/R/latent_factor.R index 4f12ead..c648ab6 100644 --- a/R/latent_factor.R +++ b/R/latent_factor.R @@ -1,6 +1,6 @@ -#' Define a Common Latent Factor for the Dynamite Model. +#' Define a Common Latent Factor for the \pkg{dynamite} Model. #' -#' This function can be used as part of [dynamite::dynamiteformula()] to define +#' This function can be used as part of a [dynamiteformula()] to define #' a common latent factor component. The latent factor is modeled as a spline #' similarly as a time-varying intercept, but instead of having equal effect on #' each group, there is an additional loading variable for each group so that diff --git a/R/lfo.R b/R/lfo.R index b329c5a..8f3d90d 100644 --- a/R/lfo.R +++ b/R/lfo.R @@ -6,7 +6,7 @@ #' For multichannel models, the log-likelihoods of all channels are combined. #' For models with groups, expected log predictive densities (ELPDs) are #' computed independently for each group, but the re-estimation of the model -#' is triggered if pareto k values of any group exceeds the threshold. +#' is triggered if Pareto k values of any group exceeds the threshold. #' #' @export #' @family diagnostics diff --git a/R/loo.R b/R/loo.R index 7525e63..004490a 100644 --- a/R/loo.R +++ b/R/loo.R @@ -1,7 +1,7 @@ #' Approximate Leave-One-Out (LOO) Cross-validation #' #' Estimates the leave-one-out (LOO) information criterion for `dynamite` -#' models using Pareto smoothed importance sampling with the `loo` package. +#' models using Pareto smoothed importance sampling with the \pkg{loo} package. #' #' @export #' @export loo diff --git a/R/mcmc_diagnostics.R b/R/mcmc_diagnostics.R index 497ba7e..c5583dd 100644 --- a/R/mcmc_diagnostics.R +++ b/R/mcmc_diagnostics.R @@ -1,4 +1,4 @@ -#' Diagnostic Values of a Dynamite Model +#' Diagnostic Values of a \pkg{dynamite} Model #' #' Prints HMC diagnostics and lists parameters with smallest effective sample #' sizes and largest Rhat values. See [hmc_diagnostics()] and @@ -73,7 +73,7 @@ mcmc_diagnostics.dynamitefit <- function(x, n = 3L, ...) { invisible(x) } -#' HMC Diagnostics for a Dynamite Model +#' HMC Diagnostics for a \pkg{dynamite} Model #' #' Prints the divergences, saturated treedepths, and low E-BFMI warnings. #' diff --git a/R/nobs.R b/R/nobs.R index fadf25e..15a19ee 100644 --- a/R/nobs.R +++ b/R/nobs.R @@ -1,4 +1,4 @@ -#' Extract the Number of Observations Used to Fit a Dynamite Model +#' Extract the Number of Observations Used to Fit a \pkg{dynamite} Model #' #' @export #' @family output diff --git a/R/plot.R b/R/plot.R index ca5c059..b17124f 100644 --- a/R/plot.R +++ b/R/plot.R @@ -241,15 +241,14 @@ plot_dynamiteformula_ggplot <- function(g, vertex_size, label_size) { #' specified by `n_params`. Option `"trace"` instead draws posterior #' densities and traceplots of the parameters. Option `"dag"` instead plots #' the directed acyclic graph of the model formula, see -#' [dynamite::plot.dynamiteformula()] for the arguments available for this -#' option. +#' [plot.dynamiteformula()] for the arguments available for this option. #' @param types \[`character(1)`]\cr Types of the parameter for which the plots #' should be drawn. Possible options can be found with the function -#' [dynamite::get_parameter_types()]. Ignored if the argument `parameters` +#' [get_parameter_types()]. Ignored if the argument `parameters` #' is supplied. #' @param parameters \[`charecter()`]\cr Parameter name(s) for which the plots #' should be drawn. Possible options can be found with the function -#' [dynamite::get_parameter_names()]. The default is all parameters, +#' [get_parameter_names()]. The default is all parameters, #' limited by `n_params`. #' @param responses \[`character()`]\cr Response(s) for which the plots should #' be drawn. Possible options are `unique(x$priors$response)`. Default is @@ -276,8 +275,8 @@ plot_dynamiteformula_ggplot <- function(g, vertex_size, label_size) { #' parameters to plot. The defaults values are 20 for time-invariant #' parameters and 3 for time-varying parameters. The default value is 5 #' for `plot_type == "trace"`. -#' @param ... Arguments passed to [dynamite::plot.dynamiteformula()] when -#' using `plot_type = "dag"`. +#' @param ... Arguments passed to [plot.dynamiteformula()] when using +#' `plot_type = "dag"`. #' @return A `ggplot` object. #' @srrstats {BS6.1, RE6.0, RE6.1, BS6.2, BS6.3, BS6.5} Implements the `plot()` #' method. Further plots can be easily constructed with the help of @@ -475,7 +474,7 @@ plot_trace <- function(x, types, parameters, responses, ) } -#' Plot Time-invariant Parameters of a Dynamite Model +#' Plot Time-invariant Parameters of a \pkg{dynamite} Model #' #' @inheritParams plot.dynamitefit #' @noRd @@ -540,7 +539,7 @@ plot_fixed <- function(coefs, level, alpha, facet, scales, n_params) { } -#' Plot Time-varying Parameters of a Dynamite Model +#' Plot Time-varying Parameters of a \pkg{dynamite} Model #' #' @inheritParams plot.dynamitefit #' @noRd diff --git a/R/predict.R b/R/predict.R index 8036354..fbc4c80 100644 --- a/R/predict.R +++ b/R/predict.R @@ -1,4 +1,4 @@ -#' Predict Method for a Dynamite Model +#' Predict Method for a \pkg{dynamite} Model #' #' Obtain counterfactual predictions for a `dynamitefit` object. #' @@ -51,7 +51,7 @@ #' are encountered. #' * `"bootstrap"` which will randomly draw from the posterior samples of #' the random effects across all original levels. -#' * `"gaussian"` which will randomly draw from a gaussian +#' * `"gaussian"` which will randomly draw from a Gaussian #' distribution using the posterior samples of the random effects #' standard deviation (and correlation matrix if applicable). #' * `"original"` which will randomly match each new level to one of diff --git a/R/predict_helpers.R b/R/predict_helpers.R index d6a5b1f..b79f1a1 100644 --- a/R/predict_helpers.R +++ b/R/predict_helpers.R @@ -409,7 +409,7 @@ clear_nonfixed <- function(newdata, newdata_null, resp_stoch, eval_type, #' @param n_group \[`integer(1)`]\cr Number of groups. #' @param orig_ids \[`character()`]\cr Group levels of the original data. #' @param new_ids \[`character()`]\cr Group levels of `newdata` in -#' [dynamite::predict.dynamitefit()]. +#' [predict.dynamitefit()]. #' @param new_levels \[`character(1)`]\cr #' Defines if and how to sample the random effects for groups not present in #' the original data. The options are @@ -417,7 +417,7 @@ clear_nonfixed <- function(newdata, newdata_null, resp_stoch, eval_type, #' are encountered. #' * `"bootstrap"` which will randomly draw from the posterior samples of #' the random effects across all original levels. -#' * `"gaussian"` which will randomly draw from a gaussian +#' * `"gaussian"` which will randomly draw from a Gaussian #' distribution using the posterior samples of the random effect #' standard deviations (and correlation matrix if applicable). #' * `"original"` which will randomly match each new level to one of diff --git a/R/print.R b/R/print.R index 48f2433..05baf61 100644 --- a/R/print.R +++ b/R/print.R @@ -1,9 +1,9 @@ -#' Print a Summary of a Dynamite Model Fit Object +#' Print a Summary of a \pkg{dynamite} Model Fit Object #' -#' Information on the estimated dynamite model can be obtained via -#' `print` including the following: The model formula, the data, the smallest -#' effective sample sizes, largest Rhat and summary statistics of the -#' time- and group-invariant model parameters. +#' Information on the estimated `dynamite` model can be obtained via +#' `print()` including the following: The model formula, the data, +#' the smallest effective sample sizes, largest Rhat and summary statistics of +#' the time-invariant and group-invariant model parameters. #' #' @export #' @rdname dynamite diff --git a/R/summary.R b/R/summary.R index 9074392..1d16c02 100644 --- a/R/summary.R +++ b/R/summary.R @@ -1,7 +1,7 @@ -#' Summary for a Dynamite Model Fit +#' Summary for a \pkg{dynamite} Model Fit #' #' The `summary()` method provides statistics of the posterior samples of the -#' model; this is an alias of [dynamite::as.data.frame.dynamitefit()] with +#' model; this is an alias of [as.data.frame.dynamitefit()] with #' `summary = TRUE`. #' #' @export diff --git a/R/update.R b/R/update.R index d7a87d1..779e25d 100644 --- a/R/update.R +++ b/R/update.R @@ -1,4 +1,4 @@ -#' Update a Dynamite Model +#' Update a \pkg{dynamite} Model #' #' Note that using a different backend for the original model fit and when #' updating can lead to an error due to different naming in `cmdstanr` and diff --git a/inst/CITATION b/inst/CITATION index 3765923..2566782 100644 --- a/inst/CITATION +++ b/inst/CITATION @@ -27,7 +27,7 @@ bibentry( title = "dynamite: Bayesian Modeling and Causal Inference for Multivariate Longitudinal Data", author = c(person("Santtu", "Tikka"), person("Jouni", "Helske")), - note = "R package version 1.5.3", + note = "R package version 1.5.4", year = "2024", url = "https://github.com/ropensci/dynamite" ) diff --git a/man/as.data.frame.dynamitefit.Rd b/man/as.data.frame.dynamitefit.Rd index 8f95062..ae67527 100644 --- a/man/as.data.frame.dynamitefit.Rd +++ b/man/as.data.frame.dynamitefit.Rd @@ -45,7 +45,7 @@ Ignored if the argument \code{parameters} is supplied. \item{times}{[\code{double()}]\cr Time point(s) to keep. If \code{NULL} (the default), all time points are kept.} -\item{groups}{[\code{character()}] Group name(s) to keep. If \code{NULL} +\item{groups}{[\code{character()}]\cr Group name(s) to keep. If \code{NULL} (the default), all groups are kept.} \item{summary}{[\code{logical(1)}]\cr If \code{TRUE}, returns posterior @@ -66,7 +66,7 @@ from the output.} \value{ A \code{tibble} containing either samples or summary statistics of the model parameters in a long format. For a wide format, see -\code{\link[=as_draws]{as_draws()}}. +\code{\link[=as_draws.dynamitefit]{as_draws.dynamitefit()}}. } \description{ Provides a \code{data.frame} representation of the posterior samples of the model @@ -101,8 +101,8 @@ Samples of the full correlation matrix can be extracted manually as factors. Samples of the full correlation matrix can be extracted manually as \code{rstan::extract(fit$stanfit, pars = "corr_matrix_psi")} if necessary. -\item \code{sigma}\cr Standard deviations of gaussian responses. -\item \code{corr}\cr Pairwise correlations of multivariate gaussian responses. +\item \code{sigma}\cr Standard deviations of Gaussian responses. +\item \code{corr}\cr Pairwise correlations of multivariate Gaussian responses. \item \code{phi}\cr Describes various distributional parameters, such as: \itemize{ \item Dispersion parameter of the Negative Binomial distribution. diff --git a/man/as.data.table.dynamitefit.Rd b/man/as.data.table.dynamitefit.Rd index af9f52f..eca7921 100644 --- a/man/as.data.table.dynamitefit.Rd +++ b/man/as.data.table.dynamitefit.Rd @@ -49,7 +49,7 @@ Ignored if the argument \code{parameters} is supplied. \item{times}{[\code{double()}]\cr Time point(s) to keep. If \code{NULL} (the default), all time points are kept.} -\item{groups}{[\code{character()}] Group name(s) to keep. If \code{NULL} +\item{groups}{[\code{character()}]\cr Group name(s) to keep. If \code{NULL} (the default), all groups are kept.} \item{summary}{[\code{logical(1)}]\cr If \code{TRUE}, returns posterior diff --git a/man/as_draws-dynamitefit.Rd b/man/as_draws-dynamitefit.Rd index c2a9c22..58eb334 100644 --- a/man/as_draws-dynamitefit.Rd +++ b/man/as_draws-dynamitefit.Rd @@ -41,7 +41,7 @@ This argument is mutually exclusive with \code{parameters}.} \item{times}{[\code{double()}]\cr Time point(s) to keep. If \code{NULL} (the default), all time points are kept.} -\item{groups}{[\code{character()}] Group name(s) to keep. If \code{NULL} +\item{groups}{[\code{character()}]\cr Group name(s) to keep. If \code{NULL} (the default), all groups are kept.} \item{...}{Ignored.} @@ -56,17 +56,15 @@ Converts the output from a \code{\link[=dynamite]{dynamite()}} call to a \code{draws_df} format of the \pkg{posterior} package, enabling the use of diagnostics and plotting methods of \pkg{posterior} and \pkg{bayesplot} packages. Note that this function returns variables in a wide format, -whereas \code{\link[=as.data.frame]{as.data.frame()}} uses the long format. +whereas \code{\link[=as.data.frame.dynamitefit]{as.data.frame.dynamitefit()}} uses the long format. } \details{ You can use the arguments \code{parameters}, \code{responses} and \code{types} to extract only a subset of the model parameters (i.e., only certain types of parameters related to a certain response variable). -See potential values for the types argument in -\code{\link[=as.data.frame.dynamitefit]{as.data.frame.dynamitefit()}} and -\code{\link[=get_parameter_names]{get_parameter_names()}} for potential values for \code{parameters} -argument. +See potential values for the types argument in \code{\link[=as.data.frame.dynamitefit]{as.data.frame.dynamitefit()}} +and \code{\link[=get_parameter_names]{get_parameter_names()}} for potential values for \code{parameters} argument. } \examples{ data.table::setDTthreads(1) # For CRAN diff --git a/man/coef.dynamitefit.Rd b/man/coef.dynamitefit.Rd index 360d0c8..a6ae013 100644 --- a/man/coef.dynamitefit.Rd +++ b/man/coef.dynamitefit.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/coef.R \name{coef.dynamitefit} \alias{coef.dynamitefit} -\title{Extract Regression Coefficients of a Dynamite Model} +\title{Extract Regression Coefficients of a \pkg{dynamite} Model} \usage{ \method{coef}{dynamitefit}( object, @@ -38,7 +38,7 @@ Ignored if the argument \code{parameters} is supplied. \item{times}{[\code{double()}]\cr Time point(s) to keep. If \code{NULL} (the default), all time points are kept.} -\item{groups}{[\code{character()}] Group name(s) to keep. If \code{NULL} +\item{groups}{[\code{character()}]\cr Group name(s) to keep. If \code{NULL} (the default), all groups are kept.} \item{summary}{[\code{logical(1)}]\cr If \code{TRUE} (default), returns posterior diff --git a/man/confint.dynamitefit.Rd b/man/confint.dynamitefit.Rd index 1b04948..8ded67a 100644 --- a/man/confint.dynamitefit.Rd +++ b/man/confint.dynamitefit.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/ci.R \name{confint.dynamitefit} \alias{confint.dynamitefit} -\title{Credible Intervals for Dynamite Model Parameters} +\title{Credible Intervals for \pkg{dynamite} Model Parameters} \usage{ \method{confint}{dynamitefit}(object, parm, level = 0.95, ...) } diff --git a/man/dynamice.Rd b/man/dynamice.Rd index 6f0c690..bb5a1a2 100644 --- a/man/dynamice.Rd +++ b/man/dynamice.Rd @@ -110,7 +110,7 @@ kept in the return object? The default is \code{FALSE}. If \code{TRUE}, the imputations will be included in the \code{imputed} field in the return object that is otherwise \code{NULL}.} -\item{stan_csv_dir}{[\code{character(1)}] A directory path to output the +\item{stan_csv_dir}{[\code{character(1)}]\cr A directory path to output the Stan .csv files when \code{backend} is \code{"cmdstanr"}. The files are saved here via \verb{$save_output_files()} to avoid garbage collection between sampling runs with different imputed datasets.} diff --git a/man/dynamite-deprecated.Rd b/man/dynamite-deprecated.Rd index 0e63fa2..aa2d0eb 100644 --- a/man/dynamite-deprecated.Rd +++ b/man/dynamite-deprecated.Rd @@ -7,7 +7,7 @@ \alias{plot_nus} \alias{plot_lambdas} \alias{plot_psis} -\title{Deprecated Functions in the dynamite Package} +\title{Deprecated Functions in the \pkg{dynamite} Package} \usage{ plot_betas(x, ...) plot_deltas(x, ...) diff --git a/man/dynamite-package.Rd b/man/dynamite-package.Rd index 89c46ee..82776ca 100644 --- a/man/dynamite-package.Rd +++ b/man/dynamite-package.Rd @@ -3,13 +3,14 @@ \docType{package} \name{dynamite-package} \alias{dynamite-package} -\title{The \code{dynamite} package.} +\title{The \pkg{dynamite} Package} \description{ Easy-to-use and efficient interface for Bayesian inference of complex panel data consisting of multiple individuals with multiple -measurements over time. Supports several observational distributions, -time-varying effects and realistic counterfactual predictions which take into -account the dynamic structure of the model. +measurements over time using dynamic multivariate panel models. +Supports several observational distributions, time-varying effects and +realistic counterfactual predictions which take into account the dynamic +structure of the model. } \section{See Also}{ \itemize{ diff --git a/man/dynamite.Rd b/man/dynamite.Rd index 02dc3a6..b2b6c0a 100644 --- a/man/dynamite.Rd +++ b/man/dynamite.Rd @@ -136,6 +136,7 @@ construction and sampling. \item \code{priors}\cr Data frame containing the used priors. \item \code{backend}\cr Either \code{"rstan"} or \code{"cmdstanr"} indicating which package was used in sampling. +\item \code{permutation}\cr Randomized permutation of the posterior draws. \item \code{call}\cr Original function call as an object of class \code{call}. } @@ -155,10 +156,10 @@ see 'Details' and the package vignettes. The \code{formula} method returns the model definition as a quoted expression. -Information on the estimated dynamite model can be obtained via -\code{print} including the following: The model formula, the data, the smallest -effective sample sizes, largest Rhat and summary statistics of the -time- and group-invariant model parameters. +Information on the estimated \code{dynamite} model can be obtained via +\code{print()} including the following: The model formula, the data, +the smallest effective sample sizes, largest Rhat and summary statistics of +the time-invariant and group-invariant model parameters. The \code{summary()} method provides statistics of the posterior samples of the model; this is an alias of \code{\link[=as.data.frame.dynamitefit]{as.data.frame.dynamitefit()}} with @@ -204,8 +205,8 @@ summary(gaussian_example_fit, } \references{ -Santtu Tikka and Jouni Helske (2023). \code{dynamite}: An \R Package for Dynamic -Multivariate Panel Models. arXiv preprint, +Santtu Tikka and Jouni Helske (2023). \pkg{dynamite}: An \R Package for +Dynamic Multivariate Panel Models. arXiv preprint, \url{https://arxiv.org/abs/2302.01607}. Jouni Helske and Santtu Tikka (2022). Estimating Causal Effects diff --git a/man/dynamiteformula.Rd b/man/dynamiteformula.Rd index 626f500..1bbd4b0 100644 --- a/man/dynamiteformula.Rd +++ b/man/dynamiteformula.Rd @@ -6,7 +6,7 @@ \alias{aux} \alias{+.dynamiteformula} \alias{print.dynamiteformula} -\title{Model formula for \pkg{dynamite}} +\title{Model Formula for \pkg{dynamite}} \usage{ dynamiteformula(formula, family, link = NULL) @@ -53,7 +53,7 @@ distributions for the observations: \itemize{ \item Categorical: \code{categorical} (with a softmax link using the first category as reference). See the documentation of the \code{categorical_logit_glm} in the -Stan function reference manual (https://mc-stan.org/users/documentation/). +Stan function reference manual \url{https://mc-stan.org/users/documentation/}. \item Multinomial: \code{multinomial} (softmax link, first category is reference). \item Gaussian: \code{gaussian} (identity link, parameterized using mean and standard deviation). @@ -79,7 +79,7 @@ Each channel is defined via the \code{obs} function, and the channels are combined with \code{+}. For example a formula \code{obs(y ~ lag(x), family = "gaussian") + obs(x ~ z, family = "poisson")} defines a model with two channels; -first we declare that \code{y} is a gaussian variable depending on a previous +first we declare that \code{y} is a Gaussian variable depending on a previous value of \code{x} (\code{lag(x)}), and then we add a second channel declaring \code{x} as Poisson distributed depending on some exogenous variable \code{z} (for which we do not define any distribution). diff --git a/man/fitted.dynamitefit.Rd b/man/fitted.dynamitefit.Rd index 275b2df..c22ddc2 100644 --- a/man/fitted.dynamitefit.Rd +++ b/man/fitted.dynamitefit.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/fitted.R \name{fitted.dynamitefit} \alias{fitted.dynamitefit} -\title{Extract Fitted Values of a Dynamite Model} +\title{Extract Fitted Values of a \pkg{dynamite} Model} \usage{ \method{fitted}{dynamitefit}( object, @@ -52,8 +52,8 @@ A \code{data.frame} containing the fitted values. \description{ Fitted values for a \code{dynamitefit} object, i.e., \eqn{E(y_t | newdata, \theta)} where \eqn{\theta} contains all the -model parameters. See also -\code{\link[=predict.dynamitefit]{predict.dynamitefit()}} for multi-step predictions. +model parameters. See also \code{\link[=predict.dynamitefit]{predict.dynamitefit()}} for multi-step +predictions. } \examples{ data.table::setDTthreads(1) # For CRAN @@ -95,7 +95,7 @@ if (!identical(.Platform$OS.type, "windows")) { ggplot2::geom_line(ggplot2::aes(y = LakeHuron), colour = "tomato") + ggplot2::theme_bw() } - } +} } } diff --git a/man/gaussian_example.Rd b/man/gaussian_example.Rd index cda6794..cc20269 100644 --- a/man/gaussian_example.Rd +++ b/man/gaussian_example.Rd @@ -3,7 +3,7 @@ \docType{data} \name{gaussian_example} \alias{gaussian_example} -\title{Simulated Data of Gaussian Responses} +\title{Simulated Data of a Gaussian Response} \format{ A data frame with 3000 rows and 5 variables: \describe{ @@ -22,10 +22,10 @@ The data was generated via \code{gaussian_example.R} in gaussian_example } \description{ -Simulated data containing gaussian response variables with two covariates. -The dataset was generated from a model with time-varying effects of -covariate x and the lagged value of the response variable, time-varying -intercept, and time-invariant effect of covariate z. The time-varying +Simulated data containing a Gaussian response variable \code{y} with two +covariates. The dataset was generated from a model with time-varying effects +of covariate \code{x} and the lagged value of the response variable, time-varying +intercept, and time-invariant effect of covariate \code{z}. The time-varying coefficients vary according to a spline with 20 degrees of freedom. } \seealso{ diff --git a/man/gaussian_example_fit.Rd b/man/gaussian_example_fit.Rd index fd1aa38..2888b1e 100644 --- a/man/gaussian_example_fit.Rd +++ b/man/gaussian_example_fit.Rd @@ -3,7 +3,7 @@ \docType{data} \name{gaussian_example_fit} \alias{gaussian_example_fit} -\title{Model Fit for the Simulated Data of Gaussian Responses} +\title{Model Fit for the Simulated Data of a Gaussian Response} \format{ A \code{dynamitefit} object. } diff --git a/man/get_code.Rd b/man/get_code.Rd index dfe46ad..1533544 100644 --- a/man/get_code.Rd +++ b/man/get_code.Rd @@ -4,7 +4,7 @@ \alias{get_code} \alias{get_code.dynamiteformula} \alias{get_code.dynamitefit} -\title{Extract the Stan Code of the Dynamite Model} +\title{Extract the Stan Code of the \pkg{dynamite} Model} \usage{ get_code(x, ...) diff --git a/man/get_data.Rd b/man/get_data.Rd index de8c7bf..66b1a8a 100644 --- a/man/get_data.Rd +++ b/man/get_data.Rd @@ -4,7 +4,7 @@ \alias{get_data} \alias{get_data.dynamiteformula} \alias{get_data.dynamitefit} -\title{Extract the Model Data of the Dynamite Model} +\title{Extract the Model Data of the \pkg{dynamite} Model} \usage{ get_data(x, ...) diff --git a/man/get_parameter_dims.Rd b/man/get_parameter_dims.Rd index 82443f1..690cca6 100644 --- a/man/get_parameter_dims.Rd +++ b/man/get_parameter_dims.Rd @@ -4,7 +4,7 @@ \alias{get_parameter_dims} \alias{get_parameter_dims.dynamiteformula} \alias{get_parameter_dims.dynamitefit} -\title{Get Parameter Dimensions of the Dynamite Model} +\title{Get Parameter Dimensions of the \pkg{dynamite} Model} \usage{ get_parameter_dims(x, ...) diff --git a/man/get_parameter_names.Rd b/man/get_parameter_names.Rd index 35d3a0c..b7b5054 100644 --- a/man/get_parameter_names.Rd +++ b/man/get_parameter_names.Rd @@ -3,7 +3,7 @@ \name{get_parameter_names} \alias{get_parameter_names} \alias{get_parameter_names.dynamitefit} -\title{Get Parameter Names of the Dynamite Model} +\title{Get Parameter Names of the \pkg{dynamite} Model} \usage{ get_parameter_names(x, types = NULL, ...) diff --git a/man/get_parameter_types.Rd b/man/get_parameter_types.Rd index 4e89701..8113a33 100644 --- a/man/get_parameter_types.Rd +++ b/man/get_parameter_types.Rd @@ -3,7 +3,7 @@ \name{get_parameter_types} \alias{get_parameter_types} \alias{get_parameter_types.dynamitefit} -\title{Get Parameter Types of the Dynamite Model} +\title{Get Parameter Types of the \pkg{dynamite} Model} \usage{ get_parameter_types(x, ...) diff --git a/man/get_priors.Rd b/man/get_priors.Rd index c2070b9..0bb74a8 100644 --- a/man/get_priors.Rd +++ b/man/get_priors.Rd @@ -4,7 +4,7 @@ \alias{get_priors} \alias{get_priors.dynamiteformula} \alias{get_priors.dynamitefit} -\title{Get Prior Definitions of a Dynamite Model} +\title{Get Prior Definitions of a \pkg{dynamite} Model} \usage{ get_priors(x, ...) @@ -43,14 +43,14 @@ of the return object to get the column name of the new variable.} A \code{data.frame} containing the prior definitions. } \description{ -Extracts the priors used in the dynamite model as a data frame. You +Extracts the priors used in the \code{dynamite} model as a data frame. You can then alter the priors by changing the contents of the \code{prior} column and supplying this data frame to \code{dynamite} function using the argument \code{priors}. See vignettes for details. } \note{ Only the \code{prior} column of the output should be altered when defining -the user-defined priors for the \code{dynamite}. +the user-defined priors for \code{dynamite}. } \examples{ data.table::setDTthreads(1) # For CRAN diff --git a/man/hmc_diagnostics.Rd b/man/hmc_diagnostics.Rd index 8e6f1df..0ef31ec 100644 --- a/man/hmc_diagnostics.Rd +++ b/man/hmc_diagnostics.Rd @@ -3,7 +3,7 @@ \name{hmc_diagnostics} \alias{hmc_diagnostics} \alias{hmc_diagnostics.dynamitefit} -\title{HMC Diagnostics for a Dynamite Model} +\title{HMC Diagnostics for a \pkg{dynamite} Model} \usage{ hmc_diagnostics(x, ...) diff --git a/man/lags.Rd b/man/lags.Rd index 4a64c6b..32937b1 100644 --- a/man/lags.Rd +++ b/man/lags.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/lags.R \name{lags} \alias{lags} -\title{Add Lagged Responses as Predictors to Each Channel of a Dynamite Model} +\title{Add Lagged Responses as Predictors to Each Channel of a \pkg{dynamite} Model} \usage{ lags(k = 1L, type = c("fixed", "varying", "random")) } diff --git a/man/lfactor.Rd b/man/lfactor.Rd index 5fd2b9c..73d3d5b 100644 --- a/man/lfactor.Rd +++ b/man/lfactor.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/latent_factor.R \name{lfactor} \alias{lfactor} -\title{Define a Common Latent Factor for the Dynamite Model.} +\title{Define a Common Latent Factor for the \pkg{dynamite} Model.} \usage{ lfactor( responses = NULL, @@ -43,7 +43,7 @@ values). This argument is common to all factors.} An object of class \code{latent_factor}. } \description{ -This function can be used as part of \code{\link[=dynamiteformula]{dynamiteformula()}} to define +This function can be used as part of a \code{\link[=dynamiteformula]{dynamiteformula()}} to define a common latent factor component. The latent factor is modeled as a spline similarly as a time-varying intercept, but instead of having equal effect on each group, there is an additional loading variable for each group so that diff --git a/man/lfo.Rd b/man/lfo.Rd index 5fd5aff..ae591ef 100644 --- a/man/lfo.Rd +++ b/man/lfo.Rd @@ -45,7 +45,7 @@ models using Pareto smoothed importance sampling. For multichannel models, the log-likelihoods of all channels are combined. For models with groups, expected log predictive densities (ELPDs) are computed independently for each group, but the re-estimation of the model -is triggered if pareto k values of any group exceeds the threshold. +is triggered if Pareto k values of any group exceeds the threshold. } \examples{ data.table::setDTthreads(1) # For CRAN diff --git a/man/loo.dynamitefit.Rd b/man/loo.dynamitefit.Rd index c12ce40..f0fec98 100644 --- a/man/loo.dynamitefit.Rd +++ b/man/loo.dynamitefit.Rd @@ -27,7 +27,7 @@ An output from \code{\link[loo:loo]{loo::loo()}} or a list of such outputs (if } \description{ Estimates the leave-one-out (LOO) information criterion for \code{dynamite} -models using Pareto smoothed importance sampling with the \code{loo} package. +models using Pareto smoothed importance sampling with the \pkg{loo} package. } \examples{ data.table::setDTthreads(1) # For CRAN diff --git a/man/mcmc_diagnostics.Rd b/man/mcmc_diagnostics.Rd index 5cce21a..a51dced 100644 --- a/man/mcmc_diagnostics.Rd +++ b/man/mcmc_diagnostics.Rd @@ -3,7 +3,7 @@ \name{mcmc_diagnostics} \alias{mcmc_diagnostics} \alias{mcmc_diagnostics.dynamitefit} -\title{Diagnostic Values of a Dynamite Model} +\title{Diagnostic Values of a \pkg{dynamite} Model} \usage{ mcmc_diagnostics(x, ...) diff --git a/man/nobs.dynamitefit.Rd b/man/nobs.dynamitefit.Rd index fabd5eb..b2301f7 100644 --- a/man/nobs.dynamitefit.Rd +++ b/man/nobs.dynamitefit.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/nobs.R \name{nobs.dynamitefit} \alias{nobs.dynamitefit} -\title{Extract the Number of Observations Used to Fit a Dynamite Model} +\title{Extract the Number of Observations Used to Fit a \pkg{dynamite} Model} \usage{ \method{nobs}{dynamitefit}(object, ...) } @@ -15,7 +15,7 @@ Total number of non-missing observations as an \code{integer}. } \description{ -Extract the Number of Observations Used to Fit a Dynamite Model +Extract the Number of Observations Used to Fit a \pkg{dynamite} Model } \examples{ data.table::setDTthreads(1) # For CRAN diff --git a/man/plot.dynamitefit.Rd b/man/plot.dynamitefit.Rd index dff7131..8282745 100644 --- a/man/plot.dynamitefit.Rd +++ b/man/plot.dynamitefit.Rd @@ -30,8 +30,7 @@ selected by \code{types} or \code{parameters}. If both \code{"types"} and specified by \code{n_params}. Option \code{"trace"} instead draws posterior densities and traceplots of the parameters. Option \code{"dag"} instead plots the directed acyclic graph of the model formula, see -\code{\link[=plot.dynamiteformula]{plot.dynamiteformula()}} for the arguments available for this -option.} +\code{\link[=plot.dynamiteformula]{plot.dynamiteformula()}} for the arguments available for this option.} \item{types}{[\code{character(1)}]\cr Types of the parameter for which the plots should be drawn. Possible options can be found with the function @@ -76,8 +75,8 @@ parameters to plot. The defaults values are 20 for time-invariant parameters and 3 for time-varying parameters. The default value is 5 for \code{plot_type == "trace"}.} -\item{...}{Arguments passed to \code{\link[=plot.dynamiteformula]{plot.dynamiteformula()}} when -using \code{plot_type = "dag"}.} +\item{...}{Arguments passed to \code{\link[=plot.dynamiteformula]{plot.dynamiteformula()}} when using +\code{plot_type = "dag"}.} } \value{ A \code{ggplot} object. diff --git a/man/predict.dynamitefit.Rd b/man/predict.dynamitefit.Rd index dcc977c..c1b932c 100644 --- a/man/predict.dynamitefit.Rd +++ b/man/predict.dynamitefit.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/predict.R \name{predict.dynamitefit} \alias{predict.dynamitefit} -\title{Predict Method for a Dynamite Model} +\title{Predict Method for a \pkg{dynamite} Model} \usage{ \method{predict}{dynamitefit}( object, @@ -67,7 +67,7 @@ group level was not present in the original data. The options are: are encountered. \item \code{"bootstrap"} which will randomly draw from the posterior samples of the random effects across all original levels. -\item \code{"gaussian"} which will randomly draw from a gaussian +\item \code{"gaussian"} which will randomly draw from a Gaussian distribution using the posterior samples of the random effects standard deviation (and correlation matrix if applicable). \item \code{"original"} which will randomly match each new level to one of diff --git a/man/update.dynamitefit.Rd b/man/update.dynamitefit.Rd index 241ffee..48253b1 100644 --- a/man/update.dynamitefit.Rd +++ b/man/update.dynamitefit.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/update.R \name{update.dynamitefit} \alias{update.dynamitefit} -\title{Update a Dynamite Model} +\title{Update a \pkg{dynamite} Model} \usage{ \method{update}{dynamitefit}( object, diff --git a/tests/testthat/test-cmdstanr.R b/tests/testthat/test-cmdstanr.R index 2becaa7..024a9d2 100644 --- a/tests/testthat/test-cmdstanr.R +++ b/tests/testthat/test-cmdstanr.R @@ -16,7 +16,7 @@ test_that("stanc_options argument works", { gaussian_example, "time", "id", - #parallel_chains = 2, + parallel_chains = 2, chains = 1, refresh = 0, backend = "cmdstanr", @@ -540,3 +540,39 @@ test_that("dynamice with cmdstanr backend works", { NA ) }) + +test_that("get_parameter_dims() works for cmdnstar models", { + skip_if_not(run_extended_tests) + skip_on_os("mac") # Seems to segfault on MacOS + set.seed(1) + f <- obs(g ~ lag(g) + lag(logp), family = "gaussian") + + obs(p ~ lag(g) + lag(logp) + lag(b), family = "poisson") + + obs(b ~ lag(b) * lag(logp) + lag(b) * lag(g), family = "bernoulli") + + aux(numeric(logp) ~ log(p + 1) | init(0)) + fit_dynamite <- suppressWarnings( + dynamite( + dformula = f, + data = multichannel_example, + time = "time", + group = "id", + backend = "cmdstanr", + show_messages = FALSE, + chains = 1, + parallel_chains = 1, + iter_sampling = 10, + iter_warmup = 10 + ) + ) + expect_equal( + get_parameter_dims(fit_dynamite), + list( + beta_g = 2L, + a_g = 1L, + sigma_g = 1L, + beta_p = 3L, + a_p = 1L, + beta_b = 5L, + a_b = 1L + ) + ) +}) diff --git a/tests/testthat/test-output.R b/tests/testthat/test-output.R index 4c5911d..451d593 100644 --- a/tests/testthat/test-output.R +++ b/tests/testthat/test-output.R @@ -417,9 +417,20 @@ test_that("gets can be got", { ) expect_equal( get_parameter_names(categorical_example_fit), - c("alpha_x", "beta_x_z", "beta_x_x_lag1B", "beta_x_x_lag1C", - "beta_x_y_lag1b", "beta_x_y_lag1c", "alpha_y", "beta_y_z", - "beta_y_x_lag1B", "beta_y_x_lag1C", "beta_y_y_lag1b", "beta_y_y_lag1c") + c( + "alpha_x", + "beta_x_z", + "beta_x_x_lag1B", + "beta_x_x_lag1C", + "beta_x_y_lag1b", + "beta_x_y_lag1c", + "alpha_y", + "beta_y_z", + "beta_y_x_lag1B", + "beta_y_x_lag1C", + "beta_y_y_lag1b", + "beta_y_y_lag1c" + ) ) expect_equal( get_parameter_dims(categorical_example_fit),