[关闭]
@fanxy 2018-03-06T18:07:53.000000Z 字数 8489 阅读 2659

第一讲 导论与经济增长

樊潇彦 复旦大学经济学院 中级宏观


Ch01_导论与经济增长.rar3855.4kB

1. 准备工作

  1. # 安装程序包
  2. install.packages("dplyr") # 读取和整理数据
  3. install.packages("tidyr")
  4. install.packages("data.table")
  5. install.packages("foreign")
  6. install.packages("readstata13")
  7. install.packages("haven")
  8. install.packages("ggplot2") # 作图
  9. install.packages("ggrepel")
  10. install.packages("dygraphs")
  11. install.packages("plotrix")
  12. install.packages("lubridate") # 处理时序数据
  13. install.packages("zoo")
  14. install.packages("mFilter")
  15. # 调用程序包
  16. library(dplyr)
  17. library(tidyr)
  18. library(data.table)
  19. library(foreign)
  20. library(readstata13)
  21. library(haven) # 有空字符时,用write_dta生成.dta
  22. library(ggplot2)
  23. library(ggrepel)
  24. library(dygraphs) # 动态作图
  25. library(plotrix) # 双轴作图
  26. library(lubridate) # 可以用year()、month()等提取日期
  27. library(zoo) # 处理时序数据
  28. library(mFilter) # HP滤波
  29. # 设定工作目录
  30. setwd("D:\\...") # 设定工作目录
  31. rm(list=ls()) # 清内存

2. 总量趋势与结构

  1. # 数据读取和整理
  2. gdp=fread("GDP.csv",header=T) # 读取数据
  3. str(gdp) # 查看数据结构
  4. gdp_var=gdp%>%select(varid,varname,var) # 指标一览
  5. gdp=gdp%>%
  6. select(-varid,-varname)%>% # 选择除varid和varname之外的所有指标
  7. gather(year,value,-var)%>% # 将列指标1952-2015按年整合
  8. mutate(year=as.numeric(year))%>% # 将字符型指标转换为数字型
  9. filter(!is.na(value))%>% # 选取value不为空值的样本
  10. arrange(var,year)%>% # 按var和year排序
  11. spread(var,value) # 按var展开为列指标
  12. head(gdp) # 查看前6个样本

2.1 数据分解

  1. # 计算对数实际人均GDP并分解
  2. gdppa=gdp%>%
  3. select(year,gdppa,gdppaid)%>%
  4. mutate(gdppaid=ifelse(year==1952,100,gdppaid)) %>%
  5. mutate(realgdppa_1952=gdp$gdppa[gdp$year==1952]*cumprod(gdppaid/100))%>%
  6. mutate(realgdppa_2015=realgdppa_1952/realgdppa_1952[gdp$year==2015]*gdppa[gdp$year==2015])
  7. gdppa_hp=hpfilter(log(gdppa$realgdppa_2015),drift=F, # 用hp滤波提取趋势和周期
  8. freq=100,type="lambda") # 年度数据lambda=100,季度和月度分别为1600和14400
  9. gdppa_hp=data.frame(year=gdppa$year, trend=gdppa_hp$trend,cycle=gdppa_hp$cycle)
  10. twoord.plot(lx = gdppa_hp$year, ly = gdppa_hp$trend,
  11. rx = gdppa_hp$year, ry = gdppa_hp$cycle,
  12. main = '对数实际人均GDP:1952-2015', xlab = '',
  13. ylab = '趋势', rylab = '周期',
  14. type = c('line','line'))

2.2 结构分析

  1. # 生产结构和支出结构
  2. gdpstr=gdp%>%
  3. mutate(sec1=gdp1/gdp)%>%
  4. mutate(sec2=gdp2/gdp)%>%
  5. mutate(sec3=gdp3/gdp)%>%
  6. mutate(con=gdpexpcpri/gdpexp)%>%
  7. mutate(inv=gdpexpi/gdpexp)%>%
  8. mutate(gov=gdpexpcgov/gdpexp)%>%
  9. select(year,sec1:gov)%>%
  10. gather(var,share,-year)
  11. ggplot(gdpstr[gdpstr$var %in% c("sec1","sec2","sec3"),],aes(year,share,color=var))+
  12. geom_line(size=1)+ labs(title="生产法GDP结构",x="",y="")+
  13. scale_colour_discrete(labels = c('第一产业','第二产业','第三产业'))+
  14. guides(color = guide_legend(title = NULL)) + theme_bw()+ theme(legend.position = 'bottom')
  15. ggplot(gdpstr[gdpstr$var %in% c("con","inv","gov"),],aes(year, share, color=var))+
  16. geom_line(size=1)+ labs(title="支出法GDP结构",x="",y="")+
  17. scale_colour_discrete(labels = c('居民消费','政府支出','资本形成'))+
  18. guides(color = guide_legend(title = NULL)) + theme_bw()+
  19. theme(legend.position = 'bottom')
  20. # 收入结构
  21. gdpinc=fread("GDP_inc.csv",header=T)
  22. gdpinc_var=gdpinc%>% select(varid,reg,var)
  23. reginfo=data.frame(reg=unique(gdpinc_var$reg))
  24. str(reginfo)
  25. reginfo=reginfo%>%
  26. mutate(reg=as.character(reg))%>%
  27. mutate(regno=31:1)%>%arrange(regno)%>%
  28. mutate(reg6=rep(c("华北","东北","华东","华南","西南","西北"),times=c(5,3,7,6,5,5)))%>%
  29. mutate(reg6no=rep(c(10,20,30,40,50,60),times=c(5,3,7,6,5,5)))
  30. gdpinc=gdpinc%>%
  31. select(-varid)%>%
  32. gather(year,value,-reg,-var)%>%
  33. spread(var,value)
  34. gdpreg_inc=gdpinc%>% # 计算分省的收入结构
  35. mutate(dep=`固定资产折旧`/`收入法`)%>%
  36. mutate(lab=`劳动者报酬`/`收入法`)%>%
  37. mutate(tax=`生产税净额`/`收入法`)%>%
  38. mutate(pro=`营业盈余`/`收入法`)%>%
  39. select(reg,year,dep:pro)%>%
  40. gather(var,share,-reg,-year)
  41. a=gdpinc%>% # 计算全国的
  42. group_by(year)%>%
  43. mutate(dep=sum(`固定资产折旧`)/sum(`收入法`))%>%
  44. mutate(lab=sum(`劳动者报酬`)/sum(`收入法`))%>%
  45. mutate(tax=sum(`生产税净额`)/sum(`收入法`))%>%
  46. mutate(pro=sum(`营业盈余`)/sum(`收入法`))%>%
  47. select(reg,year,dep:pro)%>%
  48. filter(reg=="北京")%>%
  49. mutate(reg="全国")%>%
  50. gather(var,share,-reg,-year)
  51. a=data.frame(a) # 将数据格式设定为data.frame
  52. gdpinc=rbind(gdpreg_inc,a) # 省与全国合并
  53. gdpinc=gdpinc%>%
  54. left_join(reginfo,by="reg")%>%
  55. mutate(regno=ifelse(reg=="全国",0,regno))%>%
  56. mutate(reg6no=ifelse(reg=="全国",0,reg6no))%>%
  57. mutate(reg6=ifelse(reg=="全国",reg,reg6))%>%
  58. arrange(var,year,regno)
  59. ggplot(gdpinc[gdpinc$regno==0,],aes(year,share,group=var,color=var))+geom_line(size=1)+
  60. labs(title="收入法GDP结构",x="",y="")+
  61. scale_colour_manual(values= c("dep"="grey","lab"="blue","pro"="red","tax"="purple"),
  62. labels = c('固定资产折旧','劳动者报酬','营业盈余','生产税净额'))+
  63. guides(color = guide_legend(title = NULL)) +
  64. theme_bw()+ theme(legend.position = 'bottom')

3. 区域增长与结构

  1. gdpreg=fread("GDP_reg.csv",header=T)
  2. gdpreg_var=gdpreg%>%select(varid,var,reg)

3.1 收敛

  1. gdppareg=gdpreg%>%
  2. filter(var=="GDP指数")%>%
  3. select(-varid,-var)%>%
  4. gather(year,idx,-reg)%>%
  5. arrange(reg,year)%>%
  6. group_by(reg)%>%
  7. mutate(gdpidx=cumprod(idx/100))
  8. gdppa1978=data.frame(regno=1:31,reg=gdpreg$reg[gdpreg$var=="人均GDP"],
  9. gdppa=gdpreg$`1978`[gdpreg$var=="人均GDP"])
  10. a=merge(gdppareg,gdppa1978,by="reg",all.x=T)%>%
  11. arrange(regno,year)%>%
  12. filter(year==1978)%>%
  13. rename(gdpidx1978=gdpidx)%>%
  14. rename(gdppa1978=gdppa)%>%
  15. select(reg,regno,gdpidx1978,gdppa1978)
  16. gdppareg=merge(gdppareg,a,by="reg",all.x=T)
  17. gdppareg=gdppareg%>%
  18. mutate(gdppa=gdpidx/gdpidx1978*gdppa1978)%>%
  19. select(year,regno,reg,gdppa)%>%
  20. group_by(year)%>%
  21. mutate(gdppa_rel=gdppa/max(gdppa))%>%
  22. arrange(year,regno)
  23. table(gdppareg$reg[gdppareg$gdppa_rel==1]) # 检查每年人均GDP最高的地区,全是上海
  24. g_level=gdppareg%>%
  25. filter(year %in% c(1978,2015))%>%
  26. select(-gdppa)%>%
  27. spread(year,gdppa_rel)%>%
  28. mutate(rank78=n()+1-min_rank(`1978`))%>%
  29. mutate(rank15=n()+1-min_rank(`2015`))%>%
  30. mutate(top5=ifelse(rank78<=6 | rank15<=6, reg, ""))
  31. ggplot(g_level,aes(`1978`,`2015`,color=regno))+geom_point()+
  32. geom_abline(intercept = 0, slope = 1, color="red")+
  33. geom_text(aes(`1978`,`2015`, label=top5, hjust=0.5, vjust=-0.5))+
  34. labs(title = "各地区实际人均GDP相对于上海的变化",x="1978年相对于上海",y="2015年相对于上海")+
  35. theme_bw()
  36. g_gr=gdppareg%>%
  37. filter(year %in% c(1978,1996,1997,2015))%>%
  38. select(-gdppa_rel)%>%
  39. spread(year,gdppa)%>%
  40. mutate(gr7896=exp(log(`1996`/`1978`)/18)-1)%>%
  41. mutate(gr9715=exp(log(`2015`/`1997`)/18)-1)%>%
  42. mutate(rank1=n()+1-min_rank(gr7896))%>%
  43. mutate(rank2=n()+1-min_rank(gr9715))%>%
  44. mutate(top5=ifelse(rank1<=5 | rank2<=5, reg, ""))%>%
  45. select(regno,reg,gr7896,gr9715,top5)
  46. ggplot(g_gr,aes(gr7896,gr9715,color=regno))+geom_point()+
  47. geom_abline(intercept = 0, slope = 1, color="red")+
  48. geom_text_repel(aes(label = reg))+
  49. ylim(0.06,0.15) +xlim(0.06,0.15)+
  50. labs(title = "各地区实际人均GDP增长率的变化",x="1978-1996年",y="1997-2015年")+
  51. theme_bw()
  52. g_conv=gdppareg%>%
  53. filter(year %in% c(1978,2015))%>%
  54. select(-gdppa_rel)%>%
  55. spread(year,gdppa)%>%
  56. mutate(gr=exp(log(`2015`/`1978`)/37)-1)%>%
  57. mutate(rank=n()+1-min_rank(gr))%>%
  58. mutate(top5=ifelse(rank<=5, reg, ""))%>%
  59. select(regno,reg,`1978`,gr,top5)
  60. ggplot(g_conv,aes(log(`1978`),gr,color=regno))+geom_point()+geom_smooth(method="lm")+
  61. geom_text_repel(aes(label = reg))+
  62. labs(title = "地区收敛(含直辖市)",x="1978年对数实际GDP",y="1978-2015年年均增长率")+
  63. theme_bw()
  64. ggplot(g_conv[!g_conv$reg %in% c("北京","天津","上海","重庆"),],
  65. aes(log(`1978`),gr,color=regno))+geom_point()+geom_smooth(method="lm")+
  66. geom_text_repel(aes(label = reg))+
  67. labs(title = "地区收敛(不含直辖市)",x="1978年对数实际GDP",y="1978-2015年年均增长率")+
  68. theme_bw()
  69. reg=lm(gr~log(`1978`),data=g_conv) # OLS估计
  70. summary(reg)

3.2 结构

  1. # 产出
  2. gdpstrreg1=gdpreg%>%
  3. select(-varid)%>%
  4. filter(var %in% c("GDP","第一产业","第二产业","第三产业"))%>%
  5. gather(year,value,-reg,-var)%>%
  6. filter(year>=1978)%>%
  7. spread(var,value)%>%
  8. mutate(Sector1=`第一产业`/GDP)%>%
  9. mutate(Sector2=`第二产业`/GDP)%>%
  10. mutate(Sector3=`第三产业`/GDP)%>%
  11. select(year,reg,Sector1:Sector3)%>%
  12. gather(var,share,-reg,-year)%>%
  13. left_join(reginfo,by="reg")%>%
  14. mutate(year=as.numeric(year))%>%
  15. arrange(regno,year,var)
  16. ggplot(gdpstrreg1,aes(year,share,color=reg6))+geom_point()+facet_wrap(~var,ncol=3)+
  17. scale_x_continuous(breaks=seq(1980, 2015, 5))+
  18. guides(color = guide_legend(title = NULL)) +
  19. labs(title="各地区产业结构变动:1978-2015",x="",y="")+
  20. theme_bw()+ theme(legend.position = 'bottom')
  21. # 支出
  22. gdpstrreg2=gdpreg%>%
  23. select(-varid)%>%
  24. filter(var %in% c("支出法","最终消费支出","资本形成总额"))%>%
  25. gather(year,value,-reg,-var)%>%
  26. filter(year>=1993 & year<=2014)%>%
  27. spread(var,value)%>%
  28. mutate(`最终消费`=`最终消费支出`/`支出法`)%>%
  29. mutate(`资本形成`=`资本形成总额`/`支出法`)%>%
  30. select(year,reg,`最终消费`,`资本形成`)%>%
  31. gather(var,share,-reg,-year)%>%
  32. left_join(reginfo,by="reg")%>%
  33. mutate(year=as.numeric(year))%>%
  34. arrange(regno,year,var)
  35. ggplot(gdpstrreg2,aes(year,share,color=reg6))+geom_point()+facet_wrap(~var,ncol=2)+
  36. scale_x_continuous(breaks=seq(1995, 2015, 5))+
  37. guides(color = guide_legend(title = NULL))+
  38. labs(title="各地区支出结构变动:1993-2014",x="",y="")+
  39. theme_bw()+ theme(legend.position = 'bottom')
  40. # 收入
  41. gdpstrreg3=gdpinc%>%
  42. select(regno,year,var,share)%>%
  43. filter(regno>0)%>%
  44. spread(var,share)%>%
  45. rename(`固定资产折旧`=dep)%>%
  46. rename(`劳动者报酬`=lab)%>%
  47. rename(`营业盈余`=pro)%>%
  48. rename(`生产税净额`=tax)%>%
  49. gather(var,share,-regno,-year)%>%
  50. mutate(year=as.numeric(year))%>%
  51. left_join(reginfo,by="regno")
  52. ggplot(gdpstrreg3,aes(year,share,color=reg6))+geom_point()+facet_wrap(~var,ncol=2)+
  53. scale_x_continuous(breaks=seq(1995, 2015, 5))+
  54. guides(color = guide_legend(title = NULL))+
  55. labs(title="各地区收入结构变动:1993-2014",x="",y="")+
  56. theme_bw()+ theme(legend.position = 'bottom')
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注