<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kozma Taylor on Medium]]></title>
        <description><![CDATA[Stories by Kozma Taylor on Medium]]></description>
        <link>https://medium.com/@kozmataylor?source=rss-2bd136ffba1b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*VNQuT42SkknD8_LK</url>
            <title>Stories by Kozma Taylor on Medium</title>
            <link>https://medium.com/@kozmataylor?source=rss-2bd136ffba1b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 15:56:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kozmataylor/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[如何正確的畫直方圖(histogram)？]]></title>
            <link>https://medium.com/@kozmataylor/%E5%A6%82%E4%BD%95%E6%AD%A3%E7%A2%BA%E7%9A%84%E7%95%AB%E7%9B%B4%E6%96%B9%E5%9C%96-histogram-47079972ab71?source=rss-2bd136ffba1b------2</link>
            <guid isPermaLink="false">https://medium.com/p/47079972ab71</guid>
            <category><![CDATA[histograms]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-distribution]]></category>
            <dc:creator><![CDATA[Kozma Taylor]]></dc:creator>
            <pubDate>Mon, 27 Nov 2023 03:53:58 GMT</pubDate>
            <atom:updated>2023-11-27T03:53:58.201Z</atom:updated>
            <content:encoded><![CDATA[<p>當我們蒐集到一組數值型資料，想觀察這組資料的分布，直方圖是一個直觀、且十分方便的一個圖形，因為它可以忠實呈現這組資料在各區域的數量或是比例。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*r5tytDHIX0i01uEGJPpsvw.jpeg" /><figcaption>以R語言畫的鳶尾花萼片長度分布直方圖</figcaption></figure><p>隨著時代的發展，要畫直方圖不是件困難的事情，多數工具只要一個指令、一個按鈕都可以快速的把直方圖畫出來。但是否有思考過，這些指令是怎麼運作的？到底一組資料應該切幾個區間才是適當的？到底區間的寬度要怎麼決定？</p><p>一份直方圖到底應該有多少個直條(專有名詞叫 bin，之後都用bin來表示)也就是區間，這是一個哲學問題，bin數量太少畫出的分布會不細緻；太多又容易遇到資料斷層，導致分布中斷。另外不要忘記統計圖表最重要的原則就是<strong>方便閱讀</strong>。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lq3BQq3cLfZzmWsF5Qn98w.jpeg" /><figcaption>左圖)只有兩條直條，無法得到細緻的分布。右圖)由於資料筆數只有150筆，bin切分太細勢必會有區間資料太少而造成分布上下起伏過大，甚至有斷層。</figcaption></figure><p>從1892年Karl Pearson第一次提出”histogram”這個名詞，已經有許多方法供使用者選擇。如：1926年Herbert A. Sturges、1979年David W. Scott以及1981年David Freedman &amp; Persi Diaconis。</p><p>以Sturges的提出方法為例，他提出每條bin最適合的寬度<em>C</em>是：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*v4N9KBdzQAV9jL46Jgbc9g.png" /></figure><p>其中<em>R</em>代表值域，即最大值減最小值；<em>N</em>代表筆數。而值域除以最適寬度，即為最適bin個數。</p><p>眼尖的你應該也發現了，這個最適個數除了資料筆數是2的倍數外，其他時候都不會是整數，我們可畫不出0.123456789個bin這種東西。所以取大於最適個數的整數最小值即可(俗稱無條件進位)：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/286/1*1ztymTlftQ846Hfrs4ZDpA.png" /></figure><p>以R語言的150筆鳶尾花資料為例，最適合的bin數是9個；每個bin在萼片長度bin最適合的寬度是0.4。9個bin區域分別是4.3~4.7, 4.7~5.1, 5.1~5.5, 5.5~5.9, 5.9~6.3, 6.3~6.7, 6.7~7.1, 7.1~7.5, 7.5~7.9。處理完了分布顯示問題，接下來需要一些優化讓這張圖更方便閱讀。</p><p>針對這個問題1988年出版的<em>The New S Language</em>作者們在書中提到了一個原則：最容易閱讀的bin寬度為 1, 2或5乘以10的<em>n</em>次方，其中<em>n</em>為整數。而刻度的上下界則是bin寬度的倍數</p><p>照著此原則bin的寬度由0.4變為0.5，區域的起始點從原本的4.3更動為4.0。bin的區域則改為：4.0~4.5, 4.5~5.0, 5.0~5.5, 5.5~6.0, 6.0~6.5, 6.5~7.0, 7.0~7.5, 7.5~8.0。跟視覺優化之前相比bin的個數從9個變成了8個，也就是畫直方圖時會需要犧牲一點分布呈現，讓畫面更易閱讀，畢竟”方便閱讀”是畫統計圖表最重要的一件事。</p><p>總結一下多數工具畫直方圖的流程：</p><pre>1. 決定最適合的bin數/bin寬度<br>2. 優化bin數/bin寬度，取得更容易閱讀的區間刻度<br>3. 計算各區間的資料筆數</pre><p><em>以上說明以R語言的流程為主，其他工具流程細節可能不同但大同小異。</em></p><p>References:<br>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth &amp; Brooks/Cole.<br>Freedman, D. and Diaconis, P. (1981). On the histogram as a density estimator: <em>L_2</em> theory. <em>Zeitschrift für Wahrscheinlichkeitstheorie und verwandte Gebiete</em>, <strong>57</strong>, 453–476. doi: <a href="https://doi.org/10.1007/BF01025868">10.1007/BF01025868</a>.<br>Scott, D. W. (1979). On optimal and data-based histograms. <em>Biometrika</em>, <strong>66</strong>, 605–610. doi: <a href="https://doi.org/10.2307/2335182">10.2307/2335182</a>.<br>Sturges, H. A. (1926). The choice of a class interval. <em>Journal of the American Statistical Association</em>, <strong>21</strong>, 65–66. doi: <a href="https://doi.org/10.1080/01621459.1926.10502161">10.1080/01621459.1926.10502161</a>.<br><a href="https://github.com/SurajGupta/r-source/blob/master/src/library/base/R/pretty.R">https://github.com/SurajGupta/r-source/blob/master/src/library/base/R/pretty.R</a><br><a href="https://github.com/SurajGupta/r-source/blob/master/src/library/graphics/R/hist.R">https://github.com/SurajGupta/r-source/blob/master/src/library/graphics/R/hist.R</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47079972ab71" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>