neuropsychology / psycho.R

An R package for experimental psychologists
https://neuropsychology.github.io/psycho.R/
Other
145 stars 32 forks source link

anova.merMod error with find_best_model.lmerModLmerTest #83

Open HugoNjb opened 6 years ago

HugoNjb commented 6 years ago

1

When NA values are in a dataset, using the find_best_model function returns the following error:

data <- affective
fit <- lmer(Tolerating ~ Salary + Life_Satisfaction + Concealing + (1|Sex) + (1|Age), data=data)
best <- find_best_model(fit)

Error in anova.merMod(new("lmerModLmerTest", vcov_varpar = c(0.0686147429065321, : models were not all fitted to the same size of dataset

Solution Removing NA values only for the variables used in the formula

  # Recreating the dataset without NA
  dataComplete <- get_all_vars(fit)[complete.cases(get_all_vars(fit)), ]

  # fit models
  models <- c()
  for (formula in combinations) {
    newfit <- update(fit, formula, data = dataComplete)
    models <- c(models, newfit)
  }


2

Using the same function, warning messages are always displayed:

data <- affective
fit <- lmer(Tolerating ~ Salary + Life_Satisfaction + Concealing + (1|Sex) + (1|Age), data=data)
best <- find_best_model(fit)

Warning message: In anova.merMod(new("lmerModLmerTest", vcov_varpar = c(0.0686147429065321, : failed to find model names, assigning generic names

Solution Hiding warnings when the anova are computed.

  # No warnings for this part
  options(warn = -1)

  # Model comparison
  comparison <- as.data.frame(do.call("anova", models))
  comparison$formula <- combinations

  # Re-displaying warning messages
  options(warn = 0)
DominiqueMakowski commented 6 years ago

nice solving :)

HugoNjb commented 6 years ago

Thank you !