## How to create animated graphics to illustrate spatial spillover effects

This post shows how to create animated graphics that illustrate the spatial spillover effects generated by a spatial autoregressive (SAR) model. After reading this post, you could create an animated graph like the following.

This post is organized as follows. First, I estimate the parameters of a SAR model. Second, I show why a SAR model can produce spatial spillover effects. Finally, I show how to create an animated graph that illustrates the spatial spillover effects.

**A SAR model**

I want to analyze the homicide rate in Texas counties as a function of unemployment. I suspect that the homicide rate in one county affects the homicide rate in neighboring counties.

I want to answer two questions.

- How can I set up a model that explicitly allows the homicide rate in one county to depend on the homicide rate in neighboring counties?
- Given my model, if the unemployment rate in Dallas increases to 10%, how would the homicide rate change in the neighboring counties of Dallas ?

**Fit a SAR model**

A standard linear model for the homicide rate in county i (hratei) as a function of the unemployment rate in that county’s unemployment is

A SAR model allows hratei to depend on the homicide rate in neighboring counties. I need some new notation to write down a SAR model. I let Wi,j be a positive number if county j is a neighbor of county i, zero if the j is not a neighbor of i, and zero if j=i, because no county can border itself.

Given this notation, a SAR model that allows the homicide rate in county i to depend on the homicide rate in neighboring counties can be written as

The spatial-weighting matrix that we are using has a special structure; each element is either a value c or zero, where c is greater than zero. This type of spatial-weighting matrix is known as a normalized contiguity matrix.

In Stata, we use **spmatrix** to create a spatial-weighting matrix, and we use **spregress** to fit a cross-sectional SAR model.

I begin by downloading some data on the homicide rates of U.S. counties from the Stata website and creating a subsample that uses only data on counties in Texas.

Intuitively, a file that specifies the borders of all the places of interest is known as a shape file. texas.dta is linked to the Stata version of a shape file that specifies the borders of all the counties in Texas. I now download that dataset from the Stata website and use spset to show that they are linked.

I now use spmatrix to create a normalized contiguity spatial-weighting matrix.

Now that I have my data and my spatial-weighting matrix, I can estimate the model parameters.

**Spatial spillover**

Now we are ready to answer the second question. Based on our estimation results from spregress, we can proceed in three steps.

- Predict the homicide rate using original data.
- Change Dallas’s unemployment rate to 10% and predict the homicide rate again.
- Compute the difference between two predictions and map it.

The above graph shows that a change in the unemployment rate in Dallas changes the homicide rates in the counties that are near to Dallas, in addition to the homicide rate in Dallas. The change in Dallas spills over to the nearby counties, and the effect is known as a spillover effect.

**SAR model and spatial spillover**

In this section, I show why a SAR model generates a spillover effect. In the process, I provide a formula for this effect that I use to create the animated graph.

The matrix form for a SAR model is

Solving for y yields

The mean value of y given a value of X is known as the the expectation of y conditional on X. Because ϵ is independent of X, the expectation of y conditional on X is

Note that this conditional expectation specifies the mean for each county in Texas because y is a vector.

We use this equation to define the effect of going from one set of values for X to another set. In the case at hand, I let X0 contain the covariate values in the observed data and let X1 contain the same values except that the unemployment rate in Dallas has been set to 10%. With this notation, I see that going from X0 to X1 causes the mean homicide rates for each county in Texas to change by

I now show that a technical condition assumed in SAR models produces an expression for the animated graph. SAR models are widely used because they satisfy a stability condition. Intuitively, this stability condition says that the inverse matrix (I–λW)−1 can be written as a sum of terms that decrease in size exponentially fast. This condition is that

**Create animated graphs for spillover effects**

I now describe how I generate the animated graph. Each graph plots the change using a subset of the terms in (3). The first graph plots the change computed from the first term only. The second graph plots the change computed from the first and second terms only. The third graph plots the change computed from the first three terms only. And so on.

Steps 5 through 20 perform the analogous operations.

Finally, combine graphs from step 1 to step 20, and create an animated graph.

Here is the code that implements this process.

This code uses the ereturn results produced by spregress above and its corresponding predict command.

Here is the animated graph created by this code.

**Done and undone**

In this post, I discussed spillover effects and why SAR models produce them in the context of an example using the counties in Texas. I also showed how the effects can be computed as an accumulated sum. I used the accumulated sum to create an animated graph that illustrates how the effects spill over in the counties in Texas.