From 1a7cf8a033453bca4284eca8e141117d2549dfef Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Tue, 26 Mar 2024 14:45:01 -0700 Subject: [PATCH] Fix fitted with offset/extra_time --- R/methods.R | 7 +++++-- tests/testthat/test-extra-time.R | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/R/methods.R b/R/methods.R index 5486c8338..8db613299 100644 --- a/R/methods.R +++ b/R/methods.R @@ -25,16 +25,19 @@ nobs.sdmTMB <- function(object, ...) { #' @export #' @noRd fitted.sdmTMB <- function(object, ...) { + + if (!"offset" %in% names(object)) + cli_abort("It looks like this was fit with an older version of sdmTMB. Try sdmTMB:::update_version(fit).") if (isTRUE(object$family$delta)) { inv1 <- object$family[[1]]$linkinv - p <- predict(object, type = "link", offset = object$tmb_data$offset_i) + p <- predict(object, type = "link", offset = object$offset) p1 <- inv1(p$est1) inv2 <- object$family[[2]]$linkinv p2 <- inv2(p$est2) p1 * p2 } else { inv <- object$family$linkinv - inv(predict(object, type = "link", offset = object$tmb_data$offset_i)$est) + inv(predict(object, type = "link", offset = object$offset)$est) } } diff --git a/tests/testthat/test-extra-time.R b/tests/testthat/test-extra-time.R index e7075a29a..879f1bc12 100644 --- a/tests/testthat/test-extra-time.R +++ b/tests/testthat/test-extra-time.R @@ -35,6 +35,10 @@ test_that("extra time, newdata, and offsets work", { expect_equal(ncol(p6), 2L) expect_equal(nrow(p6), nrow(pcod)) expect_equal(p6[, 1, drop = TRUE], p5[, 1, drop = TRUE]) + + f <- fitted(m) + expect_equal(length(f), 2143L) + expect_equal(round(unique(f), 2), c(31.13, 61.93, 64.98, 18.73, 22.76, 42.97, 40.66, 51.65, 26.05)) }) test_that("extra_time, newdata, get_index() work", {