Skip to content

Commit 3d19fa4

Browse files
committed
Add ggeffects::ggpredict() in vignette #435
1 parent 924ebea commit 3d19fa4

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

vignettes/articles/ggeffects.Rmd

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ Another important distinction is that `ggeffects::ggeffect()` is plotting **marg
4040
This means the effects are "marginalized" or "averaged" over the other fixed effects.
4141
`visreg::visreg()` is plotting **conditional** effects.
4242
This means they are conditional on the other predictors being set to certain values.
43-
`ggeffects::ggpredict()` also does conditional effects, but this has not yet been set up in sdmTMB using the CRAN version of ggeffects.
43+
44+
`ggeffects::ggpredict()` also does conditional effects, and this is explored at the end of this vignette.
4445

4546
```{r}
4647
library(sdmTMB)
@@ -149,3 +150,51 @@ To specify the levels rather than letting `ggeffect()` choose them, use brackets
149150
g6 <- ggeffect(fit3, terms = c("depth_scaled [-3:2.7 by=0.01]", "numeric_year [1,7,15]"))
150151
plot(g6)
151152
```
153+
154+
# `ggpredict()`
155+
156+
`ggeffects::ggpredict()` can be used to make conditional effects plots of sdmTMB models, including models with smoothers (but not currently including delta models [due to a recent change in ggeffects](https://github.com/pbs-assess/sdmTMB/issues/435)).
157+
158+
Here's an example:
159+
160+
```{r}
161+
pcod$fyear <- as.factor(pcod$year)
162+
mesh <- make_mesh(pcod, c("X", "Y"), cutoff = 20)
163+
fit <- sdmTMB(present ~ s(depth) + fyear,
164+
data = pcod,
165+
mesh = mesh,
166+
spatial = "on",
167+
family = binomial()
168+
)
169+
```
170+
171+
```{r}
172+
g <- ggpredict(fit, "depth [0:400 by=2]")
173+
plot(g)
174+
```
175+
176+
```{r}
177+
g <- ggpredict(fit, terms = c("depth [0:350 by=5]", "fyear"))
178+
plot(g)
179+
plot(g, facet = TRUE)
180+
```
181+
182+
We can extract the data to make our own plot:
183+
184+
```{r}
185+
df <- as.data.frame(g)
186+
glimpse(df)
187+
```
188+
189+
```{r}
190+
ggplot(df, aes(
191+
x, predicted,
192+
ymin = conf.low,
193+
ymax = conf.high,
194+
colour = group, fill = group
195+
)) +
196+
geom_ribbon(alpha = 0.4, colour = NA) +
197+
geom_line() +
198+
facet_wrap(vars(group)) +
199+
labs(x = "Depth (m)", y = "Predicted", colour = "Year", fill = "Year")
200+
```

0 commit comments

Comments
 (0)