[关闭]
@fanxy 2016-11-09T08:45:49.000000Z 字数 3930 阅读 2177

国际金融习题2:数据处理与应用

樊潇彦 复旦大学经济学院


1. 准备工作

  1. # 用 install.packages("**") 安装命令包后调用:
  2. library(readxl) # 读取excel数据
  3. library(stringr) # 字符串处理
  4. library(corrplot)
  5. library(igraph)
  6. library(forecast)
  7. library(stats) # 基础包,不用安装直接调用
  8. library(dplyr)
  9. library(tidyr)
  10. library(data.table)
  11. library(foreign)
  12. library(readstata13)
  13. library(haven)
  14. library(ggplot2)
  15. library(ggrepel)
  16. library(dygraphs)
  17. library(plotrix)
  18. library(lubridate)
  19. library(zoo)
  20. library(mFilter)
  21. library(RCurl)
  22. library(httr)
  23. set_config( config( ssl_verifypeer = 0L ))
  24. library(Quandl)
  25. Quandl.api_key("...") # 在Quandl(https://www.quandl.com)免费申请 api_key
  26. setwd("D:\\...") # 设置工作目录
  27. rm(list=ls()) # 清内存

2. 国际清算银行(BIS)有效汇率

从国际清算银行网页(http://www.bis.org/statistics/full_data_sets.htm)下载有效汇率数据 "Effective exchange rate indices (monthly).csv",整理后取61个国家和地区加权的名义有效汇率做图:

image_1b11k607j8qr60v1o8beaflm13.png-327.9kB
image_1b11k9vb71ue72698mvev40u1g.png-37.4kB

3. 从Quandl下载BRICS数据

在 Quandl 注册一个免费帐号,从 https://www.quandl.com/collections/economics 下载各类经济数据。

3.1 货币兑美元汇率

  1. currency=c("BRL","ZAR","CNY","RUB","INR")
  2. currency_name=data.frame(currency=currency,
  3. name=c("巴西-雷亚尔","南非-兰特","中国-元","俄罗斯-卢布","印度-卢比"),
  4. stringsAsFactors =F)
  5. list=paste("CURRFX/USD",currency,sep="") # Currency Exchange Rates - USD
  6. E_daily=data.frame()
  7. for (i in 1:length(currency)){
  8. E=Quandl(list[i], start_date="1970-01-01")
  9. E=E%>% select(Date,Rate)%>%mutate(currency=currency[i])
  10. E_daily=rbind(E_daily,E)
  11. }
  12. E_daily=E_daily%>%
  13. left_join(currency_name,by="currency")%>%
  14. mutate(label=ifelse(Date==as.Date("2013-06-04"),name,""))
  15. ggplot(E_daily[E_daily$Rate<1000,],aes(Date,Rate,color=name))+geom_line()+
  16. labs(title="金砖五国货币兑美元汇率",x="",y="")+
  17. geom_text(aes(label=label,vjust =-0.5))+
  18. guides(color=guide_legend(title=NULL))+theme_bw()+theme(legend.position="non")

image_1b11jscv4oqtpl43eenm0105q9.png-286.9kB

3.2 宏观经济数据

  1. country=c("USA","BRA","CHN","IND","RUS","ZAF")
  2. list_pi=paste("WORLDBANK/",country,"_NY_GDP_DEFL_KD_ZG", sep="") # Inflation, GDP deflator (annual %)
  3. list_g=paste("ODA/",country,"_NGDP_RPCH",sep="") # GDP at Constant Prices, % change
  4. list_M2NGDP=paste("WORLDBANK/",country,"_FM_LBL_MQMY_GD_ZS",sep="") # Money and quasi money (M2) as % of GDP
  5. list_NGDP=paste("ODA/",country,"_NGDP", sep="") # GDP at Current Prices, LCU Billions
  6. list_i=paste("WORLDBANK/",country,"_FR_INR_LEND",sep="") # Lending interest rate (%) 存款利率部分国家没有
  7. list_ca=paste("ODA/",country,"_BCA_NGDPD",sep="") # Current Account Balance, % of GDP
  8. list_var=c("pi","g","M2NGDP","NGDP","lendi","ca")
  9. Data=data.frame()
  10. for (i in 1:length(country)){
  11. pi=Quandl(list_pi[i])
  12. g=Quandl(list_g[i])
  13. M2NGDP=Quandl(list_M2NGDP[i])
  14. NGDP=Quandl(list_NGDP[i])
  15. lendi=Quandl(list_i[i])
  16. ca=Quandl(list_ca[i])
  17. var=rep(list_var,time=c(nrow(pi),nrow(g),nrow(M2NGDP),nrow(NGDP),nrow(lendi),nrow(ca)))
  18. D=rbind(pi,g,M2NGDP,NGDP,lendi,ca)
  19. Data=rbind(Data,data.frame(D,var=var,country=rep(country[i],nrow(D))))
  20. rm(list=list_var)
  21. }

3.3 数据合并

  1. # save(E_daily,Data,file="BRICS.RData") # 上述数据下载较慢,已另存
  2. load("BRICS.Rdata") # 直接加载
  3. cc=data.frame(currency=c("USD","BRL","CNY","INR","RUB","ZAR"),
  4. country=c("USA","BRA","CHN","IND","RUS","ZAF"),stringsAsFactors = F)
  5. E=E_daily%>%
  6. mutate(year=as.numeric(year(Date)))%>%
  7. group_by(currency,year)%>%
  8. mutate(rk=n()+1-min_rank(Date))%>%
  9. filter(rk==n())%>%
  10. select(year,currency,Rate,name)%>%
  11. mutate(id=paste(year,currency,sep=""))
  12. Data=Data%>%
  13. mutate(year=as.numeric(year(Date)))%>%
  14. select(-Date)%>%
  15. spread(var,Value)%>%
  16. left_join(cc,by="country")%>%
  17. mutate(id=paste(year,currency,sep=""))%>%
  18. left_join(E,by="id")%>%
  19. select(-year.y,-currency.y,-id)%>%
  20. rename(year=year.x)%>%
  21. rename(currency=currency.x)%>%
  22. mutate(M2=M2NGDP*NGDP)%>%
  23. arrange(country,year)%>%
  24. group_by(country)%>%
  25. mutate(gE=lead(Rate)/Rate-1)%>%
  26. mutate(gM2=M2/lag(M2)-1)%>%
  27. mutate(i_us=ifelse(country=="USA",lendi,NA))%>%
  28. mutate(ca_us=ifelse(country=="USA",ca,NA))%>%
  29. mutate(pi_us=ifelse(country=="USA",pi,NA))%>%
  30. group_by(year)%>%
  31. mutate(diffi=lendi-max(i_us,na.rm=T))%>%
  32. mutate(diffca=ca-max(ca_us,na.rm=T))%>%
  33. mutate(diffpi=pi-max(pi_us,na.rm=T))

4. 用BRICS数据检验汇率决定理论

  1. regPPP=lm(gE~gM2+g-1,data=Data)
  2. summary(regPPP)
  3. regpi=lm(gE~diffpi-1,data=Data)
  4. summary(regpi)
  5. regi=lm(gE~diffi-1,data=Data)
  6. summary(regi)
  7. regca=lm(gE~diffca-1,data=Data)
  8. summary(regca)

image_1b11k0afd1plg19ff1ph8ia3kkam.png-289.9kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注