Spatio-Temporal Model

I’m very new in Stan, i implemented the CAR model
Now i want to implement a temporal car model,it’s even possible in stan?

1 Like

Short answer:

Yes, probably. If you can write down a probability model and your posterior is smooth (i.e. no discrete parameters or thresholds that cause the log-posterior to “jump”), then Stan is probably one of the best ways to fit your model currently available.

Longer answer:

Depends what you mean exactly. If you want to translate the case study to a pure time-series model, the only thing that changes fundamentally is the neighborhood structure. If (as your title suggests) you want to extend this spatial model to include a time dimension, you can probably include the time-dimension neighborhood structure, but you will want to carefully consider how your “neighbors” relate in space vs. in time.

What would the model be? (In maths, or a reference, or BUGS code etc)


I would like to implement a simple car temporal model ( the explanation in the uploaded image) with an AR(1) for temporal trend.


Stan requires a joint density, so to code this up, you will want to rewrite these element-wise univariate conditional priors as multivariate normal priors over vectors, e.g., for a model with a temporal AR1 / IAR spatial field:


Where \phi_t is a vector with one element per spatial unit, \tau is a precision parameter, D is a diagonal matrix, and W is an adjacency matrix (for more info on IAR models:

So if i have to write this, can i do

for(i in 2:T)

My point is :can i use this “for” implementation?

Another question:in the model i wrote above there is no AR(1) in the spatial therm, so if i do a temporal model i decide which term has an autoregressive expression?lf i try to implement the delta term with an AR, what can i do to write the multinormal vector?

I’m not sure what t is in this loop - as written I am not sure this would work as you intend.

I’m not familiar with the prior that you outlined for delta, but if you want to put for instance a temporal AR 1 prior on delta, there are examples of AR 1 models in Stan that might be helpful.

1 Like

i wrote the “for” part for give an example of temporal implementation of the therm you wrote above.
Can you give me this examples,because i searched a lot but i find nothing about autoregressive model in stan.Thank you for the patience,im really new about this topic

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:

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 (, or you could specify the AR 1 prior using the transformed parameters block (Migrated from Google Group: AR(1) Logistic Regression Funnel).

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), 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
1 Like

Thank you a lot for the patience;
I’m really having some trouble to understand how migrate from “marginal definition of delta” to joint distribution required in Stan.I read the topic you gave me but i can’t understand the connection between the model you cited me and with my delta

Stan only requires that you provide something equal to the log posterior up to a constant that doesn’t depend on the parameters. It doesn’t have to be the joint, but that’ll work.

You can marginalize a parameter out and fit the rest in many cases, and it’s much more efficient. You can often recover inferences for the marginalized paramters that are better than what you’d get through sampling (as explained by the Rao-Blackwell theorem).

I had a faint memory of a similar discussion from years ago:

Ben Goodrich’s point about using the joint form of CAR priors rather than the set of full conditionals is specifically what I was resurrecting in recommending a joint prior for the spatial and temporal adjustments.

@Federico_Ercole - Generally the way to go from the set of full conditionals to a joint distribution is by applying Brook’s Lemma, but for specific model cases (e.g., CAR and ICAR models, and a variety of spatiotemporal areal models) you can readily find the joint distributions in many model descriptions.