From e868be28ac98a1592ae33de0443a1ef527aeeaba Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Mon, 23 Sep 2024 13:52:28 -0700 Subject: [PATCH] Allow coef to work for delta models #351 --- DESCRIPTION | 2 +- NEWS.md | 3 +++ R/methods.R | 14 +++++++++++--- man/coef.sdmTMB.Rd | 21 +++++++++++++++++++++ tests/testthat/test-methods.R | 12 ++++++++++++ 5 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 man/coef.sdmTMB.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 667d19514..302691204 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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"), diff --git a/NEWS.md b/NEWS.md index 27124a556..3256deda0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/methods.R b/R/methods.R index 58ec21638..314577abe 100644 --- a/R/methods.R +++ b/R/methods.R @@ -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 diff --git a/man/coef.sdmTMB.Rd b/man/coef.sdmTMB.Rd new file mode 100644 index 000000000..d0060e660 --- /dev/null +++ b/man/coef.sdmTMB.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/methods.R +\name{coef.sdmTMB} +\alias{coef.sdmTMB} +\title{Get fixed-effect coefficients} +\usage{ +\method{coef}{sdmTMB}(object, complete = FALSE, model = 1, ...) +} +\arguments{ +\item{object}{The fitted sdmTMB model object} + +\item{complete}{Currently ignored} + +\item{model}{Linear predictor for delta models. Defaults to the first +linear predictor.} + +\item{...}{Currently ignored} +} +\description{ +Get fixed-effect coefficients +} diff --git a/tests/testthat/test-methods.R b/tests/testthat/test-methods.R index 6b31ba3bf..99a86ffe9 100644 --- a/tests/testthat/test-methods.R +++ b/tests/testthat/test-methods.R @@ -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(