From 56d9ea88ec1ba26038492fe3dee51c64eaed449a Mon Sep 17 00:00:00 2001 From: Eric Ward <5046884+ericward-noaa@users.noreply.github.com> Date: Wed, 14 Feb 2024 05:29:01 -0800 Subject: [PATCH 1/3] Add new message about time See this discussion: https://github.com/pbs-assess/sdmTMB/discussions/298 --- R/fit.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/fit.R b/R/fit.R index 326e642ae..3545eea7b 100644 --- a/R/fit.R +++ b/R/fit.R @@ -709,6 +709,7 @@ sdmTMB <- function( if (!is.null(time)) { assert_that(time %in% names(data), msg = "Specified `time` column is missing from `data`.") + assert_that(length(is.na(as.numeric(data[[time]]))) == 0), msg = "Specified `time` column can't be coerced to a numeric value") } if (is.null(time)) { time <- "_sdmTMB_time" From d1d8efc3e1a2e7e7a19c98c31352c06877ee56d5 Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Wed, 14 Feb 2024 10:30:55 -0800 Subject: [PATCH 2/3] Remove extra `)`; length -> sum? --- R/fit.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/fit.R b/R/fit.R index 3545eea7b..aa51c4d38 100644 --- a/R/fit.R +++ b/R/fit.R @@ -709,7 +709,8 @@ sdmTMB <- function( if (!is.null(time)) { assert_that(time %in% names(data), msg = "Specified `time` column is missing from `data`.") - assert_that(length(is.na(as.numeric(data[[time]]))) == 0), msg = "Specified `time` column can't be coerced to a numeric value") + assert_that(sum(is.na(as.numeric(data[[time]]))) == 0L, + msg = "Specified `time` column can't be coerced to a numeric value") } if (is.null(time)) { time <- "_sdmTMB_time" From 8af542742d7811af7684c2a62a2a81d5bbbb5f2c Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Wed, 14 Feb 2024 10:45:20 -0800 Subject: [PATCH 3/3] Enhance error msg; add test --- R/fit.R | 2 +- tests/testthat/test-2-fit-less-basic.R | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/R/fit.R b/R/fit.R index aa51c4d38..c59da9479 100644 --- a/R/fit.R +++ b/R/fit.R @@ -710,7 +710,7 @@ sdmTMB <- function( assert_that(time %in% names(data), msg = "Specified `time` column is missing from `data`.") assert_that(sum(is.na(as.numeric(data[[time]]))) == 0L, - msg = "Specified `time` column can't be coerced to a numeric value") + msg = "Specified `time` column can't be coerced to a numeric value or contains NAs. Please remove any NAs in the time column.") } if (is.null(time)) { time <- "_sdmTMB_time" diff --git a/tests/testthat/test-2-fit-less-basic.R b/tests/testthat/test-2-fit-less-basic.R index 00adf561e..40d0d2944 100644 --- a/tests/testthat/test-2-fit-less-basic.R +++ b/tests/testthat/test-2-fit-less-basic.R @@ -507,3 +507,15 @@ test_that("sdmTMB throws error on AR1/RW with non-numeric time", { mesh = pcod_mesh_2011 )) }) + +test_that("Time with an NA gets flagged", { + skip_on_cran() + d <- pcod_2011 + d$yr <- as.character(d$year) + d$yr[2] <- NA + expect_error({ + m <- sdmTMB(density ~ 1, time = "yr", spatial = "off", spatiotemporal = "off", data = d) + }, regexp = "time") +}) + +