COM6005 Statistical Modeling · Module 3a
One-sample & Two-sample t-tests
COM6005 · Module 3a · Source: Module3a_T-test-forMean_L02_complete.pdf (35 slides)
Contents · 13 sections
🎯 本講重點 (Learning outcomes)
- 用 parametric test flowchart 揀啱 test:1-sample t / Z test、paired t、2-sample (independent) t、1-way ANOVA。
- 理解點解用 t 而唔用 Z( 未知、n 細;t 有 heavier tails,df 越大越似 Z)。
- One-sample t-test:寫 、計 、查 t-table()、用 p-value 決定。
- 識講三個等價 decision rule(T vs ppf、cdf、p-value)同 one-sided / two-sided 嘅 rejection rule。
- 由 two-sided decision rule 推出 Confidence interval。
- Two-sample:paired design(計差 W 再做 one-sample t)vs completely randomized design(pooled variance ,)。
⭐ 考試必溫 Top-5
- One-sample t:,;腫瘤例 → reject ()。
- 查 t-table:、(one-sided)→ 1.833 =
t.ppf(0.95, 9);two-sided 用 。 - Rejection rules: → ; → ; → 。
- CI:。
- Paired(,df = 對數 − 1)vs Independent(pooled ,);分辨題目屬邊種 design。
1. 揀邊個 test?Parametric Test Flowchart
Slide 2 嘅 flowchart(Test for Means, 1 Factor):
| 情況 | 條件 | Test |
|---|---|---|
| 1 Sample(同 target mean 比較) | Sample size | 1 Sample t Test |
| 1 Sample(同 target mean 比較) | Sample size | Z Test |
| 2 Samples | Independent data | 2 Sample t Test(equal variance → pooled SD;unequal variance → unpooled SD) |
| 2 Samples | Paired data | Paired t Test |
| > 2 Samples | 1-Way ANOVA |
考題通常俾一個情境,要你講用邊個 test 同點解:幾多組?同一班人前後量(paired)定兩班唔相關嘅人(independent)?n 大定細?
2. Z-distribution vs t-distribution
Slide 3 嘅圖:Normal (Z) 同 t-distribution () 疊埋比較。t 都係對稱鐘形,但中間矮少少、兩邊 tails 厚啲;df 越大越貼近 Z, 已經同 Z 幾乎重疊。
| Z-test | t-test | |
|---|---|---|
| (population SD) | 已知(或 n 大,用 s 近似) | 未知,用 sample SD s 估計 |
| Sample size | (細 sample) | |
| Distribution | t with | |
| Critical value(one-sided ) | 1.645 | 例: → 1.833(較大) |
用 s 代替 會帶多一層不確定性(s 本身都係估出嚟),所以 t 嘅 tails 厚啲,critical value 大啲——要更極端嘅證據先 reject 。n 越大,s 越準,t 就越接近 Z(t-table 最底 ∞ 行 = Z 值:1.645、1.960、2.326、2.576)。
3. One-sample t-test:設定
Slide 4:「病人接受治療後,情況整體有冇改善?」
Slide 5:
- Situation:只有一欄數據 。
- Objective:檢查治療後腫瘤大細。Variable = 腫瘤大細嘅變化(+ve 代表腫瘤縮細)。
- (腫瘤增大或不變);(腫瘤縮細)。
- 同 必須 mutually exclusive(唔重疊)同 collectively exhaustive(包晒所有可能)。
- 例:Y = the decrease in tumor size after treatment。問:有冇證據顯示治療有用?即 test against 。
- 因為量度有誤差,即使治療無效,sample mean 都唔會剛好 = 0。How large is large? → 要 t-test 去判斷。
Slide 5 原文喺 嘅括號入面有劃線修改(decrease / increases 對調過)。正確理解:Y = 縮細量(+ve 好);(冇改善:腫瘤增大或不變);(有改善)。
4. One-sample t-test:Procedure
Slide 6:
- 需要一個 critical value(取決於 significance level )。如果 T 大過 critical value,就當 T 夠大、改善嘅證據夠強 → reject 。
- 傳統上 critical value 喺 t-table 查。
4.1 查 t-table(slides 7–8)
Slide 7 嘅 t-table(“Percentage Points of the t Distribution”):column = (significance level,右尾面積),row = (degree of freedom),格入面 = critical value(x 軸值)。
- Degree of freedom:one-sample 問題 。
- Level of significance :5%(較低標準 reject )或 1%(較高標準)。
- 例:、 → 行 、欄 .05 → critical value = 1.833。
- Python:
scipy.stats.t.ppf(0.95, 9)= 1.833(t-distribution with 嘅 95% quantile)。
常用值(from table):
| 4 | 2.132 | 2.776 | 3.747 | 4.604 |
| 6 | 1.943 | 2.447 | 3.143 | 3.707 |
| 8 | 1.860 | 2.306 | 2.896 | 3.355 |
| 9 | 1.833 | 2.262 | 2.821 | 3.250 |
| ∞ | 1.645 | 1.960 | 2.326 | 2.576 |
呢個 table 嘅 係單邊(右尾)面積。Two-sided test 要查 .025 欄(例如 → 2.262),唔係 .05 欄。
5. Worked example:腫瘤數據(slides 9–12)
Changes:4.7, −2.4, 2.1, 1.9, −1.4, 5.7, −0.2, 0.3, 3.9, 2.8。Mean = 1.74 > 0。有冇證據顯示改善係正?。
- Hypotheses:;(right-tailed; 係 → 係 )。
- 。
- Test statistic:
- → →
- Critical value:、 → 1.833。(p-value = 1 − t.cdf(2.076, 9) = 0.0339)
- Decision:2.076 > 1.833()→ reject 。結論:有證據顯示治療令腫瘤縮細(「腫瘤增大或不變」被 reject)。
import numpy as np
from scipy import stats
y = np.array([4.7, -2.4, 2.1, 1.9, -1.4, 5.7, -0.2, 0.3, 3.9, 2.8])
stats.ttest_1samp(y, 0, alternative='greater') # H1 是 > → H0 是 <=
# TtestResult(statistic=2.0760103699294623, pvalue=0.03385076318199434, df=9)
# 另一種方法得到同一個 probability(slide 12)
T = y.mean() / (np.sqrt(y.var(ddof=1)) / np.sqrt(10)) # 2.0760(ddof=1 → 除 n-1)
p = 1 - stats.t.cdf(T, df=9) # 0.0339
crit = stats.t.ppf(0.95, 9) # 1.833
Slide 10 嘅解讀:p-value = 假設 正確下,得到觀察值(或更極端)嘅機會。p-value 細過 → ‘ is rejected’。;結論: 被 reject,即「腫瘤增大或不變」被 reject。
5.1 三個等價嘅 decision rule(slide 11)
T > t.ppf(0.95, 9) # test statistic 大過 critical value
t.cdf(T, 9) > 0.95 # T 嘅 cumulative probability 大過 95%
1 - t.cdf(T, 9) < 0.05 # p-value 細過 α
1 - t.cdf(T, 9) 就係 p-value。Decision rule:比較 p-value 同你揀嘅 。p-value 冇電腦好難計(所以以前用 t-table + critical value)。
5.2 Assumptions(slide 13)
- n 個 unrelated 病人;佢哋嘅嚴重程度、性別、年齡等相近。
- 需要假設: 係 independent and identically-distributed (i.i.d.) normal random numbers。
Normal assumption 可以用 Module 2c 嘅 QQ-plot 或 stats.normaltest 檢查。
6. Critical-value approach:三種 (slide 14)
| Type | Hypotheses | Reject if | Python |
|---|---|---|---|
| One-sided (right) | vs | = t.ppf(1-α, n-1) | alternative='greater' |
| One-sided (left) | vs | = t.ppf(α, n-1) | alternative='less' |
| Two-sided | vs | = t.ppf(1-α/2, n-1) | alternative='two-sided' |
Slide 15 two-sided 例:stats.ttest_1samp(y, 50, alternative='two-sided'): “equal to 50”, “does not equal to 50”。
7. Worked example:牛油有冇呃秤?(slides 16–18)
牛油廠聲稱每包 50 g。抽 10 包:45.3, 52.4, 47.9, 48.1, 51.4, 44.3, 50.2, 49.7, 46.1, 47.2。Mean = 48.26 < 50。有冇證據顯示牛油一般輕過聲稱?Y i.i.d. normal,。
- ;(left-tailed)。
- 。
- ,,;。
- Critical value:;p-value = t.cdf(−2.076, 9) = 0.0339。
- −2.076 < −1.833()→ reject :有證據顯示牛油平均輕過 50 g。
y = np.array([45.3, 52.4, 47.9, 48.1, 51.4, 44.3, 50.2, 49.7, 46.1, 47.2])
stats.ttest_1samp(y, 50, alternative='less')
# TtestResult(statistic=-2.0760103699294645, pvalue=0.03385076318199426, df=9)
牛油數據其實 = 50 − 腫瘤數據(50 − 4.7 = 45.3、50 + 2.4 = 52.4 …),所以 s 一樣、|T| 一樣、p-value 一樣(slide 18 output 同 slide 10 一樣係 0.03385)——只係方向相反。
8. Confidence interval(slides 19–20)
CI 通常由 two-sided problem 嘅 decision rule 定義。Two-sided 唔 reject 嘅條件係 ,代入 整理:
呢個就係 嘅 confidence interval——即係「所有唔會被 two-sided test reject 嘅 」嘅集合。
Meaning of 95% CI(slide 20):95% chance that the random confidence interval covers the true value(統計學家用 simulation 驗證 95%,初學者可略)。
- ;
- Margin = 2.262 × 0.8381 = 1.896
- 95% CI = 48.26 ± 1.896 = (46.36, 50.16)
注意:CI 包含 50!所以 two-sided test()喺 唔會 reject(two-sided );但 one-sided()就 reject。one-sided test 將 全放喺一邊,所以較易 reject—— 嘅方向要喺睇數據之前定好。
9. Two-sample t-test:兩種 design
Slide 21:比較兩個 sample (Treatment A)同 (Treatment B)。Slide 22:比較兩種教學法——用孿生兄弟姊妹(related)定唔相關嘅學生(unrelated)?
| Design | 描述 | 例子(slides 24, 29) | Test |
|---|---|---|---|
| Paired design | two subjects similar to each other are paired up, then the comparison of the two treatments are made on the pairs | 5 對孿生子比較教學法;兩個歌手唱同樣 5 首歌;同一病人食藥 vs placebo | Paired t-test |
| Completely randomized design | unrelated subjects are randomly assigned to two treatments; the two samples are independent | 10 個唔相關嘅學生(statistically less accurate but easy to implement) | Independent two-sample t-test(pooled variance) |
10. Paired t-test
Slide 25 孿生子數據:
| Twin | Method A (X) | Method B (Y) | Difference |
|---|---|---|---|
| 1 | 118 | 113 | 5 |
| 2 | 144 | 99 | 45 |
| 3 | 102 | 105 | −3 |
| 4 | 111 | 110 | 1 |
| 5 | 134 | 98 | 36 |
Slide 26:
- Model assumptions: i.i.d.
- Test: against
- Decision rule:reject if , number of pairs。
- p-value:reject if p-value < 。
Paired t-test 其實就係對差值 W 做 one-sample t-test()。配對抵消咗「人與人之間」嘅差異(例如孿生子天資相近),所以比 independent design 更準。
x (Drug) = 120, 132, 100, 115, 143;y (Placebo) = 115, 95, 106, 108, 97。同一病人 → paired。。
- :;:(藥物較好)。
- →
- → →
- Critical value:, → 2.132(slide 28 t-table 圈住嘅值)
- 1.775 < 2.132 → do not reject ()。冇足夠證據顯示藥物比 placebo 好。
x = np.array([120, 132, 100, 115, 143]) # Drug
y = np.array([115, 95, 106, 108, 97]) # Placebo
stats.ttest_rel(x, y, alternative='greater') # paired t-test:t ≈ 1.775, p ≈ 0.075
stats.ttest_1samp(x - y, 0, alternative='greater') # 完全一樣嘅結果
stats.t.ppf(0.95, 4) # critical value 2.132
Slide 27–28 只顯示數據同 t-table(圈住 、 嘅 2.132),冇顯示 Python output;上面嘅 、 係按 slide 26 公式自行計算。Slide 22 係教學法比較嘅示意圖,冇額外文字。
孿生子數據(slide 25)同樣做法:,, → do not reject。
11. Independent two-sample t-test(pooled variance)
Slides 29–30:10 個唔相關學生/病人,5 個用 A、5 個用 B(;)。
Slide 31 model assumptions: 同 ,兩個 independent i.i.d. samples,same population variance 。
Pooled variance 唔同於將 X、Y 合埋一齊計嘅 variance。
一個只有 8 個員工嘅虛構城市:Sector A (Education) 薪金 12534, 13290, 10326, 11973;Sector B (Medical) 76245, 79034, 73453, 77024。Overall variance 好大,但每組內部 variance 好細。合併計 variance ,但 pooled variance 只係 。Pooled variance 只量度組內差異,兩組之間嘅差異留俾分子 。同樣嘅 between-group / within-group 概念亦用喺 classification / clustering。
Slide 33:Test against ;reject if 。
- :;:(two-sided),。
- →
- ;two-sided → 查 .025 欄 → 2.447(slide 35 圈住嘅值)
- |−48.95| > 2.447 → reject :兩個 sector 平均薪金有顯著分別。
x = np.array([12534, 13290, 10326, 11973])
y = np.array([76245, 79034, 73453, 77024])
stats.ttest_ind(x, y, equal_var=True) # pooled-variance t-test:T ≈ -48.95, df = 6
stats.t.ppf(0.975, 6) # two-sided critical value 2.447
# 如果兩組 variance 唔相等 → Welch's t-test(flowchart 嘅 unpooled):
stats.ttest_ind(x, y, equal_var=False)
- Slide 33 寫 “Reject if ”,two-sided 應理解為 (呢個例子 T 係負數)。
- Paired df = 對數 − 1;Independent 。用錯 df 就查錯表。
- 同一組數據(drug vs placebo)如果錯當 independent:,,two-sided critical 2.306 → 結論亦唔同。Design 決定 test,唔係數據樣子。
Between-group / within-group variation 嘅比較係 1-way ANOVA(> 2 groups)同 Module 4 regression ANOVA F-test 嘅核心;亦同 COM6004 Cluster Analysis 嘅 SSE(within-cluster)vs between-cluster separation 同一個 idea。
自我測驗 Self-test
Q1. ,,。Test vs ,。
;,。1.5 < 1.753 → do not reject 。冇足夠證據顯示 。
Q2. 同上數據,求 嘅 95% CI。
;margin = 2.131 × 2 = 4.262;CI = (98.74, 107.26)。
Q3. stats.t.ppf(0.95, 9)、stats.t.ppf(0.975, 9)、stats.t.ppf(0.05, 9) 分別係幾多?各用喺咩 test?
1.833(right-tailed );2.262(two-sided );−1.833(left-tailed )。
Q4. ,,one-sided right。寫出三個等價嘅 decision rule 並判斷。
T > t.ppf(0.95, 9) ⇔ t.cdf(T, 9) > 0.95 ⇔ 1 − t.cdf(T, 9) < 0.05。2.5 > 1.833 → reject ()。
Q5. 以下用邊個 test?(a) 同一班學生上堂前後測驗分數 (b) 隨機分 30 人做 A 組、30 人做 B 組比較減肥效果 (c) 比較 4 個品牌電池壽命 (d) 一批 15 個零件同規格 20 mm 比較
(a) Paired t-test;(b) Independent two-sample t-test;(c) 1-way ANOVA;(d) One-sample t-test()。
Q6. Paired 數據 。Test vs ,。
;偏差平方 0 + 4 + 16 + 1 + 1 = 22;,;; → 2.132。2.86 > 2.132 → reject 。
Q7. 兩組 independent:,。Two-sided 。
→ ;;;,。2.45 > 2.228 → reject 。
Q8. 點解 pooled variance 唔等於將兩組數據合埋計 variance?用 slide 32 解釋。
合併 variance 包含組與組之間嘅差距(between-group),薪金例子兩組差 6 萬幾,令合併 variance 極大;pooled variance 只計每組圍繞自己 mean 嘅離散(within-group),反映純 noise。t-test 要嘅係 within-group noise 做分母。
Q9. 牛油例子 one-sided reject 但 95% CI (46.36, 50.16) 包含 50,矛盾嗎?
唔矛盾。95% CI 對應 two-sided test(,不 reject);one-sided test 將 5% 全放左尾, → reject。對應 one-sided 嘅應該係 90% two-sided CI(或 one-sided upper bound)。
一頁總結 Cheat sheet
- Flowchart:1 sample → t; → Z;2 samples independent → 2-sample t(pooled / unpooled);paired → paired t;> 2 → ANOVA。
- t vs Z: 未知用 s → t,tails 較厚; 時 。
- One-sample:,,(
ddof=1)。 - Table:row = df,column = (右尾);、 → 1.833 =
t.ppf(0.95, 9);two-sided 查 。 - Right:|Left:|Two:。
- p-value = 1 − t.cdf(T, df)(right-tailed); → reject。
- 腫瘤:, → reject;牛油:, → reject。
- CI:;牛油 95% CI (46.36, 50.16)。
- Assumption:i.i.d. normal。
- Paired:,,;drug/placebo → not reject。
- Independent:,,;薪金 , → reject。
- Python:
ttest_1samp、ttest_rel、ttest_ind(equal_var=True)、alternative='greater'/'less'/'two-sided'。