Skip to content

Commit

Permalink
Fix extra_time/newdata/offset issue #270
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Nov 1, 2023
1 parent c6e03dd commit d20712d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions R/predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ predict.sdmTMB <- function(object, newdata = NULL,
newdata[["_sdmTMB_fake_nd_"]] <- FALSE
fake_nd[["_sdmTMB_fake_nd_"]] <- TRUE
newdata <- rbind(newdata, fake_nd)
if (!is.null(offset)) offset <- c(offset, rep(0, nrow(fake_nd))) # issue 270
}

# If making population predictions (with standard errors), we don't need
Expand Down Expand Up @@ -828,6 +829,7 @@ predict.sdmTMB <- function(object, newdata = NULL,
# nd[[paste0("zeta_s_", object$spatial_varying[z])]] <- r$zeta_s_A[,z,1]
# }
nd$epsilon_st <- r$epsilon_st_A_vec[,1]# DELTA FIXME
nd <- nd[!nd[[object$time]] %in% object$extra_time, , drop = FALSE] # issue 270
obj <- object
}

Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-extra-time.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
test_that("extra time, newdata, and offsets work", {
# https://github.com/pbs-assess/sdmTMB/issues/270
skip_on_cran()
skip_on_ci()
pcod$os <- rep(log(0.01), nrow(pcod)) # offset
m <- sdmTMB(
data = pcod,
formula = density ~ 0,
time_varying = ~ 1,
offset = pcod$os,
family = tweedie(link = "log"),
spatial = "off",
time = "year",
extra_time = c(2006, 2008, 2010, 2012, 2014, 2016),
spatiotemporal = "off"
)
p1 <- predict(m, offset = pcod$os)
p2 <- predict(m, newdata = pcod, offset = pcod$os)
p3 <- predict(m, newdata = pcod)
p4 <- predict(m, newdata = pcod, offset = rep(0, nrow(pcod)))
expect_equal(nrow(p1), nrow(pcod))
expect_equal(nrow(p2), nrow(pcod))
expect_equal(nrow(p3), nrow(pcod))
expect_equal(nrow(p4), nrow(pcod))
expect_equal(p1$est, p2$est)
expect_equal(p3$est, p4$est)
})

0 comments on commit d20712d

Please sign in to comment.