1. 程式人生 > >單樣本和雙樣本的檢驗

單樣本和雙樣本的檢驗

1.1 單樣本t檢驗

t檢驗假設資料來自於一個正態分佈。

> daily.intake <- c(5260, 5470, 5640, 6180, 6390, 6515, 6805, 7515, 7515, 8230, 8770)
> mean(daily.intake)
[1] 6753.636
> sd(daily.intake)
[1] 1142.123
> quantile(daily.intake)
  0%  25%  50%  75% 100% 
5260 5910 6515 7515 8770 

檢驗這些攝入於推薦值7725千焦相差甚遠

> t.test(daily.intake, mu = 7225)
One Sample t-test data: daily.intake t = -1.3688, df = 10, p-value = 0.201 alternative hypothesis: true mean is not equal to 7225 95 percent confidence interval: 5986.348 7520.925 sample estimates: mean of x 6753.636

wilcoxon符號秩檢驗,實際應用基本上與t檢驗是一樣:

> wilcox.test(daily.intake, mu = 7225)

	Wilcoxon signed rank test with continuity
	correction

data:  daily.intake
V = 17, p-value = 0.168
alternative hypothesis: true location is not equal to 7225

Warning message:
In wilcox.test.default(daily.intake, mu = 7225) : 無法精確計算帶連結的p值

非引數檢驗不會出現類似於引數估計以及置信區間邊界的概念

1.2 兩樣本t檢驗

> attach(energy)
> energy
   expend stature
1    9.21   obese
2    7.53    lean
3    7.48    lean
4    8.08    lean
5    8.09    lean
6   10.15    lean
7    8.40    lean
8   10.88    lean
9    6.13    lean
10   7.90    lean
11  11.51   obese
12  12.79   obese
13   7.05    lean
14  11.85   obese
15   9.97   obese
16   7.48    lean
17   8.79   obese
18   9.69   obese
19   9.68   obese
20   7.58    lean
21   9.19   obese
22   8.11    lean
> 
t.test(expend~stature) Welch Two Sample t-test data: expend by stature t = -3.8555, df = 15.919, p-value = 0.001411 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -3.459167 -1.004081 sample estimates: mean in group lean mean in group obese 8.066154 10.297778

(~)運算子的作用是指expend是通過stature來描述的

1.3 比較方差

var.test  對兩個樣本的方差進行F檢驗

> var.test(expend~stature)

	F test to compare two variances

data:  expend by stature
F = 0.78445, num df = 12, denom df = 8,
p-value = 0.6797
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
 0.1867876 2.7547991
sample estimates:
ratio of variances 
          0.784446 

1.4 兩個樣本wilcoxon檢驗

> wilcox.test(expend~stature)

	Wilcoxon rank sum test with continuity
	correction

data:  expend by stature
W = 12, p-value = 0.002122
alternative hypothesis: true location shift is not equal to 0

Warning message:
In wilcox.test.default(x = c(7.53, 7.48, 8.08, 8.09, 10.15, 8.4,  : 無法精確計算帶連結的p值

1.5 配對t檢驗

通過作差來將問題簡化為單樣本檢驗

> attach(intake)
> intake
    pre post
1  5260 3910
2  5470 4220
3  5640 3885
4  6180 5160
5  6390 5645
6  6515 4680
7  6805 5265
8  7515 5975
9  7515 6790
10 8230 6900
11 8770 7335
> post - pre
 [1] -1350 -1250 -1755 -1020  -745 -1835 -1540
 [8] -1540  -725 -1330 -1435
> t.test(pre,post,paired = TRUE)

	Paired t-test

data:  pre and post
t = 11.941, df = 10, p-value = 3.059e-07
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 1074.072 1566.838
sample estimates:
mean of the differences 
               1320.455 

配對wilcoxon檢驗

> wilcox.test(pre,post,paired = TRUE)

	Wilcoxon signed rank test with continuity
	correction

data:  pre and post
V = 66, p-value = 0.00384
alternative hypothesis: true location shift is not equal to 0

Warning message:
In wilcox.test.default(pre, post, paired = TRUE) : 無法精確計算帶連結的p值