@fanxy
2016-09-27T17:35:01.000000Z
字数 3291
阅读 1880
樊潇彦
复旦大学经济学院
中级宏观
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:\\") # 设定工作目录
total=read_excel("../Ch03/Ch03_Data.xls",sheet="total",skip=1)
local_inc=total%>%
mutate(year=as.numeric(year(`指标名称`)))%>%
mutate(local_inc=`国家财政收入`*(100-`中央财政收入:占比`)/100)%>%
select(year,local_inc)
land=read_excel("Ch04_Data.xlsx",sheet="land")
str(land)
land=land%>%
filter(`单位` %in% c("万元") & !grepl('招拍挂|协议',`指标名称`))%>%
select(-`单位`,-`指标ID`)%>%
rename(region=`指标名称`)%>%
gather(year,value,-region)%>%
mutate(year=as.numeric(year))%>%
filter(!is.na(value))%>%
mutate(value=value/10000)%>%
mutate(region=sub("土地出让成交价款:","",region))%>%
left_join(local_inc,by="year")%>%
mutate(land2inc=ifelse(region=="全国",value/local_inc,NA))%>%
select(-local_inc)%>%
arrange(year,value)
g_land=land[land$region=="全国",]
twoord.plot(lx = g_land$year, ly = g_land$value,
rx = g_land$year, ry = g_land$land2inc,
main = '土地财政', xlab = '',
ylab = '土地出让收入(万元)', rylab = '占地方财政收入之比',
type = c('line','line'))
ggplot(land[!land$region %in% c("全国","西藏"),],aes(year,value)) +
geom_line()+facet_wrap(~region,ncol=5)+
labs(title = "各地区土地出让收入(万元)",x="",y="")+
theme_bw()
debt_mm=read_excel("Ch04_Data.xlsx",sheet="debt_mm",col_types=c("date",rep("numeric",5)))
debt_mm=debt_mm%>%
mutate(time=as.Date(`日期`))%>%
select(-`日期`,-`总计`)%>%
gather(var,value,-time)%>%
filter(!is.na(value))
ggplot(debt_mm,aes(time,value,fill=var))+
geom_bar(stat="identity")+
labs(title="地方债发行:按类别分月度数据",x="",y="")+
guides(fill=guide_legend(title=NULL))+
theme_bw()+theme(legend.position="bottom")
cen_loc=read_excel("Ch04_Data.xlsx",sheet="cen_loc",skip=1)
str(cen_loc)
cen_loc=cen_loc%>%
rename(time=`指标名称`)%>%
gather(var,i,-time)%>%
filter(!is.na(i))%>%
mutate(type=substr(var,5,6))%>%
mutate(type=ifelse(type=="地方","地方债",type))%>%
mutate(term=sub(".*:","",var))%>%
select(-var)
ggplot(cen_loc,aes(time,i,color=type))+geom_point()+geom_smooth()+
facet_wrap(~term,ncol=3)+labs(title="不同期限地方债和国债发行利率",x="",y="")+
guides(color = guide_legend(title = NULL)) +
theme_bw()+ theme(legend.position = 'bottom')
cityinv=read_excel("Ch04_Data.xlsx",sheet="cityinv",skip=1,
col_types=c("date",rep("numeric",226)))
str(cityinv)
cityinv=cityinv%>%
rename(time=`指标名称`)%>%
gather(var,i,-time)%>%
filter(!is.na(i))%>%
mutate(market=substr(var,1,2))%>%
mutate(type=sub(".*城投债","",var))%>%
mutate(type=sub("收益率.*","",type))%>%
mutate(grade=sub(".*\\(","",var))%>%
mutate(grade=sub("\\).*","",grade))%>%
mutate(term=sub(".*:","",var))%>%
select(-var)
# 查看数据
summary(cityinv$time[cityinv$market=="银行"]) # 银行间市场从2012-01-09开始
summary(cityinv$time[cityinv$market=="中债"]) # 中债从2008-08-13开始
table(cityinv$market,cityinv$grade) # 中债评级有AAA
table(cityinv$market,cityinv$type) # 银行间有远期收益率
table(cityinv$market,cityinv$term) # 中债6个月标记为0年,银行间没有9年
g=cityinv%>%
filter(market=="中债" & type=="到期" &
grade %in% c("AA-","AAA") & term %in% c("1年","3年","10年"))
ggplot(g,aes(time,i,color=term))+geom_line()+facet_wrap(~grade,ncol=2)+
labs(title="中债城投债到期收益率",x="",y="")+
guides(color=guide_legend(title=NULL))+
theme_bw()+theme(legend.position="bottom")