Lorain
Lorain

Reputation: 21

ezANOVA: 1) how to omit na data? 2) still getting error message after omitting na data?

For my repeated measures analysis using lme function I could omit not-available (na) data with the command: na.action=na.omit.

anova_lme_loc<-lme(data=rm1, fixed=Nmin~location*date, random=~1|subject,
                   na.action=na.omit)

However, when I try to do the same using the ezANOVA function I got the following notification:

anova_ez_loc=ezANOVA(data=rm1, dv=Nmin, wid=subject, within=date, 
                     between=location, na.action=na.omit)

Error in ezANOVA(data = rm1, dv = Nmin, wid = subject, within = date, : unused argument(s) (na.action = na.omit)

how can I omit my na data in ezANOVA? - solved using:

rm1_na <- na.omit(rm1)

but now I get the following error:

anova_ez_loc=ezANOVA(data=rm1_na, dv=Nmin, wid=subject, within=date, between=location)

Warning: Converting "subject" to factor for ANOVA.

Warning: Data is unbalanced (unequal N per group). Make sure you specified a well-considered value for the type argument to ezANOVA().

Error in ezANOVA_main(data = data, dv = dv, wid = wid, within = within, : One or more cells is missing data. Try using ezDesign() to check your data.

Upvotes: 2

Views: 11737

Answers (2)

Mario Reutter
Mario Reutter

Reputation: 359

Complement to the answer of @Jonathan Christensen :

When using within-subject factors, complete.cases doesn't work because it considers cases row-wise, while you need to get rid of all rows containing the ID of an incomplete case.

Here is a small script that extends complete.cases to a custom function called complete.cases.within that does everything for you:

if(!require(tidyverse)) install.packages("tidyverse"); library(tidyverse) #useful package for clean code

#helper function that returns ALL indices of matches of x in table instead of just the first one
#we need this to get all rows containing the ID of incomplete cases.
matchAll = function(x, table, nomatch=NA_integer_, incomparables=NULL) {
  which(!is.na(match(table, x, nomatch, incomparables)))
}

complete.cases.within = function(data, dv, wid) {
  incomplete = data %>% select(dv) %>% complete.cases() %>% !. #boolean vector containing incomplete rows
  toRemove = data %>% select(wid) %>% filter(incomplete) %>% .[,1] %>% unique() #all IDs containing incomplete rows
  positions = matchAll(toRemove, data[,wid])
  return(if (length(positions)==0) data else data[-positions,]) #drop all rows matching toRemove IDs
}

Upvotes: 0

Jonathan Christensen
Jonathan Christensen

Reputation: 3866

ezANOVA doesn't handle missing data, as the author outlines in this reponse to a similar question. You have two options:

  1. Remove the missing data manually. The function complete.cases may be of help here.
  2. Mike Lawrence alternatively suggests checking out ezMixed from the same package, which is more complicated but can handle missing data.

To remove the data manually, you would do something like this:

rm1.complete <- rm1[complete.cases(rm1),]

Then use rm1.complete in your analysis.

Upvotes: 2

Related Questions