## 3210448065@qq.com
## leiou123

## 2849108450@qq.com
## leiou123
## https://rstudio.cloud/project/1198888

pkg <- c('plyr', 'tidyverse', 'magrittr', 'readr', 'readxl', 'tidyr', 
         'knitr', 'kableExtra', 'forecast', 'formattable', 'DT', 
         'lubridate', 'highcharter', 'htmltools', 'echarts4r')

plyr::l_ply(pkg, require, quietly = TRUE, character.only = TRUE, .print = FALSE)



1 数据


读取样本数据。

## 读取数据
fls <- suppressWarnings(list.files('data/彩种'))
smp <- fls %>% llply(., function(x) {
    dtt <- x %>% str_replace_all('.xls', '') %>% ymd
    smpp <- read_excel(paste0('data/彩种/', x)) %>% 
      .[-nrow(.),]
    data.frame('日期' = dtt, smpp)
}) %>% bind_rows %>% 
    as_tibble %>% 
    mutate('彩种名称' = factor(彩种名称), '盈率' = as.numeric(percent(盈率))) %>% 
    mutate_if(is.character, as.numeric)
rm(fls)

smp %>% datatable(
    caption = "彩种数据", 
    escape = FALSE, filter = 'top', rownames = FALSE, 
    extensions = list('ColReorder' = NULL, 'RowReorder' = NULL, 
                      'Buttons' = NULL, 'Responsive' = NULL), 
    options = list(dom = 'BRrltpi', autoWidth = TRUE,  scrollX = TRUE, 
                   lengthMenu = list(c(10, 50, 100, 500, -1), 
                                     c('10', '50', '100', '500', 'All')), 
                   ColReorder = TRUE, rowReorder = TRUE, 
                   buttons = list('copy', 'print', 
                                  list(extend = 'collection', 
                                       buttons = c('csv', 'excel', 'pdf'), 
                                       text = 'Download'), I('colvis'))))

上图显示从2020-05-27到2020-07-24的报表数据。



2 绘图


2.1 投注人数


pl1 <- smp %>% 
  highchart() %>% 
  hc_chart('line', hcaes(x = 日期, y = 投注人数, group = 彩种名称)) %>% 
  hc_title(text = '彩种') %>%
  hc_subtitle(text = '投注人数')

tagList(pl1)
smp %>% 
    group_by(彩种名称) %>% 
    e_charts(x = 日期) %>% 
    e_line(投注人数, smooth = TRUE) %>% 
  e_datazoom(
    type = 'slider', 
    toolbox = FALSE,
    bottom = -5) %>% 
  e_tooltip() %>% 
  e_title(text = '彩种', subtext = '投注人数', left = 'center') %>% 
  e_axis_labels(x = '日期', y = '投注人数') %>%
  e_x_axis(日期, axisPointer = list(show = TRUE)) %>% 
  e_legend(
    orient = 'vertical', 
    type = c('scroll'), 
    #selectedMode = 'multiple', #https://echarts.apache.org/en/option.html#legend
    #selected = list('彩种'), 
    left = 0, top = 80) %>% 
  e_grid(left = 150, top = 90) %>% 
  #e_theme('shine') %>% 
  e_toolbox_feature('saveAsImage', title = '截图')

上图显示从2020-05-27到2020-07-24的投注人数,默认设置显示所有彩种,可以点击彩种筛选焦点彩种。


2.2 投注金额


smp %>% 
    hchart('line', hcaes(x = 日期, y = 投注金额, group = 彩种名称)) %>% 
    hc_title(text = '彩种') %>%
    hc_subtitle(text = '投注金额')
smp %>% 
    group_by(彩种名称) %>% 
    e_charts(x = 日期) %>% 
    e_line(投注金额, smooth = TRUE) %>% 
  e_datazoom(
    type = 'slider', 
    toolbox = FALSE,
    bottom = -5) %>% 
  e_tooltip() %>% 
  e_title(text = '彩种', subtext = '投注金额', left = 'center') %>% 
  e_axis_labels(x = '日期', y = '投注金额') %>%
  e_x_axis(日期, axisPointer = list(show = TRUE)) %>% 
  e_legend(
    orient = 'vertical', 
    type = c('scroll'), 
    left = 0, top = 80) %>% 
  e_grid(left = 150, top = 90) %>% 
  #e_theme('shine') %>% 
  e_toolbox_feature('saveAsImage', title = '截图')

上图显示从2020-05-27到2020-07-24的投注金额,默认设置显示所有彩种,可以点击彩种筛选焦点彩种。


2.3 中奖金额


smp %>% 
    hchart('line', hcaes(x = 日期, y = 投注金额, group = 彩种名称)) %>% 
    hc_title(text = '彩种') %>%
    hc_subtitle(text = '中奖金额')
smp %>% 
    group_by(彩种名称) %>% 
    e_charts(x = 日期) %>% 
    e_line(中奖金额, smooth = TRUE) %>% 
  e_datazoom(
    type = 'slider', 
    toolbox = FALSE,
    bottom = -5) %>% 
  e_tooltip() %>% 
  e_title(text = '彩种', subtext = '中奖金额', left = 'center') %>% 
  e_axis_labels(x = '日期', y = '中奖金额') %>%
  e_x_axis(日期, axisPointer = list(show = TRUE)) %>% 
  e_legend(
    orient = 'vertical', 
    type = c('scroll'), 
    left = 0, top = 80) %>% 
  e_grid(left = 150, top = 90) %>% 
  #e_theme('shine') %>% 
  e_toolbox_feature('saveAsImage', title = '截图')

上图显示从2020-05-27到2020-07-24的中奖金额,默认设置显示所有彩种,可以点击彩种筛选焦点彩种。


2.4 撤单金额


smp %>% 
    hchart('line', hcaes(x = 日期, y = 投注金额, group = 彩种名称)) %>% 
    hc_title(text = '彩种') %>%
    hc_subtitle(text = '撤单金额')
smp %>% 
    group_by(彩种名称) %>% 
    e_charts(x = 日期) %>% 
    e_line(撤单金额, smooth = TRUE) %>% 
  e_datazoom(
    type = 'slider', 
    toolbox = FALSE,
    bottom = -5) %>% 
  e_tooltip() %>% 
  e_title(text = '彩种', subtext = '撤单金额', left = 'center') %>% 
  e_axis_labels(x = '日期', y = '撤单金额') %>%
  e_x_axis(日期, axisPointer = list(show = TRUE)) %>% 
  e_legend(
    orient = 'vertical', 
    type = c('scroll'), 
    left = 0, top = 80) %>% 
  e_grid(left = 150, top = 90) %>% 
  #e_theme('shine') %>% 
  e_toolbox_feature('saveAsImage', title = '截图')

上图显示从2020-05-27到2020-07-24的撤单金额,默认设置显示所有彩种,可以点击彩种筛选焦点彩种。


2.5 返点金额


smp %>% 
    hchart('line', hcaes(x = 日期, y = 投注金额, group = 彩种名称)) %>% 
    hc_title(text = '彩种') %>%
    hc_subtitle(text = '返点金额')
smp %>% 
    group_by(彩种名称) %>% 
    e_charts(x = 日期) %>% 
    e_line(返点金额, smooth = TRUE) %>% 
  e_datazoom(
    type = 'slider', 
    toolbox = FALSE,
    bottom = -5) %>% 
  e_tooltip() %>% 
  e_title(text = '彩种', subtext = '返点金额', left = 'center') %>% 
  e_axis_labels(x = '日期', y = '返点金额') %>%
  e_x_axis(日期, axisPointer = list(show = TRUE)) %>% 
  e_legend(
    orient = 'vertical', 
    type = c('scroll'), 
    left = 0, top = 80) %>% 
  e_grid(left = 150, top = 90) %>% 
  #e_theme('shine') %>% 
  e_toolbox_feature('saveAsImage', title = '截图')

上图显示从2020-05-27到2020-07-24的返点金额,默认设置显示所有彩种,可以点击彩种筛选焦点彩种。


2.6 盈利


smp %>% 
    hchart('line', hcaes(x = 日期, y = 投注金额, group = 彩种名称)) %>% 
    hc_title(text = '彩种') %>%
    hc_subtitle(text = '盈利')
smp %>% 
    group_by(彩种名称) %>% 
    e_charts(x = 日期) %>% 
    e_line(盈利, smooth = TRUE) %>% 
  e_datazoom(
    type = 'slider', 
    toolbox = FALSE,
    bottom = -5) %>% 
  e_tooltip() %>% 
  e_title(text = '彩种', subtext = '盈利', left = 'center') %>% 
  e_axis_labels(x = '日期', y = '盈利') %>%
  e_x_axis(日期, axisPointer = list(show = TRUE)) %>% 
  e_legend(
    orient = 'vertical', 
    type = c('scroll'), 
    left = 0, top = 80) %>% 
  e_grid(left = 150, top = 90) %>% 
  #e_theme('shine') %>% 
  e_toolbox_feature('saveAsImage', title = '截图')

上图显示从2020-05-27到2020-07-24的盈利,默认设置显示所有彩种,可以点击彩种筛选焦点彩种。


2.7 盈率


smp %>% 
    hchart('line', hcaes(x = 日期, y = 投注金额, group = 彩种名称)) %>% 
    hc_title(text = '彩种') %>%
    hc_subtitle(text = '盈率')
smp %>% 
    group_by(彩种名称) %>% 
    e_charts(x = 日期) %>% 
    e_line(盈率, smooth = TRUE) %>% 
  e_datazoom(
    type = 'slider', 
    toolbox = FALSE,
    bottom = -5) %>% 
  e_tooltip() %>% 
  e_title(text = '彩种', subtext = '盈率', left = 'center') %>% 
  e_axis_labels(x = '日期', y = '盈率') %>%
  e_x_axis(日期, axisPointer = list(show = TRUE)) %>% 
  e_legend(
    orient = 'vertical', 
    type = c('scroll'), 
    left = 0, top = 80) %>% 
  e_grid(left = 150, top = 90) %>% 
  #e_theme('shine') %>% 
  e_toolbox_feature('saveAsImage', title = '截图')

上图显示从2020-05-27到2020-07-24的盈率,默认设置显示所有彩种,可以点击彩种筛选焦点彩种。



3 结论


3.1 总结


以上是将数据绘图,还需要通过统计模型预测,不过基于数据观测值太少1,所以暂时没有预测。


3.2 附录

suppressMessages(require('dplyr', quietly = TRUE))
suppressMessages(require('formattable', quietly = TRUE))
suppressMessages(require('knitr', quietly = TRUE))
suppressMessages(require('kableExtra', quietly = TRUE))
sys1 <- devtools::session_info()$platform %>% 
  unlist %>% data.frame(Category = names(.), session_info = .)
rownames(sys1) <- NULL
#sys1 %<>% rbind(., data.frame(
#  Category = 'Current time', 
#  session_info = paste(as.character(lubridate::now('Asia/Tokyo')), 'JST'))) %>% 
#  dplyr::filter(Category != 'os')
sys2 <- data.frame(Sys.info()) %>% mutate(Category = rownames(.)) %>% .[2:1]
names(sys2)[2] <- c('Sys.info')
rownames(sys2) <- NULL
if (nrow(sys1) == 7 & nrow(sys2) == 8) {
  sys1 %<>% rbind(., data.frame(
  Category = 'Current time', 
  session_info = paste(as.character(lubridate::now('Asia/Tokyo')), 'JST')))
} else {
  sys2 %<>% rbind(., data.frame(
  Category = 'Current time', 
  Sys.info = paste(as.character(lubridate::now('Asia/Tokyo')), 'JST')))
}
cbind(sys1, sys2) %>% 
  kable(caption = 'Additional session information:') %>% 
  kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive'))
Additional session information:
Category session_info Category Sys.info
version R version 4.0.2 (2020-06-22) sysname Linux
os Ubuntu 16.04.6 LTS release 5.3.0-1017-aws
system x86_64, linux-gnu version #18~18.04.1-Ubuntu SMP Wed Apr 8 15:12:16 UTC 2020
ui X11 nodename application-2613621-deployment-6783693-jlfct
language (EN) machine x86_64
collate C.UTF-8 login unknown
ctype C.UTF-8 user rstudio-user
tz Etc/UTC effective_user rstudio-user
date 2020-07-26 Current time 2020-07-26 23:17:18 JST
rm(sys1, sys2)

  1. binary.com Interview Question I (Extention)尝试分别使用3个月6个月12个月18个月24个月的数据,结果12个月的数据最为精准。↩︎