在R的ezANOVA函数中动态定义依赖变量和独立变量

时间:2012-09-20 15:48:03

标签: r anova

我想在循环中的多个因变量的ezANOVA包中运行ez,并将结果保存到多个变量中。每个因变量都在同一数据框的单独列中。

all.dependent.variables <- c("dv1", "dv2")

for(dependent.variable in all.dependent.variables){
  assign(paste(dependent.variable, ".aov.results", sep = ""),
    ezANOVA(aov.data,
      dv = dependent.variable,
      wid = subject,
      within = .(factor1, factor2),
      return_aov = TRUE))
}

这是一个示例数据框:

aov.data <- structure(list(subject = structure(c(10L, 11L, 12L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L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, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), .Label = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), class = "factor"), 
dv1 = c(650.2, 773.7, 686.4, 436.2, 625.3, 714.2, 892.6, 921.5, 
711.2, 670.2, 725.8, 592.8, 672.7, 731.1, 707.2, 475.1, 645.4, 
786.7, 949.5, 925.8, 715.5, 745.4, 750.8, 579.1, 683.3, 707.6, 
693.7, 492.4, 698.8, 666.9, 914.4, 853.8, 724.4, 718.8, 872.9, 
616.9, 706.4, 766.2, 676.2, 500, 753.8, 712.7, 1012.2, 947.8, 
695.3, 735.6, 843.7, 596.1, 738.3, 705.2, 718.2, 534.1, 805.3, 
814.1, 969.4, 1010.7, -999, 714.4, 815.4, 645.4, 835.4, 830.7, 
776.7, 543.7, 757.2, 841.5, 1107.8, 915.8, -999, 707.4, 809.7, 
671.1, 638.1, 726.7, 660.2, 455.7, 623.5, 716.1, 922.1, 804.5, 
718.2, 674.6, 797.4, 572, 676.7, 726.6, 690.7, 498.8, 624.3, 
764.1, 889.5, 823.4, 672.9, 701.8, 750.4, 557.2, 656.1, 701, 
655.1, 472.7, 658.8, 660.6, 860.9, 811.3, 672.5, 681.7, 849.6, 
571.2, 694, 777.5, 661.3, 488, 670.4, 725.3, 938.1, 862.7, 
616.4, 732.2, 845.9, 582.4, 694.2, 694.6, 743.8, 480.5, 736.7, 
740.9, 988.1, 827.5, 812.4, 725.5, 844.2, 628, 779.3, 770.3, 
686.9, 494.3, 681.5, 850.5, 990.7, 810.1, 692.3, 779.7, 779.8, 
590.4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
618.6, 713.9, 609.6, 468.6, 554.3, 580.8, 864.1, 843.3, 662.8, 
645.5, 714.6, 555.5, 670.7, 759.3, 652.2, 468.1, 613.5, 712.3, 
910.7, 782.4, 723.3, 742.8, 775.5, 553.2, 695.3, 726.2, 591.2, 
479.2, 626.1, 643.3, 821.5, 753.9, 818.2, 655.8, 754.4, 592.9, 
703.5, 792.5, 635, 485.3, 644.1, 667.9, 891.3, 780.9, 699.1, 
725.1, 716, 587.2, 706.5, 754.6, 694.3, 485.5, 745.5, 649.3, 
808.4, 780.5, 773.8, 676.3, 687.5, 685.3, 910.4, 821.5, 738.5, 
525, 689.6, 758.4, 1021.5, 792, 789.3, 740.5, 722.8, 717.1, 
653.3, 743.6, 620.3, 460.1, 575.3, 647.1, 849.3, 647, 691.2, 
596.4, 714.6, 531.6, 678.7, 754.7, 600.4, 463.8, 560.8, 636.6, 
844.3, 766.6, 725.3, 628.7, 784.4, 547.9, 630.3, 656.7, 705.3, 
443.3, 607, 630.7, 861.5, 754.4, 770.8, 664.5, 728.3, 546.4, 
741.5, 694, 620.4, 459.3, 587.9, 626.2, 893.8, 756.1, 731.8, 
680.2, 836.4, 566.7, 619.4, 686.4, 704, 445.3, 652.7, 735.3, 
839.4, 833.4, 763.7, 614.5, 794.4, 562.5, 713.2, 735.4, 655.4, 
501.1, 635.6, 661.2, 880.6, 747.8, 807.8, 757.7, 772.4, 560.1, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 662.7, 
682.5, 590.1, 443.6, 623.2, 656.6, 852.6, 676.8, 646.6, 646, 
677.6, 518, 664, 665.4, 609.8, 464.2, 696.5, 661, 894.7, 
661.1, 659.6, 657.8, 713.4, 531.5, 739.5, 695.1, 656.5, 498.4, 
648.1, 710, 897.8, 685, 671.3, 657, 767.5, 545.3, 808.6, 
697.5, 667.2, 463.4, 695.3, 652.4, 857.2, 690.3, 766.1, 696.1, 
690.5, 558.8, 746, 708.4, 690, 515.5, 788.8, 929.6, 802.1, 
619.5, 510.8, 654.1, 811.8, 706.5, 977.8, 697.9, 700.9, 497.9, 
700.9, 811.5, 969.3, 723, 886, 815.7, 757.5, 639.5, 688.4, 
704, 617.8, 435.2, 628.8, 603.3, 865.3, 661.6, 645.9, 598.1, 
646.8, 477.2, 646.8, 760.8, 634.3, 452.2, 600.1, 648.2, 923.8, 
625.5, 676.9, 647.3, 688.6, 513.2, 591.9, 641.6, 632.6, 469.6, 
606.4, 610.9, 835.1, 667.8, 599.7, 581.2, 704.4, 502.9, 746.9, 
684.1, 689.3, 475.9, 692, 689, 824.9, 625.9, 696.4, 706.3, 
715.3, 510.9, 650.9, 640.1, 663.5, 471.6, 682.3, 683.2, 831.9, 
702, 685, 624.6, 698.2, 521.6, 759.8, 730.6, 661.1, 473.2, 
644.4, 738.7, 932.5, 685.1, 816, 722.1, 783.3, 526.2, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), dv2 = c(2.941, 
2.941, 5.882, 0, 0, 2.941, 8.824, 23.529, 35.294, 8.824, 
17.647, 5.882, 2.941, 2.941, 2.941, 0, 0, 8.824, 8.824, 44.118, 
29.412, 8.824, 2.941, 5.882, 5.882, 0, 5.882, 5.882, 0, 2.941, 
23.529, 20.588, 17.647, 8.824, 8.824, 11.765, 2.941, 2.941, 
8.824, 17.647, 0, 5.882, 26.471, 14.706, 47.059, 0, 17.647, 
14.706, 23.529, 0, 23.529, 38.235, 17.647, 52.941, 61.765, 
55.882, 94.118, 5.882, 41.176, 55.882, 17.647, 23.529, 35.294, 
32.353, 20.588, 44.118, 55.882, 44.118, 85.294, 17.647, 41.176, 
55.882, 0, 0, 0, 0, 0, 5.882, 5.882, 0, 29.412, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 5.882, 0, 32.353, 0, 5.882, 0, 0, 0, 0, 
0, 0, 2.941, 5.882, 0, 20.588, 0, 0, 0, 2.941, 0, 0, 0, 0, 
0, 11.765, 2.941, 35.294, 0, 0, 0, 2.941, 0, 5.882, 0, 0, 
20.588, 29.412, 8.824, 55.882, 0, 2.941, 5.882, 0, 0, 5.882, 
2.941, 2.941, 2.941, 14.706, 0, 52.941, 0, 11.765, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.941, 
0, 0, 5.882, 5.882, 29.412, 32.353, 5.882, 5.882, 0, 2.941, 
0, 5.882, 0, 0, 0, 11.765, 41.176, 23.529, 5.882, 5.882, 
0, 17.647, 5.882, 17.647, 2.941, 2.941, 8.824, 26.471, 26.471, 
44.118, 11.765, 11.765, 26.471, 2.941, 0, 5.882, 0, 0, 8.824, 
17.647, 23.529, 44.118, 2.941, 5.882, 11.765, 20.588, 5.882, 
17.647, 26.471, 17.647, 55.882, 47.059, 55.882, 61.765, 5.882, 
32.353, 55.882, 41.176, 2.941, 38.235, 35.294, 0, 76.471, 
50, 67.647, 76.471, 14.706, 55.882, 55.882, 5.882, 2.941, 
0, 0, 0, 0, 2.941, 0, 35.294, 0, 2.941, 0, 5.882, 0, 0, 0, 
0, 8.824, 8.824, 2.941, 20.588, 0, 0, 0, 5.882, 2.941, 0, 
0, 0, 0, 2.941, 0, 35.294, 2.941, 0, 0, 0, 0, 2.941, 0, 0, 
2.941, 0, 0, 38.235, 0, 2.941, 0, 8.824, 2.941, 0, 0, 0, 
32.353, 26.471, 17.647, 41.176, 2.941, 0, 0, 2.941, 2.941, 
5.882, 0, 0, 11.765, 8.824, 0, 55.882, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.941, 0, 5.882, 
0, 0, 0, 5.882, 11.765, 26.471, 8.824, 2.941, 2.941, 2.941, 
0, 8.824, 0, 0, 0, 11.765, 2.941, 11.765, 0, 8.824, 0, 8.824, 
2.941, 11.765, 5.882, 2.941, 2.941, 26.471, 20.588, 26.471, 
0, 2.941, 17.647, 11.765, 0, 2.941, 0, 2.941, 17.647, 20.588, 
20.588, 26.471, 8.824, 5.882, 11.765, 35.294, 14.706, 32.353, 
41.176, 5.882, 44.118, 52.941, 47.059, 73.529, 17.647, 50, 
47.059, 35.294, 11.765, 32.353, 50, 8.824, 73.529, 76.471, 
47.059, 82.353, 14.706, 47.059, 41.176, 0, 0, 0, 2.941, 0, 
0, 14.706, 2.941, 8.824, 0, 0, 0, 8.824, 2.941, 0, 0, 0, 
5.882, 0, 2.941, 2.941, 0, 5.882, 0, 8.824, 0, 2.941, 0, 
0, 2.941, 2.941, 2.941, 11.765, 5.882, 0, 0, 2.941, 0, 2.941, 
0, 0, 2.941, 2.941, 0, 17.647, 5.882, 2.941, 0, 5.882, 5.882, 
11.765, 0, 0, 8.824, 32.353, 0, 44.118, 2.941, 5.882, 0, 
2.941, 5.882, 11.765, 0, 0, 8.824, 17.647, 2.941, 50, 8.824, 
5.882, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0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, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("level1", "level2", "level3"
), class = "factor")), .Names = c("subject", "dv1", "dv2", 
"factor1", "factor2"), row.names = c(NA, 540L), class = "data.frame")

这个问题是,R将索引dependent.variable解释为数据帧aov.data中列的说明符,因此返回以下错误:

  

“dependent.variable”不是所提供数据框中的变量。

我尝试用eval()print()包装索引,但无济于事。

3 个答案:

答案 0 :(得分:3)

好的,这绝对不是你的错...... ezANOVA会在内部做一些聪明的评估,这会让你感到困惑。这是一个似乎有用的可怕咒语,但最好是(1)联系包维护者,看看他们是否能找到更优雅地修复内部的方法; (2)考虑一下你是否可以通过一个更标准的anova评估器来解决这个问题,而这个评估器没有做太多的内部混乱......

fitlist <- lapply(all.dependent.variables,
       function(x) {
           e1 <- expression(ezANOVA(aov.data,
               dv = x,
               wid = subject,
               within = .(factor1, factor2),
               return_aov = TRUE))
           ## now force evaluation of the "dv" component of the call
           e1[[1]][["dv"]] <- eval(e1[[1]][["dv"]])
           eval(e1)
       })

答案 1 :(得分:3)

又一个不太好的解决方案:

lapply(all.dependent.variables, function(i) {
  eval(parse(text=
    paste0('ezANOVA(data=aov.data,
                    dv=', i,',
                    wid=subject,
                    between=.(factor1, factor2),
                    return_aov = TRUE)')
  ))
})

答案 2 :(得分:1)

根据上述建议,这是一个如何使ezanova参数在循环中被接受的更通用的解决方案。 我猜没有人需要看到如何将函数放入循环中,因此我将不再赘述。 这是我带有示例的通用解决方案。

df<-data.frame(id=rep(seq(1,80),each=81,1),
               IV1=rep(LETTERS[1:3],each=1,2160),
               IV2=rep(LETTERS[4:6],each=3,720),
               IV3=rep(LETTERS[7:9],each=9,240),
               IV4=rep(LETTERS[10:12],each=27,80),
               DV1=rnorm(2160,mean=10,sd=1),
               DV2=rnorm(2160,mean=20,sd=1),
               DV3=rnorm(2160,mean=20,sd=1),
               DV4=rnorm(2160,mean=20,sd=1),
               stringsAsFactors=FALSE)
dv="DV1";wid="id";within="IV1";within_full=NULL;between=NULL;within_covariates=NULL;between_covariates=NULL;observed=NULL;diff=NULL;reverse_diff=FALSE;type=3;white.adjust=FALSE;detailed=TRUE;return_aov=TRUE

a<-function(argument) {
  result<-argument
  if(isTRUE(argument))
    result<-"TRUE"
  if(isFALSE(argument))
    result<-"FALSE"
  if(is.null(argument))
    result<-"NULL"
  if(length(argument)>1){
    result<-toString(argument)
    result<-paste0(c(".(",result,")"),collapse = " ")
  }
  return(result)
}
ez_text<-paste("ez::ezANOVA(\tdata=df,\n",
               "\t\tdv=",a(dv),",\n",
               "\t\twid=",a(wid),",\n",
               "\t\twithin=",a(within),",\n",
               "\t\twithin_full=",a(within_full),",\n",
               "\t\tbetween=",a(between),",\n",
               "\t\twithin_covariates=",a(within_covariates),",\n",
               "\t\tbetween_covariates=",a(between_covariates),",\n",
               "\t\tobserved=",a(observed),",\n",
               "\t\tdiff=",a(diff),",\n",
               "\t\treverse_diff=",a(reverse_diff),",\n",
               "\t\ttype=",a(type),",\n",
               "\t\twhite.adjust=",a(white.adjust),",\n",
               "\t\tdetailed=",a(detailed),",\n",
               "\t\treturn_aov=",a(return_aov),")")
cat(ez_text)
ez<-eval(parse(text=ez_text))
ez

生成的数据应该模拟平衡的析因设计,但是此示例并不完美,因为必须汇总数据,因为仅使用了一个IV。我认为该功能可以在任何抛出的情况下运行。

一个完全成熟的功能看起来像这样

rm(list=ls())
df<-data.frame(id=rep(seq(1,80),each=81,1),
               IV1=rep(LETTERS[1:3],each=1,2160),
               IV2=rep(LETTERS[4:6],each=3,720),
               IV3=rep(LETTERS[7:9],each=9,240),
               IV4=rep(LETTERS[10:12],each=27,80),
               DV1=rnorm(2160,mean=10,sd=1),
               DV2=rnorm(2160,mean=20,sd=1),
               DV3=rnorm(2160,mean=20,sd=1),
               DV4=rnorm(2160,mean=20,sd=1),
               stringsAsFactors=FALSE)

dv="DV1";wid="id";within="IV1";within_full=NULL;between=NULL;within_covariates=NULL;between_covariates=NULL;observed=NULL;diff=NULL;reverse_diff=FALSE;type=3;white.adjust=FALSE;detailed=TRUE;return_aov=TRUE

report_factorial_anova<-function(df,dv,wid,within=NULL,within_full=NULL,between=NULL,within_covariates=NULL,between_covariates=NULL,observed=NULL,diff=NULL,reverse_diff=FALSE,type=3,white.adjust=FALSE,detailed=TRUE,return_aov=TRUE) {
  options(contrasts=c("contr.sum","contr.poly"))
  result<-emf<-list()
  post_hoc<-omnibus<-data.frame()
  column_design<-unique(c(wid,within,within_full,between,within_covariates,between_covariates))
  df<-plyr::ddply(df,column_design,plyr::numcolwise(mean,na.rm=TRUE))

  for(dependent in dv) {
    a<-function(argument) {
      result<-argument
      if(isTRUE(argument))
        result<-"TRUE"
      if(isFALSE(argument))
        result<-"FALSE"
      if(is.null(argument))
        result<-"NULL"
      if(length(argument)>1){
        result<-toString(argument)
        result<-paste0(c(".(",result,")"),collapse = " ")
      }
      return(result)
    }
    ez_text<-paste("ez::ezANOVA(\tdata=df,\n",
                   "\t\tdv=",a(dependent),",\n",
                   "\t\twid=",a(wid),",\n",
                   "\t\twithin=",a(within),",\n",
                   "\t\twithin_full=",a(within_full),",\n",
                   "\t\tbetween=",a(between),",\n",
                   "\t\twithin_covariates=",a(within_covariates),",\n",
                   "\t\tbetween_covariates=",a(between_covariates),",\n",
                   "\t\tobserved=",a(observed),",\n",
                   "\t\tdiff=",a(diff),",\n",
                   "\t\treverse_diff=",a(reverse_diff),",\n",
                   "\t\ttype=",a(type),",\n",
                   "\t\twhite.adjust=",a(white.adjust),",\n",
                   "\t\tdetailed=",a(detailed),",\n",
                   "\t\treturn_aov=",a(return_aov),")")
    cat(ez_text)
    ez<-eval(parse(text=ez_text))
    if(!is.null(ez$`Levene's Test for Homogeneity of Variance`)){
      ez$`Levene's Test for Homogeneity of Variance`<-data.frame(Effect=ez$ANOVA$Effect,ez$`Levene's Test for Homogeneity of Variance`,check.names=FALSE)
      names(ez$`Levene's Test for Homogeneity of Variance`)[which("p"==names(ez$`Levene's Test for Homogeneity of Variance`))]<-"p[Levene]"
    }
    ez$ANOVA<-data.frame(dv=dependent,sjstats::anova_stats(ez$aov))
    # influence<-influence.measures(unlist(ez$aov))
    # influence_measures<-data.frame(df$id,influence$infmat,influence$is.inf,check.names=FALSE)
    names(ez$ANOVA)[which("term"==names(ez$ANOVA))]<-"Effect"
    result_omnibus<-Reduce(function(x,y) merge(x,y,by="Effect",all=TRUE,sort=FALSE,no.dups=FALSE),ez[names(ez)!="aov"])
    result_omnibus[,grep("<.05",names(result_omnibus))]<-NULL
    names(result_omnibus)<-gsub(".x","",names(result_omnibus),fixed=TRUE)
    names(result_omnibus)<-gsub(".y","",names(result_omnibus),fixed=TRUE)
    ez$omnibus<-result_omnibus
    omnibus<-plyr::rbind.fill(omnibus,result_omnibus)
    rm(result_omnibus)
    iv<-unique(c(within,within_full,between))
    for(i in 1:length(iv))
      emf[[i]]<-combn(iv,i)
    for(cf in emf)
      for (rcf in 1:nrow(cf)) {
        means<-emmeans::emmeans(ez$aov,formula(paste("~",paste(as.character(cf[rcf,]),collapse="*"))))
        paired_comparison<-data.frame(dv=dependent,graphics::pairs(means))
        post_hoc<-plyr::rbind.fill(post_hoc,paired_comparison)
      }
    result[[dependent]]<-ez
  }
  result<-list(omnibus=omnibus,post_hoc=post_hoc,object=result)
  return(result)
}

report_factorial_anova(df=df,dv=c("DV1","DV2"),wid="id",within=c("IV1","IV2"),within_full=c("IV1","IV2"),between=NULL,within_covariates=NULL,between_covariates=NULL,observed=NULL,diff=NULL,reverse_diff=FALSE,type=3,white.adjust=FALSE,detailed=TRUE,return_aov=TRUE)
report_factorial_anova(df=df,dv=c("DV1","DV2"),wid="id",within=c("IV1","IV2"),within_full=c("IV1","IV2"),between=NULL,within_covariates="DV3",between_covariates=NULL,observed=NULL,diff=NULL,reverse_diff=FALSE,type=3,white.adjust=FALSE,detailed=TRUE,return_aov=TRUE)
report_factorial_anova(df=df,dv=c("DV1","DV2"),wid="id",within=NULL,within_full=NULL,between=c("IV1","IV2"),within_covariates=NULL,between_covariates=NULL,observed=NULL,diff=NULL,reverse_diff=FALSE,type=3,white.adjust=FALSE,detailed=TRUE,return_aov=TRUE)
report_factorial_anova(df=df,dv="DV1",wid="id",within=NULL,within_full=NULL,between=c("IV1","IV2"),within_covariates=NULL,between_covariates=NULL,observed=NULL,diff=NULL,reverse_diff=FALSE,type=3,white.adjust=FALSE,detailed=TRUE,return_aov=TRUE)
report_factorial_anova(df=df,dv="DV1",wid="id",within="IV3",within_full="IV3",between=c("IV1","IV2"),within_covariates=NULL,between_covariates=NULL,observed=NULL,diff=NULL,reverse_diff=FALSE,type=3,white.adjust=FALSE,detailed=TRUE,return_aov=TRUE)
report_factorial_anova(df=df,dv="DV1",wid="id",within=c("IV1","IV2","IV3","IV4"),within_full=c("IV1","IV2","IV3","IV4"),between=,within_covariates=NULL,between_covariates=NULL,observed=NULL,diff=NULL,reverse_diff=FALSE,type=3,white.adjust=FALSE,detailed=TRUE,return_aov=TRUE)