Skip to content

Commit 5812dcf

Browse files
committed
Fix time-varying vignette example #398
1 parent a00e32d commit 5812dcf

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

vignettes/articles/basic-intro.Rmd

+27-23
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ glimpse(pcod)
4646
```
4747

4848
The most basic model structure possible in sdmTMB replicates a GLM as can be fit with `glm()` or a GLMM as can be fit with lme4 or glmmTMB, for example.
49-
The spatial components in sdmTMB are included as random fields using a triangulated mesh with vertices, known as knots, used to approximate the spatial variability in observations.
49+
The spatial components in sdmTMB are included as random fields using a triangulated mesh with vertices, known as knots, used to approximate the spatial variability in observations.
5050
Bilinear interpolation is used to approximate a continuous spatial field (Rue et al., 2009; Lindgren et al., 2011) from the estimated values of the spatial surface at these knot locations to other locations including those of actual observations.
5151
These spatial random effects are assumed to be drawn from Gaussian Markov random fields (e.g., Cressie & Wikle, 2011; Lindgren et al., 2011) with covariance matrices that are constrained by Matérn covariance functions (Cressie & Wikle, 2011).
5252

53-
There are different options for creating the spatial mesh (see `sdmTMB::make_mesh()`).
54-
We will start with a relatively coarse mesh for a balance between speed and accuracy (`cutoff = 10`, where cutoff is in the units of X and Y (km here) and represents the minimum distance between knots before a new mesh vertex is added).
53+
There are different options for creating the spatial mesh (see `sdmTMB::make_mesh()`).
54+
We will start with a relatively coarse mesh for a balance between speed and accuracy (`cutoff = 10`, where cutoff is in the units of X and Y (km here) and represents the minimum distance between knots before a new mesh vertex is added).
5555
Smaller values create meshes with more knots.
5656
You will likely want to use a higher resolution mesh (more knots) in applied scenarios, but care must be taken to avoid overfitting.
5757
The circles represent observations and the vertices are the knot locations.
@@ -146,8 +146,8 @@ And similarly for the random effect and variance parameters:
146146
tidy(m3, "ran_pars", conf.int = TRUE)
147147
```
148148

149-
Note that standard errors are not reported when coefficients are in log space, but the confidence intervals are reported. These parameters are defined as follows:
150-
149+
Note that standard errors are not reported when coefficients are in log space, but the confidence intervals are reported. These parameters are defined as follows:
150+
151151
* `range`: A derived parameter that defines the distance at which 2 points are effectively independent (actually about 13% correlated). If the `share_range` argument is changed to `FALSE` then the spatial and spatiotemporal ranges will be unique, otherwise the default is for both to share the same range.
152152

153153
* `phi`: Observation error scale parameter (e.g., SD in Gaussian).
@@ -181,11 +181,11 @@ ggplot(pcod, aes(X, Y, col = resids)) +
181181
coord_fixed()
182182
```
183183

184-
Those were fast to calculate but can look 'off' even when the model is consistent with the data.
184+
Those were fast to calculate but can look 'off' even when the model is consistent with the data.
185185
MCMC-based residuals are more reliable but slow.
186186
We can calculate them through some help from the sdmTMBextra package. <https://github.com/pbs-assess/sdmTMBextra>.
187-
In practice you would like want more `mcmc_iter` and `mcmc_warmup`.
188-
Total samples is `mcmc_iter - mcmc_warmup`.
187+
In practice you would like want more `mcmc_iter` and `mcmc_warmup`.
188+
Total samples is `mcmc_iter - mcmc_warmup`.
189189
We will also just use the spatial model here so the vignette builds quickly.
190190

191191
```{r residuals-mcmc, fig.width = 8}
@@ -229,7 +229,7 @@ plot_map <- function(dat, column) {
229229
}
230230
```
231231

232-
There are four kinds of predictions that we get out of the model.
232+
There are four kinds of predictions that we get out of the model.
233233

234234
First, we will show the predictions that incorporate all fixed effects and random effects:
235235

@@ -254,7 +254,7 @@ plot_map(predictions, exp(est_non_rf)) +
254254
ggtitle("Prediction (fixed effects only)")
255255
```
256256

257-
We can look at the spatial random effects that represent consistent deviations in space through time that are not accounted for by our fixed effects.
257+
We can look at the spatial random effects that represent consistent deviations in space through time that are not accounted for by our fixed effects.
258258
In other words, these deviations represent consistent biotic and abiotic factors that are affecting biomass density but are not accounted for in the model.
259259

260260
```{r plot-spatial-effects, fig.width = 6}
@@ -263,7 +263,7 @@ plot_map(predictions, omega_s) +
263263
ggtitle("Spatial random effects only")
264264
```
265265

266-
And finally we can look at the spatiotemporal random effects that represent deviation from the fixed effect predictions and the spatial random effect deviations.
266+
And finally we can look at the spatiotemporal random effects that represent deviation from the fixed effect predictions and the spatial random effect deviations.
267267
These represent biotic and abiotic factors that are changing through time and are not accounted for in the model.
268268

269269
```{r plot-spatiotemporal-effects, fig.width = 8}
@@ -273,8 +273,8 @@ plot_map(predictions, epsilon_st) +
273273
ggtitle("Spatiotemporal random effects only")
274274
```
275275

276-
We can also estimate the uncertainty in our spatiotemporal density predictions using simulations from the joint precision matrix by setting `nsim > 0` in the predict function.
277-
Here we generate 100 estimates and use `apply()` to calculate upper and lower confidence intervals, a standard deviation, and a coefficient of variation (CV).
276+
We can also estimate the uncertainty in our spatiotemporal density predictions using simulations from the joint precision matrix by setting `nsim > 0` in the predict function.
277+
Here we generate 100 estimates and use `apply()` to calculate upper and lower confidence intervals, a standard deviation, and a coefficient of variation (CV).
278278

279279
```{r sim-cv}
280280
sim <- predict(m3, newdata = grid_yrs, nsim = 100)
@@ -333,28 +333,32 @@ ggeffects::ggeffect(m3, "depth [0:500 by=1]") %>% plot()
333333

334334
### Time-varying effects
335335

336-
We could also let the effect of depth vary through time.
337-
In this example, it helps to give each year a separate mean effect (`as.factor(year)`).
338-
The `~ 0` part of the formula omits the intercept.
339-
For models like this that take longer to fit, we might want to set `silent = FALSE` so we can monitor progress.
336+
We could also let the effect of depth vary through time.
337+
We set up the time-varying coefficients to follow an AR1 process by setting `type_varying_type = "ar1"`.
338+
With `"ar1"` or `"rw0" (random walk), the fixed effects represent the starting point of the time series
339+
and the time-varying process represents deviations from this over time.
340+
We include a full length of time increments with `extra_time` to ensure we estimate time-varying coefficient values
341+
for each year, including any years that are missing from our data.
342+
For this example, we turn off the spatiotemporal random effects because we were having convergence issues with them turned on.
340343

341344
```{r tv-effect}
342345
m4 <- sdmTMB(
343-
density ~ 0 + as.factor(year),
346+
density ~ 1 + depth_scaled + depth_scaled2,
344347
data = pcod,
345-
time_varying = ~ 0 + depth_scaled + depth_scaled2,
346-
time_varying_type = "rw0",
348+
time_varying = ~ 1 + depth_scaled + depth_scaled2,
349+
time_varying_type = "ar1",
350+
extra_time = seq(min(pcod$year), max(pcod$year)),
347351
mesh = mesh,
348352
family = tweedie(link = "log"),
349353
spatial = "on",
350354
time = "year",
351-
spatiotemporal = "IID"
355+
spatiotemporal = "off"
352356
)
353357
m4
354-
AIC(m4)
355358
```
356359

357-
To plot these, we make a data frame that contains all combinations of the time-varying covariate and time. This is easily created using `expand.grid()` or `tidyr::expand_grid()`.
360+
To plot these, we make a data frame that contains all combinations of the time-varying covariate and time.
361+
This is easily created using `expand.grid()` or `tidyr::expand_grid()`.
358362

359363
```{r tv-depth-eff, fig.width = 6}
360364
nd <- expand.grid(

0 commit comments

Comments
 (0)