Skip to content

Commit

Permalink
Add error msg if coord NAs in make_mesh #365
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Sep 23, 2024
1 parent 1be189e commit 870ad8c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: sdmTMB
Title: Spatial and Spatiotemporal SPDE-Based GLMMs with 'TMB'
Version: 0.6.0.9006
Version: 0.6.0.9007
Authors@R: c(
person(c("Sean", "C."), "Anderson", , "sean@seananderson.ca",
role = c("aut", "cre"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# sdmTMB (development version)

* Add helpful error message if some coordinates in make_mesh() are NA. #365

* Add informative message if fitting with an offset but predicting with offset
argument left at NULL on newdata. #372

Expand Down
8 changes: 8 additions & 0 deletions R/mesh.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ make_mesh <- function(data, xy_cols,
cli_abort(msg)
}

all_x_non_na <- sum(is.na(data[[xy_cols[[1]]]])) == 0L
all_y_non_na <- sum(is.na(data[[xy_cols[[2]]]])) == 0L
if (!all_x_non_na || !all_y_non_na) {
msg <- c("Some coordinates in `xy_cols` were NA.", "
Remove or fix these rows before proceeding.")
cli_abort(msg)
}

if (max(data[[xy_cols[1]]]) > 1e4 || max(data[[xy_cols[2]]] > 1e4)) {
msg <- paste0(
"The x or y column values are fairly large. ",
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-mesh.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test_that("make_mesh returns informative error if some coordinates are NA", {
d <- data.frame(x = c(NA, runif(10)), y = c(NA, runif(10)))
expect_error({make_mesh(d, xy_cols = c("x", "y"), cutoff = 1)}, regexp = "NA")

d <- data.frame(x = c(1, runif(10)), y = c(NA, runif(10)))
expect_error({make_mesh(d, xy_cols = c("x", "y"), cutoff = 1)}, regexp = "NA")

d <- data.frame(x = c(NA, runif(10)), y = c(1, runif(10)))
expect_error({make_mesh(d, xy_cols = c("x", "y"), cutoff = 1)}, regexp = "NA")

d <- data.frame(x = c(1, runif(10)), y = c(1, runif(10)))
mesh <- make_mesh(d, xy_cols = c("x", "y"), cutoff = 1)
})

0 comments on commit 870ad8c

Please sign in to comment.