1 導入編

はじめに

  • {tidyquant}は金融データを分析するために以下のパッケージを統合したもの
    • {TTR} {xts} {zoo} {quantmod} {PerformanceAnalytics}
  • {tidyverse}とも統合することでシームレスなデータ分析が可能
  • {ggplot2}とも連携することでグラフィックスも充実
  • 参考資料:Introduction to tidyquant



登録関数

# 登録関数の数
ls("package:tidyquant") %>% length()
## [1] 45
# 登録関数の数
ls("package:tidyquant") %>% print()
##  [1] "as_tibble"                  "as_xts"                    
##  [3] "as_xts_"                    "coord_x_date"              
##  [5] "coord_x_datetime"           "FANG"                      
##  [7] "geom_barchart"              "geom_bbands"               
##  [9] "geom_bbands_"               "geom_candlestick"          
## [11] "geom_ma"                    "geom_ma_"                  
## [13] "palette_dark"               "palette_green"             
## [15] "palette_light"              "scale_color_tq"            
## [17] "scale_fill_tq"              "theme_tq"                  
## [19] "theme_tq_dark"              "theme_tq_green"            
## [21] "tq_exchange"                "tq_exchange_options"       
## [23] "tq_get"                     "tq_get_options"            
## [25] "tq_get_stock_index_options" "tq_index"                  
## [27] "tq_index_options"           "tq_mutate"                 
## [29] "tq_mutate_"                 "tq_mutate_fun_options"     
## [31] "tq_mutate_xy"               "tq_mutate_xy_"             
## [33] "tq_performance"             "tq_performance_"           
## [35] "tq_performance_fun_options" "tq_portfolio"              
## [37] "tq_portfolio_"              "tq_repeat_df"              
## [39] "tq_transform"               "tq_transform_xy"           
## [41] "tq_transmute"               "tq_transmute_"             
## [43] "tq_transmute_fun_options"   "tq_transmute_xy"           
## [45] "tq_transmute_xy_"



組込データ

# データセットの確認
FANG %>% print()
## # A tibble: 4,032 × 8
##    symbol       date  open  high   low close    volume adjusted
##     <chr>     <date> <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
## 1      FB 2013-01-02 27.44 28.18 27.42 28.00  69846400    28.00
## 2      FB 2013-01-03 27.88 28.47 27.59 27.77  63140600    27.77
## 3      FB 2013-01-04 28.01 28.93 27.83 28.76  72715400    28.76
## 4      FB 2013-01-07 28.69 29.79 28.65 29.42  83781800    29.42
## 5      FB 2013-01-08 29.51 29.60 28.86 29.06  45871300    29.06
## 6      FB 2013-01-09 29.67 30.60 29.49 30.59 104787700    30.59
## 7      FB 2013-01-10 30.60 31.45 30.28 31.30  95316400    31.30
## 8      FB 2013-01-11 31.28 31.96 31.10 31.72  89598000    31.72
## 9      FB 2013-01-14 32.08 32.21 30.62 30.95  98892800    30.95
## 10     FB 2013-01-15 30.64 31.71 29.88 30.10 173242600    30.10
## # ... with 4,022 more rows
# 4銘柄の株価データ等を格納している
FANG %>% nest(date:adjusted)
## # A tibble: 4 × 2
##   symbol                  data
##    <chr>                <list>
## 1     FB <tibble [1,008 × 7]>
## 2   AMZN <tibble [1,008 × 7]>
## 3   NFLX <tibble [1,008 × 7]>
## 4   GOOG <tibble [1,008 × 7]>