Defining dependent and independent variables dynamically in the ezANOVA function
Asked Answered
C

3

5

I want to run an ezANOVA from the ez package on multiple dependent variables in a loop and save the result into several variables. Each dependent variable is within a separate column of the same data frame.

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))
}

This is an example data frame:

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, 4L, 5L, 6L,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, 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, 0, 0, 0, 0,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, 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")

The problem with this approach is that R interprets the index dependent.variable as the specifier for a column in the dataframe aov.data and therefore returns the following error:

"dependent.variable" is not a variable in the data frame provided.

I've tried wrapping the index with eval() or print() but to no avail.

Cavicorn answered 20/9, 2012 at 15:48 Comment(3)
can you please give us a reproducible example? tinyurl.com/reproducible-000 . In general, if you're using assign() there's a better way to do things ...Nonmetallic
Thanks for the quick response. I added a data frame with which this should work.Cavicorn
Let me add, thought, that I don't think the problem is due to assign(). I've tried a variant where all results are stored into elements of a list (aov.results[[dependent.variable]] <- ...).Cavicorn
R
6

Yet another not good solution:

lapply(all.dependent.variables, function(i) {
  eval(parse(text=
    paste0('ezANOVA(data=aov.data,
                    dv=', i,',
                    wid=subject,
                    between=.(factor1, factor2),
                    return_aov = TRUE)')
  ))
})
Raul answered 22/2, 2013 at 21:19 Comment(1)
very nice I like this solutionConcertgoer
N
3

OK, this is definitely not your fault ... ezANOVA does some clever evaluation stuff internally which screws you up. Here's a horrible incantation that seems to work, but it might be best to (1) contact the package maintainer and see if they can find a way to fix the internals more elegantly; (2) consider whether you can get by with a more standard anova evaluator that doesn't do so much internal messing around ...

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)
       })
Nonmetallic answered 20/9, 2012 at 16:42 Comment(4)
Clever? I would say not-so-clever if it cripples the function for general use.Glee
Clever is not necessarily a compliment in this context ... I do sympathize with the package author to some extent -- once you start to do any amount of non-standard evaluation, for whatever (initially good) reason, it always seems to come back and bite you ...Nonmetallic
Thank you, I would vote your answer up, but I don't have enough reputation to do so. ;)Cavicorn
This did not work for me-- for some unknown reason it ran a bunch of identical ANOVAs using the first variable from all.dependent.variables. However, it did work with this change: e1[[1]][["dv"]] <- eval(x) instead of e1[[1]][["dv"]] <- eval(e1[[1]][["dv"]]Holpen
C
1

Following the recommendations above, this a more general solution of how to make ezanova arguments accepted in loops. I guess nobody needs to see how to put a function in a loop and thus I will not go into this. This is my generalized solution with an example.

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

The generated data is supposed to simulate a balanced factorial design however this example is not perfect, since data have to be aggregated because only one IV is used. I think however this function will run with anything thrown into it.

A fully fledged function looks like this bellow

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)
Concertgoer answered 12/2, 2020 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.