library(tidyverse)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
## Registered S3 method overwritten by 'rvest':
##   method            from
##   read_xml.response xml2
## -- Attaching packages --------------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.1.1       √ purrr   0.3.2  
## √ tibble  2.1.1       √ dplyr   0.8.0.1
## √ tidyr   0.8.3       √ stringr 1.4.0  
## √ readr   1.3.1       √ forcats 0.4.0
## -- Conflicts ------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(RCurl)
## Loading required package: bitops
## 
## Attaching package: 'RCurl'
## The following object is masked from 'package:tidyr':
## 
##     complete
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
library(readxl)
library(zoo)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(ggthemes)
download.file("http://www.rieti.go.jp/jp/database/policyuncertainty/data/japan_policy_uncertainty_data.xlsx",
  "Jp_Ucs.xlsx",
  mode = "wb"
)
tblUncertain.JP <- read_excel("Jp_Ucs.xlsx", range = "A2:F1000", skip = 1) %>%
  dplyr::mutate(stat_date = as.Date(as.yearmon(stringr::str_c(Year, "-", Month)))) %>%
  select(-Year, -Month) %>%
  print() %>%
  gather("index_name", "value", -stat_date) %>%
  mutate(index_name = str_remove(index_name, " Policy Uncertainty Index$")) %>%
  mutate(country = "JPN") %>%
  print()
## # A tibble: 998 x 5
##    `Economic Polic~ `Fiscal Policy ~ `Monetary Polic~ `Trade Policy U~
##               <dbl>            <dbl>            <dbl>            <dbl>
##  1             69.2             87.2             54.5             70.8
##  2             91.6            102.              52.5             75.1
##  3             91.5            112.              73.0            194. 
##  4             94.5             97.7             91.6            180. 
##  5             75.2             92.8             52.7            142. 
##  6             84.5            104.              72.1            144. 
##  7             61.1             73.0             20.8             55.1
##  8             46.0             47.4             53.8             71.8
##  9             61.2             60.9             73.1             91.2
## 10             98.0            111.             165.             117. 
## # ... with 988 more rows, and 1 more variable: stat_date <date>
## # A tibble: 3,992 x 4
##    stat_date  index_name value country
##    <date>     <chr>      <dbl> <chr>  
##  1 1987-01-01 Economic    69.2 JPN    
##  2 1987-02-01 Economic    91.6 JPN    
##  3 1987-03-01 Economic    91.5 JPN    
##  4 1987-04-01 Economic    94.5 JPN    
##  5 1987-05-01 Economic    75.2 JPN    
##  6 1987-06-01 Economic    84.5 JPN    
##  7 1987-07-01 Economic    61.1 JPN    
##  8 1987-08-01 Economic    46.0 JPN    
##  9 1987-09-01 Economic    61.2 JPN    
## 10 1987-10-01 Economic    98.0 JPN    
## # ... with 3,982 more rows
ggplotUncatin.JP <- tblUncertain.JP %>%
  group_by(index_name) %>%
  filter(stat_date > "2008-01-01" & index_name == "Economic") %>%
  ggplot(mapping = aes(x = stat_date, y = value, colour = index_name)) +
  geom_point() + geom_line() + geom_tile()
ggplotUncatin.JP %>% print()

download.file("https://www.policyuncertainty.com/media/US_Policy_Uncertainty_Data.xlsx",
  "Us_Ucn.xlsx",
  mode = "wb"
)
tblUncertain.US <- read_excel("Us_Ucn.xlsx",  skip = 0) %>%
  dplyr::mutate(stat_date = as.Date(as.yearmon(stringr::str_c(Year, "-", Month)))) %>%
  select(-Year, -Month) %>%
  print() %>%
  gather("index_name", "value", -stat_date) %>%
  mutate(index_name = str_remove(index_name, " Policy Uncertainty Index$")) %>%
  mutate(index_name=if_else(index_name=="News_Based_Policy_Uncert_Index",
                            "Policy",index_name)) %>%
  mutate(country = "USA") %>%
  print()
## # A tibble: 415 x 3
##    Three_Component_Index News_Based_Policy_Uncert_Index stat_date 
##                    <dbl>                          <dbl> <date>    
##  1                 125.                           104.  1985-01-01
##  2                  99.0                           78.3 1985-02-01
##  3                 112.                           101.  1985-03-01
##  4                 103.                            84.8 1985-04-01
##  5                 120.                            98.1 1985-05-01
##  6                 133.                           120.  1985-06-01
##  7                 128.                           111.  1985-07-01
##  8                 127.                           111.  1985-08-01
##  9                 127.                           111.  1985-09-01
## 10                 116.                            93.1 1985-10-01
## # ... with 405 more rows
## # A tibble: 830 x 4
##    stat_date  index_name            value country
##    <date>     <chr>                 <dbl> <chr>  
##  1 1985-01-01 Three_Component_Index 125.  USA    
##  2 1985-02-01 Three_Component_Index  99.0 USA    
##  3 1985-03-01 Three_Component_Index 112.  USA    
##  4 1985-04-01 Three_Component_Index 103.  USA    
##  5 1985-05-01 Three_Component_Index 120.  USA    
##  6 1985-06-01 Three_Component_Index 133.  USA    
##  7 1985-07-01 Three_Component_Index 128.  USA    
##  8 1985-08-01 Three_Component_Index 127.  USA    
##  9 1985-09-01 Three_Component_Index 127.  USA    
## 10 1985-10-01 Three_Component_Index 116.  USA    
## # ... with 820 more rows
tblUncertain <- bind_rows(tblUncertain.US, tblUncertain.JP) %>%
  group_by(country, index_name) %>% 
  filter(stat_date > "2014-01-01" & index_name %in%c("Economic","Policy"))

ggplot.Uncern <- tblUncertain %>%
  arrange(stat_date) %>% 
  #group_by(index_name,country) %>%
  #filter(stat_date > "2014-01-01" & index_name %in%c("Economic","Policy")) %>%
  ggplot(mapping = aes(x = stat_date, y = value ,colour=country )) +
  geom_point() + geom_line()  +theme_economist() +scale_colour_solarized()+
  xlab("")+
  ggtitle ("政策不確実性指数")
ggplot.Uncern %>% print()