统计学(四):方差分析

1.单因子方差分析

  • 加载R包
library(reshape2)
library(car)
library(ggplot2)
library(ggpubr)
library(ggsci)
library(ggsignif)
library(userfriendlyscience)
library(psych)
  • 创建数据
 df = data.frame(A = c(7.76,7.71,8.43,8.47,10.3,6.67,11.73,5.78,6.61,6.97),
           B = c(12.14,13.6,14.42,13.85,17.53,14.16,14.94,13.01,14.18,17.72),
           C = c(10.85,8.59,7.19,9.36,9.59,8.81,8.22,9.95,11.26,8.68))
  • 数据融合
data = melt(df)
colnames(data) = c('group','ATP_expr')
as.factor(data$group)
  • 描述性统计
describeBy(data$ATP_expr,data$group)
  • 方差分析
fit = aov(ATP_expr~group,data) 
  • 正态性检验
res = fit$residuals 
ggqqplot(res) 

统计学(四):方差分析_第1张图片

  • 方差齐次性检验
leveneTest(ATP_expr~group,data)
  • 两两比较
summary(fit)
Tu = TukeyHSD(fit) # 多重比较
p = Tu$group[,4]
p.signif = ifelse(p < 0.05, 
                  ifelse(p < 0.01,ifelse(p < 0.001, '***', '**'),'*'),
                  'ns')
  • 可视化
comp = list(c('A','B'), c('A','C'), c('B','C'))
ggboxplot(data, x = "group", y = "ATP_expr",
          fill = "group", 
          palette = "jco",
          bxp.errorbar = T,
          add = 'jitter',  
          short.panel.labs = F) +
  geom_signif(comparisons = comp,
              y_position = c(16,20,18),
              annotations = unname(p.signif))

统计学(四):方差分析_第2张图片

2.双因子方差分析

  • 方差分析
ToothGrowth$dose = as.factor(ToothGrowth$dose)
fit = aov(data = ToothGrowth,len~supp*dose)
  • 正态性检验
res = fit$residuals 
ggqqplot(res) 

统计学(四):方差分析_第3张图片

  • 方差齐次性检验
leveneTest(len~supp*dose,ToothGrowth)
  • 两两比较
summary(fit) 
TukeyHSD(fit)
Tu = TukeyHSD(fit) # 多重比较
all_pvalue = Tu$`supp:dose`
rownames(all_pvalue)
p = all_pvalue[c(2,4,11,7,9,14),4]
p.signif = ifelse(p < 0.05, 
                  ifelse(p < 0.01,ifelse(p < 0.001, '***', '**'),'*'),
                  'ns')
  • 可视化
annotation <- data.frame(supp= rep(c('OJ','VC'),each =3),
                         start=c("0.5", "0.5",'1'), 
                         end=c('1','2','2'),
                         y = c(34,38,36),
                         label=unname(p.signif))
ggboxplot(ToothGrowth, x = "dose", y = "len",
          fill = "dose",
          bxp.errorbar = T,
          add = 'jitter',
          palette = c("#00AFBB", "#FC4E07", "#E7B800"), # 自定义色彩
          facet.by = "supp", 
          short.panel.labs = FALSE,xlab = '') +
  geom_signif(data = annotation,
              aes(xmin=start, xmax=end, annotations=label, y_position=y),
              manual=TRUE) 

统计学(四):方差分析_第4张图片

  • 条形图+误差棒
ggbarplot(ToothGrowth, x = "dose", y = "len",
          fill = "dose",
          add = 'mean_se',
          palette = c("#00AFBB", "#FC4E07", "#E7B800"), # 自定义色彩
          facet.by = "supp", 
          short.panel.labs = FALSE,xlab = '') +
  geom_signif(data = annotation,
              aes(xmin=start, xmax=end, annotations=label, y_position=y),
              manual=TRUE) 

统计学(四):方差分析_第5张图片

p1 = all_pvalue[c(1,10,15),4]
p.signif = ifelse(p1 < 0.05, 
                  ifelse(p1 < 0.01,ifelse(p1 < 0.001, '***', '**'),'*'),
                  'ns')
annotation <- data.frame(dose= c(0.5,1,2),
                         start='VC', 
                         end= 'OJ',
                         y = 36,
                         label=unname(p.signif))
ggboxplot(ToothGrowth, x = "supp", y = "len",
          fill = "supp",
          bxp.errorbar = T,
          add = 'jitter',
          palette = c("#00AFBB", "#FC4E07"), # 自定义色彩
          facet.by = "dose", 
          short.panel.labs = FALSE,xlab = '') +
  geom_signif(data = annotation,
              aes(xmin=start, xmax=end, annotations=label, y_position=y),
              manual=TRUE) 

统计学(四):方差分析_第6张图片

你可能感兴趣的:(R-统计学)