Skip to content

Commit

Permalink
Fix fitted with offset/extra_time
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Mar 26, 2024
1 parent b7b11ab commit 1a7cf8a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-extra-time.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit 1a7cf8a

Please sign in to comment.