Skip to content

Commit

Permalink
Allow coef to work for delta models #351
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Sep 23, 2024
1 parent 870ad8c commit e868be2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 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.6.0.9007
Version: 0.6.0.9008
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)

* Add `model` (linear predictor number) argument to coef() method. Also,
write documentation for `?coef.sdmTMB`. #351

* Add helpful error message if some coordinates in make_mesh() are NA. #365

* Add informative message if fitting with an offset but predicting with offset
Expand Down
14 changes: 11 additions & 3 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,20 @@ fitted.sdmTMB <- function(object, ...) {
#'
#' @param object The fitted sdmTMB model object
#' @param complete Currently ignored
#' @param model Linear predictor for delta models. Defaults to the first
#' linear predictor.
#' @param ... Currently ignored
#' @importFrom stats coef
#' @export
#' @noRd
coef.sdmTMB <- function(object, complete = FALSE, ...) {
x <- tidy(object)
coef.sdmTMB <- function(object, complete = FALSE, model = 1, ...) {
if (is_delta(object)) {
assert_that(length(model) == 1L)
model <- as.integer(model)
assert_that(model %in% c(1L, 2L))
msg <- paste0("Returning coefficients from linear predictor ", model, " based on the `model` argument.")
cli_inform(msg)
}
x <- tidy(object, model = model)
out <- x$estimate
names(out) <- x$term
out
Expand Down
21 changes: 21 additions & 0 deletions man/coef.sdmTMB.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ test_that("coef and vcov and confint work", {
expect_true(grepl("Estimate", colnames(x))[3])
})

test_that("coef works with delta models and informs as needed", {
skip_on_cran()
fit <- sdmTMB(
density ~ depth,
data = pcod_2011, spatial = "off",
family = delta_gamma()
)
expect_message(x <- coef(fit), regexp = "model")
expect_message(x <- coef(fit, model = 1), regexp = "model")
expect_message(x <- coef(fit, model = 2), regexp = "model")
})

test_that("various methods work", {
skip_on_cran()
fit <- sdmTMB(
Expand Down

0 comments on commit e868be2

Please sign in to comment.