第20章 连续型变量组间差异比较

您所在的位置:网站首页 比较两组差异分析 第20章 连续型变量组间差异比较

第20章 连续型变量组间差异比较

2024-07-17 15:47| 来源: 网络整理| 查看: 265

20.2 参数检验 20.2.1 独立样本t检验

当我们对两组相互独立的连续型数据进行比较时,如果这两组数据符合正态分布,可以通过独立样本t检验(参数检验)比较两组数据的总体均值是否有显著的差异;如果这两组数据不符合正态分布,则需要选择Mann-Whitney U检验(非参数检验)。对于符合正态分布的数据,方差齐性或方差不齐会影响检验方式的选择,当方差齐性时,选择Student’s t检验,当方差不齐时,选择Welch t检验,具体说明可参考此链接。两组数据的独立样本检验的具体流程如下:

如果我们要对两所学校某次数学统考的成绩进行比较,就可以考虑独立样本检验。

library(tidyverse) library(purrr) # 正常情况下只需要tidyverse,电子书生成需要调用 library(broom) # 正常情况下只需要tidyverse,电子书生成需要调用 # 创建数据框 set.seed(1) df_ind % pivot_longer( # 将宽数据转为长数据 cols = starts_with("school"), # 选择需要进行数据转换的变量 names_to = "school", # 设置存储目标变量的变量名 values_to = "score" # 设置存储目标数据的变量名 ) %>% nest( # 对数据按group打包成子数据框 .by = school, # 设置打包分组 .key = "score_value" # 设置存储新生成子数据框的变量名 ) %>% mutate( t_model = map( # 使用map()函数创建新变量model用于存储正态性检验结果 score_value, # 设置map()函数的目标变量 ~ks.test( # 设置目标函数ks.test()进行正态性检验 x=.x$score, # 设置目标数据 y="pnorm", # 设置预期数据分布类型 mean=mean(.x$score), # 设置预期正态分布均值 sd=sd(.x$score) # 设置预期正态分布标准差 ) ), t_results = map( # 使用map()函数创建新变量results用于整理检验结果 t_model, # 设置map()函数的目标变量 ~tidy(.x) # 设置目标函数tidy()整理检验结果 ) ) %>% unnest(t_results) # 拆解results变量 ## # A tibble: 2 × 7 ## school score_value t_model statistic p.value method alternative ## ## 1 school1 0.0147 0.781 Asymptoti… two-sided ## 2 school2 0.0153 0.739 Asymptoti… two-sided # 方差齐性检验 var.test(x=df_ind$school1, y=df_ind$school2) ## ## F test to compare two variances ## ## data: df_ind$school1 and df_ind$school2 ## F = 0.56529, num df = 1999, denom df = 1999, p-value < 2.2e-16 ## alternative hypothesis: true ratio of variances is not equal to 1 ## 95 percent confidence interval: ## 0.5178297 0.6171085 ## sample estimates: ## ratio of variances ## 0.5652939 # 方差不齐,采用 Welch t 检验 t.test(x=df_ind$school1, y=df_ind$school2, var.equal=FALSE) ## ## Welch Two Sample t-test ## ## data: df_ind$school1 and df_ind$school2 ## t = -9.5521, df = 3711.7, p-value < 2.2e-16 ## alternative hypothesis: true difference in means is not equal to 0 ## 95 percent confidence interval: ## -6.664615 -4.394661 ## sample estimates: ## mean of x mean of y ## 74.79067 80.32031

对两所学校的数学成绩进行正态性检验发现,两组数据均呈现正态分布(P值分别为0.781和0.739)。方差齐性结果显示,两组数据方差不齐(P<2.2e-16)。因此选择Welch t检验比较两组数据,结果显示两组数据之间有显著性差异(P<2.2e-16)。

20.2.2 配对样本t检验

当我们需要对同一个样本的两次重复测试或同一对象经过两种不同处理后的结果进行比较时,可以采用配对检验。在进行配对检验之前,需要对两次测试的差值进行正态性检验,当差值符合正态分布时,采用t.test()函数进行配对样本t检验(参数检验);当差值不符合正态分布时,采用wilcox.test()函数进行Wilcoxon符号秩和检验(非参数检验)。不论那种函数,都需要声明参数paired=TRUE。配对样本检验的具体流程如下:

当我们想对同一个班期中与期末的数学考试成绩进行比较,看数学考试成绩是否有提高时,可以使用配对样本检验。

# 创建数据框 set.seed(1) df_pair % # 逐行进行运算 mutate( t1 = t0+rnorm(1, mean=10, sd=2), # 在t0基础上加一个符合N(10,4)正态分布的随机数 t_delta = t1-t0 ) # 对t1-t0差值进行正态性检验 shapiro.test(df_pair$t_delta) ## ## Shapiro-Wilk normality test ## ## data: df_pair$t_delta ## W = 0.99645, p-value = 0.7431 # 差值显著性>0.05,说明差值符合正态分布,进行配对样本t检验 t.test(x=df_pair$t0, y=df_pair$t1, paired=TRUE) ## ## Paired t-test ## ## data: df_pair$t0 and df_pair$t1 ## t = -82.788, df = 299, p-value < 2.2e-16 ## alternative hypothesis: true mean difference is not equal to 0 ## 95 percent confidence interval: ## -10.215831 -9.741433 ## sample estimates: ## mean difference ## -9.978632

配对样本t检验的显著性P<0.05,说明期中与期末的数学考试成绩是有显著性差异的。再看Paired t-test结果里的mean difference(第一项t0减第二项t1)为负,说明期末成绩显著高于期中成绩。

20.2.3 单因素方差分析

当我们对两组以上相互独立的连续型数据进行比较时,如果不同组的数据均符合正态分布且满足方差齐性检验,则使用oneway.test()函数同时声明参数var.equal=TRUE进行单因素方差分析(参数检验);如果不同组的数据均符合正态分布但不满足方差齐性时,则使用oneway.test()函数同时声明参数var.equal=FALSE进行Welch’s ANOVA检验;如果数据不符合正态分布,则需要使用kruskal.test()函数进行Kruskal-Wallis检验(非参数检验)。

单因素方差分析的方差齐性检验汇总如下:

检验方法 函数 包 描述 说明 Bartlett \(\chi^2\)检验 bartlett.test() stats(R自带) 所检验的数据需要服从正态分布 当P>0.05时,说明方差齐性 Levene检验 leveneTest() car 所检验的数据可以不服从正态分布,结果更为稳健 当P>0.05时,说明方差齐性

比如我们想对三所学校某次数学统考的成绩进行比较,看总体上的数学成绩是否有差异,如果有,我们想知道哪些班之间的成绩有差异,就可以用单因素方差分析。

library(car) # 创建数据框 set.seed(1) df_anova % nest( .by = "school", .key = "score_value" ) %>% mutate( t_model = map( score_value, ~ks.test(x=.x$score, y="pnorm", mean=mean(.x$score), sd=sd(.x$score)) ), t_results = map( t_model, ~tidy(.x) ) ) %>% unnest(t_results) ## # A tibble: 3 × 7 ## school score_value t_model statistic p.value method alternative ## ## 1 school1 0.0178 0.911 Asymptoti… two-sided ## 2 school2 0.0186 0.878 Asymptoti… two-sided ## 3 school3 0.0204 0.797 Asymptoti… two-sided # 各组数据均符合正态分布 # 进一步进行方差齐性检验,使用car包的leveneTest()函数 leveneTest(score~school, data=df_anova_long) ## Levene's Test for Homogeneity of Variance (center = median) ## Df F value Pr(>F) ## group 2 924.26 < 2.2e-16 *** ## 2997 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 # 方差不齐,选择 Welch anova 检验 anova_group % mutate( t_model = map( score_value, ~ks.test(x=.x$score, y="pnorm", mean=mean(.x$score), sd=sd(.x$score)) ), t_results = map( t_model, ~tidy(.x) ) ) %>% unnest(t_results) ## # A tibble: 6 × 8 ## gender height score_value t_model statistic p.value method alternative ## ## 1 M short 0.0451 0.811 Asymp… two-sided ## 2 M normal 0.0414 0.882 Asymp… two-sided ## 3 M tall 0.0458 0.796 Asymp… two-sided ## 4 F short 0.0450 0.814 Asymp… two-sided ## 5 F normal 0.0396 0.913 Asymp… two-sided ## 6 F tall 0.0524 0.642 Asymp… two-sided # 各组数据均符合正态分布 # 对gender和height两个变量的数据进行方差齐性检验 leveneTest(score~gender, data=df_multi_anova) ## Levene's Test for Homogeneity of Variance (center = median) ## Df F value Pr(>F) ## group 1 2.2011 0.1382 ## 1198 leveneTest(score~height, data=df_multi_anova) ## Levene's Test for Homogeneity of Variance (center = median) ## Df F value Pr(>F) ## group 2 2.4783 0.08432 . ## 1197 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 # 数据正态分布,方差齐性均满足,进行多因素方差分析 multi_anova % nest( .by = c("group", "time"), .key = "weight_data" ) %>% mutate( t_model = map( weight_data, ~ks.test(x=.x$weight, y="pnorm", mean=mean(.x$weight), sd=sd(.x$weight)) ), t_results = map( t_model, ~tidy(.x) ) ) %>% unnest(t_results) ## # A tibble: 6 × 8 ## group time weight_data t_model statistic p.value method alternative ## ## 1 A t0 0.0332 0.422 Asympto… two-sided ## 2 A t1 0.0233 0.842 Asympto… two-sided ## 3 A t2 0.0220 0.887 Asympto… two-sided ## 4 B t0 0.0250 0.775 Asympto… two-sided ## 5 B t1 0.0270 0.689 Asympto… two-sided ## 6 B t2 0.0226 0.868 Asympto… two-sided # 进行重复测试方差分析 repeat_anova


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3