Skip to content

Commit

Permalink
Add message on offset newdata prediction #372
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Sep 23, 2024
1 parent 4b785e0 commit 1be189e
Show file tree
Hide file tree
Showing 4 changed files with 29 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.9005
Version: 0.6.0.9006
Authors@R: c(
person(c("Sean", "C."), "Anderson", , "sean@seananderson.ca",
role = c("aut", "cre"),
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# sdmTMB (development version)

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

* Fix passing of `offset` argument through in `sdmTMB_cv()`. Before it was being
omitted in the prediction (i.e., set to 0). #372

Expand Down
9 changes: 9 additions & 0 deletions R/predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,15 @@ predict.sdmTMB <- function(object, newdata = NULL,
cli_abort("`area` should be of the same length as `nrow(newdata)` or of length 1.")
}

# newdata, null offset in predict, and non-null in fit #372
if (isFALSE(nd_arg_was_null) && is.null(offset) && !all(object$offset == 0)) {
msg <- c(
"Fitted object contains an offset but the offset is `NULL` in `predict.sdmTMB()`.",
"Prediction will proceed assuming the offset vector is 0 in the prediction.",
"Specify an offset vector in `predict.sdmTMB()` to override this.")
cli_inform(msg)
}

if (!is.null(offset)) {
if (nrow(proj_X_ij[[1]]) != length(offset))
cli_abort("Prediction offset vector does not equal number of rows in prediction dataset.")
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-offset.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ test_that("offset gets passed through cross validation as expected #372", {
expect_true(length(unique(y$cv_predicted)) == 684L)
})

test_that("predicting on newdata with a non-null offset in fit but a null offset in predict informs the user appropriately", {
dat <- subset(dogfish, catch_weight > 0)
fit <- sdmTMB(
catch_weight ~ 1,
data = dat,
family = Gamma("log"),
offset = "area_swept",
spatial = "off"
)
pred <- predict(fit)
pred <- predict(fit, offset = rep(0, nrow(dat)))
pred <- predict(fit, newdata = qcs_grid, offset = rep(0, nrow(qcs_grid)))
pred <- predict(fit, newdata = qcs_grid)
expect_message({pred <- predict(fit, newdata = qcs_grid)}, regexp = "offset")
})

# #
# # offset/prediction setting checks:
#
Expand Down

0 comments on commit 1be189e

Please sign in to comment.