Skip to content

Commit

Permalink
Fix #370
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Sep 13, 2024
1 parent 1418f42 commit cb83a62
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
37 changes: 19 additions & 18 deletions R/tmb-sim.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,24 +268,25 @@ sdmTMB_simulate <- function(formula,
if (!is.null(fit$time)) d[[fit$time]] <- data[[fit$time]]
d[[mesh$xy_cols[1]]] <- data[[mesh$xy_cols[1]]]
d[[mesh$xy_cols[2]]] <- data[[mesh$xy_cols[2]]]
d[["omega_s"]] <- if (all(s$omega_s_A != 0)) s$omega_s_A
d[["epsilon_st"]] <- if (all(s$epsilon_st_A_vec != 0)) s$epsilon_st_A_vec
d[["zeta_s"]] <- if (all(s$zeta_s_A != 0)) s$zeta_s_A

# Warnings for pieces collapsing to 0. The sum() > 0 piece is needed because parameters that are input are turned to 0x0 matrices
if (sum(sigma_O) > 0) {
if(is.null(d[["omega_s"]])) cli::cli_alert_info(paste("Warning: spatial field is collapsing to 0 but sigma_O was specified.",
"Please check the spatial range and cutoff distance used to construct the mesh."))
}
if (sum(sigma_E) > 0) {
if(is.null(d[["epsilon_st"]])) cli::cli_alert_info(paste("Warning: spatiotemporal field is collapsing to 0 but sigma_E was specified.",
"Please check the spatial range and cutoff distance used to construct the mesh."))
}
if (sum(sigma_Z) > 0) {
if(is.null(d[["zeta_s"]])) cli::cli_alert_info(paste("Warning: spatially varying coefficient field is collapsing to 0 but sigma_Z was specified.",
"Please check the spatial range and cutoff distance used to construct the mesh."))
}


d[["omega_s"]] <- if (sum(sigma_O) > 0) s$omega_s_A
d[["epsilon_st"]] <- if (sum(sigma_E) > 0) s$epsilon_st_A_vec
d[["zeta_s"]] <- if (sum(sigma_Z) > 0) s$zeta_s_A

# # Warnings for fields collapsing to 0
# info_collapse <- function(sig, vec, .par, .name) {
# if (sum(sig) > 0 && all (vec == 0)) {
# msg <- paste0("The ", .name, " has been returned as all zeros although ", .par,
# " was specified as > 0. Try making your mesh finer, e.g., with a lower ",
# "`cutoff` or a higher number of knots. Triangle edge length needs to be ",
# "lower than the range size (distance correlation is effectively independent.")
# cli::cli_alert_info(msg)
# }
# }
# info_collapse(sigma_O, s$omega_s_A, "sigma_O", "spatial field")
# info_collapse(sigma_E, s$epsilon_st_A_vec, "sigma_E", "spatiotemporal field")
# info_collapse(sigma_Z, s$zeta_s_A, "sigma_Z", "spatially varying coefficient field")

if (any(family$family %in% c("truncated_nbinom1", "truncated_nbinom2"))) {
d[["mu"]] <- family$linkinv(s$eta_i, phi = phi)
} else {
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-tmb-simulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,28 @@ test_that("simulate.sdmTMB returns the right length", {
s <- simulate(m, nsim = 2)
expect_equal(nrow(s), nrow(pcod))
})

test_that("coarse meshes with zeros in simulation still return fields #370", {
set.seed(123)
predictor_dat <- data.frame(
X = runif(100), Y = runif(100),
a1 = rnorm(100), year = rep(1:2, each = 50))
mesh <- sdmTMB::make_mesh(predictor_dat, xy_cols = c("X", "Y"), n_knots = 30)
sim_dat <- sdmTMB::sdmTMB_simulate(
formula = ~ 1 + a1,
data = predictor_dat,
time = "year",
mesh = mesh,
family = gaussian(),
range = 0.5,
sigma_E = 0.1,
phi = 0.1,
sigma_O = 0.2,
seed = 42,
B = c(0.2, -0.4) # B0 = intercept, B1 = a1 slope
)
nm <- names(sim_dat)
expect_true("omega_s" %in% nm)
expect_true("epsilon_st" %in% nm)
expect_false("zeta_s" %in% nm)
})

0 comments on commit cb83a62

Please sign in to comment.