独立性样本t检验,检验两个总体的均值相等的假设
调用格式1:t.test(y~x,data)#y为数值型变量,x为分类变量
调用格式2: t.test(y1,y2) #y1 y2 均为数值型变量
library(MASS)
attach(UScrime)
str(UScrime)
## 'data.frame': 47 obs. of 16 variables:
## $ M : int 151 143 142 136 141 121 127 131 157 140 ...
## $ So : int 1 0 1 0 0 0 1 1 1 0 ...
## $ Ed : int 91 113 89 121 121 110 111 109 90 118 ...
## $ Po1 : int 58 103 45 149 109 118 82 115 65 71 ...
## $ Po2 : int 56 95 44 141 101 115 79 109 62 68 ...
## $ LF : int 510 583 533 577 591 547 519 542 553 632 ...
## $ M.F : int 950 1012 969 994 985 964 982 969 955 1029 ...
## $ Pop : int 33 13 18 157 18 25 4 50 39 7 ...
## $ NW : int 301 102 219 80 30 44 139 179 286 15 ...
## $ U1 : int 108 96 94 102 91 84 97 79 81 100 ...
## $ U2 : int 41 36 33 39 20 29 38 35 28 24 ...
## $ GDP : int 394 557 318 673 578 689 620 472 421 526 ...
## $ Ineq: int 261 194 250 167 174 126 168 206 239 174 ...
## $ Prob: num 0.0846 0.0296 0.0834 0.0158 0.0414 ...
## $ Time: num 26.2 25.3 24.3 29.9 21.3 ...
## $ y : int 791 1635 578 1969 1234 682 963 1555 856 705 ...
Prob#数值型变量
## [1] 0.084602 0.029599 0.083401 0.015801 0.041399 0.034201 0.042100
## [8] 0.040099 0.071697 0.044498 0.016201 0.031201 0.045302 0.053200
## [15] 0.069100 0.052099 0.076299 0.119804 0.019099 0.034801 0.022800
## [22] 0.089502 0.030700 0.041598 0.069197 0.041698 0.036099 0.038201
## [29] 0.023400 0.075298 0.041999 0.042698 0.049499 0.040799 0.020700
## [36] 0.006900 0.045198 0.053998 0.047099 0.038801 0.025100 0.088904
## [43] 0.054902 0.028100 0.056202 0.046598 0.052802
So#分类变量
## [1] 1 0 1 0 0 0 1 1 1 0 0 0 0 0 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0
## [36] 0 1 0 1 1 0 0 1 0 1 0 0
t.test(Prob,So)##
##
## Welch Two Sample t-test
##
## data: Prob and So
## t = -4.1938, df = 46.207, p-value = 0.0001229
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.4341078 -0.1525605
## sample estimates:
## mean of x mean of y
## 0.04709138 0.34042553
t.test(Po1,Po2)##
##
## Welch Two Sample t-test
##
## data: Po1 and Po2
## t = 0.80073, df = 91.66, p-value = 0.4254
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -7.055871 16.587786
## sample estimates:
## mean of x mean of y
## 85.00000 80.23404