121.5_R_T检验

t检验

2021年11月9日

10:21

摘要:

依托实验:吗啡小鼠镇静实验

实验结果处理:t检验

。。。。。。。。。。。。。。。。。。。。。。。

代码

 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 读取数据,从csv为例子

alldata <- read.csv("datas.csv")
#对数据的读取情况基本查看
print(is.data.frame(alldata)) # 是否存在
print(ncol(alldata))   #多少列
print(nrow(alldata))    #多少行
print(summary(alldata))

duizao30 <- alldata[7]
shiyan30 <- alldata[4]
#install.packages("carData",repos = "https://mirrors.ustc.edu.cn/CRAN/") 
library(carData)
#install.packages("car",repos = "https://mirrors.ustc.edu.cn/CRAN/") 
library(car)
?t.test
if(FALSE){
  ## Default S3 method:
#t.test(x, y = NULL,
    #   alternative = c("two.sided"单侧双侧, "less"上限, "greater"下限),
     "  mu = 0总体均值, paired = FALSE配对否, var.equal = FALSE方差等否,
       conf.level = 0.95, ...)

## S3 method for class 'formula'
t.test(formula, data, subset, na.action, ...)
"}
#
#danyanbtjianyan
datadan <- as.matrix(as.vector(shiyan30))
t.test(datadan,alternative = "two.sided",mu = 8)
if(false){"
  	One Sample t-test

data:  datadan
t = 5.6545, df = 23'就是v自由度', p-value = 9.342e-06‘p值小于0.05接受H1’
alternative hypothesis: true mean is not equal to 8
95 percent confidence interval:
 20.03088 33.91212这个区间内是95%置信区间,该区间内认为和均值没有显著差异。
sample estimates:
mean of x 
  26.9715 在置信区间之内故没有差异。
  "}

#
#
#两独立样本(随机分为两组一组给药,一组不给药)
dataliang1 <- as.matrix(as.vector(shiyan30))
dataliang2 <- as.matrix(as.vector(duizao30))
shiyanzu <- dataliang1
duizaozu <- dataliang2
t.test(shiyanzu,duizaozu,var.equal = TRUE)#假设方差相等(方差同了再比较均值)
#结论p>0.05接受原假设,有证据显著表明没有差异。
if(false){"
  Two Sample t-test

data:  shiyanzu and duizaozu
t = 4.1118, df = 46, p-value = 0.0001603
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
  7.293327 21.282173
sample estimates:
mean of x mean of y 
 26.97150  12.68375 
  "}
###
##
#配对样本t检验(一个人培训前培训后,一只老鼠给药前后)
datapn1 <- as.matrix(as.vector(alldata[5]))
datapn2 <- as.matrix(as.vector(alldata[7]))
t.test(datapn1,datapn2,paired = TRUE)
# =
t.test(datap1-datap2,mu = 0)
#p<0.05拒绝原假设,数据有差异
if(FALSE){"
  Paired t-test

data:  datap1 and datap2
t = 5.0135, df = 23, p-value =
4.511e-05
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
  9.142484 21.987266
sample estimates:
mean of the differences 
               15.56487 

  ###
  One Sample t-test(p1-p2)

data:  datap1 - datap2
t = 5.0135, df = 23, p-value =
4.511e-05
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
  9.142484 21.987266
sample estimates:
mean of x 
 15.56487 
"}

单样本t检验

步骤

*1.*提出假设

H0: μ**0 = μ;HA*:* μ μ**0

2.计算t

t=**( \hat{X} - μ**0 /S\hat{X}

*3.*统计推断

在R中的实现

// 单样本T检验

data <- C(1,2,3,4,5,6,7,8,9)

*shapiro.test(data) #p>0.05,*符合正态分布

t.test(data,mu=4.5) #mu**表示平均数

、、、

查看r的结果

  • 正态分布的结果
  • t检验的结果

p=一个数>0.05,所以拒绝H0,接受HA。

方差齐的非配对双样本t检验

步骤及算法

*1.*提出假设

H0: μ1 = μ2;HA: μ1 ≠ μ2

2.计算t

t=( \hat{X1} - \hat{X2} )/ S \hat{X1} - \hat{X2}

其中:

1

*3.*统计推断

在r中的实现

//非配对样本两样的t检验

high<-c(134,146,106,119,124,161,107,83,113,129,97,123)

low<-c(70,118,101,85,107,132,94)

x <- c(high,low)

group <- c(rep(“high”,12),rep(“low”,7))

shapiro.test(high) #正态性检验

shapiro.test(low) #正态性检验

bartlett.test(x~group)#方差齐性检验

t.test(high,low,paired = FALSE,var.equal = T) #非配对: paired = FALSE 方差齐: var.equal = T

、、、

方差齐性的检验结果

> bartlett.test(x~group)#方差齐性检验

Bartlett test of homogeneity of variances

data: x by group

Bartlett’s K-squared = 0.0066764, df = 1, p-value = 0.9349 #接近1表明方差齐

、、、

t检验的结果

> t.test(high,low,paired = FALSE,var.equal = T)

Two Sample t-test

data: high and low

t = 1.9157, df = 17, p-value = 0.07238

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

-1.942543 40.275876

sample estimates:

mean of x mean of y

120.1667 101.0000

、、、

p-value = 0.07238>0.05,所以不能否定H0

方差不齐的非配对的t检验

步骤及方法

*1.*提出假设

H0: μ1 = μ2;HA: μ1 ≠ μ2

2.计算t'

t’=( \hat{X1} - \hat{X2} )/ S \hat{X1} - \hat{X2}

其中

1

2

3

*3.*统计推断

在R中的实现

//非配对样本t检验

high<-c(134,146,106,119,124,161,107,83,113,129,97,123)

low<-c(70,118,101,85,107,132,94)

x <- c(high,low)

group <- c(rep(“high”,12),rep(“low”,7))

shapiro.test(high)

shapiro.test(low)

bartlett.test(x~group)#方差齐性检验

t.test(high,low,paired = FALSE,var.equal = F)

、、、

t检验结果

> t.test(high,low,paired = FALSE,var.equal = F)

Welch Two Sample t-test

data: high and low

t = 1.9319, df = 13.016, p-value = 0.07543

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

-2.263671 40.597005

sample estimates:

mean of x mean of y

120.1667 101.0000

、、、

p-value = 0.07238>0.05,所以不能否定H0

配对双样本t检验

步骤及算法

*1.*提出假设

H0: μd = μ1 - μ2 = 0;HA: μd = μ1 - μ2 ≠ 0

2.计算t

t=\hat{d} / S\hat{d} ,df =n-1

其中

1

2

*3.*统计推断

在R中的实现

、、、

//配对两样本的t检验

ds <- c(82.5,85.2,87.6,89.9,89.4,90.1,87.8,87.0,88.5,92.4)

cs <- c(91.7,94.2,93.3,97.0,96.4,91.5,97.2,96.2,98.5,95.8)

library(carData)

library(car)

leveneTest(ds,cs)

d <- ds-cs

shapiro.test(d) #方差齐性检验

t.test(ds,cs,paired = T,alternative = “two.sided”,cond.lvel=0.95)

、、、

t检验结果

> t.test(ds,cs,paired = T,alternative = “two.sided”,cond.lvel=0.95)

Paired t-test

data: ds and cs

t = -7.8601, df = 9, p-value = 2.548e-05

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

-9.1949 -5.0851

sample estimates:

mean of the differences

​ -7.14

、、、

p-value = 2.548e-05 < 0.01,所以否定Ho,接受HA

NOTE

总体均值的t检验。

大样本用z检验小样本用z检验。

总体均值的单样本t检验

检验均值之间是否具有差异。

小结

CTRL-/