diff --git a/R/predict.R b/R/predict.R index f81352ad4..03c64d46e 100644 --- a/R/predict.R +++ b/R/predict.R @@ -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 @@ -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 } diff --git a/tests/testthat/test-extra-time.R b/tests/testthat/test-extra-time.R new file mode 100644 index 000000000..0c5f27062 --- /dev/null +++ b/tests/testthat/test-extra-time.R @@ -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) +})