Skip to content

Commit ae77911

Browse files
authored
Merge pull request #402 from pbs-assess/issue_347
Issue 347
2 parents f1be165 + a3563d8 commit ae77911

File tree

6 files changed

+73
-6
lines changed

6 files changed

+73
-6
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: sdmTMB
33
Title: Spatial and Spatiotemporal SPDE-Based GLMMs with 'TMB'
4-
Version: 0.6.0.9023
4+
Version: 0.6.0.9024
55
Authors@R: c(
66
person(c("Sean", "C."), "Anderson", , "sean@seananderson.ca",
77
role = c("aut", "cre"),

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464

6565
* Add `get_eao()` to calculate effective area occupied.
6666

67+
* Add option for `area` to be passed in as the name of a column in the
68+
dataframe to be used for area weighting. Used in `get_index()`,
69+
`get_cog()`, `get_eao()`, etc.
70+
6771
# sdmTMB 0.6.0
6872

6973
* Pass several arguments to `DHARMa::plotQQunif()`.

R/index.R

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#' @param bias_correct Should bias correction be implemented [TMB::sdreport()]?
66
#' @param level The confidence level.
77
#' @param area Grid cell area. A vector of length `newdata` from
8-
#' [predict.sdmTMB()] or a value of length 1, which will be repeated
9-
#' internally to match.
8+
#' [predict.sdmTMB()] *or* a value of length 1 which will be repeated
9+
#' internally to match *or* a character value representing the column
10+
#' used for area weighting.
1011
#' @param silent Silent?
1112
#' @param ... Passed to [TMB::sdreport()].
1213
#'
@@ -107,6 +108,11 @@
107108
#' }
108109
#' @export
109110
get_index <- function(obj, bias_correct = FALSE, level = 0.95, area = 1, silent = TRUE, ...) {
111+
# if offset is a character vector, use the value in the dataframe
112+
if (is.character(area)) {
113+
area <- obj$data[[area]]
114+
}
115+
110116
d <- get_generic(obj, value_name = "link_total",
111117
bias_correct = bias_correct, level = level, trans = exp, area = area, ...)
112118
names(d)[names(d) == "trans_est"] <- "log_est"
@@ -119,6 +125,12 @@ get_index <- function(obj, bias_correct = FALSE, level = 0.95, area = 1, silent
119125
#' @export
120126
get_cog <- function(obj, bias_correct = FALSE, level = 0.95, format = c("long", "wide"), area = 1, silent = TRUE, ...) {
121127
if (bias_correct) cli_abort("Bias correction with get_cog() is currently disabled.")
128+
129+
# if offset is a character vector, use the value in the dataframe
130+
if (is.character(area)) {
131+
area <- obj$data[[area]]
132+
}
133+
122134
d <- get_generic(obj, value_name = c("cog_x", "cog_y"),
123135
bias_correct = bias_correct, level = level, trans = I, area = area, ...)
124136
d <- d[, names(d) != "trans_est", drop = FALSE]
@@ -145,6 +157,12 @@ get_eao <- function(obj,
145157
...
146158
) {
147159
if (bias_correct) cli_abort("Bias correction with get_eao() is currently disabled.")
160+
161+
# if offset is a character vector, use the value in the dataframe
162+
if (is.character(area)) {
163+
area <- obj$data[[area]]
164+
}
165+
148166
d <- get_generic(obj, value_name = c("log_eao"),
149167
bias_correct = bias_correct, level = level, trans = exp, area = area, ...)
150168
names(d)[names(d) == "trans_est"] <- "log_est"
@@ -155,6 +173,11 @@ get_eao <- function(obj,
155173
get_generic <- function(obj, value_name, bias_correct = FALSE, level = 0.95,
156174
trans = I, area = 1, silent = TRUE, ...) {
157175

176+
# if offset is a character vector, use the value in the dataframe
177+
if (is.character(area)) {
178+
area <- obj$data[[area]]
179+
}
180+
158181
reinitialize(obj$fit_obj)
159182

160183
if ((!isTRUE(obj$do_index) && value_name[1] == "link_total") || value_name[1] == "cog_x" || value_name[[1]] == "log_eao") {

man/get_index.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sdmTMBcontrol.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-index.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,43 @@ test_that("Index integration with area vector works with extra time and possibly
114114
expect_equal(ind$se - ind0$se[ind0$year %in% seq(2011, 2017, 2)], c(0, 0, 0, 0))
115115
})
116116

117+
test_that("get_index works", {
118+
skip_on_cran()
119+
120+
pcod_spde <- make_mesh(pcod, c("X", "Y"), n_knots = 50, type = "kmeans")
121+
m <- sdmTMB(
122+
data = pcod,
123+
formula = density ~ 0 + as.factor(year),
124+
spatiotemporal = "off", # speed
125+
time = "year", mesh = pcod_spde,
126+
family = tweedie(link = "log")
127+
)
128+
129+
# add some jittered area data to qcs_grid for testing
130+
qcs_grid$area <- runif(nrow(qcs_grid), 0.9, 1.1)
131+
nd <- replicate_df(qcs_grid, "year", unique(pcod$year))
132+
133+
predictions <- predict(m, newdata = nd, return_tmb_object = TRUE)
134+
135+
# get predictions with area passed as vector
136+
ind <- get_index(predictions, area = nd$area)
137+
# get predictions with area as a named column
138+
ind2 <- get_index(predictions, area = "area")
139+
expect_equal(ind, ind2)
140+
141+
# get predictions with area passed as vector
142+
eao <- get_eao(predictions, area = nd$area)
143+
# get predictions with area as a named column
144+
eao2 <- get_eao(predictions, area = "area")
145+
expect_equal(eao, eao2)
146+
147+
# get predictions with area passed as vector
148+
cog <- get_cog(predictions, area = nd$area)
149+
# get predictions with area as a named column
150+
cog2 <- get_cog(predictions, area = "area")
151+
expect_equal(cog, cog2)
152+
})
153+
117154
# test_that("get_index faster epsilon bias correction", {
118155
# skip_on_cran()
119156
#

0 commit comments

Comments
 (0)