diff --git a/R/fit.R b/R/fit.R index 326e642ae..c59da9479 100644 --- a/R/fit.R +++ b/R/fit.R @@ -709,6 +709,8 @@ sdmTMB <- function( if (!is.null(time)) { 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 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") +}) + +