Skip to content

anova.merMod error with find_best_model.lmerModLmerTest #83

@HugoNjb

Description

@HugoNjb

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions