本文簡要介紹 python 語言中 scipy.stats.median_test
的用法。
用法:
scipy.stats.median_test(*samples, ties='below', correction=True, lambda_=1, nan_policy='propagate')#
執行 Mood 中位數檢驗。
檢驗兩個或多個樣本是否來自具有相同中位數的總體。
讓
n = len(samples)
是樣本數。計算所有數據的“grand median”,並通過將每個樣本中的值分類為高於或低於總中位數形成列聯表。列聯表,以及更正和lambda_, 傳遞給scipy.stats.chi2_contingency計算檢驗統計量和 p 值。- sample1, sample2, …: array_like
樣本集。必須至少有兩個樣本。每個樣本必須是包含至少一個值的一維序列。樣本不需要具有相同的長度。
- ties: str,可選
確定等於總中位數的值如何在列聯表中分類。字符串必須是以下之一:
"below": Values equal to the grand median are counted as "below". "above": Values equal to the grand median are counted as "above". "ignore": Values equal to the grand median are not counted.
默認值為“below”。
- correction: 布爾型,可選
如果為真,並且隻有兩個樣本,則在計算與列聯表相關的檢驗統計量時應用 Yates 的連續性校正。默認為真。
- lambda_: float 或 str,可選
默認情況下,此測試中計算的統計量是 Pearson 的卡方統計量。lambda_允許使用來自Cressie-Read 功率散度族的統計信息。看scipy.stats.power_divergence詳情。默認值為 1(皮爾遜卡方統計量)。
- nan_policy: {‘propagate’, ‘raise’, ‘omit’},可選
定義當輸入包含 nan 時如何處理。 ‘propagate’ 返回 nan,‘raise’ 引發錯誤,‘omit’ 執行忽略 nan 值的計算。默認為‘propagate’。
- res: MedianTestResult
包含屬性的對象:
- 統計 浮點數
檢驗統計量。返回的統計數據由 lambda_ 確定。默認值為 Pearson 的卡方統計量。
- p值 浮點數
檢驗的 p 值。
- 中位數 浮點數
大中位數。
- 表格 ndarray
列聯表。表格的形狀是 (2, n),其中 n 是樣本數。第一行保存總中位數以上的值的計數,第二行保存總中位數以下的值的計數。該表允許使用例如
scipy.stats.chi2_contingency
或如果有兩個樣本使用scipy.stats.fisher_exact
進行進一步分析,而無需重新計算表。如果nan_policy
是 “propagate” 並且輸入中有 nans,則table
的返回值為None
。
參數 ::
返回 ::
注意:
參考:
[1]Mood, A. M.,統計理論導論。 McGraw-Hill (1950),第 394-399 頁。
[2]Zar, J. H.,生物統計分析,第 5 版。普倫蒂斯·霍爾 (2010)。請參閱第 8.12 節和第 10.15 節。
例子:
一位生物學家進行了一項實驗,其中有三組植物。第 1 組有 16 株植物,第 2 組有 15 株植物,第 3 組有 17 株植物。每株植物都會產生許多種子。每組的種子數為:
Group 1: 10 14 14 18 20 22 24 25 31 31 32 39 43 43 48 49 Group 2: 28 30 31 33 34 35 36 40 44 55 57 61 91 92 99 Group 3: 0 3 9 22 23 25 25 33 34 34 40 45 46 48 62 67 84
以下代碼將 Mood 中位數檢驗應用於這些樣本。
>>> g1 = [10, 14, 14, 18, 20, 22, 24, 25, 31, 31, 32, 39, 43, 43, 48, 49] >>> g2 = [28, 30, 31, 33, 34, 35, 36, 40, 44, 55, 57, 61, 91, 92, 99] >>> g3 = [0, 3, 9, 22, 23, 25, 25, 33, 34, 34, 40, 45, 46, 48, 62, 67, 84] >>> from scipy.stats import median_test >>> res = median_test(g1, g2, g3)
中位數是
>>> res.median 34.0
列聯表是
>>> res.table array([[ 5, 10, 7], [11, 5, 10]])
p 太大,無法得出中位數不相同的結論:
>>> res.pvalue 0.12609082774093244
“G-test” 可以通過將
lambda_="log-likelihood"
傳遞給median_test
來執行。>>> res = median_test(g1, g2, g3, lambda_="log-likelihood") >>> res.pvalue 0.12224779737117837
中位數在數據中出現了多次,因此如果使用例如
ties="above"
,我們將得到不同的結果:>>> res = median_test(g1, g2, g3, ties="above") >>> res.pvalue 0.063873276069553273
>>> res.table array([[ 5, 11, 9], [11, 4, 8]])
此示例表明,如果數據集不大且值等於中位數,則 p 值可能對關係的選擇很敏感。
相關用法
- Python SciPy stats.median_abs_deviation用法及代碼示例
- Python SciPy stats.median_absolute_deviation用法及代碼示例
- Python SciPy stats.multiscale_graphcorr用法及代碼示例
- Python SciPy stats.multivariate_normal用法及代碼示例
- Python SciPy stats.multivariate_hypergeom用法及代碼示例
- Python SciPy stats.mode用法及代碼示例
- Python SciPy stats.monte_carlo_test用法及代碼示例
- Python SciPy stats.multivariate_t用法及代碼示例
- Python SciPy stats.matrix_normal用法及代碼示例
- Python SciPy stats.mood用法及代碼示例
- Python SciPy stats.moyal用法及代碼示例
- Python SciPy stats.maxwell用法及代碼示例
- Python SciPy stats.mvsdist用法及代碼示例
- Python SciPy stats.moment用法及代碼示例
- Python SciPy stats.multinomial用法及代碼示例
- Python SciPy stats.mielke用法及代碼示例
- Python SciPy stats.mannwhitneyu用法及代碼示例
- Python SciPy stats.anderson用法及代碼示例
- Python SciPy stats.iqr用法及代碼示例
- Python SciPy stats.genpareto用法及代碼示例
- Python SciPy stats.skewnorm用法及代碼示例
- Python SciPy stats.cosine用法及代碼示例
- Python SciPy stats.norminvgauss用法及代碼示例
- Python SciPy stats.directional_stats用法及代碼示例
- Python SciPy stats.invwishart用法及代碼示例
注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.stats.median_test。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。