diff --git a/R/pred.R b/R/pred.R index d3bcf3f..b87274a 100644 --- a/R/pred.R +++ b/R/pred.R @@ -413,6 +413,15 @@ 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.\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) fitY$vcov <- add_vcov(fitY) diff --git a/R/simulate.R b/R/simulate.R index 4042707..07d8c31 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 <- coefficients[vapply(coefficients, 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){