Spatial-temporal aggregated predictor (STAP) models can be used in a longitudinal setting to model within and across subject differences by including subject specific means and subject differences from means. These difference in differences models can have a causal interpretation if certain assumptions are met. In a standard regression modeling framework, this would require a simple transformation of the time-varying confounders. For STAP models, since the variables being centered are random, they must be manipulated within each iteration of the sampler, requiring a different function and modeling syntax in rstap (as of v.1.1.0), which will be demonstrated in this vignette.

We’ll begin by loading the neccessary libraries


The data generation is similar to standard longitudinal simulation so we will omit the code here. It can be found on the Github site in the vignettes folder.

Note that we’ll generate the outcome under the difference in difference parameterization (see formula below) to make assessment of the parameters easy, and we’ll only include a subject specific intercept and slope, which rstap is currently limited to.

#> Warning: `as_tibble.matrix()` requires a matrix with column names or a `.name_repair` argument. Using compatibility `.name_repair`.
#> This warning is displayed once per session.

Simulating and Estimating a STAP model

Consider the following model examining the average of some continuous outcome as a function of coffee shop exposure and confounders, conditional on latent subject covariates, \(b_i\)

\[ E[Y_{ij}|b_i] = \alpha + \Delta X_{ij}(\theta_s)\beta_{i,Coffee} + \bar{X}_{ij}(\theta_s)\bar{\beta}_{i,Coffee} + + Z_{1}\delta_{i,sex} + b_i + b_i*w_{ij} \] where \[ \Delta X(\theta_s) = \overbrace{\sum_{d\in\mathcal{D}} w_d(\frac{d}{\theta_s}) }^{X_{ij}(\theta_s)} - \underbrace{\sum_{j=1}^{n_i}\sum_{d\in\mathcal{D}} w_d(\frac{d}{\theta_s})}_{\bar{X}_{ij}(\theta_s)} \]

The \(\bar{X}_{ij}\) serves as an estimate of a given subject’s average exposure to coffee shops across all measurements, so that \(\Delta X_{ij}(\theta_s)\) represents the difference in “usual” exposure implying that \(\beta_{i,Coffee}\) represents the within subject difference of increased Coffee exposure on the outcome, while \(\bar{\beta}\) represents the difference across or between subjects for one unit increase in \(\bar{X}_{ij}(\theta_s)\), the average subject exposure.

We combine these two main effect estimates with standard, non time-varying confounders - only sex is used here - as well as random intercepts and slopes for each subject to account for correlation within subjects across measurements. Although one will also have to include other built environment features that may be often colocated with one another to truly remove all sources of confounding, requiring a lot of data, this model represents the best attempt one can make at estimating the causal effect of a built environment feature.

We’ll fit the model with priors that reflect our relative uncertainty about the spatial and temporal scales and place a fairly uninformative prior on the subject-specific variance. We’ll also use a Weibull survival/cdf function to model the spatial/temporal relationship. This is more flexible than we need, but should still allow us to capture the true information. For information on how priors are set on the subject specific covariance, see the rstanarm vignette’s write up here. The functional form will be the same as the model under which it was simulated since this vignette is for exposition.

We can now check our model estimates and see how they compare to the true values.


More information on the parameterization of this model from an academic and technical perspective can be found in:

  1. S. L. Morgan.Handbook of causal analysis for social research. Springer, 2013

while the underlying fundamentals of the difference in differences model and its causal interpretation can be found in more lay-accessible book, “Mastering ’Metrics”, whose citation is below.

  1. Angrist, Joshua D, and Jörn-Steffen Pischke. Mastering ’metrics: The Path from Cause to Effect. 2015. Print.