Skip to content

Commit ce404b3

Browse files
committed
Add to multispecies vignette
1 parent 7e7f1b3 commit ce404b3

File tree

3 files changed

+92
-8
lines changed

3 files changed

+92
-8
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: sdmTMB
33
Title: Spatial and Spatiotemporal SPDE-Based GLMMs with 'TMB'
4-
Version: 0.6.0.9020
4+
Version: 0.6.0.9021
55
Authors@R: c(
66
person(c("Sean", "C."), "Anderson", , "[email protected]",
77
role = c("aut", "cre"),

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# sdmTMB (development version)
22

3+
* Add vignette on multispecies models with sdmTMB (or any case where one wants
4+
additional spatial and or spatiotemporal fields by some group).
5+
36
* Add EDF (effective degrees of freedom) printing to smoothers with
47
`print.sdmTMB()` and `summary.sdmTMB()`. Set argument `edf = TRUE`.
58
E.g. `print(fit, edf = TRUE)`. #383 #387

vignettes/articles/multispecies.Rmd

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,95 @@ fit <- sdmTMB(
170170
family = gaussian(),
171171
spatial = "off",
172172
time = "species_year",
173-
spatiotemporal = "iid",
174-
spatial_varying = ~ 0 + factor(species),
175-
control = sdmTMBcontrol(map = map_list)
173+
spatiotemporal = "iid"
174+
# spatial_varying = ~ 0 + factor(species),
175+
# control = sdmTMBcontrol(map = map_list)
176176
)
177177
fit
178178
```
179179

180-
### Model 4: hack species into the time element for spatial models
180+
### Model 4: species-specific spatiotemporal fields using the `spatial_varying` argument
181+
182+
We can fit the same model by using the `spatial_varying` argument. This would give us the added flexibility of letting each species' spatiotemporal field have its own variance if we wanted or if expanding our model to have another category of independent random fields. E.g., we might also have age or length bins.
183+
184+
First, we'll have the spatial fields share their variance and the spatiotemporal fields share their variance:
185+
186+
```{r}
187+
# quick hack to force all levels of species and species:factor interactions in formula:
188+
mm1 <- model.matrix(~ 0 + factor(species), sim_dat)
189+
mm2 <- model.matrix(~ 0 + factor(year):factor(species), sim_dat)
190+
mm <- cbind(mm1, mm2)
191+
sim_dat2 <- cbind(sim_dat, mm)
192+
193+
# make our map vector:
194+
n_sp <- ncol(mm1)
195+
n_st <- ncol(mm2)
196+
map_list2 <- list(ln_tau_Z = factor(
197+
c(rep(1, n_sp),
198+
rep(2, n_st))
199+
))
200+
map_list2
201+
202+
# hack together a model formula based on our hand constructed model matrix:
203+
svc_formula <- as.formula(paste0("~ `", paste(colnames(mm), collapse = "` + `"), "`"))
204+
svc_formula
205+
206+
fit_svc <- sdmTMB(
207+
observed ~ fyear * factor(species),
208+
data = sim_dat2,
209+
mesh = mesh,
210+
family = gaussian(),
211+
spatial = "off",
212+
time = "year",
213+
spatiotemporal = "off",
214+
spatial_varying = svc_formula,
215+
control = sdmTMBcontrol(map = map_list2)
216+
)
217+
```
218+
219+
We now have exactly the same model, just specified differently:
220+
221+
```{r}
222+
logLik(fit)
223+
logLik(fit_svc)
224+
```
225+
226+
Say we wanted to let the spatial and spatiotemporal variances be different for each species. We could do that by changing the map vector:
227+
228+
```{r}
229+
colnames(mm)
230+
map_list3 <- list(ln_tau_Z = factor(
231+
c(c(1, 2),
232+
rep(3, n_st/2),
233+
rep(4, n_st/2)
234+
)))
235+
# check:
236+
data.frame(map_value = map_list3$ln_tau_Z, svc_term = colnames(mm))
237+
238+
fit_svc_separate <- sdmTMB(
239+
observed ~ fyear * factor(species),
240+
data = sim_dat2,
241+
mesh = mesh,
242+
family = gaussian(),
243+
spatial = "off",
244+
time = "year",
245+
spatiotemporal = "off",
246+
spatial_varying = svc_formula,
247+
control = sdmTMBcontrol(map = map_list3)
248+
)
249+
fit_svc_separate
250+
```
251+
252+
Now we have separate SDs for the spatial and spatiotemporal fields across species.
253+
254+
But in this case, marginal AIC does not indicate an improvement from this added flexibility:
255+
256+
```{r}
257+
AIC(fit_svc, fit_svc_separate)
258+
```
259+
260+
261+
### Model 5: hack species into the time element for spatial models
181262

182263
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.
183264

@@ -198,7 +279,7 @@ fit_fake_time
198279
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:
199280

200281
```{r}
201-
fit_svc <- sdmTMB(
282+
fit_svc3 <- sdmTMB(
202283
observed ~ 0 + factor(species),
203284
data = sim_dat,
204285
mesh = mesh,
@@ -207,14 +288,14 @@ fit_svc <- sdmTMB(
207288
spatial_varying = ~ 0 + factor(species),
208289
control = sdmTMBcontrol(map = list(ln_tau_Z = factor(c(1, 1))))
209290
)
210-
fit_svc
291+
fit_svc3
211292
```
212293

213294
We can prove they're identical:
214295

215296
```{r}
216297
logLik(fit_fake_time)
217-
logLik(fit_svc)
298+
logLik(fit_svc3)
218299
```
219300

220301
### Putting it all together

0 commit comments

Comments
 (0)