均值检验

Reads: 932 Edit

1 T检验

1.1 单样本T检验

> x=c(0.051,0.926,0.209,0.358,1.672,-1.191,1.404,1.112,0.108,-0.429,0.746,1.304,0.292,-1.587,-0.815)
> t.test(x,mu=0.5)

    One Sample t-test

data:  x
t = -0.89423, df = 14, p-value = 0.3863
alternative hypothesis: true mean is not equal to 0.5
95 percent confidence interval:
 -0.2567293  0.8113960
sample estimates:
mean of x 
0.2773333 

说明: p-value = 0.3863>0.05,因而接受原假设,认为x的均值等于0.5

> t.test(x,mu=1)

    One Sample t-test

data:  x
t = -2.9022, df = 14, p-value = 0.01159
alternative hypothesis: true mean is not equal to 1
95 percent confidence interval:
 -0.2567293  0.8113960
sample estimates:
mean of x 
0.2773333

说明: p-value = 0.01159 < 0.05,因而拒绝原假设,认为x的均值不等于1

1.2 独立样本T检验

> x=c(0.051,0.926,0.209,0.358,1.672,-1.191,1.404,1.112,0.108,-0.429,0.746,1.304,0.292,-1.587,-0.815)
> y=c(-0.488,-0.466,2.464,-0.959,0.592,-0.925,0.0206,0.048,-1.857,0.708)
> z=c(2.405,0.774,1.280,1.110,2.777,2.445,-0.018,-0.172)
> t.test(x,y)

    Welch Two Sample t-test

data:  x and y
t = 0.81049, df = 16.675, p-value = 0.4291
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.5842699  1.3114166
sample estimates:
 mean of x  mean of y 
 0.2773333 -0.0862400 

说明:p-value = 0.4291>0.05,因而接受原假设,认为x和y的均值相等。

> t.test(x,z)

    Welch Two Sample t-test

data:  x and z
t = -2.2269, df = 12.556, p-value = 0.04494
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -2.06795945 -0.02762389
sample estimates:
mean of x mean of y 
0.2773333 1.3251250 

说明:p-value = 0.04494<0.05,因而拒绝原假设,认为x和z的均值不相等。

1.3 配对样本T检验

> length(x)
[1] 15
> length(y)
[1] 10
> x1=x[1:10]
> t.test(x1,y,paired=TRUE)

    Paired t-test

data:  x1 and y
t = 1.2091, df = 9, p-value = 0.2574
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.4426336  1.4591136
sample estimates:
mean of the differences 
                0.50824 

说明:p-value = 0.2574>0.05,因而接受原假设,认为x1和y的均值相等。

2 方差分析

2.1 单因素方差分析

我们已R中自带的杀虫剂数据为例进行演示。数据中给出了6种杀虫剂在实验中能够杀灭的害虫数量,采用单因素方差分析比较6种杀虫剂杀虫数量均值是否相等!

> data(InsectSprays)
> View(InsectSprays)

r-68

> aov_result<-aov(count ~ spray, data=InsectSprays)
> summary(aov_result)
            Df Sum Sq Mean Sq F value Pr(>F)    
spray        5   2669   533.8    34.7 <2e-16 ***
Residuals   66   1015    15.4                   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

说明:p值小于0.05,因而拒绝原假设,认为三种杀虫剂的杀虫效果不同。

2.2 双因素方差分析

我们以consum.xlsx的数据为例进行演示,数据给出了某购物中心消费者性别、购物方式、消费金额的数据。采用双因素方差分析性别和购物方式是否为消费金融的影响因素。

> library(readxl)
> consumData=read_excel("D:/Desktop/consum.xlsx",sheet="Sheet1")
> View(consumData)

r-69

说明:读取的数据不符合双因素方差分析的要求,需要对数据进行处理,使性别、购物方式两个因素的类别仅有一个样本,删除其他样本(这里仅仅为了演示,构造双因素方差分析所需的数据结构)。

> library(dplyr)
> newData=consumData %>% distinct(gender, style,.keep_all = TRUE)
> newData
# A tibble: 6 x 3
  gender style amtspent
   <dbl> <dbl>    <dbl>
1      0     1     416.
2      0     2     577.
3      1     1     185.
4      1     2     335.
5      1     3     276.
6      0     3     385.
> aov_result2=aov(amtspent~gender+style,data = newData)
> summary(aov_result2)
            Df Sum Sq Mean Sq F value Pr(>F)
gender       1  56516   56516   5.316  0.104
style        1    900     900   0.085  0.790
Residuals    3  31896   10632 

说明:gender对应的P值为0.104>0.05,因而接受原假设,认为性别不是购物花费金额的影响因素;同理,购物方式也不是购物花费金额的影响因素。

注意:双因素方差分析的应用非常局限,因为每个因素类别只对应一个样本。现实中更多的情况是每个因素类别可以对应多个样本,这就需要用有交互作用的双因素方差分析来研究。

2.3 有交互作用的双因素方差分析

我们仍以consum.xlsx的数据为例进行演示,数据给出了某购物中心消费者性别、购物方式、消费金额的数据。采用双因素方差分析性别和购物方式是否为消费金融的影响因素。

> library(readxl)
> consumData=read_excel("D:/Desktop/consum.xlsx",sheet="Sheet1")
> View(consumData)

r-69

> aov_result3=aov(amtspent~gender+style+gender:style,data = newData)
> summary(aov_result3)
             Df Sum Sq Mean Sq F value Pr(>F)
gender        1  56516   56516   4.025  0.183
style         1    900     900   0.064  0.824
gender:style  1   3814    3814   0.272  0.654
Residuals     2  28082   14041 

说明:gender对应的P值为0.183>0.05,因而接受原假设,认为性别不是购物花费金额的影响因素;同理,购物方式也不是购物花费金额的影响因素;两者的交互作用也不是购物花费金额的影响因素。



获取案例数据,请关注微信公众号并回复:R_dt7


Comments

Make a comment