在R中做T.TEST检验

t.test是统计学中最常用的检验方法,用来检验两种数据的均值是否相等,用于对正态分布的整体估值,通常应用于小样本(n < 30)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
> x = rnorm(100,mean=20,sd=2)
> y = rnorm(80,mean=17,sd=0.5)
> x
[1] 20.10501 19.57169 20.22245 21.28158 22.79906 20.51546 14.67365 17.04364
[9] 25.95344 19.31148 20.43485 17.98477 19.74916 19.26762 21.72743 20.11575
[17] 21.89669 20.80891 19.20210 23.74879 20.86405 18.60392 19.64830 20.93985
[25] 20.06913 19.31531 19.51718 14.83523 17.86656 23.17153 23.24995 18.28544
[33] 16.90067 18.18929 19.24018 23.06328 19.89651 20.65407 21.44609 19.09518
[41] 19.93449 20.50547 18.71952 20.53826 16.79557 16.36828 18.46189 21.87571
[49] 20.23504 22.98625 20.57800 20.25816 22.77162 19.65173 18.93720 20.96241
[57] 22.07938 20.84764 15.96894 20.95383 22.98853 18.52296 22.12153 19.24400
[65] 17.60788 21.69468 19.72411 18.76660 20.39496 20.03006 21.53487 21.54606
[73] 23.51990 22.38181 22.53281 20.71092 26.15736 19.86159 24.47946 21.85552
[81] 19.16387 16.26043 21.92526 18.09403 18.98212 18.78348 19.75857 20.55351
[89] 19.89423 21.28345 17.95772 20.36063 24.53150 18.56979 18.97283 20.20293
[97] 19.52201 20.56878 18.66845 19.60917

> y
[1] 16.88846 16.71937 16.89191 17.11247 17.15868 17.02920 16.98368 17.17061
[9] 16.75688 16.63579 17.24011 17.03478 17.10650 16.72028 16.34144 16.76368
[17] 17.22986 17.35607 17.70713 16.36813 16.83038 16.50520 16.76058 16.09259
[25] 16.70426 16.38888 16.47690 15.72234 17.51965 17.27447 16.83134 17.28090
[33] 16.92229 16.49557 17.17620 17.00118 17.69339 17.61332 16.40824 16.01501
[41] 16.89645 16.51672 16.31224 17.22569 16.72381 16.60916 17.52845 16.65373
[49] 17.23515 16.70682 16.81280 16.60288 16.47891 17.14051 17.21594 17.34832
[57] 16.95022 16.43481 17.06931 16.42438 17.15994 16.89638 16.86685 16.72054
[65] 18.01892 16.76284 17.09799 16.83591 16.14628 16.02412 16.37865 16.69834
[73] 16.87643 16.55011 17.43718 16.12465 17.52974 16.85233 16.85341 16.32574

> ttest = t.test(x,y)
> ttest
Welch Two Sample t-test

data: x and y
t = 15.506, df = 109.45, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
2.922444 3.778957
sample estimates:
mean of x mean of y
20.20033 16.84963

> names(ttest)
[1] "statistic" "parameter" "p.value" "conf.int" "estimate"
[6] "null.value" "stderr" "alternative" "method" "data.name"

> ttest$p.value # p-value
[1] 2.189551e-29

> ttest$conf.int # 置信度
[1] 2.922444 3.778957
attr(,"conf.level")
[1] 0.95

> ttest$estimate # 均值
mean of x mean of y
20.20033 16.84963

> ttest$null.value
difference in means
0

> ttest$alternative
[1] "two.sided"

> ttest$method # 检验方法
[1] "Welch Two Sample t-test"

> ttest$parameter # degree of freedom,自由度
df
109.4474

> ttest$statistic # t, ratio of difference between two groups with difference within groups,即组间差异与组内差异的比值;
t # t=(mean of x - mean of y)/stderr = (20.20033-16.84963)/0.2160862 = 15.50631
15.50631

> ttest$data.name # data
[1] "x and y"

> ttest$stderr # standard error of the difference,均值差的标准误(均值标准误的差)
[1] 0.2160862
  • 本文作者:括囊无誉
  • 本文链接: R/R_ttest/
  • 版权声明: 本博客所有文章均为原创作品,转载请注明出处!
------ 本文结束 ------
坚持原创文章分享,您的支持将鼓励我继续创作!