@fanxy
2016-10-28T18:38:45.000000Z
字数 2390
阅读 1661
樊潇彦
复旦大学经济学院
中级宏观
library(readxl) # 读取excel数据
library(stringr) # 字符串处理
library(corrplot)
library(igraph)
library(forecast)
library(stats) # 基础包,不用安装直接调用
library(dplyr)
library(tidyr)
library(data.table)
library(foreign)
library(readstata13)
library(haven)
library(ggplot2)
library(ggrepel)
library(dygraphs)
library(plotrix)
library(lubridate)
library(zoo)
library(mFilter)
setwd("D:\\") # 设定工作目录
m_us=read_excel("Ch07_Data.xls",sheet="m_us",skip=2)
colnames(m_us)=c("year","g_gdp","pi","m1","m2")
m_us=m_us%>%filter(!is.na(`g_gdp`))%>%
mutate(year=as.numeric(year(year)))%>%
mutate(g_m1=m1/lag(m1)*100-100)%>%
mutate(g_m2=m2/lag(m2)*100-100)%>%
mutate(diff1=g_m1-pi-g_gdp)%>%
mutate(diff2=g_m2-pi-g_gdp)%>%
gather(var,value,-year)%>%
filter(!is.na(value) & !var%in%c("m1","m2"))%>%
mutate(label=ifelse(year==1986,var,""))
sd(m_us$value[m_us$var=="diff2"]) # 3.62
mean(m_us$value[m_us$var=="diff2"]) # -0.03
ggplot(m_us[m_us$var=="diff2",],
aes(year,value))+geom_line(size=1)+geom_smooth()+
scale_x_continuous(breaks = seq(1960,2015,5))+
geom_vline(xintercept =c(1983,1994,2007), ,linetype = "dotdash",col="red")+
labs(title="美国货币供给的弗里德曼规则",x="",y="")+
guides(color=guide_legend(title=NULL))+theme_bw()+ theme(legend.position="non")
basic_i=read_excel("Ch07_Data.xls",sheet="basic_i",skip=2)
basic_i=basic_i%>%
mutate(time=as.yearmon(`指标名称`))%>%
select(-`指标名称`)%>%
gather(var,value,-time)%>%
mutate(var=sub("[(]月[)]","",var))%>%
mutate(label=ifelse(time==as.yearmon("2015-01"),var,""))
ggplot(basic_i,aes(time,value,color=var))+geom_line(size=1)+
labs(title="存款准备金率与存贷款基准利率",x="",y="")+
scale_x_continuous(breaks = year(seq(as.Date("1985/1/1"),as.Date("2016/4/1"),"3 years")))+
geom_text(aes(label=label,hjust =1))+
guides(color=guide_legend(title=NULL))+theme_bw()+theme(legend.position="non")
mm_i=read_excel("Ch07_Data.xls",sheet="mm_i",skip=2)
mm_i=mm_i%>%
rename(time=`指标名称`)%>%
gather(market,value,-time)%>%
filter(!is.na(value))%>%
mutate(period=sub(".*:","",market))%>%
mutate(market=sub(":.*","",market))%>%
mutate(period=sub("7天","1周",period))%>%
mutate(period=sub("1年","一年",period))%>%
filter(period %in% c("1周","3个月","一年"))
ggplot(mm_i,aes(time,value,color=period))+geom_line(size=0.8)+facet_wrap(~market)+
labs(title="SHIBOR和银行间质押式回购加权利率",x="",y="")+
guides(color=guide_legend(title=NULL))+theme_bw()+theme(legend.position="bottom")