Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for lmer and glmer mixed model estimation #9

Closed
cbjrobertson opened this issue Apr 2, 2020 · 7 comments
Closed

Added support for lmer and glmer mixed model estimation #9

cbjrobertson opened this issue Apr 2, 2020 · 7 comments
Labels
enhancement New feature or request

Comments

@cbjrobertson
Copy link

Currently, run_specs does not appear to support specification of models with random effects (i.e. glmer or lmer models). I note that since #3, run_specs supports customisable glms, but would love to see a random effects parameter added to run_specs, so e.g. the following usage would be possible:

my_glmer <- function(formula,data){
    glmer(formula, data = data, family = binomial)
}

results <- run_specs(df = example_data,
                      y = c("y1", "y2"),
                      x = c("x1", "x2"),
                      model = c("lm"),
                      controls = c("c1", "c2"),
                      random_groups = c("group1"),
                      random_variance_components = c("1","x1","c1"),
                      subsets = list(group1 = unique(example_data$group1),
                                    group2 = unique(example_data$group2)))

where random_groups specified the level 2 grouping variables, and random_variance_components specified whether to specify randomly varying intercepts only, or slopes also for listed variables.

@masurp masurp added the enhancement New feature or request label Apr 2, 2020
@masurp
Copy link
Owner

masurp commented Apr 2, 2020

You can specify glmer or lmer models at least with random intercepts. But you have to add the random effect part to the formula using paste(). See below for an example.

my_glmer <- function(formula,data){
    glmer(paste(formula, " + (1|group1)", data = data, family = binomial)
}

results <- run_specs(df = example_data,
                      y = c("y1", "y2"),
                      x = c("x1", "x2"),
                      model = c("my_glmer"),
                      controls = c("c1", "c2"),
                      subsets = list(group2 = unique(example_data$group2)))

However, it is true that we haven't included a full-on framework for random effect modelling. So what you're suggestions makes a lot of sense. Will work on this for the next version.

@cbjrobertson
Copy link
Author

Hi -- cool!

Since posting, I've actually written a limited update which would at least allow researchers to specify slope components as well. It doesn't permute slope and intercept possibilities across a specified set, as I've suggested above, and also doesn't exclude formulas which specify random slope components when no fixed slopes for the variable in question are also specified, but it might help you get started. I'll make a PU on your new branch and you can decide if you want to carry on working in the direction I've started:)

@masurp
Copy link
Owner

masurp commented Apr 2, 2020

Haha. Check out the branch "add_random_effects". I just committed some functionality to add random groups. Simply estimating a random intercept model is comparatively easy then.

Great, I'll check your updates. The question to me is: How should random slopes be included? Similar to control variables (no random slope, each individually, all together)? Then again, it depends on respective specifications which random slopes can be included and which not. Have to think about this more.

@cbjrobertson
Copy link
Author

Lol. I know. I went back and you'd already started! Yeah, it gets quite complicated as many specifications that are technically possible to implement don't really make theoretical sense (particularly including a random slope component when no fixed component for the predictor is also included). Happy to help where I can...

@masurp
Copy link
Owner

masurp commented Apr 2, 2020

Had a short look at your update. Looks good, but I would like to be more systematic about it. As you point out, with this approach, we get specifications that don't make sense...

If you don't mind, let me dwell a bit on it and I will get back to you in time.

Thanks for raising this issue and also thanks for your update.

@cbjrobertson
Copy link
Author

cbjrobertson commented Apr 2, 2020

Not to worry, I didn't expect you to merge it anyway. It's not general enough. I played with added a filter function to exclude non-sensical permutations, i.e. in line# 62 of setup_specs.R adding:

- as_tibble
+ as_tibble %>%
    filter(unlist(Map(function(x, y) grepl(x, y), x, random_var_components)))

but that does't work, and I'm not even sure that would be the most principled way of setting up the code. Anyway, cheers. Thanks for the cool package.

@masurp
Copy link
Owner

masurp commented Dec 7, 2020

We made small updates to specr to allow for more flexibility in conducting analyses with random effects.

Given the vast possibility to include random intercepts, slopes, interactions, etc., we did not include extra arguments in run_specs() but extended the functionality to include custom model estimation functions. This – among other things – allows to specify multilevel models with various random effects. For a vignette, see:
https://masurp.github.io/specr/articles/random_effects.html

@masurp masurp closed this as completed Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants