Home > OS >  Function to simplify computation of corrected degrees of freedom and p in ezANOVA after sphericity c
Function to simplify computation of corrected degrees of freedom and p in ezANOVA after sphericity c

Time:12-04

Using ezANOVA, I can compute an ANOVA, Mauchly's Test for Sphericity as well as the Greenhouse-Geisser and Huynd-Feldt epsilon for correction, however, I must then compute the corrected df and p manually. I would like to use a function to compute the correction automatically.

I wrote this code to compute the corrections with the Greenhouse-Geisser for one variable in my ANOVA (ANOVA.sr) and insert the corrected value in the original ANOVA. I can then extract ANOVA.sr$ANOVA to a table and have the correct values.

ANOVA.sr$ANOVA$DFn[2] <- ifelse(ANOVA.sr$`Mauchly's Test for Sphericity`$p[1] < 0.05, #If Mauchly is significant
                                ANOVA.sr$ANOVA$DFn[2]*ANOVA.sr$`Sphericity Corrections`$GGe[1], #Compute the Greenhouse Correction
                                ANOVA.sr$ANOVA$DFn[2]) #If not, keep the same value

ANOVA.sr$ANOVA$DFd[2] <- ifelse(ANOVA.sr$`Mauchly's Test for Sphericity`$p[1] < 0.05, #If Mauchly is significant
                                ANOVA.sr$ANOVA$DFd[2]*ANOVA.sr$`Sphericity Corrections`$GGe[1], #Compute the Greenhouse Correction
                                ANOVA.sr$ANOVA$DFd[2]) #If not, keep the same value

ANOVA.sr$ANOVA$p[2] <- ifelse(ANOVA.sr$`Mauchly's Test for Sphericity`$p[1] < 0.05, #If Mauchly is significant
                                ANOVA.sr$`Sphericity Corrections`$'p[GG]'[1], #Replace p values with corrected p
                                ANOVA.sr$ANOVA$p[2]) #If not, keep the same value

However, as you can see, this solution is not practical and certainly not scalable. With a 2x3 ANOVA, I would have to use the code 3 times, once for each IV and once for the interaction and this gets worse the more IV are included in the ANOVA.

I tried writing this function for computing DFn for all variables and interactions in the ANOVA:

Greenhouse_correction <- function(x) {
  for (i in 2:length(x$ANOVA$DFn)){
x$ANOVA$DFn[i] <- ifelse(x$`Mauchly's Test for Sphericity`$p[i-1] < 0.05,
       x$ANOVA$DFn[i]*x$`Sphericity Corrections`$GGe[i-1],
       x$ANOVA$DFn[i])
  }
}

where x would be the ANOVA list output from ezANOVA, however when I try to apply it to my ANOVA, I get a NULL result.

Any help is appreciated!

CodePudding user response:

It looks like the for loop in your function is not returning anything. In R, the last evaluated expression in a function is returned by default. In your function, the last expression evaluated is the ifelse statement, but that just assigns a new value to an element of the DFn vector, it doesn't return anything.

You can fix this by explicitly returning the modified DFn vector at the end of the function. You can do this by adding return(x$ANOVA$DFn) at the end of the function.

Here's what the updated function would look like:

Greenhouse_correction <- function(x) {
  for (i in 2:length(x$ANOVA$DFn)){
    x$ANOVA$DFn[i] <- ifelse(x$`Mauchly's Test for Sphericity`$p[i-1] < 0.05,
                             x$ANOVA$DFn[i]*x$`Sphericity Corrections`$GGe[i-1],
                             x$ANOVA$DFn[i])
  }
  return(x$ANOVA$DFn)
}

This should return the corrected DFn vector when you apply the function to your ANOVA. You can then use the returned vector to replace the original DFn vector in your ANOVA, like this:

ANOVA.sr$ANOVA$DFn <- Greenhouse_correction(ANOVA.sr)

You can use a similar approach to create a function that corrects the DFd and p values in your ANOVA as well.

  • Related