# 補充:Exploratory Data Analysis ## stem-and-leaf display 當我們要去探索一個 unknown distribution 的 characteristics 時,我們會去取一個含 $n$ 個 observations 的 sample: \begin{equation} x_1, \ x_2, \ ..., \ x_n \end{equation} 得到這些 observations,我們常常會需要把它們由小排到大。其中一種簡便的方式叫做 <font color = "snake">stem-and-leaf display</font>。 直接來看個例子,假設我們有五十筆考試成績的資料如下: ![image](https://hackmd.io/_uploads/HJx1ZpxF0.png) 如果要整理這些 data,我們可以用像是 histogram 來表示: ![image](https://hackmd.io/_uploads/SJaVbpxtC.png) 但是 histogram 沒辦法紀錄原本的值(像是在上圖中我們只能看到在每個區間的比例),而 stem-and-leaf display 卻可以做到。這種方法很簡單,直接看個圖就清楚了: ![image](https://hackmd.io/_uploads/rkDpbTxKA.png) > 我們把每一筆 data 的十位數當作 <font color = "snake">stem</font>,個位數當作 <font color = "snake">leaf</font>,且 stem 按照數字大小排好。 > > 上面分數的例子裡,第一筆是 $93$,所以我們在 stem $=9$ 的隔壁那欄 leaves 第一個數字擺上 $3$。 > > 如果我們遇到具有相同 stem 的 data,那就把 leaf 擺到前一個 leaf 後面。 > > $\rightarrow$ 根據這個表,我們也可以去記錄每個 stem 值的 frequency(stem 為這個數字的 data 共出現幾次。) ### ordered stem-and-leaf display 如果我們要再進一步把每個 stem 的 leaves 也由小排到大,那就稱作 <font color = "snake">ordered stem-and-leaf display</font>。 跟上面的例子相同,只是 leaves 也照小到大排: ![image](https://hackmd.io/_uploads/rk6dXaeFC.png) #### modification 同樣是 ordered stem-and-leaf display,但是我們可以對這個表做一些 modification,假設和前面都是同個例子,我們想再對每個十位數相同的分數再進一步將個位數區分成兩類: - 尾數為 $\{0,1,2,3,4\}$ 的(stem 用一個 $*$ 表示) - 尾數為 $\{5,6,7,8,9\}$ 的(stem 用一個 $・$ 表示) 因此每個 stem 值都可以分成兩種,如下圖: ![image](https://hackmd.io/_uploads/SySi76eYR.png) ## order statistics 前面我們把 data 表示成 ordered stem-and-leaf diagram 是有原因的,不過我們要先來定義一個名詞: :::info 對一個含 $n$ 個 observations 的 sample: \begin{equation} x_1, \ x_2, \ ..., \ x_n \end{equation} 如果我們將這些 observations 由小到大排好,那產生出來的 ordered data 就稱作 <font color = "blue">order statistics</font>。 ::: 統計學家發現 order statistics 和一些跟它們有關的 functions 很有用。 > 這部分下一小節會講,如果我有寫 XD 延續前面的例子,如果我們把那五十筆成績由小到大排好,形成 order statistics,那就會長這樣: ![image](https://hackmd.io/_uploads/Bk01P6eF0.png) 從像這樣的 order statistics 或是前面對應的 ordered stem-and-leaf display,我們很容易就能找到 <font color = "snake">sample percentiles</font>。 在講 sample percentiles 之前,先來定義一些會用到的名詞。 ### percentile, median, quartiles 我們說 <font color = "snake">$(100p)$th percentile</font> 是一個值 ==$\pi_p$==,使得我們的 pdf $f(x)$ 在從最左端到 $\pi_p$ 底下的面積為 $p$,也就是: :::info \begin{equation} p = \int_{-\infty}^{\pi_p}f(x)\,dx = F(\pi_p) \end{equation} ::: 舉例來說,常用的 ++$50$th percentile++ 就是 <font color = "snake">median</font>,我們用 ==$m=\pi_{0.5}$== 表示。 > $\because 50$th percentile $=(100 \times 50)$th percentile > $\therefore \ p=0.5 \ \Rightarrow \pi_p = \pi_{0.5}$ 除此之外,我們也另外給 $25, \ 50$th percentile 一個名稱: - $25$th percentile $\Rightarrow$ <font color = "snake">first quartile</font>,用 ==$q_1 = \pi_{0.25}$== 表示。 - $75$th percentile $\Rightarrow$ <font color = "snake">third quartile</font>,用 ==$q_3 = \pi_{0.75}$== 表示。 > 當然 median 也可以叫作 <font color = "snake">second quartile</font> $m=\pi_{0.5}=q_2$ 簡單看個圖例就很清楚了: ![image](https://hackmd.io/_uploads/HJk23agtA.png) --- ## sample percentiles ### 計算方法 回到前面的 sample percentile。 假設我們想知道一個 sample 的 median,也就是 $50$th percentile,那我們要怎麼求呢?也就是按照上面圖例,我們要怎麼知道 $\pi_{0.5}$ 是多少?意思又等同於如果我們想知道 $X$ 要落在什麼樣的值以內,發生的機率會佔所有可能的 $50%$,這要怎麼求? 先說結論再看解釋: :::success 如果要取 $(100p)$th sample percentile $\pi_p$ $\rightarrow$ 等同取 $(n+1)p$ th order statistic ::: 其實這個方法的想法來自下圖中藍色框框的說明: ![image](https://hackmd.io/_uploads/ByNhtRxtR.png) ### 例一 直接看個例子: ![image](https://hackmd.io/_uploads/Hk-liReYA.png) > 因為 $(n+1)p$ 的 $p$ 一定是一個介於零和一之間的小數,所以乘一乘就很有可能得到小數的結果。 > > $\rightarrow$ 如果結果是整數 $t$,那我們就很簡單的取第 $t$ 小的 observation 值 $y_t$。 > $\rightarrow$ 如果結果是小數,其實很直觀的,我們就用整數的部分 $r$ 去取第 $r$ 小的值 $y_r$,再加上剩下的小數部分 $\frac{a}{b}$ 去乘上 order statistic 中 $y_r$ 和下一項 $y_{r+1}$ 之間的差距。 > > 進一步說明如下圖: ### 當 (n+1)p 為小數 ![image](https://hackmd.io/_uploads/S19CfJbtA.png) > 因為在求 $\pi_p$ 時所計算的 $(n+1)p$ 不是剛好落在某個整數,因此我們以 ==$\tilde{\pi}_p$== 表示。 > > 此外,當我們觀察原本的式子,會發現如果整理一下,會得到第二個等號的式子,由於我們可以由 $y_r$ 和 $y_{r+1}$ 各自乘上一個比例去求得介於它們之間的 $\tilde{\pi}_p$,所以這樣子的觀點也有了 linear interpolation 的意義在。 ### 計算方式的來由 前面我們直接就給出了求 sample percentile 的方式,但有兩個問題: :::danger 1. 為什麼我們取 $(n+1)p$ th order statistic 而不是 $np$ th order statistic? 2. 這兩者之間為什麼有這樣的對應關係,背後的意義是什麼? ::: 先來回答第一個問題,直接看我的筆記的圖我想就很清楚了: ![image](https://hackmd.io/_uploads/SJdONyWKC.png) 至於第二個問題,一樣看筆記圖,如果覺得轉不過來,就再看一次 $\pi_p$ 和 $Y_r$ 所代表的意義: ![image](https://hackmd.io/_uploads/HyITVJZKA.png) ### 例二 根據前面五十筆考試成績的例子。 假設我們要計算 median $\pi_{0.5}$, first quartile $\pi_{0.25}$, third quartile $\pi_{0.75}$,我們會發現為了算這三個值,我們求出來的 $(n+1)p$ 都是小數。 因此像上面一樣,當 $(n+1)p$ 為小數時,我們就多加個波浪符號表示,因為都是小數,所以都會用到兩個值 $y_{r}, \ y_{r+1}$,詳細計算過程見再下一張圖。 > 圖中 $(n+1)p$ 用紫色標示並圈起來,$y_{r}, \ y_{r+1}$ 則是用綠色框圈起。 ![image](https://hackmd.io/_uploads/BktcvyZYA.png) ![image](https://hackmd.io/_uploads/H1KRuJbtC.png) > 意思是: > - 大概有 $50 \%$ 的人分數落在 $71$ 分以下 > - 大概有 $25 \%$ 的人分數落在 $58.75$ 分以下 > - 大概有 $75 \%$ 的人分數落在 $81.25$ 分以下 這些值的 notation 基本上和前面都相同,只是多加上波浪符號,也就是變成: \begin{equation} \begin{split} \tilde{q}_1 &= \tilde{\pi}_{0.25} \\ \tilde{q}_2 &= \tilde{\pi}_{0.50} = \tilde{m} \\ \tilde{q}_3 &= \tilde{\pi}_{0.75} \\ \end{split} \end{equation} ## 專有名詞 除了 quartile, percentile 以外,我們還有其他的一些特殊名稱,這裡我們列出一些下面會用到或常用的。 ### decile 我們稱每十個 percentile 為 <font color = "snake">decile</font>,例如: - second decile $=20$th percentile - ninth decile $=90$th percentile ### five-number summary 我們將: :::info 1. first quartile $\tilde{q}_1$ 2. second quartile $\tilde{q}_2$ = median $\tilde{m}$ 3. third quartile $\tilde{q}_3$ 4. minimum 5. maximum ::: 這五個值合稱為 <font color = "snake">five-number summary</font> ### interquartile range (IQ / IQR) <font color = "snake">interquartile range (IQ / IQR)</font>,代表++第三和第一個 quartile 間的差距++,也就是: :::info \begin{equation} \tilde{q}_3 - \tilde{q}_1 = \tilde{\pi}_{0.75} - \tilde{\pi}_{0.25} \end{equation} ::: ### range / midrange - <font color = "snake">range</font>:maximum 和 minimum 間的差距 - <font color = "snake">midrange</font>:maximum 和 minimum 的平均,也就是兩者相加除二 $\frac{y_1 + y_n}{2}$ ## box-and-whisker diagram: 呈現 five-number summary 的方式 其中一種以圖像來呈現 five-number summary 的方式叫做 <font color = "snake">box-and-whisker diagram (box plot)</font>,我們直接用一個例子來看畫這個 diagram 的方式: 假設我們有一百筆 data,如果將小數第二位用圖中的說明分類,形成的 ordered stem-and-leaf diagram 如下: ![image](https://hackmd.io/_uploads/r1q2IK4t0.png) 我們可以直接看出最小值 $y_1 = 0.85$,最大值 $y_{100} = 1.06$,其餘三個 quartile 稍微計算後得到的結果如下,這五個值為這一百筆 data 的 five-number summary: ![image](https://hackmd.io/_uploads/BJUrwt4tC.png) 要建構這個 five-number summary 的 box-and-whisker diagram,首先我們根據 data 大概的值畫一條水平軸。 接著在這條軸上面畫一個 box,兩邊分別為 first, third quartile,box 裡面,我們根據 median 的值畫一條垂直線。 box 兩邊我們要畫 whisker,也就是從 box 的左邊 first quartile 的中間畫一條水平線延伸到 minimum;box 的右邊 third quartile 的中間畫一條水平線延伸到 maximum。 ![image](https://hackmd.io/_uploads/By8Twt4tR.png) - 右邊的 whisker 比較長、box 的右半部大於左半部,代表了我們的 data <font color = "green">skewed to the right</font>。 > 因為圖形在這樣的情況下,代表有比較多的 data 會落在 $\tilde{q}_2$ 到 max。 > > $\rightarrow$ 如果我們畫出這一百筆 data 的 histogram 或看它的 stem-and-leaf diagram,也能發現這樣的 skewness。 ## outliers 有時候我們會想挑出那些看起來++明顯比其他 observations 大或小的 observations++,也就是說我們想找出 <font color = "snake">outliers</font>。 課本沒有特別去定義 outliers,但在 Engineering Statistics Handbook 中,它定義 outliers 如下: > 關於 Engineering Statistics Handbook 可見參考資料連結。 :::info An outlier is an observation that lies an abnormal distance from other values in a random sample from a population. ::: > 和我們上面大概描述的類似,outlier 就是 random sample 中,和其他的值之間有著「不正常」的距離的 observations。 > > 至於怎麼樣才算 “abnormal”,要由分析資料的人來決定。 有兩種用圖像來找 outliers 的方式,一種是利用 scatter plots,另一種是利用 box-and-whisker diagram,課本講的是後者,因此我們就來看看用 box plot 的情形。 我們直接再看一個例子,順便看看如果 data 是 <font color = "green">skewed to the left</font> 的情形。 假設我們有一些 data,省略得到 five-number summary 的過程,我們直接看這些 data 的 box plot: ![image](https://hackmd.io/_uploads/Bk3zJc4YC.png) 這個 box-and-whisker diagram 的 box 左半部和右半部一樣大,但是 left whisker 比 right whisker 長上許多,代表這些 data 是 skewed to the left。 為了找出 outliers,我們要在這張圖上畫幾條「圍籬」把比較極端的 data 區隔出來,我們建立兩種 fences: - <font color = "snake">inner fence</font> $\Rightarrow$ 距離 box 左右兩側 $1.5$ IQR 的位置 - <font color = "snake">outer fence</font> $\Rightarrow$ 距離 box 左右兩側 $3$ IQR 的位置 如果以數學表示: :::info \begin{equation} \begin{split} \text{lower inner fence: } \quad &q_1 - 1.5 \text{ IQR} \\ \text{upper inner fence: } \quad &q_3 + 1.5 \text{ IQR} \\ \text{lower outer fence: } \quad &q_1 - 3 \text{ IQR} \\ \text{upper outer fence: } \quad &q_3 + 3 \text{ IQR} \\ \end{split} \end{equation} ::: > lower 指的也就是左側($q_1$ 的左方),upper 指的是右側($q_3$ 的右方)。 其中: - 介於 inner fence 和 outer fence 之間的 observations,就是 <font color = "snake">suspected outliers</font> - 在 outer fence 之外的 observations,就是 <font color = "snake">outliers</font>。 直接看我們這個例子的 box plot 畫完 inner, outer fences 後的情形: > Note:在圖中 > - 超過 inner fence 的 observations 我們會用一個圈(·)表示,或是有的 program 會用 asterisk (*)表示。 > - whiskers 我們只會畫到 inner fences 上或以內。 ![image](https://hackmd.io/_uploads/BkwYfcVK0.png) 從上面的例子看來,從 data 中找出 outliers 的方式好像很簡單,畫畫圖就好了,但是這僅限於 data 是 univariate 的情形,當 data 為 multivariate 時,辨識 outliers 的方式就會變得複雜許多。 因為篇幅的關係,這篇筆記就結束在這裡,涵蓋了 Hogg 的 Probabiltiy 課本 6.2 節的內容。剩下的 multivariate case 補充可參考筆記「[補充:multivariate outliers](https://hackmd.io/@pipibear/rkalaWUtR)」。 # 參考資料 - Hogg,Tanis,Zimmerman, Probability and Statistical Inference, 9th ed(2015), p.91-92, 238-245 > p. 238-245 為 Section 6.2 Exploratory Data Analysis - PennState STAT415 [18.3 Sample Percentiles](https://online.stat.psu.edu/stat415/lesson/18/18.3) - NIST: [Engineering Statistics Handbook - 7.1.6. What are outliers in data?](https://www.itl.nist.gov/div898/handbook/prc/section1/prc16.htm)