Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a helper function for bootstrapping
- Loading branch information
Showing
7 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ Imports: | |
parallel, | ||
stats | ||
Suggests: | ||
boot (>= 1.3-10), | ||
coala (>= 0.2.1), | ||
knitr, | ||
rmarkdown, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#' Parametric Bootstrapping of Jaatha Estimates | ||
#' | ||
#' This function is a helper function for using the \code{\link[boot]{boot}} | ||
#' function to bootstrap Jaatha estimates. Each bootstap replication requires | ||
#' a complete jaatha estimation on data simulated with the original parameter | ||
#' estimates. Therefore, bootstrapping is normally computationally demanding and | ||
#' should be executed on a computing cluster. The jaatha analyses are | ||
#' resticted to a single CPU cores, so that as many replicas as possible can | ||
#' be executed in parallel using the corresponding options of | ||
#' \code{\link[boot]{boot}}. | ||
#' | ||
#' @param model The jaatha model | ||
#' @param data The jaatha data | ||
#' @param results The results of an \code{\link{jaatha}} analysis performed with | ||
#' the same \code{model} and \code{data} as passed to this function. | ||
#' @param R The number of bootstrapping replicates that are performed. | ||
#' @param ... Additional arguments that are passed on \code{\link[boot]{boot}}. | ||
#' It is highly recommended to use its \code{parallel} and \code{ncpus} | ||
#' options to parallelize the bootstrap replicates. | ||
#' @return The result of \code{\link[boot]{boot}}. This object can be used to | ||
#' estimate standard errors or confidence intervals of the estimates using | ||
#' the functions available in package \pkg{boot}. | ||
#' | ||
#' @export | ||
boot_jaatha <- function(model, data, results, R, ...) { | ||
require_package("boot") | ||
|
||
args <- results$args | ||
sim_func <- model$get_sim_func() | ||
|
||
log_folder <- tempfile("logs_") | ||
message("Logfiles in folder: ", tempdir()) | ||
message("This might take a while...") | ||
dir.create(log_folder) | ||
|
||
jaatha_stat <- function(data) { | ||
null <- capture.output({ | ||
results <- jaatha(model, data, | ||
repetitions = args$repetition, | ||
sim = args$sim, | ||
max_steps = args$max_steps, | ||
init_method = args$init_method, | ||
cores = 1) | ||
}, | ||
file = tempfile(paste0("boot_log_", Sys.getpid(), "_")), | ||
type = "message") | ||
|
||
results$estimate | ||
} | ||
|
||
simulate_data <- function(data, param) { | ||
sim_data <- sim_func(param) | ||
create_jaatha_data.default(sim_data, model) | ||
} | ||
|
||
boot::boot(data, jaatha_stat, R = R, | ||
sim = "parametric", | ||
ran.gen = simulate_data, | ||
mle = results$estimate, ...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
context("Bootstrapping") | ||
|
||
test_that("the front-end for boot works", { | ||
skip_if_not_installed("boot") | ||
|
||
model <- create_test_model() | ||
data <- create_test_data(model) | ||
|
||
results <- list(estimate = c(5.05, 5.05), | ||
loglikelihood = -5, | ||
converged = TRUE, | ||
args = list(repetitions = 1, | ||
sim = 10, | ||
max_steps = 10, | ||
init_method = "middle")) | ||
|
||
capture.output(boot_values <- boot_jaatha(model, data, results, 10)) | ||
expect_is(boot_values, "boot") | ||
}) |