84 views

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:

library(dplyr)

library(stringr)

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"),

"(",

final_formula[2],

", data = ",

final_formula[3],

")"

)

output <- eval(parse(text = final_formula))

return(output)

} # end function definition

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.

STG

--

Stefan Th. Gries

----------------------------------

Univ. of California, Santa Barbara

http://tinyurl.com/stgries

----------------------------------

--

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 statforling-wit...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

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:

`library(dplyr)`

input_df <-

input_df %>%

mutate(lex_freq_log = log(.$lex_freq + 1))

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

lmerTest::step(model1)

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

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

Forward

0 new messages

Search

Clear search

Close search

Google apps

Main menu