统计学(四):方差分析

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) 

正态QQ图

  • 方差齐次性检验
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.双因子方差分析

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

4ubmV0L3dlaXhpbl80MzcwMDA1MA==,size_16,color_FFFFFF,t_70)

  • 方差齐次性检验
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) 

在这里插入图片描述

  • 条形图+误差棒
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) 

在这里插入图片描述

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) 

在这里插入图片描述

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值