Skip to content

Commit 3c678d1

Browse files
authored
Merge 6bf9b3d into 4b94b11
2 parents 4b94b11 + 6bf9b3d commit 3c678d1

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

R/args.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,8 +1021,12 @@ validate_exe_file <- function(exe_file) {
10211021
#' @param num_procs Number of CmdStan processes.
10221022
#' @param model_variables A list of all parameters with their types and
10231023
#' number of dimensions. Typically the output of model$variables().
1024+
#' @param warn_partial Should a warning be thrown if inits are only specified
1025+
#' for a subset of parameters? Can be controlled by global option
1026+
#' `cmdstanr_warn_inits`.
10241027
#' @return A character vector of file paths.
1025-
process_init_list <- function(init, num_procs, model_variables = NULL) {
1028+
process_init_list <- function(init, num_procs, model_variables = NULL,
1029+
warn_partial = getOption("cmdstanr_warn_inits", TRUE)) {
10261030
if (!all(sapply(init, function(x) is.list(x) && !is.data.frame(x)))) {
10271031
stop("If 'init' is a list it must be a list of lists.", call. = FALSE)
10281032
}
@@ -1048,7 +1052,7 @@ process_init_list <- function(init, num_procs, model_variables = NULL) {
10481052
}
10491053
}
10501054
}
1051-
if (length(missing_parameter_values) > 0) {
1055+
if (length(missing_parameter_values) > 0 && isTRUE(warn_partial)) {
10521056
warning_message <- c(
10531057
"Init values were only set for a subset of parameters. \nMissing init values for the following parameters:\n"
10541058
)
@@ -1062,6 +1066,7 @@ process_init_list <- function(init, num_procs, model_variables = NULL) {
10621066
warning_message <- c(warning_message, paste0(line_text, paste0(missing_parameter_values[[i]], collapse = ", "), "\n"))
10631067
}
10641068
}
1069+
warning_message <- c(warning_message, "\nTo disable this message use options(cmdstanr_warn_inits = FALSE).\n")
10651070
message(warning_message)
10661071
}
10671072
}

tests/testthat/test-model-init.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ test_that("error if init function specified incorrectly", {
240240
})
241241

242242
test_that("print message if not all parameters are initialized", {
243+
options(cmdstanr_warn_inits = NULL) # should default to TRUE
243244
init_list <- list(
244245
list(
245246
alpha = 1
@@ -271,6 +272,23 @@ test_that("print message if not all parameters are initialized", {
271272
)
272273
})
273274

275+
test_that("No message printed if options(cmdstanr_warn_inits=FALSE)", {
276+
options(cmdstanr_warn_inits = FALSE)
277+
expect_message(
278+
utils::capture.output(mod_logistic$optimize(data = data_list_logistic, init = list(list(a = 0)), seed = 123)),
279+
regexp = NA
280+
)
281+
expect_message(
282+
utils::capture.output(mod_logistic$optimize(data = data_list_logistic, init = list(list(alpha = 1)), seed = 123)),
283+
regexp = NA
284+
)
285+
expect_message(
286+
utils::capture.output(mod_logistic$sample(data = data_list_logistic, init = list(list(alpha = 1),list(alpha = 1)), chains = 2, seed = 123)),
287+
regexp = NA
288+
)
289+
options(cmdstanr_warn_inits = TRUE)
290+
})
291+
274292
test_that("Initial values for single-element containers treated correctly", {
275293
modcode <- "
276294
data {

0 commit comments

Comments
 (0)