Stata:绘制宏观时序图

发布时间:2022-05-03 阅读 1832

Stata连享会   主页 || 视频 || 推文 || 知乎 || Bilibili 站

温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。

New! lianxh 命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc, ihelp, rdbalance, gitee, installpkg

课程详情 https://gitee.com/lianxh/Course

课程主页 https://gitee.com/lianxh/Course

⛳ Stata 系列推文:

PDF下载 - 推文合集

作者:史柯 (中央财经大学)
邮箱shike2231128@gmail.com

编者按:本文部分内容摘译自下文,特此致谢!
Source:Nakamura E, Steinsson J. Identification in macroeconomics[J]. Journal of Economic Perspectives, 2018, 32(3): 59-86. -PDF- -Appendix- -Slides- -Replication-


目录


1. 文献概述

与微观经济问题相比,宏观经济学的实证检验更困难。以货币政策为例,美联储对实际利率的设定并不是随机的,相反,美联储雇佣了数百名经济学家来仔细研究相关经济数据,以便使货币政策尽可能地内生。这意味着,需要进行相当仔细的研究,来确定某一货币政策未来产出影响是否是外生的,只有这样才能用于检验货币非中性。

以往关于货币非中性的三个最主要的实证研究是:Friedman 和 Schwartz (1963) 提出的关于货币政策在大萧条中的作用;20 世纪 80 年代初的 Volcker 反通货膨胀以及伴随而来的双重衰退;1973 年布雷顿森林体系的崩溃带来的美国实际汇率波动的急剧中断。

本文将以货币非中性的关键研究为基础,介绍 Stata 中宏观时序图的绘制。

2. 货币非中性的三个研究

2.1 Friedman 和 Schwartz (1963)

Friedman 和 Schwartz (1963) 指出美国在世界大战期间颁布了三次重大的货币政策,时间分别为:1920 年 1 月至 6 月,1931 年 10 月,和 1936 年 7 月至 1937 年 1 月。我们关注在大萧条期间发生的后两个货币政策。分析美联储的货币政策对美国工业生产的影响,为我们提供了货币非中性的实证证据。

我们以 1929 年 7 月的工业生产为作为基准水平,研究 1925 年至 1942 年美国工业生产水平的变动。首先计算其他时间美国工业生产相对于 1929 年 7 月的百分比:

. import excel "RawData/INDPRO.xlsx", clear cellrange(A11:B1192) firstrow // 数据可从文章的 Replication 文件中获取
. rename observation_date         Date
. rename INDPRO                   Production
. qui replace Date = mofd(Date)
. format Date %tm
. qui sum Production if Date == m(1929m7)
. local base = r(mean)
. qui gen IP = Production*100/`base'
. drop Production

第一个货币政策的时间和美国退出金本位的事件时间在我们的数据中均为时间点,可通过选项 tline 绘制垂直于 t 轴的线来标记,并通过 lcolor 进行颜色区分。Friedman 和 Schwartz (1963) 的第二个货币政策的时间区间,通过 twoway area 命令绘制散点与 x 轴围成的区域来标记。

因此图像绘制可以分解为所有时间的相对工业生产水平折线图,第二个货币政策的时间区间的区域图,以及两个特殊时间点的标记。

 * 将我们关注的第一个事件时间点 (1931 年 10 月) 设为暂元,用于后续绘图标记
. local FSDate1 1931m10

. /*
> 第二个事件时间 (1936 年 7 月至 1937 年 1 月) 为时间段,
> 故设为略高于所有数据工业生产相对水平的常数变量,
> 用于后续绘图标记中绘制矩形区域
> */
. gen FSDate2 = 160 if Date>=m(1936m6) & Date<=m(1937m1)

. *同样将美国脱离金本位的事件时间设为暂元,用于后续绘图标记
. local OffGold 1933m4

. twoway (area FSDate2 Date, bcolor(ltblue)) (line IP Date, lc(navy))   ///
>     if Date>=m(1925m1) & Date<=m(1942m1), tlabel(1925m1(60)1940m1,    ///
>     format(%tmCY)) tmtick(1925m1(12)1940m1) tline(`FSDate1', noextend ///
>     lcolor(ltblue)) tline(`OffGold', noextend lcolor(black))          ///
>     xtitle("Year") ytitle("") ylabel(40(40)160) legend(off)           ///
>     graphregion(color(white)) plotregion(lstyle(foreground)) 

2.2 Volcker 反通货膨胀

另一个经常被引用的关于货币非中立的证据是 Volcker 反通货膨胀,以及上世纪 70 年代末和 80 年代初伴随而来的双重衰退。我们主要关注 1965 年至 1995 年美国联邦基金利率、通货膨胀和失业率的变动。

我们关注的时间段为 1979 年 8 月至 1982 年八月,通过 twoway area 实现区域高亮。通过 twoway line 命令绘制基金利率、通货膨胀和失业率的变动以及 twoway area 实现区域高亮的标记:

. * 导入数据
. import excel "RawData/CPIAUCSL.xlsx", clear cellrange(A11:B856) firstrow
. tempfile temp
. save `temp'
. import excel "RawData/FEDFUNDS.xlsx", clear cellrange(A11:B766) firstrow
. qui merge 1:1 observation_date using `temp', nogenerate
. save `temp', replace
. import excel "RawData/UNRATE.xlsx", clear cellrange(A11:B844) firstrow
. qui merge 1:1 observation_date using `temp', nogenerate
. rename observation_date Date
. rename CPIAUCSL CPI
. rename FEDFUNDS FFR
. rename UNRATE   Unrate
. qui replace Date = mofd(Date)
. format Date %tm
. tsset  Date 
. qui gen infla12 = (CPI-L12.CPI)/(L12.CPI)*100
. label var infla12 "Yearly Chcange in the CPI"

. * 标记时间段
. gen VolckerDisinflation = 20 if Date>=m(1979m8) & Date<=m(1982m8)

. twoway (area VolckerDisinflation Date, bcolor(ltblue) yaxis(1) )         ///
>     (line FFR     Date, yaxis(1) lp(solid) lc(dknavy))                   ///
>     (line infla12 Date, yaxis(1) lp(solid) lc(red*0.5))                  ///
>     (line Unrate  Date, yaxis(2) lp(shortdash) lc(dkgreen) lw(medthick)) ///
>     if Date>=m(1968m1) & Date<=m(1994m12),                               ///
>     tlabel(1970m1(60)1995m1,format(%tmCY)) tmtick(1968m1(12)1995m1)      ///
>     ylabel(0(5)20, format(%8.0g) axis(1)) ylabel(2(2)10, format(%8.0g)   ///
>     axis(2)) graphregion(color(white)) plotregion(lstyle(foreground))    ///
>     xtitle("Year") ytitle("", axis(2)) legend(off)

2.3 布雷顿森林体系的崩溃

Mussa (1986) 认为,与 1973 年 2 月布雷顿森林固定汇率体系崩溃有关的货币政策变化,导致了美国汇率波动的大幅增加,下面绘制这一事件背景下的汇率变动。通过 line 命令实现实际汇率图像的绘制,通过 tline 完成 1973 年 2 月这一时间的标记。此外,lc 设置线条颜色,lp 设置线条图案,lw 设置线条宽度:

. * 导入数据    
. import excel "RawData/OECD.xlsx", clear sheet("OECD") firstrow
. qui replace Date = mofd(Date)
. format Date %tm
. tsset  Date 
. keep if Date < m(1999m1)
. qui gen NER_DEU = 1/USD_EURO
. qui replace NER_DEU = NER_DEU*1.95583 if Date < m(1999m1)
. qui gen RER_DEU = (1/NER_DEU)*(CPI_DEU)*(1/CPI_US)
. qui gen RER_DEU_growth = (RER_DEU-L.RER_DEU)/(L.RER_DEU)*100
. label var NER_DEU "The US-German Nominal Exchange Rate"
. label var RER_DEU "The US-German Real Exchange Rate"
. label var RER_DEU_growth "Monthly change in the US-German Real Exchange Rate"    
   
. line RER_DEU_growth Date if Date < m(1986m1), lp(solid) lc(navy) ///
>     tlabel(1960m1(60)1985m1,format(%tmCY)) yscale(range(-15 15)) /// 
>     ylabel(-15(5)15) tline(1973m2, noextend lcolor(ltblue))      ///
>     graphregion(color(white)) plotregion(lstyle(foreground))     ///
>     xtitle("Year") ytitle("Percent")     

3. 基于美联储的记录研究货币政策冲击

3.1 Romer 和 Romer (1989) 事件

Romer 和 Romer (1989) 认为,美联储的记录同样可用于识别自然实验。基于这些记录可以识别二战后美联储的反通货膨胀措施。Romer 和 Romer (1989) 确定了六次这样的事件,又于 1994 年补充了第七次事件,这些事件为证明货币政策的实质性影响提供了有力证据。

通过 line 命令绘制失业率的变动以及 tline 实现六次事件的标记:

. * 导入数据
. import excel "RawData/UNRATE.xlsx", clear cellrange(A11:B844) firstrow
. rename observation_date Date
. rename UNRATE Unrate
. qui replace Date = mofd(Date)
. format Date %tm
. tsset  Date 
  
. * 使用局部暂元标记六次 Romer 和 Romer (1989) 中识别的事件的时间
. local RomerDate 1955m9 1968m12 1974m4 1978m8 1979m10 1988m1

. line Unrate Date if Date>=m(1950m1)&Date<=m(2000m12), lc(navy)           ///
>     tline(`RomerDate',noextend lcolor(ltblue)) tlabel(1950m1(120)2000m1, ///
>     format(%tmCY)) xtitle("Year") ytitle("") ylabel(2(2)11)              ///
>     graphregion(color(white)) plotregion(lstyle(foreground)) 

3.2 911 恐怖袭击

以上提到的识别方法需要非常强大的隐含假设,即通过控制一些变量的滞后项可以捕获政策的所有内生变化。但是这在实践中似乎不太可能成立。美联储的政策决定是基于大量的数据来制定的,不同的考虑因素 (包括一些高度特殊的事件) 会影响美联储的货币政策,涵盖所有可能的因素基本无法实现。

下面以 2001 年 9 月 11 日纽约的恐怖袭击为例,分析 911 事件这一突发的特殊事件对美联储的联邦基金利率目标和一个月的欧元美元利率的影响。其中,欧洲美元利率可以被认为是下个月联邦基金的平均预期利率。

. * 导入数据
. import excel "RawData/DED1.xlsx", clear cellrange(A11:B11951) firstrow
. tempfile temp
. save `temp'
. import excel "RawData/DFEDTAR.xlsx", clear cellrange(A11:B9588) firstrow
. qui merge 1:1 observation_date using `temp', nogenerate
. rename observation_date Date
. rename DED1 ED
. rename DFEDTAR Target
. tsset Date
. drop if Date<d(01dec2000) | Date>d(01jan2003)

. tostring Date, gen(DateLabel) format(%tdd_m) force
. replace DateLabel = "" if _n == 1
. forvalues i=2/`=_N'{
  2.     qui replace DateLabel="" if (Target[`i'] == Target[`i'-1] & _n == `i')
  3. }

. * 构建当天开始时的联邦基金利率目标 (上一天的联邦利率基金目标)
. gen mTarget = L.Target 
. // Terrorist attack: Target rate changed before the market opened
. replace mTarget = mTarget[_n+1] if Date == d(17sep2001)

. * 修改 9 月 17 日开市时目标汇率,因为 911 恐怖袭击之后首次开市的 9 月 17 日的目标汇率在开市前改变
. replace mTarget = mTarget[_n+1] if Date == d(17sep2001)

. * 在计划外的联邦公开市场委员会会议的日期后加星号:
. replace DateLabel = DateLabel+"*" if DateLabel == "3 Jan"
. replace DateLabel = DateLabel+"*" if DateLabel == "18 Apr"
. replace DateLabel = DateLabel+"*" if DateLabel == "17 Sep"

. * 用于设定左侧面板中日期标签的位置,取值为 3 代表在三点钟方向
. gen DLabel = DateLabel[_n+1]
. gen pos=4
. replace pos=3 if DLabel=="27 Jun"
. replace pos=3 if DLabel=="21 Aug"
. replace pos=5 if DLabel=="17 Sep*"
. replace pos=3 if DLabel=="11 Dec"

分别使用 twoway connected 命令和 twoway line 命令绘制联邦基金目标利率、欧洲美元利率。其中 mlabel(DLabel) 规定了标记符号的内容,mlabvposition(pos) 规定了标记符号的位置,mlabgap(*2.5) 规定了标记符号距离标记点的距离,msize(vtiny) 规定了标记符号的大小,以上有关标记符号的设定实现了日期的标注。

color(ltblue) 规定了欧洲美元利率线图的颜色是,tlabel(#`ntick1', format(% tdMon)) 规定了 t 轴刻度数为 ntick1,且规定了刻度标签的格式。tscale (range (01dec2000 28feb2002) 规定了 t 轴的范围。具体如下:

. * 计算 ntick 用于设定 t 轴刻度个数:
. local ntick1 = floor((d(28feb2002)-d(01dec2000))/30)+1

. tw (connected mTarget Date, mlabel(DLabel) mlabvposition(pos) mlabgap(*2.5)  ///
>     msize(vtiny) mlabtextstyle(body)) (line ED Date, color(ltblue))         ///
>     if Date>=d(01dec2000) & Date<=d(28feb2002), graphregion(color(white))   ///
>     plotregion(lstyle(foreground)) legend(off) xtitle("") ytitle("Percent") ///
>     tlabel(#`ntick1',format(%tdMon)) tscale(range(01dec2000 28feb2002)) 

. * 将图像保存,用于合并
. graph save "Output/Figure5a.gph", replace

接下来绘制 9 月 17 日的细节图,与总体图思路一致,具体代码如下:

. * 首先生成变量用于设定标记符号标签 (时间) 的位置
. // Position for the date label in the right panel
. gen pos2=5
. replace pos2=4 if DLabel=="21 Aug"
. replace pos2=4 if DLabel=="2 Oct"

. * 计算 ntick2 用于设定 t 轴刻度个数
. local ntick2 = floor((d(01nov2001)-d(01aug2001))/30)+1

. // Markets closed due to the terrorist attacks
. gen closed = 4 if Date>=d(11sep2001) & Date<=d(17sep2001)
(755 missing values generated)

. * 绘制联邦基金目标利率以及欧洲美元利率。
. tw (area closed Date, bcolor(ltblue)) (scatter ED Date, mstyle(o) legend(off)) ///
>     (connected mTarget Date,legend(off) mlabel(DLabel) mlabvposition(pos2)     ///
>     mlabgap(*10) msize(vtiny) mlabtextstyle(heading) color(dknavy))            ///
>     if Date>=d(01aug2001) & Date<d(01nov2001), graphregion(color(white))       ///
>     plotregion(lstyle(foreground)) xtitle("") ytitle("Percent")                ///
>     tlabel(#`ntick2',format(%tdMon)) tscale(range(01aug2001 01nov2001)) 

. * 将图像保存
. graph save "Output/Figure5b.gph", replace

. * 与已经保存的整体图合并,
. graph combine "Output/Figure5a.gph" "Output/Figure5b.gph", ///
>     rows(1) xsize(20) ysize(10) graphregion(color(white))

4. 相关推文

Note:产生如下推文列表的 Stata 命令为:
lianxh 时间序列 绘图, m
安装最新版 lianxh 命令:
ssc install lianxh, replace

相关课程

免费公开课

最新课程-直播课

专题 嘉宾 直播/回看视频
最新专题 文本分析、机器学习、效率专题、生存分析等
研究设计 连玉君 我的特斯拉-实证研究设计-幻灯片-
面板模型 连玉君 动态面板模型-幻灯片-
面板模型 连玉君 直击面板数据模型 [免费公开课,2小时]
  • Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。

课程主页

课程主页

关于我们

  • Stata连享会 由中山大学连玉君老师团队创办,定期分享实证分析经验。
  • 连享会-主页知乎专栏,700+ 推文,实证分析不再抓狂。直播间 有很多视频课程,可以随时观看。
  • 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标,输入简要关键词,以便快速呈现历史推文,获取工具软件和数据下载。常见关键词:课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法

连享会小程序:扫一扫,看推文,看视频……

扫码加入连享会微信群,提问交流更方便

✏ 连享会-常见问题解答:
https://gitee.com/lianxh/Course/wikis

New! lianxhsongbl 命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh