[R-sig-ME] Graphing interaction terms in LME4, glmer

Shane Gleason sgleason at siu.edu
Tue Jan 29 17:09:40 CET 2013


Hi Ista,

It was a version problem.  After upgrading to 2.15.2 your solution 
worked like a charm.  However, for those that have older systems or just 
want to jazz it up the documentation on the effects package 
(particularly Fox (2003- JSS) and the CRAN help file are wonderful and 
cover almost every eventuality.

Take care everyone,

Shane

On 01/28/2013 08:11 PM, Ista Zahn wrote:
> Hi Shane,
>
> I think there may be some version issues here: on my system my example
> worked fine.
>
> I have
>
> R version 2.15.2 (2012-10-26)
> Platform: x86_64-unknown-linux-gnu (64-bit)
>
> locale:
>   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
>   [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C
>   [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
>   [7] LC_PAPER=C                 LC_NAME=C
>   [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] grid      stats     graphics  grDevices utils     datasets  methods
> [8] base
>
> other attached packages:
> [1] effects_2.2-3    colorspace_1.2-0 nnet_7.3-5       MASS_7.3-23
> [5] lme4_0.999999-0  Matrix_1.0-10    lattice_0.20-13
>
> loaded via a namespace (and not attached):
> [1] compiler_2.15.2 nlme_3.1-107    stats4_2.15.2   tools_2.15.2
> Best,
> Ista
> On Mon, Jan 28, 2013 at 7:29 PM, Shane Gleason <sgleason at siu.edu> wrote:
>> Hello everyone,
>>
>> Thank you kindly to everyone who replied.  Ista's comment was the one that
>> had exactly what I needed- though it did take some tinkering.  For the
>> benefit of the next person who comes along, I'm adding the changes I needed
>> to make to Ista's code:
>>
>> Running the code Ista initially provided, the following error in bold comes
>> up
>>
>>
>>> data(cake, package="lme4")
>>> cake$a2 <- ifelse(cake$angle < mean(cake$angle, na.rm=TRUE), 0, 1)
>>>
>>> fm1 <- lmer(a2 ~ recipe * temp + (1|recipe:replicate),
>> +             data = cake,
>> +             family = "binomial",
>> +             REML = FALSE)
>>> library(effects)
>>> plot(effect("recipe:temp", fm1), grid=TRUE)
>> Error in plot(effect("recipe:temp", fm1), grid = TRUE) :
>>    error in evaluating the argument 'x' in selecting a method for function
>> 'plot': Error: $ operator not defined for this S4 class
>>
>> The solution to this is to follow on to the next line in the example in the
>> effects display from John Fox's JSS paper http://www.jstatsoft.org/v08/i15:
>>
>> detach(package:lme4) # if previously attached
>> library(nlme)
>> data(cake, package="lme4")
>>
>> cake$rep <- with(cake, paste( as.character(recipe), as.character(replicate),
>> sep=""))
>> fm2 <- lme(angle ~ recipe * temperature, data=cake,
>>             random = ~ 1 | rep, method="ML")
>> plot(effect("recipe", "temperature", fm2), grid=TRUE)
>>
>> However, this produces a heck of a lot of panels, which are kind of
>> difficult to interpret.... so we force everything onto one graph- which is
>> accomplished by adding the following line (also from John Fox's working
>> paper (pg 14): (note, I haven't updated this to reflect the cake example
>>
>>> plot(effect("recipeism*extraversion", cowles.mod,
>> +
>> xlevels=list(neuroticism=0:24, extraversion=seq(0, 24, 6))),
>> +
>> multiline=TRUE, ylab="Probability(Volunteer)")
>>
>> You all have been a great help.  I hope this summary e-mail helps others in
>> the future.
>>
>> Shane
>>
>>
>>
>>
>> On 01/28/2013 05:13 PM, Ista Zahn wrote:
>>
>> Hi Shane,
>>
>> Take a look at the effects package, I believe it has what you are
>> looking for. Here is a modified example from ?effect
>>
>> library(lme4)
>>
>> data(cake, package="lme4")
>> cake$a2 <- ifelse(cake$angle < mean(cake$angle, na.rm=TRUE), 0, 1)
>>
>> fm1 <- lmer(a2 ~ recipe * temp + (1|recipe:replicate),
>>              data = cake,
>>              family = "binomial",
>>              REML = FALSE)
>>
>> library(effects)
>> plot(effect("recipe:temp", fm1), grid=TRUE)
>>
>>
>> HTH,
>> Ista
>>
>> On Mon, Jan 28, 2013 at 5:32 PM, Shane Gleason <sgleason at siu.edu> wrote:
>>
>> Hello everyone,
>>
>> I am working with lme4, specifically the glmer command.  In a multilevel
>> model I have an interaction term which is significant. However, with my
>> discipline we stress that an interaction term cannot be evaluated by p value
>> alone and one must reference a graphical display of the interaction, with
>> confidence intervals drawn around it.
>>
>> That is to say if the interaction is X*Z then the interaction might be
>> significant when X=1 and Z=3, but may not be significant if X=4 and Z=9.
>>
>> I have searched around and I can't seem to find any indication of how to
>> graph out the interaction (though I see some references to it for lmer (but
>> never glmer)).  So, my question is two-part- 1: Does this assumption about
>> how interaction terms work hold for MLMs?  2: If the answer to 1 is yes,
>> does anyone know of software I can use to graph out these interactions?
>>
>> Thanks,
>>
>> Shane
>>
>> --
>> Shane Gleason
>> Doctoral Candidate
>> Southern Illinois University:Carbondale
>> Department of Political Science
>> Faner 3172
>>
>> _______________________________________________
>> R-sig-mixed-models at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>>
>>
>> --
>> Shane Gleason
>> Doctoral Candidate
>> Southern Illinois University:Carbondale
>> Department of Political Science
>> Faner 3172

-- 
Shane Gleason
Doctoral Candidate
Southern Illinois University:Carbondale
Department of Political Science
Faner 3172



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