# Space-time models in Stan / BRMS

**URL:** <https://discourse.mc-stan.org/t/space-time-models-in-stan-brms/4735>\
**Category:** brms\
**Tags:** ecology\
**Created:** [July 4, 2018, 7:27am UTC](https://discourse.mc-stan.org/t/space-time-models-in-stan-brms/4735 "2018-07-04T07:27:26Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![ucfagls](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ucfagls/32/1073_2.png) [@ucfagls](https://discourse.mc-stan.org/u/ucfagls)\
**Post date:** [August 26, 2019, 10:39pm UTC](https://discourse.mc-stan.org/t/space-time-models-in-stan-brms/4735/8 "2019-08-26T22:39:02Z")

</div>

I’ve worked with similar kinds of data and have used tensor product splines to estimate the space-time components. You can use the `t2()` function in the formula to set up these kinds of terms. For example a smooth anisotropic spatial smooth interacting with a time component would be set up as

```
t2(x, y, time, k = c(10,10,15))

```

where `x` and `y` are the spatial coordinates, `time` is something like the observation year, and `k` is the basis size for each marginal smooth. An isotropic version would be

```
t2(x, y, time, d = c(2,1), k = c(50,15))

```

where the `d` sets the `x` and `y` coordinates to be from a 2d thin-plate spline, which has a single maximum wiggliness (here set to say 50).

The `t2()` in _brms_ uses _mgcv_ to set up the smoothers so as long as it goes into a `t2()` you can use any of a wide range of smoother types in the _brms_ model.

You mention polygons (as if the data are areal observations?); if so, then this kind of spatial data can be accommodated in the spline framework via a Markov random field using the `bs = 'mrf'` option in _mgcv_ and hence _brms_. You need to pass `t2()` a factor indexing the region/polygon for each observation rather than `x,y` pairs, but you can do this via a neighbourhood object generated from a polygon shapefile object read into R. See `?mgcv:::mrf` for the detail.

These are not the kinds of models you mentioned but they are somewhat similar.

---

_[View the full topic](https://discourse.mc-stan.org/t/space-time-models-in-stan-brms/4735)._
