function to create minimal adequate model w/ interaction variables

Skip to first unread message


Jun 30, 2016, 11:26:05 PM6/30/16
to StatForLing with R
I want to look at both the maximal model and minimal adequate model of 48 mixed effects linear regressions (with random intercepts only) created with lme4::lmer. Manually creating the minimal adequate model for these 48 models would be laborious.

Has anyone written a function to automate the creation of the minimal adequate model using a backward selection procedure (as described in SFLWR, 2nd ed. p. 260, using car::Anova(type = 3)) that correctly deals with interaction variables. I don't want the function to delete non-significant main effect variables if they are involved in a significant interaction variable. 

I've written a function that correctly creates the minimal adequate model of models without interaction variables, but I can't figure out a reasonably succinct way to do so when the model has interaction variable. I don't want to reinvent the wheel if someone is willing to share their wheel with me, that is, a function or script.

Thanks in advance if you are willing to share your function or script.

For what it's worth, here's what I've written to get the minimal adequate model w/o interaction variables:

get_min_adequate <- function(input) {
  # input <- model1
  output <- input
  ar <- car::Anova(output, type = 3)
  ar <- broom::tidy(ar) %>% rename(p_value = `Pr..Chisq.`)
  top_row <- ar %>% arrange(desc(p_value)) %>% slice(1)
  highest_p_value <- top_row[1, 'p_value']
  if (highest_p_value > 0.05) {
    while (highest_p_value > 0.05) {
      cat("removing ", top_row[1, 'term'], "\n")
      new_formula <- str_c("update(output, ~. -", top_row[1, 'term'], ")")
      output <- eval(parse(text = new_formula))
      top_row <- car::Anova(output, type = 3) %>% broom::tidy() %>% rename(p_value = `Pr..Chisq.`) %>% arrange(desc(p_value)) %>% slice(1)
      highest_p_value <- top_row[1, 'p_value']
    }  # next iteration
  }  # end if p value if > 0.05
  else {
    output <- input
  final_formula <- summary(output) %>% .$call
  final_formula <- as.character(final_formula)
  final_formula <- str_c(
    str_replace(final_formula[1], regex("^lme4"), "lmerTest"),
    ", data = ",
  output <- eval(parse(text = final_formula))
}  # end function definition

Stefan Th. Gries

Jun 30, 2016, 11:28:53 PM6/30/16
to StatForLing with R

I think the package lmerTest has such a function. I'll look it up and will let you know if you don't already have it by then.

Stefan Th. Gries
Univ. of California, Santa Barbara

You received this message because you are subscribed to the Google Groups "StatForLing with R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit

Stefan Th. Gries

Jun 30, 2016, 11:32:07 PM6/30/16
to StatForLing with R

Yes, it's just lmerTest::step.


Jul 1, 2016, 12:17:42 AM7/1/16
to StatForLing with R
Sweet! This is exactly what I needed. Thanks for pointing it out to me. Gotta love the open source R community!

BTW, lmerTest::step throws an error if you log a frequency measure in the formula to lme4::lmer, for example,
lme4::lmer(dep_var ~ log(lex_freq + 1), ...)

Error in mat %*% rho$fixEffs : non-conformable arguments

You simply need to log any frequency measurements beforehand, for example:
(lex_freq_log = log(.$lex_freq + 1))

<- lme4::lmer(dep_var ~ lex_freq_log, ...)

Thanks again (to Stefan for pointing out this function to me and to Alexandra Kuznetsova and collaborators for the lmerTest package).

Stefan Th. Gries

Jul 1, 2016, 12:20:47 AM7/1/16
to StatForLing with R

> BTW, lmerTest::step throws an error if you log a frequency measure in the formula to lme4::lmer

Several functions do that, some of the effects package functions react similarly so it's always best to create those things before the model fitting (and only then attach).

Reply all
Reply to author
0 new messages