Skip to content

Commit

Permalink
Improve ggeffects compatibility #203
Browse files Browse the repository at this point in the history
Now works with:
- + (1 | factor)
- + s(other_covariate)

Adding unit tests
  • Loading branch information
seananderson committed Apr 26, 2023
1 parent 9b6102c commit 7678752
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ Effect.sdmTMB <- function(focal.predictors, mod, ...) {
coefficients = coefs,
vcov = vc,
family = fam,
formula = formula(mod)
# formula = formula(mod) # includes random intercepts...
formula = remove_s_and_t2(mod$split_formula[[1]]$fixedFormula)
)
effects::Effect.default(focal.predictors, mod, ..., sources = args)
}
Expand Down
51 changes: 51 additions & 0 deletions tests/testthat/test-ggeffect.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
test_that("ggeffects + sdmTMB", {
skip_on_cran()
skip_on_ci()
skip_if_not_installed("INLA")
skip_if_not_installed("ggeffects")
skip_if_not_installed("ggplot2")

d <- pcod_2011
d$fyear <- as.factor(d$year)
fit <- sdmTMB(present ~ depth_scaled + I(depth_scaled^2) + fyear,
data = d,
mesh = pcod_mesh_2011,
family = binomial()
)
g <- ggeffects::ggeffect(fit, "depth_scaled [-2.5:2.5, by=.1]")
expect_true(inherits(g, "ggeffects"))
plot(g)

e <- effects::effect("depth_scaled", fit)
e
expect_true(inherits(e, "eff"))

fit2 <- sdmTMB(present ~ depth_scaled + I(depth_scaled^2) + (1 | fyear),
data = d,
mesh = pcod_mesh_2011,
family = binomial()
)
effects::effect("depth_scaled", fit2)
g <- ggeffects::ggeffect(fit2, "depth_scaled [-2.5:2.5, by=.1]")
plot(g)
expect_true(inherits(g, "ggeffects"))

fit3 <- sdmTMB(present ~ s(year, k = 3) + depth_scaled + I(depth_scaled^2),
data = d,
mesh = pcod_mesh_2011,
family = binomial()
)
effects::effect("depth_scaled", fit3)
g <- ggeffects::ggeffect(fit3, "depth_scaled [-2.5:2.5, by=.1]")
plot(g)
expect_true(inherits(g, "ggeffects"))

fit4 <- sdmTMB(present ~ s(depth_scaled, k = 4),
data = d,
mesh = pcod_mesh_2011,
family = binomial()
)
expect_error({
effects::effect("depth_scaled", fit4)
}, regexp = "missing")
})

0 comments on commit 7678752

Please sign in to comment.