Skip to content

Commit

Permalink
Add some sdmTMBextra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Feb 8, 2024
1 parent 2db9fa6 commit af853ef
Showing 1 changed file with 60 additions and 40 deletions.
100 changes: 60 additions & 40 deletions tests/testthat/test-5-residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -294,43 +294,63 @@ test_that("MCMC residuals throw error as needed", {
}, regexp = "mcmc")
})

# test_that("MCMC residuals work with sdmTMBextra", {
# skip_on_cran()
# skip_if_not_installed("INLA")
# skip_if_not_installed("sdmTMBextra")
# skip_if_not_installed("rstan")
# d <- pcod_2011
# set.seed(1)
# d$offset <- rnorm(nrow(d))
# fit_dg <- sdmTMB(
# density ~ 1,
# data = d,
# mesh = pcod_mesh_2011,
# offset = d$offset,
# family = delta_gamma()
# )
# set.seed(1)
# p0 <- sdmTMBextra::predict_mle_mcmc(fit_dg, mcmc_iter = 11, mcmc_warmup = 10)
# set.seed(1)
# p1 <- sdmTMBextra::predict_mle_mcmc(fit_dg,
# mcmc_iter = 11, mcmc_warmup = 10,
# model = 1
# )
# set.seed(1)
# p2 <- sdmTMBextra::predict_mle_mcmc(fit_dg,
# mcmc_iter = 11, mcmc_warmup = 10,
# model = 2
# )
# expect_equal(p0, p1)
# set.seed(1)
# r1 <- residuals(fit_dg, type = "mle-mcmc", mcmc_samples = p1)
# set.seed(1)
# r2 <- residuals(fit_dg, type = "mle-mcmc", mcmc_samples = p2)
# qqnorm(r1)
# qqnorm(r2)
# expect_false(identical(r1, r2))
# expect_true(max(r1) < 5)
# expect_true(min(r1) > -5)
# expect_true(max(r2) < 5)
# expect_true(min(r2) > -5)
# })
test_that("MCMC residuals work with sdmTMBextra", {
skip_on_cran()
skip_if_not_installed("sdmTMBextra")
skip_if_not_installed("rstan")
skip_on_ci()
d <- pcod_2011
set.seed(1)
d$offset <- rnorm(nrow(d))
fit_dg <- sdmTMB(
density ~ 1,
data = d,
mesh = pcod_mesh_2011,
offset = d$offset,
family = delta_gamma()
)
set.seed(1)
p0 <- sdmTMBextra::predict_mle_mcmc(fit_dg, mcmc_iter = 11, mcmc_warmup = 10)
set.seed(1)
p1 <- sdmTMBextra::predict_mle_mcmc(fit_dg,
mcmc_iter = 11, mcmc_warmup = 10,
model = 1
)
set.seed(1)
p2 <- sdmTMBextra::predict_mle_mcmc(fit_dg,
mcmc_iter = 11, mcmc_warmup = 10,
model = 2
)
expect_equal(p0, p1)
set.seed(1)
r1 <- residuals(fit_dg, type = "mle-mcmc", mcmc_samples = p1)
set.seed(1)
r2 <- residuals(fit_dg, type = "mle-mcmc", mcmc_samples = p2)
qqnorm(r1)
qqnorm(r2)
expect_false(identical(r1, r2))
expect_true(max(r1) < 5)
expect_true(min(r1) > -5)
expect_true(max(r2) < 5)
expect_true(min(r2) > -5)
})

test_that("predict_mle_mcmc() works with extra_time #297", {
skip_on_cran()
skip_if_not_installed("sdmTMBextra")
skip_if_not_installed("rstan")
skip_on_ci()
fit <- sdmTMB(
density ~ 0 + as.factor(year),
time = "year",
spatiotemporal = "off",
spatial = "on",
mesh = pcod_mesh_2011,
data = pcod_2011,
family = tweedie(link = "log"),
extra_time = c(2012, 2014, 2016)
)
set.seed(123)
samps <- sdmTMBextra::predict_mle_mcmc(fit, mcmc_iter = 11, mcmc_warmup = 10)
expect_true(nrow(samps) == 972L)
})

0 comments on commit af853ef

Please sign in to comment.