From 15d8445fcc1af7268280e5ca052cf90883255c41 Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Mon, 24 Feb 2025 20:35:21 -0800 Subject: [PATCH] Test for Inf in covariate columns #408 --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/fit.R | 6 ++++++ tests/testthat/test-index.R | 14 ++++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index e4ac8dd0..35e1e1f2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: sdmTMB Title: Spatial and Spatiotemporal SPDE-Based GLMMs with 'TMB' -Version: 0.6.0.9028 +Version: 0.6.0.9029 Authors@R: c( person(c("Sean", "C."), "Anderson", , "sean@seananderson.ca", role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index 57602ce1..eb5687c2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # sdmTMB (development version) +* Add check for `Inf`/`-Inf` values before fitting. #408 + * Add priors for `breakpt()` and `logistic()` parameters. #403 * Add linear component of smoothers to `tidy()`. #90 diff --git a/R/fit.R b/R/fit.R index c7673856..d8b84989 100644 --- a/R/fit.R +++ b/R/fit.R @@ -859,6 +859,12 @@ sdmTMB <- function( formula_no_bars_no_sm <- remove_s_and_t2(formula_no_bars) X_ij[[ii]] <- model.matrix(formula_no_bars_no_sm, data) mf[[ii]] <- model.frame(formula_no_bars_no_sm, data) + vars <- colnames(mf[[ii]]) + for (g in seq_along(vars)) { + if (any(is.infinite(mf[[ii]][, g, drop = TRUE]))) { + cli_abort("Column `{vars[g]}` had an Inf/-Inf value. Please remove this before fitting the model.") + } + } mt[[ii]] <- attr(mf[[ii]], "terms") # parse everything mgcv + smoothers: diff --git a/tests/testthat/test-index.R b/tests/testthat/test-index.R index f4b86228..0ec65141 100644 --- a/tests/testthat/test-index.R +++ b/tests/testthat/test-index.R @@ -187,3 +187,17 @@ test_that("get_index works", { # # }) +# https://github.com/pbs-assess/sdmTMB/issues/408 +test_that("Models error our nicely with Inf or -Inf covariates before get_index()", { + d <- pcod + d$depth_scaled[1] <- -Inf + expect_error(m <- sdmTMB( + data = d, + formula = density ~ 0 + as.factor(year) + depth_scaled, + spatiotemporal = "off", # speed + spatial = "off", # speed + time = "year", + family = delta_gamma(type = "poisson-link") + ), regexp = "Inf") +}) +