單一樣本柯爾莫哥洛夫-斯米爾諾夫適合度檢定 (One Sample Kolmogorov-Smirnov Goodness of fit test)

套路49: 單一樣本柯爾莫哥洛夫-斯米爾諾夫適合度檢定
(One Sample Kolmogorov-Smirnov Goodness of fit test)

1. 使用時機: 單一樣本柯爾莫哥洛夫-斯米爾諾夫適合度檢定用來分析數據之分佈型(distribution)
2. 分析類型: 無母數分析(non-parametric analysis)
3. 單一樣本柯爾莫哥洛夫-斯米爾諾夫檢定前提假設: 無。
4. 範例資料: 某研究在一棵25公尺高的樹幹的不同高度(m)觀察到某種蛾。資料如下:
編號
1
2
3
4
5
6
7
8
9
10
11
12
13
14
高度
1.4
2.6
3.3
4.2
4.7
5.6
6.4
7.7
9.3
10.6
11.5
12.4
18.6
22.3
蛾數
1
1
1
1
1
2
1
1
1
1
1
1
1
1
試問蛾在樹上不同高度的分布是否為均勻分布(uniform distribution)?
H0: 蛾在樹上不同高度的分布是均勻分布。HA: 蛾在樹上不同高度的分布不是均勻分布。

5. 使用R計算單一樣本柯爾莫哥洛夫-斯米爾諾夫檢定方法一:
第一步: 安裝EnvStats程式套件
第二步: 呼叫EnvStats程式套件備用
  library(EnvStats)
第三步: 輸入建立資料
  y <- c(1.4, 2.6, 3.3, 4.2, 4.7, 5.6, 6.4, 7.7, 9.3, 10.6, 11.5, 12.4, 18.6, 22.3)
第四步: 使用EnvStats程式套件的gofTest函數代入y計算柯爾莫哥洛夫-斯米爾諾夫檢定
  gofTest(y~1, test = "ks", distribution = "unif", alternative = "two.sided")
  # y~1只有一組樣本(one-sample test)
  # test = "ks" 執行柯爾莫哥洛夫-斯米爾諾夫檢定
  # distribution = "unif" 檢定的是資料是否為均勻分布(uniform distribution)
第五步: 判讀結果
Results of Goodness-of-Fit Test
-------------------------------
Test Method:                     Kolmogorov-Smirnov GOF
Hypothesized Distribution:          Uniform
Estimated Parameter(s):            min = 1.4
                                   max = 22.3
Estimation Method:               mle
Data:                           y
Sample Size:                     14
Test Statistic:                     ks = 0.3308271
Test Statistic Parameter:            n = 14
P-value:                         0.07241889
Alternative Hypothesis:            True cdf does not equal the Uniform Distribution.
Warning message:
In ksGofTest(x = c(1.4, 2.6, 3.3, 4.2, 4.7, 5.6, 6.4, 7.7, 9.3,  :
  The standard Kolmogorov-Smirnov test is very conservative (Type I error smaller than assumed; high Type II error) for testing departures from the Uniform distribution when you have to estimate the distribution parameters.
  # P-value < 0.05H0: 蛾在樹上不同高度的分布是均勻分布不成立。
  # P-value > 0.05H0: 蛾在樹上不同高度的分布是均勻分布成立。

6. 使用R計算單一樣本柯爾莫哥洛夫-斯米爾諾夫檢定方法二:
第一步: 安裝dgof程式套件
第二步: 呼叫dgof程式套件備用
  library(dgof)
第三步: 輸入建立資料
  x <- c(1.4, 2.6, 3.3, 4.2, 4.7, 5.6, 6.4, 7.7, 9.3, 10.6, 11.5, 12.4, 18.6, 22.3)
第四步: 使用dgof程式套件的ks.test函數代入y計算柯爾莫哥洛夫-斯米爾諾夫檢定
  dgof::ks.test(x, punif, alternative = "two.sided", exact = TRUE, tol = 1e-8,
 simulate.p.value = TRUE, B = 2000)
  # dgof::ks.test 基本模組(base)也有ks.test函數為了區別,再函數名稱前加程式套件dgof::
  # x只有一組樣本(one-sample test)
  # punif 檢定的是資料是否為均勻分布(uniform distribution)
  # exact = TRUE, simulate.p.value = TRUE, B = 2000估計p值。
第五步: 判讀結果
        One-sample Kolmogorov-Smirnov test
data:  x
D = 1, p-value < 2.2e-16
alternative hypothesis: two-sided
  # p < 0.05H0: 蛾在樹上不同高度的分布是均勻分布不成立。
  # p > 0.05H0: 蛾在樹上不同高度的分布是均勻分布成立。

7. 使用R計算單一樣本柯爾莫哥洛夫-斯米爾諾夫檢定方法三:
第一步: 安裝kolmim程式套件
第二步: 呼叫kolmim程式套件備用
  library(kolmim)
第三步: 輸入建立資料
  x <- c(1.4, 2.6, 3.3, 4.2, 4.7, 5.6, 6.4, 7.7, 9.3, 10.6, 11.5, 12.4, 18.6, 22.3)
第四步: 使用kolmim程式套件的ks.test.imp函數代入x計算柯爾莫哥洛夫-斯米爾諾夫檢定
  ks.test.imp(x, punif)
  # x只有一組樣本(one-sample test)
  # punif 檢定的是資料是否為均勻分布(uniform distribution)
第五步: 判讀結果
        One-sample two-sided exact Kolmogorov-Smirnov test
data:  x
D = 1, p-value = 2.22e-16
alternative hypothesis: two-sided
  # p < 0.05H0: 蛾在樹上不同高度的分布是均勻分布不成立。
  # p > 0.05H0: 蛾在樹上不同高度的分布是均勻分布成立。

8. 使用R計算單一樣本柯爾莫哥洛夫-斯米爾諾夫檢定方法四:
第一步: 輸入建立資料
  x <- c(1.4, 2.6, 3.3, 4.2, 4.7, 5.6, 6.4, 7.7, 9.3, 10.6, 11.5, 12.4, 18.6, 22.3)
第二步: 使用基本模組(base)中的ks.test函數代入x計算柯爾莫哥洛夫-斯米爾諾夫檢定
  stats::ks.test(x, punif, alternative = "two.sided", exact = TRUE)
  # x只有一組樣本(one-sample test)
  # punif 檢定的是資料是否為均勻分布(uniform distribution)
  # exact = TRUE估計p
第三步: 判讀結果
        One-sample Kolmogorov-Smirnov test
data:  x
D = 1, p-value < 2.2e-16
alternative hypothesis: two-sided
  # p < 0.05H0: 蛾在樹上不同高度的分布是均勻分布不成立。
  # p > 0.05H0: 蛾在樹上不同高度的分布是均勻分布成立。

來勁了嗎? 想知道更多?? 補充資料(連結):
4. 柯爾莫哥洛夫-斯米爾諾夫檢驗計算公式 (https://en.wikipedia.org/wiki/Kolmogorov%E2%80%93Smirnov_test)
5. 關於R基礎R繪圖及統計快速入門:
   b. Cookbook for R: http://www.cookbook-r.com/
   d. Statistical tools for high-throughput data analysis (STHDA): http://www.sthda.com/english/
e. The Handbook of Biological Statistics: http://www.biostathandbook.com/
f. An R Companion for the Handbook of Biological Statistics: http://rcompanion.org/rcompanion/index.html
6. Zar, JH. 2010. Biostatistical Analysis, Fifth Edition, Pearson.

留言

這個網誌中的熱門文章

統計不球人 目錄 (Table of Contents)

三因子變異數分析 (Three Way ANOVA)

比較二或多組變異數Levene’s 檢定 (Levene’s Test for Comparing Two or More Variances)