Skip to content

Commit 69b18af

Browse files
authored
Merge pull request #361 from pbs-assess/ericward-noaa-patch-5
Multispecies / multiresponse vignette
2 parents 4360c63 + ae837b6 commit 69b18af

File tree

1 file changed

+245
-0
lines changed

1 file changed

+245
-0
lines changed

vignettes/web_only/multispecies.Rmd

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
---
2+
title: "Fitting multispecies models with sdmTMB"
3+
date: "`r Sys.Date()`"
4+
output: rmarkdown::html_vignette
5+
vignette: >
6+
%\VignetteIndexEntry{Fitting multispecies models with sdmTMB}
7+
%\VignetteEngine{knitr::rmarkdown}
8+
%\VignetteEncoding{UTF-8}
9+
---
10+
11+
**If the code in this vignette has not been evaluated, a rendered version is available on the [documentation site](https://pbs-assess.github.io/sdmTMB/index.html) under 'Articles'.**
12+
13+
```{r setup, include = FALSE, cache = FALSE}
14+
EVAL <- identical(Sys.getenv("NOT_CRAN"), "true")
15+
knitr::opts_chunk$set(
16+
collapse = TRUE,
17+
warning = FALSE,
18+
message = FALSE,
19+
comment = "#>",
20+
fig.width = 7,
21+
fig.asp = 0.618,
22+
eval = EVAL,
23+
purl = EVAL
24+
)
25+
```
26+
27+
```{r packages, message=FALSE}
28+
library(sdmTMB)
29+
```
30+
31+
For some applications, we might be interested in fitting a model that includes multiple responses such as 2+ species, or multiple size or age classes within a species. The most important step in fitting these models is understanding which parameters are shared, and which parameters are species-specific.
32+
33+
Below, we illustrate a series of models. We'll start by simulating a 2-species dataset. Each species is allowed to have unique spatial standard deviations (`sigma_O`) as well as different year effects.
34+
35+
```{r sim_dat}
36+
set.seed(1)
37+
predictor_dat <- data.frame(
38+
X = runif(1000), Y = runif(1000),
39+
year = rep(1:5, each = 200)
40+
)
41+
predictor_dat$fyear <- as.factor(predictor_dat$year)
42+
mesh <- make_mesh(predictor_dat, xy_cols = c("X", "Y"), cutoff = 0.1)
43+
sim_dat_A <- sdmTMB_simulate(
44+
formula = ~ 0 + fyear,
45+
data = predictor_dat,
46+
time = "year",
47+
mesh = mesh,
48+
range = 0.2,
49+
family = gaussian(),
50+
seed = 42,
51+
sigma_O = 0.2,
52+
phi = 0.1,
53+
sigma_E = 0.3,
54+
B = runif(5, min = 5, max = 8) # 5 random year effects
55+
)
56+
sim_dat_A$species <- "A"
57+
sim_dat_B <- sdmTMB_simulate(
58+
formula = ~ 0 + fyear,
59+
data = predictor_dat,
60+
time = "year",
61+
mesh = mesh,
62+
range = 0.2,
63+
family = gaussian(),
64+
seed = 43,
65+
sigma_O = 0.3,
66+
phi = 0.1,
67+
sigma_E = 0.3,
68+
B = runif(5, min = 5, max = 8) # 5 random year effects
69+
)
70+
sim_dat_B$species <- "B"
71+
sim_dat <- rbind(sim_dat_A, sim_dat_B)
72+
sim_dat$fyear <- factor(sim_dat$year)
73+
```
74+
75+
We'll start by making an SPDE mesh across the full dataset.
76+
77+
```{r mesh_fig, fig.asp=1}
78+
mesh <- make_mesh(sim_dat, c("X", "Y"), cutoff = 0.1)
79+
plot(mesh)
80+
```
81+
82+
### Model 1: species-specific intercepts
83+
84+
As a first model, we can include species-specific year effects. This can be done in a couple ways. One option would be to estimate the `species * year` interaction, letting the year effects for each species be independent. Here, all other parameters and random effect values (range, spatial field, spatial variance, spatiotemporal fields, spatiotemporal variances) are shared.
85+
86+
```{r}
87+
fit <- sdmTMB(
88+
observed ~ fyear * species,
89+
data = sim_dat,
90+
time = "year",
91+
spatiotemporal = "iid",
92+
mesh = mesh,
93+
family = gaussian()
94+
)
95+
fit
96+
```
97+
98+
### Model 2: species-specific spatial fields
99+
100+
We may be interested in fitting a model that lets the spatial patterning differ by species. These kinds of models can be expressed using spatially varying coefficients. Note that we use `spatial = off` because this represents a global spatial intercept---turning this off is akin to using a `-1` of `0` in a main formula including a factor. Both species take their spatial fields from the `spatial_varying` field here.
101+
102+
```{r}
103+
fit <- sdmTMB(
104+
observed ~ fyear * species,
105+
data = sim_dat,
106+
mesh = mesh,
107+
family = gaussian(),
108+
spatial = "off",
109+
time = "year",
110+
spatiotemporal = "iid",
111+
spatial_varying = ~ 0 + factor(species)
112+
)
113+
fit
114+
```
115+
116+
You'll notice that there are two rows of entries for `sigma_Z` our spatially varying random field standard deviation:
117+
118+
```{r}
119+
tidy(fit, "ran_pars")
120+
```
121+
122+
This means that our model is trying to estimate separate species-specific variance terms for the species-specific spatial fields (say *that* 10 times fast!). Here, that matches how we simulated the data. In other contexts, especially if we ran into estimation issues, we might want to share those SDs.
123+
124+
If we wanted to estimate species-specific spatial fields with a single shared variance (meaning the net magnitude of the peaks and valleys in the fields were similar but the wiggles themselves were species specific), we could do that by specifying a custom map argument and passing it into `sdmTMBcontrol()`. Any shared factor levels in the `map` are gathered to have 'mirrored' or shared parameter values. We assign these to `ln_tau_Z` because, internally, this is the parameter that gets converted into the spatially-varying field variances (the SDs of those fields are `sigma_Z`).
125+
126+
This case is pretty simple, but for more complicated cases we could figure out the structure of our needed `map` vector as follows:
127+
128+
```{r}
129+
colnames(model.matrix(~ 0 + factor(species), data = sim_dat))
130+
```
131+
132+
So, we need a vector of length two with shared factor values:
133+
134+
```{r}
135+
map_list <- list(ln_tau_Z = factor(c(1, 1)))
136+
```
137+
138+
Then, we can use our map list to share the spatially varying coefficient SDs:
139+
140+
```{r}
141+
fit <- sdmTMB(
142+
observed ~ fyear * factor(species),
143+
data = sim_dat,
144+
mesh = mesh,
145+
family = gaussian(),
146+
spatial = "off",
147+
time = "year",
148+
spatiotemporal = "iid",
149+
spatial_varying = ~ 0 + factor(species),
150+
control = sdmTMBcontrol(map = map_list)
151+
)
152+
fit
153+
```
154+
155+
Notice the spatially varying coefficient SD is now shared.
156+
157+
### Model 3: species-specific spatiotemporal fields
158+
159+
In all of the examples above, spatiotemporal fields are included, but shared among species. As another example, we can extend the above approaches to set up a model that includes spatiotemporal fields unique to each species.
160+
161+
One approach to including separate spatiotemporal fields by species is creating a new variable that is a concatenation of species and year (or any given time step factor). For example, we can then implement this form of species-specific spatiotemporal variation by changing the `time` argument to be `time = "species_year"`.
162+
163+
```{r}
164+
sim_dat$species_year <- factor(paste(sim_dat$species, sim_dat$year))
165+
map_list <- list(ln_tau_Z = factor(c(1, 1)))
166+
fit <- sdmTMB(
167+
observed ~ fyear * factor(species),
168+
data = sim_dat,
169+
mesh = mesh,
170+
family = gaussian(),
171+
spatial = "off",
172+
time = "species_year",
173+
spatiotemporal = "iid",
174+
spatial_varying = ~ 0 + factor(species),
175+
control = sdmTMBcontrol(map = map_list)
176+
)
177+
fit
178+
```
179+
180+
### Model 4: hack species into the time element for spatial models
181+
182+
If we only wanted to fit a spatial model but had several species (or other groups), one approach is to pretend our species (or other group) is the time element.
183+
184+
```{r}
185+
sim_dat$numeric_species <- as.numeric(factor(sim_dat$species)) # needs to be numeric
186+
fit_fake_time <- sdmTMB(
187+
observed ~ 0 + factor(species),
188+
data = sim_dat,
189+
mesh = mesh,
190+
family = gaussian(),
191+
spatial = "off",
192+
time = "numeric_species", #< hack
193+
spatiotemporal = "iid" #< 'AR1' or 'RW' probably wouldn't make sense here
194+
)
195+
fit_fake_time
196+
```
197+
198+
This is just a convenience though. We could instead do the same thing using the `spatial_varying` argument making sure to 'map' the field variances to be shared to match the above:
199+
200+
```{r}
201+
fit_svc <- sdmTMB(
202+
observed ~ 0 + factor(species),
203+
data = sim_dat,
204+
mesh = mesh,
205+
family = gaussian(),
206+
spatial = "off",
207+
spatial_varying = ~ 0 + factor(species),
208+
control = sdmTMBcontrol(map = list(ln_tau_Z = factor(c(1, 1))))
209+
)
210+
fit_svc
211+
```
212+
213+
We can prove they're identical:
214+
215+
```{r}
216+
logLik(fit_fake_time)
217+
logLik(fit_svc)
218+
```
219+
220+
### Putting it all together
221+
222+
These examples illustrate a number of ways that species-specific effects can be included in `sdmTMB` models, and can be extended to other categories/groups/cohorts within a species for which one wants to control the amount of information shared between groups (e.g., age-, size-, or stage-specific estimates). A brief summary of these approaches can be summarized as:
223+
224+
```{r echo=FALSE}
225+
desc <- data.frame(
226+
Form = c(
227+
"Main effects",
228+
"Spatial effects",
229+
"Spatial effects w/shared variance",
230+
"Spatiotemporal effects"),
231+
Implementation = c(
232+
"Year-by-species interactions or smooths by year",
233+
"Spatially varying coefficients",
234+
"Spatially varying coefficients + map argument",
235+
"Species-year factor as time variable")
236+
)
237+
if (require("knitr", quietly = TRUE)) {
238+
knitr::kable(desc)
239+
} else
240+
print(desc)
241+
```
242+
243+
### Further extensions
244+
245+
As long as you're willing to treat spatiotemporal and group-level fields (e.g., for different species or age cohorts) as independent, sdmTMB can be used to fit models to these data. For example, this allows sdmTMB to be used for standardization of age or length composition data as in [Thorson and Haltuch (2018) CJFAS](https://doi.org/10.1139/cjfas-2018-0015). The approach is to similar to the above and we plan to write a separate vignette on the topic.

0 commit comments

Comments
 (0)