1. Introduction

This is an casting page of testing the efficiency of the coding and also some useful r packages for the research on Betting Strategy and Model Validation. Some packages are introduce from other websites while I made it a scrapbook to easier for references.

## Setup Options, Loading Required Libraries and Preparing Environment

options(warn = -1)

## Loading the packages and setting adjustment
suppressMessages(library('BBmisc'))
suppressAll(library('utils'))
suppressAll(library('plyr'))
suppressAll(library('magrittr'))
suppressAll(library('data.table'))
suppressAll(library('tidyverse'))
suppressAll(library('doParallel'))

registerDoParallel(cores = detectCores())
## Load saved dataset to save the loading time.
## directly load the dataset from running chunk `read-data-summary-table` and also chunk `scrap-data`. 
## The spboData for filtering leagues and matches scores purpose. Kindly refer to file named 
## `compacted data - shinyData.txt` inside folder `data`.

## Run above codes and save.images() and now directly load for shinyApp use.
suppressAll(load('./regressionApps/shinyData.RData', envir = .GlobalEnv))

## -------- chunk `bank-roll` -----------
## Re-categorise the soccer financial settlement date. Due to I have no the history matches dataset from bookmakers. The scrapped spbo time is not stable (always change, moreover there just an information website) where firm A is the firm who placed bets with millions HKD (although the kick-off time might also changed after placed that particular bet), therefore I follow the kick-off time of the firm A.
#'@ dat <- dat[order(dat$DateUK),] %>% mutate(DateUS = as.Date(format(DateUK, tz = 'EST', usetz = TRUE, format = '%Y-%m-%d %H:%M:%S'))) #daily settlement will base on variable `DateUS`.

2. R Packages

2.1 Read Files

In my previous assignments, there has some useful function for read files :

Below is codes for merge files, there has another function which is plyr::join_all() which is very efficient, however the datetime format will became 0 value but reshape::merge_all() can combine the data efficiently but taken longer time.

suppressAll(nrow(do.call(rbind, llply(as.list(seq(2011, 2015)), function(x) data.frame(Sess = x, read.csv(paste0('./data/', x, '.csv'))), .parallel = FALSE))))

suppressAll(nrow(bind_rows(llply(as.list(seq(2011, 2015)), function(x) data.frame(Sess = x, read.csv(paste0('./data/', x, '.csv'))), .parallel = FALSE))))

suppressAll(system.time(do.call(rbind, llply(as.list(seq(2011,2015)), function(x) data.frame(Sess = x, read.csv(paste0('./data/', x, '.csv'))), .parallel = FALSE))))

suppressAll(system.time(bind_rows(llply(as.list(seq(2011, 2015)), function(x) data.frame(Sess = x, read.csv(paste0('./data/', x, '.csv'))), .parallel = FALSE))))

suppressAll(system.time(bind_rows(llply(as.list(seq(2011, 2015)), function(x) data.frame(Sess = x, fread(input = paste0('./data/', x, '.csv'))) %>% tbl_df, .parallel = FALSE))))

You can also embed plots, for example:

#'@ system.time(Reduce(function(x,y) {merge(x,y,all=TRUE)}, llply(list(df1,df1.sps,df1.pst),function(x) x[[1]])))

#'@ system.time(merge_all(list(df1[[1]],df1.sps[[1]],df1.pst[[1]])))

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

#'@ system.time(merge(socData, othData, all=TRUE))

#'@ system.time(merge_all(list(socData, othData)))

2.2 html Format Table

There are quite some r packages for ploting html table :

suppressMessages(library('knitr'))
suppressMessages(library('xtable'))
suppressMessages(library('htmlTable'))
suppressMessages(library('plyr'))
suppressMessages(library('dplyr'))
suppressMessages(library('formattable'))

## Re-categorise the soccer financial settlement date. Due to I have no the history matches dataset from bookmakers. The scrapped spbo time is not stable (always change, moreover there just an information website) where firm A is the firm who placed bets with millions HKD (although the kick-off time might also changed after placed that particular bet), therefore I follow the kick-off time of the firm A.
dat <- suppressAll(dat[order(dat$DateUK),] %>% mutate(DateUS = as.Date(format(DateUK, tz = 'EST', usetz = TRUE, format = '%Y-%m-%d %H:%M:%S')))) #daily settlement will base on variable `DateUS`.

BR <- ddply(dat, .(DateUS), summarise, Stakes = sum(Stakes), Return = sum(Return), PL = sum(PL), n = length(Sess), rRates = Return / Stakes) %>% mutate(CumStakes = cumsum(Stakes), SPL = cumsum(PL), BR = 4600 + SPL, gRates = BR/CumStakes)

summary(BR)
##      DateUS               Stakes         Return             PL          
##  Min.   :2011-01-07   Min.   :   4   Min.   :   0.0   Min.   :-2527.41  
##  1st Qu.:2012-04-01   1st Qu.: 328   1st Qu.: 293.5   1st Qu.: -126.69  
##  Median :2013-06-04   Median : 759   Median : 773.2   Median :   14.39  
##  Mean   :2013-05-13   Mean   :1155   Mean   :1179.4   Mean   :   24.82  
##  3rd Qu.:2014-06-29   3rd Qu.:1520   3rd Qu.:1584.4   3rd Qu.:  182.97  
##  Max.   :2015-07-19   Max.   :6108   Max.   :6613.1   Max.   : 3005.05  
##        n              rRates         CumStakes            SPL       
##  Min.   :  1.00   Min.   :0.0000   Min.   :     40   Min.   :-4587  
##  1st Qu.:  9.00   1st Qu.:0.8128   1st Qu.: 384506   1st Qu.:10615  
##  Median : 19.00   Median :1.0240   Median : 827028   Median :19262  
##  Mean   : 28.49   Mean   :1.0341   Mean   : 815158   Mean   :17774  
##  3rd Qu.: 38.00   3rd Qu.:1.2596   3rd Qu.:1209336   3rd Qu.:24086  
##  Max.   :148.00   Max.   :2.2033   Max.   :1663757   Max.   :38113  
##        BR               gRates         
##  Min.   :   13.05   Min.   :  0.00008  
##  1st Qu.:15215.39   1st Qu.:  0.02431  
##  Median :23862.28   Median :  0.02683  
##  Mean   :22373.80   Mean   :  0.12675  
##  3rd Qu.:28686.30   3rd Qu.:  0.03405  
##  Max.   :42712.54   Max.   :114.00000
summary(BR) %>% formattable 
##      DateUS                 Stakes                 Return            
##  "Min.   :2011-01-07  " "Min.   :   4        " "Min.   :   0.0      "
##  "1st Qu.:2012-04-01  " "1st Qu.: 328        " "1st Qu.: 293.5      "
##  "Median :2013-06-04  " "Median : 759        " "Median : 773.2      "
##  "Mean   :2013-05-13  " "Mean   :1155        " "Mean   :1179.4      "
##  "3rd Qu.:2014-06-29  " "3rd Qu.:1520        " "3rd Qu.:1584.4      "
##  "Max.   :2015-07-19  " "Max.   :6108        " "Max.   :6613.1      "
##        PL                     n                    rRates            
##  "Min.   :-2527.41    " "Min.   :  1.00      " "Min.   :0.0000      "
##  "1st Qu.: -126.69    " "1st Qu.:  9.00      " "1st Qu.:0.8128      "
##  "Median :   14.39    " "Median : 19.00      " "Median :1.0240      "
##  "Mean   :   24.82    " "Mean   : 28.49      " "Mean   :1.0341      "
##  "3rd Qu.:  182.97    " "3rd Qu.: 38.00      " "3rd Qu.:1.2596      "
##  "Max.   : 3005.05    " "Max.   :148.00      " "Max.   :2.2033      "
##    CumStakes                 SPL                     BR              
##  "Min.   :     40     " "Min.   :-4587       " "Min.   :   13.05    "
##  "1st Qu.: 384506     " "1st Qu.:10615       " "1st Qu.:15215.39    "
##  "Median : 827028     " "Median :19262       " "Median :23862.28    "
##  "Mean   : 815158     " "Mean   :17774       " "Mean   :22373.80    "
##  "3rd Qu.:1209336     " "3rd Qu.:24086       " "3rd Qu.:28686.30    "
##  "Max.   :1663757     " "Max.   :38113       " "Max.   :42712.54    "
##      gRates            
##  "Min.   :  0.00008   "
##  "1st Qu.:  0.02431   "
##  "Median :  0.02683   "
##  "Mean   :  0.12675   "
##  "3rd Qu.:  0.03405   "
##  "Max.   :114.00000   "
summary(BR) %>% formattable %>% as.htmlwidget
kable(summary(BR))
DateUS Stakes Return PL n rRates CumStakes SPL BR gRates
Min. :2011-01-07 Min. : 4 Min. : 0.0 Min. :-2527.41 Min. : 1.00 Min. :0.0000 Min. : 40 Min. :-4587 Min. : 13.05 Min. : 0.00008
1st Qu.:2012-04-01 1st Qu.: 328 1st Qu.: 293.5 1st Qu.: -126.69 1st Qu.: 9.00 1st Qu.:0.8128 1st Qu.: 384506 1st Qu.:10615 1st Qu.:15215.39 1st Qu.: 0.02431
Median :2013-06-04 Median : 759 Median : 773.2 Median : 14.39 Median : 19.00 Median :1.0240 Median : 827028 Median :19262 Median :23862.28 Median : 0.02683
Mean :2013-05-13 Mean :1155 Mean :1179.4 Mean : 24.82 Mean : 28.49 Mean :1.0341 Mean : 815158 Mean :17774 Mean :22373.80 Mean : 0.12675
3rd Qu.:2014-06-29 3rd Qu.:1520 3rd Qu.:1584.4 3rd Qu.: 182.97 3rd Qu.: 38.00 3rd Qu.:1.2596 3rd Qu.:1209336 3rd Qu.:24086 3rd Qu.:28686.30 3rd Qu.: 0.03405
Max. :2015-07-19 Max. :6108 Max. :6613.1 Max. : 3005.05 Max. :148.00 Max. :2.2033 Max. :1663757 Max. :38113 Max. :42712.54 Max. :114.00000
htmlTable(summary(BR))
DateUS</th>
Stakes</th>
Return</th>
  PL</th>
  n</th>
rRates</th>
CumStakes
 SPL</th>
  BR</th>
gRates</th>
Min. :2011-01-07 Min. : 4 Min. : 0.0 Min. :-2527.41 Min. : 1.00 Min. :0.0000 Min. : 40 Min. :-4587 Min. : 13.05 Min. : 0.00008
1st Qu.:2012-04-01 1st Qu.: 328 1st Qu.: 293.5 1st Qu.: -126.69 1st Qu.: 9.00 1st Qu.:0.8128 1st Qu.: 384506 1st Qu.:10615 1st Qu.:15215.39 1st Qu.: 0.02431
Median :2013-06-04 Median : 759 Median : 773.2 Median : 14.39 Median : 19.00 Median :1.0240 Median : 827028 Median :19262 Median :23862.28 Median : 0.02683
Mean :2013-05-13 Mean :1155 Mean :1179.4 Mean : 24.82 Mean : 28.49 Mean :1.0341 Mean : 815158 Mean :17774 Mean :22373.80 Mean : 0.12675
3rd Qu.:2014-06-29 3rd Qu.:1520 3rd Qu.:1584.4 3rd Qu.: 182.97 3rd Qu.: 38.00 3rd Qu.:1.2596 3rd Qu.:1209336 3rd Qu.:24086 3rd Qu.:28686.30 3rd Qu.: 0.03405
Max. :2015-07-19 Max. :6108 Max. :6613.1 Max. : 3005.05 Max. :148.00 Max. :2.2033 Max. :1663757 Max. :38113 Max. :42712.54 Max. :114.00000
print(xtable(summary(BR)), type = 'html')
## <!-- html table generated in R 3.3.2 by xtable 1.8-2 package -->
## <!-- Wed Dec 14 02:32:12 2016 -->
## <table border=1>
## <tr> <th>  </th> <th>     DateUS </th> <th>     Stakes </th> <th>     Return </th> <th>       PL </th> <th>       n </th> <th>     rRates </th> <th>   CumStakes </th> <th>      SPL </th> <th>       BR </th> <th>     gRates </th>  </tr>
##   <tr> <td align="right"> 1 </td> <td> Min.   :2011-01-07   </td> <td> Min.   :   4   </td> <td> Min.   :   0.0   </td> <td> Min.   :-2527.41   </td> <td> Min.   :  1.00   </td> <td> Min.   :0.0000   </td> <td> Min.   :     40   </td> <td> Min.   :-4587   </td> <td> Min.   :   13.05   </td> <td> Min.   :  0.00008   </td> </tr>
##   <tr> <td align="right"> 2 </td> <td> 1st Qu.:2012-04-01   </td> <td> 1st Qu.: 328   </td> <td> 1st Qu.: 293.5   </td> <td> 1st Qu.: -126.69   </td> <td> 1st Qu.:  9.00   </td> <td> 1st Qu.:0.8128   </td> <td> 1st Qu.: 384506   </td> <td> 1st Qu.:10615   </td> <td> 1st Qu.:15215.39   </td> <td> 1st Qu.:  0.02431   </td> </tr>
##   <tr> <td align="right"> 3 </td> <td> Median :2013-06-04   </td> <td> Median : 759   </td> <td> Median : 773.2   </td> <td> Median :   14.39   </td> <td> Median : 19.00   </td> <td> Median :1.0240   </td> <td> Median : 827028   </td> <td> Median :19262   </td> <td> Median :23862.28   </td> <td> Median :  0.02683   </td> </tr>
##   <tr> <td align="right"> 4 </td> <td> Mean   :2013-05-13   </td> <td> Mean   :1155   </td> <td> Mean   :1179.4   </td> <td> Mean   :   24.82   </td> <td> Mean   : 28.49   </td> <td> Mean   :1.0341   </td> <td> Mean   : 815158   </td> <td> Mean   :17774   </td> <td> Mean   :22373.80   </td> <td> Mean   :  0.12675   </td> </tr>
##   <tr> <td align="right"> 5 </td> <td> 3rd Qu.:2014-06-29   </td> <td> 3rd Qu.:1520   </td> <td> 3rd Qu.:1584.4   </td> <td> 3rd Qu.:  182.97   </td> <td> 3rd Qu.: 38.00   </td> <td> 3rd Qu.:1.2596   </td> <td> 3rd Qu.:1209336   </td> <td> 3rd Qu.:24086   </td> <td> 3rd Qu.:28686.30   </td> <td> 3rd Qu.:  0.03405   </td> </tr>
##   <tr> <td align="right"> 6 </td> <td> Max.   :2015-07-19   </td> <td> Max.   :6108   </td> <td> Max.   :6613.1   </td> <td> Max.   : 3005.05   </td> <td> Max.   :148.00   </td> <td> Max.   :2.2033   </td> <td> Max.   :1663757   </td> <td> Max.   :38113   </td> <td> Max.   :42712.54   </td> <td> Max.   :114.00000   </td> </tr>
##    </table>

2.3 Econometric and Investment

2.3.1 数据管理:

包括数据集抓取、存储、读取、时间序列、数据处理等,涉及R包

  1. zoo (时间序列对象)
  2. xts (时间序列处理)
  3. timeSeries (Rmetrics系时间序列对象)
  4. timeDate (Rmetrics系时间序列处理)
  5. data.table (数据处理)
  6. quantmod (数据下载和图形可视化)
  7. RQuantLib (QuantLib数据接口)
  8. WindR (Wind数据接口)
  9. RJDBC (数据库访问接口)
  10. rhadoop (Hadoop访问接口)
  11. rhive (Hive访问接口)
  12. rredis (Redis访问接口)
  13. rmongodb (MongoDB访问接口)
  14. SparkR (Spark访问接口)
  15. fImport (Rmetrics系数据访问接口)等。

2.3.2 指标计算:

包括金融市场的技术指标的各种计算方法,涉及R包

  1. TTR (技术指标),
  2. TSA (时间序列计算)
  3. urca (单位根检验)
  4. fArma (Rmetrics系ARMA计算)
  5. fAsianOptions (Rmetrics系亚洲期权定价)
  6. fBasics (Rmetrics系计算工具)
  7. fCopulae (Rmetrics系财务分析)
  8. fExoticOptions (Rmetrics系期权计算)
  9. fGarch (Rmetrics系Garch模型)
  10. fNonlinear (Rmetrics系非线模型)
  11. fOptions (Rmetrics系期权定价)
  12. fRegression (Rmetrics系回归分析)
  13. fUnitRoots (Rmetrics系单位根检验)等。

2.3.3 回测交易:

包括金融数据建模,并验证用历史数据验证模型的可靠性,涉及R包

  1. FinancialInstrument(金融产品),
  2. quantstrat(策略模型和回测),
  3. blotter(账户管理)
  4. fTrading(Rmetrics系交易分析)

2.3.4 投资组合:

对多策略或多模型进行管理和优化,涉及R包

  1. PortfolioAnalytics(组合分析和优化)
  2. stockPortfolio(股票组合管理)
  3. fAssets(Rmetrics系组合管理)等

2.3.5 风险管理:

对持仓进行风险指标的计算和风险提示,涉及R包 1. PerformanceAnalytics(风险分析) 2. fPortfolio(Rmetrics系组合优化) 3. fExtremes(Rmetrics系数据处理)

2.4 Time Format

  1. lubridate (very easy to convert the time format or select the year, month, week, day, hour, min, scd variables, however some format unable fit to tbl_df or it became 0 value when plot a datatable graph)
  2. zoo
  3. ts
  4. chron
  5. timeDate
  6. xts

(I am moving this from the comments to the answer portion of stackoverflow at the request of the original poster.)

There is an article in R News 4/1 (“R Help Desk”, p. 29) that specifically compares Date, POSIXct and chron. (The 1st two are in core of R and chron is a package.)

  • timeDate class (in the timeDate package) is based on POSIXct but has extra time zone/financial center support.
  • For regularly spaced series the the tis package supports many notions of dates.
  • The mondate package supports accounting dates.
  • The zoo time series package supports just about any date/time class and also has yearmon and yearqtr for ts compatibility.
  • The xts time series package works on top of zoo and handles the most common date/time classes by translating them to POSIXct and back again.
  • There is also information in the Time Series CRAN Task View.

Reference : Which R time/date class and package to use?

2.5 Data Visualization

In my first time ploting graph in Betting Strategy and Model Validation, I use ggplot2 with some ggplotthemes and directlable etc to enhance the data visualization effects but later on I use interactive JS plot. Below r packages are useful for plot graph.

  • ggplot2 - one of the best static visualization packages in R
  • ggvis - interactive plots from the makers of ggplot2
  • rCharts - R interface to multiple javascript charting libraries
  • plotly - convert ggplot2 figures to interactive plots easily
  • googleVis - use Google Chart Tools from R
  • RECharts (two packages from Xie Yihui and Wei Taiyun)
  • highcharter - An interative d3 chart
# workable if run in chunk but but not in knit.
suppressAll(library('quantmod'))
suppressAll(source('./function/compareKelly.R', local = TRUE))

if(file.exists('./data/K1.rds')){
  K1 <- read_rds(path = './data/K1.rds')
} else {
  K1 <- vKelly(dat)
}

#'@ compareKelly(K1, chart = TRUE, type = 'multiple') # workable if run in chunk but but not in knit.

suppressAll(source('./function/plotChart.R', local = TRUE))
fund <- compareKelly(K1)

plotFund <- llply(fund, function(x) {
  event.dates = as.Date(period.apply(
  diff(Op(x)), INDEX = endpoints(x), FUN = max) %>% data.frame %>% rownames, format = '%Y-%m-%d')
  
  plotChart(x, type = 'multiple', event.dates = event.dates, 
            event = seq(length(event.dates)), chart.type = 'Cl')
  })# workable if run in chunk but but not in knit.

#'@ invisible(plotFund)
plotFund

graph 2.5.1A : example of sportsbook investment fund size growth.

## Due to the highcharter::highchart() return function 

suppressPackageStartupMessages(library('BBmisc'))
suppressAll(library('plyr'))
suppressAll(library('magrittr'))
suppressAll(library('tidyverse'))
suppressAll(library('quantmod'))
suppressMessages(library('highcharter'))
suppressAll(source('./function/compareKelly.R', local = TRUE))

if(file.exists('./data/K1.rds')){
  K1 <- read_rds(path = './data/K1.rds')
} else {
  K1 <- vKelly(dat)
}

#'@ compareKelly(K1, chart = TRUE, type = 'multiple') # workable if run in chunk but but not in knit.

#'@ suppressAll(source('./function/plotChart.R', local = TRUE))
Fund <- compareKelly(K1)$Kelly1

event.dates = as.Date(period.apply(
  diff(Op(Fund)), INDEX = endpoints(Fund), FUN = max) %>% data.frame %>% rownames, format = '%Y-%m-%d')
event = seq(length(event.dates)); chart.type = NULL

## put remarks on big gap within highest and lowest within a day.
    #'@ event <- Hi(Fund) - Lo(Fund) # need to modify...
    # single chart high-low candle stick might need to 
    # label the reason and event to cause a hight volatility.
    
    initial <- Op(Fund)[1, ] %>% unique
    chart.type <- ifelse(is.null(chart.type), 'Cl', chart.type)
    
    ## comparison of fund size and growth of various Kelly models
    #'@ event <- c('netEMEdge', 'PropHKPriceEdge', 'PropnetProbBEdge', 'KProbHKPrice',
    #'@              'KProbnetProbB', 'KProbFixed', 'KProbFixednetProbB', 'KEMProb',
    #'@              'KEMProbnetProbB', 'KProbHalf','KProbHalfnetProbB', 'KProbQuarter',
    #'@              'KProbQuarternetProbB', 'KProbAdj','KProbAdjnetProbB', 'KHalfAdj',
    #'@              'KHalfAdjnetProbB', 'KEMQuarterAdj', 'KEMQuarterAdjnetProbB')
    
    ## add dates for event...
    ##   label the high volatility daily event.
    if(is.null(event.dates)) {
      event.dates <- as.Date(period.apply(
        diff(Op(Fund)), INDEX = endpoints(Fund), FUN = max) %>% data.frame %>% 
          rownames, format = '%Y-%m-%d')
    } else {
      event.dates <- as.Date(event.dates)
    }
    
    ## id of event label, event text
    id <- seq(length(event.dates))
    
    if(is.null(event)) {
      event <- id
    } else {
      event <- event
    }
    
    if(length(event) == length(event.dates)) {
      event <- event
      event.dates <- event.dates
    } else {
      stop('The vector length of event must be same with vector length of event.dates.')
    }
    
    if(chart.type == 'Op') {
      Fund <- Op(Fund)
    } else if(chart.type == 'Hi') {
      Fund <- Hi(Fund)
    } else if(chart.type == 'Lo') {
      Fund <- Lo(Fund)
    } else if(chart.type == 'Cl') {
      Fund <- Cl(Fund)
    } else {
      stop('Kindly choose chart.type = "Op", chart.type = "Hi", chart.type = "Lo", chart.type = "Cl".')
    }
    
    plotc <- highchart(type = "stock") %>% 
      hc_title(text = "Charting some Funds") %>% 
      hc_subtitle(text = paste("Data extracted using various Kelly functions. Initial fund size : $", initial)) %>% 
      hc_add_series_xts(Fund[, 1], id = names(Fund)[1]) %>% 
      hc_add_series_xts(Fund[, 2], id = names(Fund)[2]) %>% 
      hc_add_series_xts(Fund[, 3], id = names(Fund)[3]) %>% 
      hc_add_series_xts(Fund[, 4], id = names(Fund)[4]) %>% 
      hc_add_series_xts(Fund[, 5], id = names(Fund)[5]) %>% 
      hc_add_series_xts(Fund[, 6], id = names(Fund)[6]) %>% 
      hc_add_series_xts(Fund[, 7], id = names(Fund)[7]) %>% 
      hc_add_series_xts(Fund[, 8], id = names(Fund)[8]) %>% 
      hc_add_series_xts(Fund[, 9], id = names(Fund)[9]) %>% 
      hc_add_series_xts(Fund[,10], id = names(Fund)[10]) %>% 
      hc_add_series_xts(Fund[,11], id = names(Fund)[11]) %>% 
      hc_add_series_xts(Fund[,12], id = names(Fund)[12]) %>% 
      hc_add_series_xts(Fund[,13], id = names(Fund)[13]) %>% 
      hc_add_series_xts(Fund[,14], id = names(Fund)[14]) %>% 
      hc_add_series_xts(Fund[,15], id = names(Fund)[15]) %>% 
      hc_add_series_xts(Fund[,16], id = names(Fund)[16]) %>% 
      hc_add_series_xts(Fund[,17], id = names(Fund)[17]) %>% 
      hc_add_series_xts(Fund[,18], id = names(Fund)[18]) %>% 
      hc_add_series_xts(Fund[,19], id = names(Fund)[19]) %>% 
      
      ## add event remarks onto the chart.
      hc_add_series_flags(event.dates, title = paste0('E', event), #label of the event box
                          text = paste('Event : High volatility ', event), id = id) %>% #text inside the event box
      hc_add_theme(hc_theme_flat())
 plotc   

graph 2.5.1B : example of sportsbook investment fund size growth.

## Need to correct the content, below just a sample...
suppressAll(library('formattable'))
load('./KellyApps/shinyData.RData')

## Random pick 2 observations among each stratified levels of Result.
K <- llply(split(dat, dat$Result), function(x) x[sample(nrow(x), 2), c('Result', 'Return', 'EUPrice', 'HKPrice', 'Stakes', 'Rebates')]) %>% ldply(., .id = 'Result') %>% mutate(KReturn = currency(Return), Return = currency(Return), KStakes = currency(Stakes), Stakes = currency(Stakes), Rebates = percent(Rebates), Change = percent(KStakes / Stakes - 1)) %>% .[sample(nrow(.), nrow(.)), ]

## http://www.w3schools.com/colors/colors_picker.asp
K %>% formattable(list(
    Result = formatter('span', style = x ~ ifelse(x == 'Win', style(color = '#269900', font.weight = 'bold'), ifelse(x == 'Half Win', style(color = '#40FF00'), ifelse(x == 'Push', style(color = '#FFFF00'), ifelse(x == 'Half Loss', style(color = '#FF8C1A'), ifelse(x == 'Loss', style(color = '#FF0000', font.weight = 'bold'), NA)))))),
    KReturn = formatter('span', style = ~ style(color = ifelse(KReturn >= KStakes, 'green', 'red')), ~ icontext(ifelse(KReturn >= KStakes, 'plus-sign', 'minus-sign'), KReturn)),
    Return = formatter('span', style = ~ style(color = ifelse(Return >= Stakes, 'green', 'red')), ~ icontext(ifelse(Return >= Stakes, 'plus-sign', 'minus-sign'), Return)),
    EUPrice = color_tile('white', '#003D99'),
    HKPrice = color_tile('white', '#003D99'),
    KStakes = color_tile('white', '#CC9900'),
    Stakes = color_tile('white', '#CC9900'),
    
    Change = formatter('span', style = ~ style(color = ifelse(Change < 0, 'red', 'green')), ~ icontext(ifelse(Change < 0, 'arrow-down', 'arrow-up'), Change))))
Result Return EUPrice HKPrice Stakes Rebates KReturn KStakes Change
7 Loss $0.00 2.03 1.03 $134.00 -28.00% $0.00 $134.00 0.00%
10 Push $20.00 1.96 0.96 $20.00 0.00% $20.00 $20.00 0.00%
1 Cancelled $120.00 1.95 0.95 $120.00 0.00% $120.00 $120.00 0.00%
12 Win $29.25 2.25 1.25 $13.00 4.00% $29.25 $13.00 0.00%
11 Win $100.50 2.01 1.01 $50.00 10.00% $100.50 $50.00 0.00%
9 Push $52.00 1.66 0.66 $52.00 0.00% $52.00 $52.00 0.00%
4 Half Loss $10.00 2.06 1.05 $20.00 -2.00% $10.00 $20.00 0.00%
5 Half Win $144.90 2.22 1.22 $90.00 12.00% $144.90 $90.00 0.00%
8 Loss $0.00 1.89 0.89 $42.00 -9.00% $0.00 $42.00 0.00%
3 Half Loss $7.00 2.23 1.24 $14.00 -2.00% $7.00 $14.00 0.00%
6 Half Win $268.78 2.02 1.02 $178.00 20.00% $268.78 $178.00 0.00%
2 Cancelled $26.00 2.01 1.01 $26.00 0.00% $26.00 $26.00 0.00%

table 2.5.2 : 12 x 9 : Sample data of firm A annual investment summary table.

Reference : - Interactive visualizations with R - a minireview - Interactive DataViz with R - Coursera Data Mining (in my previous assignments, there are some r useful packages like DT, rCharts::dTable() and formattable)

Also other libraries for creating interactive visualizations from R do exist, such as clickme, RIGHT, ggobi, iplots, gg2v, rVega, cranvas and r2d3. Some of these are not under active development anymore. I might include some of those into the comparison here in the future. The d3Network package is also worth checking if you need cool interactive network visualizations.

options(warn = 0)

3. Conclusion

From the research, I learned from some articles which compare the efficiency of data measurement which will apply in future data analyse and data mining etc.