# Spatio-Temporal Model

**URL:** <https://discourse.mc-stan.org/t/spatio-temporal-model/3170>\
**Category:** Modeling\
**Created:** [February 1, 2018, 3:12pm UTC](https://discourse.mc-stan.org/t/spatio-temporal-model/3170 "2018-02-01T15:12:11Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![mbjoseph](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mbjoseph/32/870_2.png) [@mbjoseph](https://discourse.mc-stan.org/u/mbjoseph)\
**Post date:** [February 4, 2018, 4:14pm UTC](https://discourse.mc-stan.org/t/spatio-temporal-model/3170/9 "2018-02-04T16:14:49Z")

</div>

Going back to your original model specification, assuming that you can convert those element-wise univariate normal priors to multivariate normal priors over vectors (over all spatial units for each time step of `phi`, and over all time steps for `delta`) the Stan code might look something like:

```stan
data {
  ...
  int K; // number of spatial units
  int T; // number of time steps
}
parameters {
  ...
  vector[K] phi[T];
  vector[T] delta;
}
model {
  ...
  for (t in 1:T) 
    // phi[t] is a vector with one element per spatial unit
    phi[t] ~ multi_normal...

  delta ~ multi_normal...
}

```

There are ways to implement CAR priors more efficiently that do not require using any of the `multi_normal*` functions, and these are described in the case studies mentioned above.

To use a temporal AR 1 prior on `delta`, you could use the covariance matrix for an AR 1 process ([https://stats.stackexchange.com/questions/295102/how-to-write-variance-covariance-matrix-of-ar1-process-in-r](https://stats.stackexchange.com/questions/295102/how-to-write-variance-covariance-matrix-of-ar1-process-in-r)), or you could specify the AR 1 prior using the transformed parameters block ([Migrated from Google Group: AR(1) Logistic Regression Funnel](http://discourse.mc-stan.org/t/migrated-from-google-group-ar-1-logistic-regression-funnel/776)).

I’d recommend starting with the simplest version of this model that you can, and then slowly add complexity and verify that you can recover parameters for a sequence of models that are increasing in complexity (essentially following [Stan best practices](https://github.com/stan-dev/stan/wiki/Stan-Best-Practices)), e.g.,

1. Ridiculously simple Poisson model
2. Model with a non-time varying spatial effect
3. Model with a time varying spatial effect
4. Model with a time varying spatial effect + a temporal effect

---

_[View the full topic](https://discourse.mc-stan.org/t/spatio-temporal-model/3170)._
