Skip to content

Commit

Permalink
Check for character in SVC formula #269
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Nov 1, 2023
1 parent d20712d commit 6e9d387
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,14 @@ sdmTMB <- function(

spatial_varying_formula <- spatial_varying # save it
if (!is.null(spatial_varying)) {
mf1 <- model.frame(spatial_varying, data)
for (i in seq_len(ncol(mf1))) {
if (is.character(mf1[[i]])) {
cli_warn(paste0("Detected '{colnames(mf1)[i]}' as a character term in the ",
"'spatial_varying' formula. We suggest you make this a factor if you plan ",
"to predict with only some factor levels. `as.factor({colnames(mf1)[i]})`."))
}
}
z_i <- model.matrix(spatial_varying, data)
.int <- sum(grep("(Intercept)", colnames(z_i)) > 0)
if (length(attr(z_i, "contrasts")) && !.int && !omit_spatial_intercept) { # factors with ~ 0 or ~ -1
Expand Down
35 changes: 35 additions & 0 deletions tests/testthat/test-3-spatial-varying.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,38 @@ test_that("Delta model with spatially varying factor predictor and no spatiotemp
expect_s3_class(m, "sdmTMB")
expect_true(sum(is.na(m$sd_report$sd)) == 0L)
})

test_that("Factor handling for SVC models works #269", {
skip_on_cran()
skip_on_ci()
set.seed(1)
pcod_2011$vessel <- sample(c("A", "B"), size = nrow(pcod_2011), replace = TRUE)
pcod_2011$vessel <- as.factor(pcod_2011$vessel)
fit <- sdmTMB(present ~ vessel,
spatial_varying = ~ vessel,
spatial = "on",
mesh = pcod_mesh_2011,
data = pcod_2011
)
p1 <- predict(fit, pcod_2011)
p2 <- predict(fit, newdata = pcod_2011)
expect_equal(p1$est, p2$est)

p3 <- predict(fit, newdata = pcod_2011[pcod_2011$vessel == "A", ])
p4 <- p2[p2$vessel == "A", ]
expect_equal(p3$est, p4$est)
})

test_that("SVC throws a warning if character class #269", {
skip_on_cran()
skip_on_ci()
pcod_2011$vessel <- sample(c("A", "B"), size = nrow(pcod_2011), replace = TRUE)
expect_warning({
fit <- sdmTMB(present ~ vessel,
spatial_varying = ~ vessel,
spatial = "on",
mesh = pcod_mesh_2011,
data = pcod_2011
)
}, regexp = "character")
})

0 comments on commit 6e9d387

Please sign in to comment.