温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
原文:How to create animated graphics to illustrate spatial spillover effects 中文版PPT 在线浏览 6 March 2018 作者:Di Liu, Senior Econometrician[1]
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.
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.
A standard linear model for the homicide rate in county
A SAR model allows
Given this notation, a SAR model that allows the homicide rate in county
where
Stacking the neighborhood information in
The spatial-weighting matrix that we are using has a special structure; each element is either a value
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.
. /* Get data for Texas counties' homicide rate */
. copy http://www.stata-press.com/data/r15/homicide1990.dta ., replace
. use homicide1990
(S.Messner et al.(2000), U.S southern county homicide rates in 1990)
. keep if sname == "Texas"
(1,158 observations deleted)
. save texas, replace
file texas.dta saved
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.
. /* Get data for Texas counties' homicide rate */
. copy http://www.stata-press.com/data/r15/homicide1990_shp.dta, replace
. spset
Sp dataset texas.dta
data: cross sectional
spatial-unit id: _ID
coordinates: _CX, _CY (planar)
linked shapefile: homicide1990_shp.dta
I now use spmatrix to create a normalized contiguity spatial-weighting matrix.
. /* Create a spatial contiguity matrix */
. spmatrix create contiguity W
Now that I have my data and my spatial-weighting matrix, I can estimate the model parameters.
. /* Estimate SAR model parameters */
. spregress hrate unemployment, dvarlag(W) gs2sls
(254 observations)
(254 observations (places) used)
(weighting matrix defines 254 places)
Spatial autoregressive model Number of obs = 254
GS2SLS estimates Wald chi2(2) = 14.23
Prob > chi2 = 0.0008
Pseudo R2 = 0.0424
------------------------------------------------------------------------------
hrate | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
hrate |
unemployment | .4584241 .152503 3.01 0.003 .1595237 .7573245
_cons | 2.720913 1.653105 1.65 0.100 -.5191143 5.960939
-------------+----------------------------------------------------------------
W |
hrate | .3414964 .1914865 1.78 0.075 -.0338103 .7168031
------------------------------------------------------------------------------
Wald test of spatial terms: chi2(1) = 3.18 Prob > chi2 = 0.0745
Now we are ready to answer the second question. Based on our estimation results from spregress, we can proceed in three steps.
. preserve /* save data temporarily */
. /* Step 1: predict homicide rate using original data */
. predict y0
(option rform assumed; reduced-form mean)
. /* Step 2: change Dallas unemployment rate to 10%, and predict again*/
. replace unemployment = 10 if cname == "Dallas"
(1 real change made)
. predict y1
(option rform assumed; reduced-form mean)
. /* Step 3: Compute the prediction difference and map it*/
. generate double y_diff = y1 - y0
. grmap y_diff, title("Global spillover")
. restore /* return to original data */
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.
连享会计量方法专题……
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
The mean value of
Note that this conditional expectation specifies the mean for each county in Texas because
We use this equation to define the effect of going from one set of values for
where
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
Plugging the formula from (2) into the effect in (1) yields
which is the expression for the effect that I use to generate the animated graph.
Each term in (3) has some intuition, which is most easily presented in terms of my example. The first term (
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.
The first four steps of the code do the following.
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.
1 /* get estimate of spatial lag parameter lambda */
2 local lambda = _b[W:hrate]
3
4 /* xb based on original data */
5 predict xb0, xb
6
7 /* xb based on modified data */
8 replace unemployment = 10 if cname == "Dallas"
9 predict xb1, xb
10
11 /* compute the outcome change in the first step */
12 generate dy = xb1 - xb0
13 format dy %9.2f
14
15 /* Initialize Wy, lamWy, */
16 generate Wy = dy
17 generate lamWy = dy
18
19 /* map the outcome change in step 1 */
20 grmap dy
21 graph export dy_0.png, replace
22 local input dy_0.png
23
24 /* compute the outcome change from step 2 to 11 */
25 forvalues p=1/20 {
26 spgenerate tmp = W*Wy
27 replace lamWy = `lambda'^`p'*tmp
28 replace Wy = tmp
29 replace dy = dy + lamWy
30 grmap dy
31 graph export dy_`p'.png, replace
32 local input `input' dy_`p'.png
33 drop tmp
34 }
35
36 /* convert graphs into a animated graph */
37 shell convert -delay 150 -loop 0 `input' glsp.gif
38
39 /* delete the generated pgn file */
40 shell rm -fR *.png
This code uses the ereturn results produced by spregress above and its corresponding predictcommand.
Here is the animated graph created by this code.
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.
连享会-直播课 上线了!
http://lianxh.duanshu.com
免费公开课:
直击面板数据模型 - 连玉君,时长:1小时40分钟 Stata 33 讲 - 连玉君, 每讲 15 分钟. 部分直播课 课程资料下载 (PPT,dofiles等)
支持回看,所有课程可以随时购买观看。
专题 | 嘉宾 | 直播/回看视频 |
---|---|---|
⭐ 最新专题 ⭐ | DSGE, 因果推断, 空间计量等 | |
⭕ Stata数据清洗 | 游万海 | 直播, 2 小时,已上线 |
研究设计 | 连玉君 | 我的特斯拉-实证研究设计,-幻灯片- |
面板模型 | 连玉君 | 动态面板模型,-幻灯片- |
面板模型 | 连玉君 | 直击面板数据模型 [免费公开课,2小时] |
Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。
关于我们
课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法
等
连享会小程序:扫一扫,看推文,看视频……
扫码加入连享会微信群,提问交流更方便
✏ 连享会学习群-常见问题解答汇总:
✨ https://gitee.com/arlionn/WD
Posts by Di Liu, Senior Econometrician: https://blog.stata.com/author/dliu/