From 8eb5fad9a7780b62b8718206dac0de2110e20054 Mon Sep 17 00:00:00 2001 From: Andy Baxter Date: Fri, 1 Mar 2024 10:29:58 +0000 Subject: [PATCH 1/4] suggested error catch and message --- R/pred.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/pred.R b/R/pred.R index d3bcf3f..9966889 100644 --- a/R/pred.R +++ b/R/pred.R @@ -413,6 +413,9 @@ pred_fun_Y <- function(model, yrestrictions, outcome_type, outcome_name, # Fit GLM for outcome variable using user-specified model and entire dataset fitY <- stats::glm(model, family = outcome_fam, data = obs_data, y = TRUE) } + if(anyNA(coefficients(fitY))) { + stop("`NA` coefficients produced in prediction model. This may be due to (multi)collinearity in `ymodel` predictor variables, or including `time_name` variable in a 'continuous_eof' model.") + } fitY$rmse <- add_rmse(fitY) fitY$stderr <- add_stderr(fitY) fitY$vcov <- add_vcov(fitY) From 1f94392858d68276cf2719fea1504ea395ef02e7 Mon Sep 17 00:00:00 2001 From: Andy Baxter Date: Fri, 1 Mar 2024 11:13:35 +0000 Subject: [PATCH 2/4] fuller error message with listed problem vars --- R/pred.R | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/R/pred.R b/R/pred.R index 9966889..b87274a 100644 --- a/R/pred.R +++ b/R/pred.R @@ -414,7 +414,13 @@ pred_fun_Y <- function(model, yrestrictions, outcome_type, outcome_name, fitY <- stats::glm(model, family = outcome_fam, data = obs_data, y = TRUE) } if(anyNA(coefficients(fitY))) { - stop("`NA` coefficients produced in prediction model. This may be due to (multi)collinearity in `ymodel` predictor variables, or including `time_name` variable in a 'continuous_eof' model.") + stop( + "`NA` coefficients produced in prediction model. ", + "This may be due to (multi)collinearity in `ymodel` predictor variables, ", + "or including `time_name` variable in a 'continuous_eof' model.\n", + "Variables returning `NA` coefficients:\n - ", + paste(names(coefficients(fitY)[is.na(coefficients(fitY))]), collapse = "\n - ") + ) } fitY$rmse <- add_rmse(fitY) fitY$stderr <- add_stderr(fitY) From c3e04ef3c3cd5b723cfc186c7d0f05c17d93d807 Mon Sep 17 00:00:00 2001 From: Andy Baxter Date: Fri, 1 Mar 2024 11:47:31 +0000 Subject: [PATCH 3/4] added messages in covmodels predictions --- R/simulate.R | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/R/simulate.R b/R/simulate.R index 4042707..bfa5e3f 100644 --- a/R/simulate.R +++ b/R/simulate.R @@ -145,6 +145,28 @@ simulate <- function(o, fitcov, fitY, fitD, min_time, show_progress, pb, int_visit_type, ...){ set.seed(subseed) + invalid_coefs <- vapply(lapply(fitcov, coefficients), anyNA, logical(1)) |> any() + + if (invalid_coefs) { + + coefficients <- lapply(fitcov, coefficients) + + errored_parts <- out_coefs[vapply(out_coefs, anyNA, logical(1))] |> + lapply(\(coefs) {names(coefs[is.na(coefs)])}) + + + paste(names(errored_parts), errored_parts, sep = "\n - ", collapse = "\n\n") + + stop( + "`NA` coefficients produced in covariate prediction model. ", + "This may be due to (multi)collinearity in `covmodels` predictor variables, ", + "or including `time_name` variable in a 'continuous_eof' model.\n", + "Variables returning `NA` coefficients:\n", + paste(names(errored_parts), errored_parts, sep = "\n - ", collapse = "\n\n") + ) + + } + # Mechanism of passing intervention variable and intervention is different for parallel # and non-parallel versions if (parallel){ From fa02ce1f51dfe4f4cd6e6eec1b0dc89b735c12c7 Mon Sep 17 00:00:00 2001 From: Andy Baxter Date: Fri, 1 Mar 2024 11:52:24 +0000 Subject: [PATCH 4/4] correcting variable call --- R/simulate.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/simulate.R b/R/simulate.R index bfa5e3f..07d8c31 100644 --- a/R/simulate.R +++ b/R/simulate.R @@ -151,7 +151,7 @@ simulate <- function(o, fitcov, fitY, fitD, coefficients <- lapply(fitcov, coefficients) - errored_parts <- out_coefs[vapply(out_coefs, anyNA, logical(1))] |> + errored_parts <- coefficients[vapply(coefficients, anyNA, logical(1))] |> lapply(\(coefs) {names(coefs[is.na(coefs)])})