Skip to content

Commit

Permalink
fix dynamice return object, add keep_imputed arg, rerun fit objects, …
Browse files Browse the repository at this point in the history
…run-extended
  • Loading branch information
santikka committed Apr 5, 2024
1 parent 69d4c62 commit 487ec04
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 9 deletions.
17 changes: 13 additions & 4 deletions R/dynamice.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@
#' @inheritParams dynamite
#' @param mice_args \[`list()`]\cr
#' Arguments passed to [mice::mice()] excluding `data`.
#' @param impute_format \[`character(1L)`]\cr Format of the data that will be
#' @param impute_format \[`character(1)`]\cr Format of the data that will be
#' passed to the imputation method. Should be either `"wide"` (the default)
#' or `"long"` corresponding to wide format and long format imputation.
#' @param keep_imputed \[`logical(1)`]\cr Should the imputed datasets be
#' 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`.
#' @export
dynamice <- function(dformula, data, time, group = NULL,
priors = NULL, backend = "rstan",
verbose = TRUE, verbose_stan = FALSE,
stanc_options = list("O0"),
threads_per_chain = 1L, grainsize = NULL,
custom_stan_model = NULL, debug = NULL,
mice_args = list(), impute_format = "wide", ...) {
mice_args = list(), impute_format = "wide",
keep_imputed = FALSE, ...) {
stopifnot_(
requireNamespace("mice"),
"Please install the {.pkg mice} package to use multiple imputation."
Expand Down Expand Up @@ -54,6 +59,10 @@ dynamice <- function(dformula, data, time, group = NULL,
!inherits(impute_format, "try-error"),
"Argument {.arg impute_format} must be either {.val long} or {.val wide}."
)
stopifnot_(
checkmate::test_flag(x = keep_imputed),
"Argument {.arg keep_imputed} must be a single {.cls logical} value."
)
data <- droplevels(data)
data <- data.table::as.data.table(data)
stopifnot_(
Expand Down Expand Up @@ -160,8 +169,8 @@ dynamice <- function(dformula, data, time, group = NULL,
priors = priors,
backend = backend,
permutation = sample(n_draws),
imputed = imputed,
call = tmp$call, # TODO?
imputed = onlyif(keep_imputed, imputed),
call = tmp$call # TODO?
),
class = "dynamitefit"
)
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion data-raw/latent_factor_example_fit.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Code to create `latent_factor_example_fit` object

library(dynamite)
library("dynamite")

# Note the very small number of post-warmup iterations due to the data size
# restrictions in CRAN.
Expand Down
Binary file modified data/categorical_example_fit.rda
Binary file not shown.
Binary file modified data/gaussian_example_fit.rda
Binary file not shown.
Binary file modified data/gaussian_simulation_fit.rda
Binary file not shown.
Binary file modified data/multichannel_example_fit.rda
Binary file not shown.
8 changes: 7 additions & 1 deletion man/dynamice.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions tests/testthat/test-extended.R
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,11 @@ test_that("custom stan model works", {
),
NA
)
expect_equal(
rstan::extract(custom_fit$stanfit, permuted = FALSE),
rstan::extract(gaussian_example_fit$stanfit, permuted = FALSE)
expect_true(
all.equal(
rstan::extract(custom_fit$stanfit, permuted = FALSE),
rstan::extract(gaussian_example_fit$stanfit, permuted = FALSE)
)
)
})

Expand All @@ -324,6 +326,7 @@ test_that("dynamice works", {
refresh = 0,
backend = "rstan",
impute_format = "long",
keep_imputed = FALSE,
mice_args = list(m = 5, print = FALSE)
),
NA
Expand All @@ -339,6 +342,7 @@ test_that("dynamice works", {
refresh = 0,
backend = "rstan",
impute_format = "wide",
keep_imputed = FALSE,
mice_args = list(m = 5, print = FALSE)
),
NA
Expand Down

0 comments on commit 487ec04

Please sign in to comment.