[R-sig-ME] extracting name of random slope from mer object

Sven Hohenstein sven.hohenstein at uni-potsdam.de
Fri Jul 26 15:23:20 CEST 2013


Hi Dan,

you can try to parse the model formula:

gsub(" \\|.*", "", findbars(formula(model)))

Best,
Sven



Quoting Daniel Ezra Johnson <danielezrajohnson at gmail.com>:

> Thanks, Sven, for your help.
>
> I'm running 0.9999992 which I guess (contrary to ordinary mathematics) is
> older than 0.99999911. And I'm not seeing the attribute "predvars.fixed"
> anywhere. But also, if my model is something like this:
>
> lmer(y ~ x1 + (x1 | subject))
>
> Then what I want to capture is "x1" but if I remove the fixed effects then
> I won't have it...
>
> Dan
>
>
> On Fri, Jul 26, 2013 at 8:28 AM, Sven Hohenstein <
> sven.hohenstein at uni-potsdam.de> wrote:
>
>> Hi Dan,
>>
>> you can use the following approach to extract the names of random-slope
>> variables:
>>
>> # the model terms
>> term <- terms(model)
>> # all variables
>> vars <- as.character(attr(terms, "predvars")[-1])
>> # fixed variables
>> varsFix <- as.character(attr(terms, "predvars.fixed")[-1])
>> # random factors
>> varsRan <- names(ranef(model))
>>
>> # Now, the names of the fixed variables and the random factors
>> # can be removed from the vector including all variable names:
>> Reduce(setdiff, list(vars, varsFix, varsRan))
>>
>>
>> This approach works with lme4 version 0.99999911-7. It might not work with
>> models created with earlier versions of lme4.
>>
>> Best,
>> Sven
>>
>>
>>
>>
>>
>>
>> Quoting Daniel Ezra Johnson <danielezrajohnson at gmail.com>:
>>
>>  Hello,
>>>
>>> I can extract the name(s) of random intercept variable(s) with this
>>> command:
>>>
>>>  names(VarCorr(model))
>>>>
>>>
>>> Is there any easy way to extract the names of random slope variables?
>>> I can't find it anywhere in str(model).
>>>
>>> Thanks a lot,
>>> Dan
>>>
>>> P.S. I know I could compare the list of all variables, e.g.
>>>
>>>  names(model.frame(model))
>>>>
>>>
>>> with the headings inside e.g. ranef(model), and match it up that way, but
>>> I
>>> was wondering if there was anything easier.
>>>
>>> Also, I can't use model at call because I'm calling lmer() out of another
>>> function!
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________**_________________
>>> R-sig-mixed-models at r-project.**org   
>>> <R-sig-mixed-models at r-project.org>mailing list
>>> https://stat.ethz.ch/mailman/**listinfo/r-sig-mixed-models<https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models>
>>>
>>>
>> --
>> Sven Hohenstein
>> Department of Psychology
>> University of Potsdam
>> Karl-Liebknecht-Str. 24-25
>> 14476 Potsdam
>> Germany
>> Tel.: ++49 331-977 2370
>>
>>
>



More information about the R-sig-mixed-models mailing list