Skip to content

Commit

Permalink
Improvements to do_index = TRUE/get_index errors
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Nov 7, 2023
1 parent f4743e2 commit ccdce68
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
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.4.1.9001
Version: 0.4.1.9002
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)

* Improve warnings/errors around use of `do_index = TRUE` and `get_index()`
if `newdata = NULL`. #276

* Fix prediction with `offset` when `newdata` is `NULL` but `offset` is
specified. #274

Expand Down
12 changes: 9 additions & 3 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ NULL
#' the TMB object doesn't have to be rebuilt with [predict.sdmTMB()] and
#' [get_index()]. If `TRUE`, then `predict_args` must have a `newdata` element
#' supplied and `area` can be supplied to `index_args`.
#' @param predict_args A list of arguments to pass to [predict.sdmTMB()] if
#' `do_index = TRUE`.
#' @param index_args A list of arguments to pass to [get_index()] if
#' Most users can ignore this option.
#' @param predict_args A list of arguments to pass to [predict.sdmTMB()] **if**
#' `do_index = TRUE`. Most users can ignore this option.
#' @param index_args A list of arguments to pass to [get_index()] **if**
#' `do_index = TRUE`. Currently, only `area` is supported. Bias correction
#' can be done when calling [get_index()] on the resulting fitted object.
#' Most users can ignore this option.
#' @param bayesian Logical indicating if the model will be passed to
#' \pkg{tmbstan}. If `TRUE`, Jacobian adjustments are applied to account for
#' parameter transformations when priors are applied.
Expand Down Expand Up @@ -1384,6 +1386,10 @@ sdmTMB <- function(
version = utils::packageVersion("sdmTMB")),
class = "sdmTMB")

if ((!is.null(predict_args) || !is.null(index_args)) && isFALSE(do_index)) {
cli_abort(c("`predict_args` and/or `index_args` were set but `do_index` was FALSE.",
"`do_index` must be TRUE for these arguments to take effect.")) #276
}
if (do_index) {
args <- list(object = out_structure, return_tmb_data = TRUE)
args <- c(args, predict_args)
Expand Down
5 changes: 5 additions & 0 deletions R/index.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ get_generic <- function(obj, value_name, bias_correct = FALSE, level = 0.95,
cli_abort(paste0("`obj` needs to be created with ",
"`predict(..., return_tmb_object = TRUE).`"))
}

if (!"report" %in% names(obj$obj)) {
cli_abort(c("It looks like the predict function was run without `newdata` specified.",
"Re-run the predict function with `newdata` specified.")) #276
}
test <- suppressWarnings(tryCatch(obj$obj$report(obj$obj$env$last.par),
error = function(e) NA))
if (all(is.na(test)))
Expand Down
12 changes: 7 additions & 5 deletions man/sdmTMB.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions tests/testthat/test-4-index-calculation.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,36 @@ test_that("get_index(), get_index_sims(), and get_cog() work", {
expect_equal(cog$est[which(cog$coord=="X")], cog_wide$est_x)
})

test_that("index errors are returned as needed", {
skip_on_ci()
skip_on_cran()

g <- replicate_df(qcs_grid, "year", unique(pcod_2011$year))

expect_error(
fit <- sdmTMB(
density ~ 1,
data = pcod_2011,
spatial = "off", spatiotemporal = "off",
family = tweedie(link = "log"),
time = "year",
predict_args = list(newdata = g),
index_args = list(area = 1)
), regexp = "do_index" # missing!
)

fit <- sdmTMB(
density ~ 1,
data = pcod_2011, spatial = "off", spatiotemporal = "off",
family = tweedie(link = "log"),
time = "year"
)
p1 <- predict(fit, newdata = NULL, return_tmb_object = TRUE)
expect_error(get_index(p1), "newdata") # missing!

p2 <- predict(fit, newdata = g, return_tmb_object = TRUE)
suppressMessages(
i <- get_index(p2)
)
expect_s3_class(i, "data.frame")
})

0 comments on commit ccdce68

Please sign in to comment.