Skip to content

Commit

Permalink
remove thin from lfo, document imputation functions as internal, run-…
Browse files Browse the repository at this point in the history
…extended
  • Loading branch information
helske committed Apr 7, 2024
1 parent e20596b commit c77c2b0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

* Estimation of dynamic multivariate panel models with multiple imputation is now available via the function `dynamice()` which uses the `mice` package.
* `predict` and `fitted` functions no longer permutes the posterior samples when all samples are used i.e. when `n_draws = NULL` (default). This also corrects the standard error estimates of `loo`, which were not correct earlier due to the mixing of chains.
* Added an argument `thin` for the `loo`, `lfo`, `predict` and `fitted` method.
* Added an argument `thin` for the `loo`, `predict` and `fitted` method.
* Print method now only prints the run time for fastest and slowest chain instead of all chains.

# dynamite 1.4.11
Expand Down
8 changes: 6 additions & 2 deletions R/dynamice.R
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,12 @@ parse_predictors_wide <- function(dformula, value_vars, idx_time, group_var) {

#' Compute Lagging Values of an Imputed Response
#'
#' Function for computing the lagged values of an imputed response in `mice`.
#'
#' @inheritParams mice::mice.impute.norm
#' @param group_var \[`character(1)`]\cr Name of the grouping variable.
#' @param resp \[`character(1)`]\cr Name of the response variable.
#' @noRd
#' @keywords internal
#' @export
mice.impute.lag <- function(y, ry, x, wy = NULL,
group_var, resp, ...) {
Expand All @@ -464,10 +466,12 @@ mice.impute.lag <- function(y, ry, x, wy = NULL,

#' Compute Leading Values of an Imputed Response
#'
#' Function for computing the leading values of an imputed response in `mice`.
#'
#' @inheritParams mice::mice.impute.norm
#' @param group_var \[`character(1)`]\cr Name of the grouping variable.
#' @param resp \[`character(1)`]\cr Name of the response variable.
#' @noRd
#' @keywords internal
#' @export
mice.impute.lead <- function(y, ry, x, wy = NULL,
group_var, resp, ...) {
Expand Down
11 changes: 2 additions & 9 deletions R/lfo.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
#' the LFO computations to the console.
#' @param k_threshold \[`numeric(1)`]\cr Threshold for the Pareto k estimate
#' triggering refit. Default is 0.7.
#' @param thin \[`integer(1)`]\cr Use only every `thin` posterior sample when
#' computing LFO. This can be beneficial with when the model object contains
#' large number of samples. Default is `1` meaning that all samples are used.
#' @param ... Additional arguments passed to [rstan::sampling()] or
#' [cmdstanr::sample()], such as `chains` and `cores` (`parallel_chains` in
#' `cmdstanr`).
Expand Down Expand Up @@ -54,7 +51,7 @@
#' }
#' }
#'
lfo <- function(x, L, verbose = TRUE, k_threshold = 0.7, thin = 1, ...) {
lfo <- function(x, L, verbose = TRUE, k_threshold = 0.7, ...) {
stopifnot_(
is.null(x$imputed),
"Leave-future-out cross-validation is not supported for models
Expand All @@ -76,10 +73,6 @@ lfo <- function(x, L, verbose = TRUE, k_threshold = 0.7, thin = 1, ...) {
checkmate::test_number(x = k_threshold),
"Argument {.arg k_threshold} must be a single {.cls numeric} value."
)
stopifnot_(
checkmate::test_int(x = thin, lower = 1L, upper = ndraws(x)),
"Argument {.arg thin} must be a single positive {.cls integer}."
)
time_var <- x$time_var
group_var <- x$group_var
tp <- sort(unique(x$data[[time_var]]))
Expand All @@ -100,7 +93,7 @@ lfo <- function(x, L, verbose = TRUE, k_threshold = 0.7, thin = 1, ...) {
}
fit <- update_(x, data = d, refresh = 0, ...)

idx_draws <- seq.int(1, ndraws(x), by = thin)
idx_draws <- seq.int(1, ndraws(fit))
n_draws <- length(idx_draws)
# would be faster to use only data
# x$data[eval(time) >= tp[L] - x$stan$fixed]
Expand Down
6 changes: 1 addition & 5 deletions man/lfo.Rd

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

0 comments on commit c77c2b0

Please sign in to comment.