INPUTしたらOUTPUT!

忘れっぽいんでメモっとく

Rでロト6を当てたい(3)

前回までで以下のように数字選びの方針が決まったので実際にピックアップしていく。

  1. 奇数は2〜4個
  2. 23以上の数字は2〜4個
  3. 前回出た数字の中から1個
  4. 11回以上出ていない数字の中から1個
  5. 5回以内に出た数字の中から3個


hot1, hot5, hot10, coldの抽出

hot1, hot5, hot10, coldの抽出は以下のように行なう。

> # 前回の当選番号
> hot1 <- nums %>% 
+   filter(time == max(nums$time)) %>%
+   select(num)
> 
> # 直近5回の当選暗号
> hot5 <- nums %>% 
+   filter(time >= max(nums$time) - 5) %>%
+   count(num)
> 
> # 直近10回の当選暗号
> hot10 <- nums %>% 
+   filter(time >= max(nums$time) - 10) %>%
+   count(num)
> 
> # 直近11回以上当選していない番号
> cold <- seq(1, 43)[!(seq(1, 43) %in% hot10$num)]
> 


hot5から3つを選ぶ組み合わせ

直近5回以内に当選した数字から直前に当選した数字を除き、3つ選ぶ組み合わせを列挙する。

> # 直近5回に当選した番号から3つ選ぶ組み合わせ
> hot5.comb <- hot5$num[!(hot5$num %in% hot1$num)] %>%
+   # 3つを選ぶ組み合わせを列挙
+   combn(3) %>%
+   # 転置
+   t() %>%
+   as.data.frame()
> 
> # 後でマージするために連番を追加
> hot5.comb$seq <- seq(1, dim(hot5.comb)[1])
> names(hot5.comb) <- c("hot5.1", "hot5.2", "hot5.3", "seq")
> head(hot5.comb)
  hot5.1 hot5.2 hot5.3 seq
1      1      2      5   1
2      1      2      6   2
3      1      2     10   3
4      1      2     12   4
5      1      2     13   5
6      1      2     15   6
>


hot1, hot5, hot10, coldの組み合わせ

複数の因子から全ての組み合わせを求めるにはexpand.grid()を使用する。

> # 全ての組み合わせを列挙
> num.comb <- expand.grid(
+   hot1  = hot1$num,
+   seq   = hot5.comb$seq,
+   hot10 = hot10$num[!(hot10$num %in% hot5$num)],
+   cold  = cold) %>%
+   # hot5の組み合わせと結合
+   inner_join(hot5.comb, by = "seq") %>%
+   select(hot1, hot5.1, hot5.2, hot5.3, hot10, cold)
> head(num.comb)
  hot1 hot5.1 hot5.2 hot5.3 hot10 cold
1    3      1      2      5     4    8
2   11      1      2      5     4    8
3   19      1      2      5     4    8
4   30      1      2      5     4    8
5   38      1      2      5     4    8
6   42      1      2      5     4    8
> 


奇数および23以上の数字の数でフィルタ

奇数の数が2〜4個、23以上の数字が2〜4となる組み合わせのみに絞る。ここももっとスマートにできるかも。

> # 奇数の数と23以上の数をカウント
> num.comb  <- num.comb %>%
+   mutate(odd.n = hot1 %% 2 +
+            hot5.1 %% 2 +
+            hot5.2 %% 2 +
+            hot5.3 %% 2 +
+            hot10 %% 2 +
+            cold %% 2,
+          high.n = hot1 %/% 23 +
+            hot5.1 %/% 23 +
+            hot5.2 %/% 23 +
+            hot5.3 %/% 23 +
+            hot10 %/% 23 +
+            cold %/% 23)
> 
> # 奇数2〜4個、23以上2〜4個に絞込み
> num.comb.buy <- num.comb %>%
+   filter(odd.n >= 2 & odd.n <= 4 & high.n >= 2 & high.n <= 4) %>%
+   select(hot1, hot5.1, hot5.2, hot5.3, hot10, cold)
> head(num.comb.buy)
  hot1 hot5.1 hot5.2 hot5.3 hot10 cold
1   30      1      2     27     4    8
2   38      1      2     27     4    8
3   42      1      2     27     4    8
4   30      1      2     29     4    8
5   38      1      2     29     4    8
6   42      1      2     29     4    8
> dim(num.comb.buy)
[1] 258744      6
> 

って25万通りもあるじゃねーかヽ(`Д´)ノ


サンプリング

25万通りの組み合わせなんか買えないので25万の中からランダムに5個サンプリングする。

> set.seed(******)
> num.comb.buy[sample(dim(num.comb.buy)[1], 5), ]
       hot1 hot5.1 hot5.2 hot5.3 hot10 cold
255465   42     13     15     36    26   41
231563   11      1     22     29    40   39
137338    3      1     28     29     7   34
115680   42      2     12     31     9   33
247360   30     10     27     36    20   41
>


結果

5等でした。(実際に買ったのは上記とは異なる組み合わせ)



datascienceと呼べる内容か微妙だけどdatascience for me案件の分析は楽しい。
同じことをExcelSQLでやろうとすると大変そうだけどRだと簡単に数字を選べるのでしばらく継続してみる。