diff --git a/R/utils.R b/R/utils.R index c50e8db6..a4e00b5f 100644 --- a/R/utils.R +++ b/R/utils.R @@ -428,7 +428,12 @@ cforde <- function(params, stop("For all entered evidence rows, no matching leaves could be found. This is probably because evidence lies outside of the distribution calculated by FORDE. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde(). For categorical data, consider setting alpha>0 in forde().") } else { if (grepl("warning$", nomatch)) { - warning("For some entered evidence rows, no matching leaves could be found. This is probably because evidence lies outside of the distribution calculated by FORDE. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde(). For categorical data, consider setting alpha>0 in forde().") + wrn <- "For some entered evidence rows, no matching leaves could be found. This is probably because evidence lies outside of the distribution calculated by FORDE. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde(). For categorical data, consider setting alpha>0 in forde()." + if (grepl("^force", nomatch)) { + warning(paste(wrn, "Sampling from all leaves with equal probability (can be changed with 'nomatch' argument).")) + } else { + warning(paste(wrn, "Returning NA for those rows (can be changed with 'nomatch' argument).")) + } } conds_impossible <- conds_conditioned[!(conds_conditioned %in% relevant_leaves[,unique(c_idx)])] relevant_leaves <- setorder(rbind(relevant_leaves, data.table(c_idx = conds_impossible, f_idx = NA_integer_, f_idx_uncond = NA_integer_))) @@ -459,7 +464,12 @@ cforde <- function(params, cvg_new[, cvg := NA] } if (grepl("warning$", nomatch)) { - warning("All leaves have zero likelihood. This is probably because evidence contains an (almost) impossible combination. Sampling from all leaves with equal probability.") + wrn <- "All leaves have zero likelihood. This is probably because evidence contains an (almost) impossible combination." + if (grepl("^force", nomatch)) { + warning(paste(wrn, "Sampling from all possible leaves with equal probability.")) + } else { + warning(paste(wrn, "Returning NA.")) + } } } else { cvg_new[, cvg := exp(cvg - max(cvg))] @@ -474,7 +484,12 @@ cforde <- function(params, cvg_new <- cvg_new[leaf_zero_lik == FALSE, ] } if (grepl("warning$", nomatch)) { - warning("All leaves have zero likelihood for some entered evidence rows. This is probably because evidence contains an (almost) impossible combination. Sampling from all leaves with equal probability.") + wrn <- "All leaves have zero likelihood for some entered evidence rows. This is probably because evidence contains an (almost) impossible combination." + if (grepl("^force", nomatch)) { + warning(paste(wrn, "Sampling from all possible leaves with equal probability (can be changed with 'nomatch' argument).")) + } else { + warning(paste(wrn, "Returning NA for those rows (can be changed with 'nomatch' argument).")) + } } } if (any(cvg_new[, !leaf_zero_lik])) { diff --git a/tests/testthat/test-conditions.R b/tests/testthat/test-conditions.R index 43c9ce6a..fe60fd14 100644 --- a/tests/testthat/test-conditions.R +++ b/tests/testthat/test-conditions.R @@ -30,14 +30,14 @@ test_that("if nomatch='force_warning', run through with a warning", { psi_no <- forde(arf, iris, finite_bounds = "no", parallel = FALSE) expect_warning(x_synth <- forge(psi_no, evidence = data.frame(Sepal.Length = 100), nomatch = "force_warning", n_synth = 10, parallel = FALSE), - "All leaves have zero likelihood for some entered evidence rows\\. This is probably because evidence contains an \\(almost\\) impossible combination\\. Sampling from all leaves with equal probability\\.") + "All leaves have zero likelihood for some entered evidence rows\\. This is probably because evidence contains an \\(almost\\) impossible combination\\. Sampling from all possible leaves with equal probability \\(can be changed with 'nomatch' argument\\)\\.") expect_true(all(!is.na(x_synth))) # No matching leaf case (finite bounds) psi_global <- forde(arf, iris, finite_bounds = "global", parallel = FALSE) expect_warning(x_synth <- forge(psi_global, evidence = data.frame(Sepal.Length = 100), nomatch = "force_warning", n_synth = 10, parallel = FALSE), - "For some entered evidence rows, no matching leaves could be found\\. This is probably because evidence lies outside of the distribution calculated by FORDE\\. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde\\(\\)\\. For categorical data, consider setting alpha>0 in forde\\(\\)\\.") + "For some entered evidence rows, no matching leaves could be found\\. This is probably because evidence lies outside of the distribution calculated by FORDE\\. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde\\(\\)\\. For categorical data, consider setting alpha>0 in forde\\(\\)\\. Sampling from all leaves with equal probability \\(can be changed with 'nomatch' argument\\)\\.") expect_true(all(!is.na(x_synth))) }) @@ -60,14 +60,14 @@ test_that("if nomatch='na_warning', run through with a warning and return NA", { psi_no <- forde(arf, iris, finite_bounds = "no", parallel = FALSE) expect_warning(x_synth <- forge(psi_no, evidence = data.frame(Sepal.Length = 100), nomatch = "na_warning", n_synth = 10, parallel = FALSE), - "All leaves have zero likelihood for some entered evidence rows\\. This is probably because evidence contains an \\(almost\\) impossible combination\\. Sampling from all leaves with equal probability\\.") + "All leaves have zero likelihood for some entered evidence rows\\. This is probably because evidence contains an \\(almost\\) impossible combination\\. Returning NA for those rows \\(can be changed with 'nomatch' argument\\)\\.") expect_true(all(is.na(x_synth[, -1]))) # No matching leaf case (finite bounds) psi_global <- forde(arf, iris, finite_bounds = "global", parallel = FALSE) expect_warning(x_synth <- forge(psi_global, evidence = data.frame(Sepal.Length = 100), nomatch = "na_warning", n_synth = 10, parallel = FALSE), - "For some entered evidence rows, no matching leaves could be found\\. This is probably because evidence lies outside of the distribution calculated by FORDE\\. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde\\(\\)\\. For categorical data, consider setting alpha>0 in forde\\(\\)\\.") + "For some entered evidence rows, no matching leaves could be found\\. This is probably because evidence lies outside of the distribution calculated by FORDE\\. For continuous data, consider setting epsilon>0 or finite_bounds='no' in forde\\(\\)\\. For categorical data, consider setting alpha>0 in forde\\(\\)\\. Returning NA for those rows \\(can be changed with 'nomatch' argument\\)\\.") expect_true(all(is.na(x_synth[, -1]))) })