Issue with plotting using visreg_delta each model #354
-
Hi! I’m currently getting an error when I try to plot for each model using visreg_delta. I’m using sdmTMB_0.6.0. Here’s my code: Using the following commands, I can get the plots for all three covariates for model 1: I get this error: “Error in eval(predvars, data, env) : object “cov1” not found” even if I already removed cov1 in fitting model 2. May I kindly ask for your suggestions on how to resolve this issue? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Thanks, I can confirm that issue. It's related to the predict code always iterating over building the model matrices for both linear predictors but visreg prepares a data frame here based only on the 2nd linear predictor. I.e., it's going to happen if the 2 linear predictors have different formulas. Here's an example of making a conditional effects plot yourself: library(sdmTMB)
library(ggplot2)
library(dplyr) df <- pcod
df$fyear <- as.factor(df$year)
df$cov1 <- df$depth
df$cov2 <- df$year
fit <- sdmTMB(
list(density ~ poly(cov1, 2) + poly(cov2, 2),
density ~ poly(cov2, 2)),
data = df,
time = "year",
family = delta_gamma(),
spatiotemporal = "off",
spatial = "off"
)
nd <- expand.grid(cov1 = mean(df$cov1), year = df$year[1],
cov2 = seq(min(df$cov2), max(df$cov2), length.out = 100))
head(nd)
#> cov1 year cov2
#> 1 190.4401 2003 2003.000
#> 2 190.4401 2003 2003.141
#> 3 190.4401 2003 2003.283
#> 4 190.4401 2003 2003.424
#> 5 190.4401 2003 2003.566
#> 6 190.4401 2003 2003.707 p <- predict(fit, newdata = nd, se_fit = TRUE, re_form = NA, model = 2)
p <- mutate(p, upr = est + 1.96 * est_se, lwr = est - 1.96 * est_se)
ggplot(p, aes(cov2, exp(est), ymin = exp(lwr), ymax = exp(upr))) +
geom_ribbon(fill = "grey60") + geom_line() Created on 2024-06-20 with reprex v2.1.0 In terms of visreg, it's a major pain to accommodate within sdmTMB and I think I'd rather put the time into getting delta models working properly with ggpredict and ggeffect in the ggeffects package and deprecate visreg support. ggeffects: There aren't examples of ggpredict in their yet, but you can see examples here: |
Beta Was this translation helpful? Give feedback.
Thanks, I can confirm that issue. It's related to the predict code always iterating over building the model matrices for both linear predictors but visreg prepares a data frame here based only on the 2nd linear predictor. I.e., it's going to happen if the 2 linear predictors have different formulas.
Here's an example of making a conditional effects plot yourself: