【初心者向け】度数分布(Frequency Table)/度数分布図（Histogram）と最頻値（Mode）


統計学における**最頻値**または**モード**（<font color=blue>mode</font>）の概念について、日本工業規格は「<font color=purple>離散分布の場合は確率関数が，連続分布の場合は密度関数が，最大となる確率変数の値。分布が多峰性の場合は，それぞれの極大値を与える確率変数の値</font>」と定めている様です。
[最頻値 - Wikipedia](https://ja.wikipedia.org/wiki/%E6%9C%80%E9%A0%BB%E5%80%A4)


しばしば「<font color=purple>データ群や確率分布で最も頻繁に出現する値</font>」と表現されますが、実際にはデータ処理者が「<font color=purple>大きな塊を掴み取りに行く</font>」人間臭い側面も。そう、まさに**度数分布**(<font color=blue>Frequency Table</font>)概念の顕現たる**度数分布図**(<font color=blue>Histogram</font>)の作成過程がそうな訳です。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/597772/75ba8ece-ef9f-4cb6-e30c-5765bf33bc61.png)

<font color=purple>統計言語Rによる実現例</font>

```R
K <- 100
N <- 1000
pi.est <- c(NULL)
for (k in seq(1,K)) {
x <- runif(N, min=-1, max=1)
y <- runif(N, min=-1, max=1)
Data<-sum(sqrt(x*x+y*y))/N
pi.est<-c(Data,pi.est)
}
hist(pi.est, breaks=50)
rug(pi.est)
```
実際には最初の元データの中身はこんな感じだったりします。

```R
#再現性確保の為のデータ保存
TestData01<-c(0.7575710,0.7626254,0.7720857,0.7636143,0.7559374,0.7636892 ,0.7697000,0.7744773,0.7718450,0.7661308,0.7643701,0.7773141,0.7601370,0.7625076,0.7657443,0.7642925,0.7652136,0.7626871,0.7651091,0.7838558,0.7577962,0.7679088,0.7691799,0.7778908,0.7651222,0.7745353,0.7630110,0.7704386,0.7693323,0.7626189,0.7589299,0.7469987,0.7606626,0.7728721,0.7608035,0.7766716,0.7748260,0.7754264,0.7671104,0.7588898,0.7763099,0.7590662,0.7713618,0.7706218,0.7752811,0.7783880,0.7727876,0.7694160,0.7689929,0.7661827,0.7569213,0.7565509,0.7594678,0.7557077,0.7661841,0.7831111,0.7671762,0.7670367,0.7456229,0.7633547,0.7806836,0.7537715,0.7705932,0.7695142,0.7594232,0.7629710,0.7571859,0.7698099,0.7585800,0.7565154,0.7619804,0.7723939,0.7621805,0.7769550,0.7552302,0.7477231,0.7605412,0.7620618,0.7601785,0.7479306,0.7770422,0.7799119,0.7709682,0.7657947,0.7612600,0.7643751,0.7653982,0.7593435,0.7823729,0.7659631,0.7529565,0.7603398,0.7592395,0.7607167,0.7566061,0.7573243,0.7640064,0.7661753,0.7667829,0.7758546)

#ヒストグラムとラグプロットの再表示
hist(TestData01, breaks=50)
rug(TestData01)
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/597772/d36ce97a-2a11-af28-9b99-55444376754c.png)

これをひたすら「**整形**」していく訳です。
[Rで度数分布表を作る](http://tips-r.blogspot.com/2014/11/r.html)

```R
h<-hist(TestData01)
h
$breaks
[1] 0.745 0.750 0.755 0.760 0.765 0.770 0.775 0.780 0.785

$counts
[1] 4 2 19 24 23 13 11 4

$density
[1] 8 4 38 48 46 26 22 8

$mids
[1] 0.7475 0.7525 0.7575 0.7625 0.7675 0.7725 0.7775 0.7825

$xname
[1] "TestData01"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"

#breaksが階級を区切る値で、countsが度数。
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/597772/ef6da229-8f0d-85de-d4d6-e76864db90c9.png)

```R

h <- hist(TestData01, breaks=50)
n <- length(h$counts) # 階級の数
class_names <- NULL # 階級の名前格納用
for(i in 1:n) {
class_names[i] <- paste(h$breaks[i], "～", h$breaks[i+1])
}
frequency_table <- data.frame(class=class_names, frequency=h$counts)

library(xtable)
print(xtable(frequency_table), type = "html")
```
<table border="1">
<tbody>
<tr>
<th> </th>
<th>class</th>
<th>frequency</th>
</tr>
<tr>
<td align="right">1</td>
<td>0.745 ～ 0.746</td>
<td align="right">1</td>
</tr>
<tr>
<td align="right">2</td>
<td>0.746 ～ 0.747</td>
<td align="right">1</td>
</tr>
<tr>
<td align="right">3</td>
<td>0.747 ～ 0.748</td>
<td align="right">2</td>
</tr>
<tr>
<td align="right">4</td>
<td>0.748 ～ 0.749</td>
<td align="right">0</td>
</tr>
<tr>
<td align="right">5</td>
<td>0.749 ～ 0.75</td>
<td align="right">0</td>
</tr>
<tr>
<td align="right">6</td>
<td>0.75 ～ 0.751</td>
<td align="right">0</td>
</tr>
<tr>
<td align="right">7</td>
<td>0.751 ～ 0.752</td>
<td align="right">0</td>
</tr>
<tr>
<td align="right">8</td>
<td>0.752 ～ 0.753</td>
<td align="right">1</td>
</tr>
<tr>
<td align="right">9</td>
<td>0.753 ～ 0.754</td>
<td align="right">1</td>
</tr>
<tr>
<td align="right">10</td>
<td>0.754 ～ 0.755</td>
<td align="right">0</td>
</tr>
<tr>
<td align="right">11</td>
<td>0.755 ～ 0.756</td>
<td align="right">3</td>
</tr>
<tr>
<td align="right">12</td>
<td>0.756 ～ 0.757</td>
<td align="right">4</td>
</tr>
<tr>
<td align="right">13</td>
<td>0.757 ～ 0.758</td>
<td align="right">4</td>
</tr>
<tr>
<td align="right">14</td>
<td>0.758 ～ 0.759</td>
<td align="right">3</td>
</tr>
<tr>
<td align="right">15</td>
<td>0.759 ～ 0.76</td>
<td align="right">5</td>
</tr>
<tr>
<td align="right">16</td>
<td>0.76 ～ 0.761</td>
<td align="right">7</td>
</tr>
<tr>
<td align="right">17</td>
<td>0.761 ～ 0.762</td>
<td align="right">2</td>
</tr>
<tr>
<td align="right">18</td>
<td>0.762 ～ 0.763</td>
<td align="right">7</td>
</tr>
<tr>
<td align="right">19</td>
<td>0.763 ～ 0.764</td>
<td align="right">4</td>
</tr>
<tr>
<td align="right">20</td>
<td>0.764 ～ 0.765</td>
<td align="right">4</td>
</tr>
<tr>
<td align="right">21</td>
<td>0.765 ～ 0.766</td>
<td align="right">7</td>
</tr>
<tr>
<td align="right">22</td>
<td>0.766 ～ 0.767</td>
<td align="right">5</td>
</tr>
<tr>
<td align="right">23</td>
<td>0.767 ～ 0.768</td>
<td align="right">4</td>
</tr>
<tr>
<td align="right">24</td>
<td>0.768 ～ 0.769</td>
<td align="right">1</td>
</tr>
<tr>
<td align="right">25</td>
<td>0.769 ～ 0.77</td>
<td align="right">6</td>
</tr>
<tr>
<td align="right">26</td>
<td>0.77 ～ 0.771</td>
<td align="right">4</td>
</tr>
<tr>
<td align="right">27</td>
<td>0.771 ～ 0.772</td>
<td align="right">2</td>
</tr>
<tr>
<td align="right">28</td>
<td>0.772 ～ 0.773</td>
<td align="right">4</td>
</tr>
<tr>
<td align="right">29</td>
<td>0.773 ～ 0.774</td>
<td align="right">0</td>
</tr>
<tr>
<td align="right">30</td>
<td>0.774 ～ 0.775</td>
<td align="right">3</td>
</tr>
<tr>
<td align="right">31</td>
<td>0.775 ～ 0.776</td>
<td align="right">3</td>
</tr>
<tr>
<td align="right">32</td>
<td>0.776 ～ 0.777</td>
<td align="right">3</td>
</tr>
<tr>
<td align="right">33</td>
<td>0.777 ～ 0.778</td>
<td align="right">3</td>
</tr>
<tr>
<td align="right">34</td>
<td>0.778 ～ 0.779</td>
<td align="right">1</td>
</tr>
<tr>
<td align="right">35</td>
<td>0.779 ～ 0.78</td>
<td align="right">1</td>
</tr>
<tr>
<td align="right">36</td>
<td>0.78 ～ 0.781</td>
<td align="right">1</td>
</tr>
<tr>
<td align="right">37</td>
<td>0.781 ～ 0.782</td>
<td align="right">0</td>
</tr>
<tr>
<td align="right">38</td>
<td>0.782 ～ 0.783</td>
<td align="right">1</td>
</tr>
<tr>
<td align="right">39</td>
<td>0.783 ～ 0.784</td>
<td align="right">2</td>
</tr>
</tbody>
</table>

まぁ何というか…「**伝統の職人芸の世界**」？