# Dynamic panel data models with Stan?

**URL:** <https://discourse.mc-stan.org/t/dynamic-panel-data-models-with-stan/5136>\
**Category:** Modeling\
**Created:** [August 9, 2018, 2:09pm UTC](https://discourse.mc-stan.org/t/dynamic-panel-data-models-with-stan/5136 "2018-08-09T14:09:06Z")\
**Posts on this page:** 1\
**Showing post:** 41

<div class="post-metadata">

**Author:** ![Max\_Mantei](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/max_mantei/32/7912_2.png) [@Max\_Mantei](https://discourse.mc-stan.org/u/Max_Mantei)\
**Post date:** [November 15, 2019, 9:57pm UTC](https://discourse.mc-stan.org/t/dynamic-panel-data-models-with-stan/5136/41 "2019-11-15T21:57:12Z")

</div>

Hi @ignacio!

I’m not the great @rtrangucci, but let me give it a try.

So, unfortunately the notation changed a bit over the course of this thread—but, basically what’s `u[i]` in the code is \mu\_i in equation, right? So far so good.

In the answers above you can find that \mathbb{E}[Y\_{i,t}]=\mu\_i, so in the code that is the `u[i]`—the mean of `y[1,i]`.

For the variance we have

\begin{align} \text{Var}[Y\_{i,t}]&=\text{Var}[\delta Y\_{i,t-1}]+\text{Var}[\epsilon\_{i,t}] \\ \text{Var}[Y\_{i,t}]&=\delta^2\text{Var}[Y\_{i,t-1}] +\sigma^2\_\epsilon\\ \text{Var}[Y\_{i,t}] - \delta^2\text{Var}[Y\_{i,t-1}]&=\sigma^2\_\epsilon. \end{align}

Now, I think we need to assume \text{Var}[Y\_{i,t}] = \text{Var}[Y\_{i,t-1}], which is reasonable (assume iid residuals / a stationary process). Then,

\begin{align} \text{Var}[Y\_{i,t}] - \delta^2\text{Var}[Y\_{i,t}]&=\sigma^2\_\epsilon\\ \text{Var}[Y\_{i,t}](1 - \delta^2)&=\sigma^2\_\epsilon\\ \text{Var}[Y\_{i,t}]&=\frac{\sigma^2\_\epsilon}{(1 - \delta^2)}\\ \text{Sd}[Y\_{i,t}]&=\frac{\sigma\_\epsilon}{\sqrt{1 - \delta^2}}, \end{align}

which is the `sigma_e / sqrt(one_minus_delta_sq)` part of the code.

I learned this at the [Helsinki StanCon Tutorial with Jonah](https://github.com/jgabry/stancon2018helsinki_intro/blob/master/Pest_Control_Example.Rmd). Have a look—he also discusses a GP formulation of this AR(1) process.

Cheers! :)

---

_[View the full topic](https://discourse.mc-stan.org/t/dynamic-panel-data-models-with-stan/5136)._
