# [R] [effects] Wrong xlevels in effects plot for mixed effects model when multiline = TRUE

Gerrit Eichner gerr|t@e|chner @end|ng |rom m@th@un|-g|e@@en@de
Mon Nov 9 22:51:42 CET 2020

```Dear John,

thank you for prompt reply and your hints. The problem is that our
lmer model is much more complicated and has several interaction
terms:

Mass ~ Sex + I(YoE - 1996) + I(PAI/0.1 - 16) + I(gProt/10 - 6.2) +
I(Age/10 - 7.2) + I((Age/10 - 7.2)^2) + Diuretics +
Sex:I(PAI/0.1 - 16) + Sex:I(gProt/10 - 6.2) +
Sex:I(Age/10 - 7.2) + Sex:I((Age/10 - 7.2)^2) +
I(YoE - 1996):I(Age/10 - 7.2) + I(PAI/0.1 - 16):I(Age/10 - 7.2) +
I(gProt/10 - 6.2):I(Age/10 - 7.2) +
(I(Age/10 - 7.2) + I((Age/10 - 7.2)^2) | ID)

so that allEffects is quite efficient, and since I want to place
several interaction terms with Age in one figure with Age on the
horizontal axis the argument x.var = "Age" in plot would be very

Further hints using the above complex model: The following works well:
eff <- Effect(c("gProt", "Age"), m,
xlevels = list(gProt = 1:6 * 30, Age = 60:100))
plot(eff, lines=list(multiline=TRUE), x.var = "Age")

But this fails (note that Age is missing in xlevels):
eff <- Effect(c("gProt", "Age"), m, xlevels = list(gProt = 1:6 * 30))
plot(eff, lines=list(multiline=TRUE), x.var = "Age")

And that just led me to a solutution also for allEffects: Specifying
Age in xlevels for allEffects (although it seems unnecessary when
x.var = "Age" is used in plot) produces the correct graphical
output! :-)

Thank you very much for your support and the brilliant effects
package in general! :-)

Best regards  --  Gerrit

---------------------------------------------------------------------
Dr. Gerrit Eichner                   Mathematical Institute, Room 212
gerrit.eichner using math.uni-giessen.de   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
Fax: +49-(0)641-99-32109            http://www.uni-giessen.de/eichner
---------------------------------------------------------------------

Am 09.11.2020 um 19:51 schrieb John Fox:
> Dear Gerrit,
>
> This looks like a bug in plot.eff(), which I haven't yet tracked down,
> but the following should give you what you want:
>
> eff <- Effect(c("gProt", "Age"), m, xlevels = list(gProt = 1:6 * 30,
> Age=60:100))
> plot(eff, lines=list(multiline=TRUE))
>
> or
>
> eff <- predictorEffect("Age", m, xlevels = list(gProt = 1:6 * 30))
> plot(eff, lines=list(multiline=TRUE))
>
> A couple of comments on your code, unrelated to the bug in plot.eff():
>
> You don't need allEffects() because there's only one high-order fixed
> effect in the model, I(gProt/10 - 6.2):I(Age/10 - 7.2) (i.e., the
> interaction of gProt with Age).
>
> x.var isn't intended as an argument for plot() with allEffects() because
> there generally isn't a common horizontal axis for all of the high-order
> effect plots.
>
> Finally, thank you for the bug report. Barring unforeseen difficulties,
> we'll fix the bug in due course.
>
> I hope this helps,
>   John
>
> John Fox, Professor Emeritus
> McMaster University
> web: https://socialsciences.mcmaster.ca/jfox/
>
> On 2020-11-09 8:06 a.m., Gerrit Eichner wrote:
>> Dear list members,
>>
>> I observe a strange/wrong graphical output when I set the xlevels
>> in (e. g.) allEffects for an lmer model and plot the effects with
>> multiline = TRUE. I have compiled a reprex for which you need the
>> lmer model and the environment in which the model was fitted. They
>> are contained in the zip file at
>> https://jlubox.uni-giessen.de/dl/fiSzTCc3bW8z2npZvPpqG1xr/m-and-G1.zip
>> After unpacking the following should work:
>>
>> m <- readRDS("m.rds")   # The lmer-model.
>> G1 <- readRDS("G1.rds") # Environment in which the model
>>                           # was fitted; needed by alaEffects.
>> summary(m) # Just to see the model.
>>
>> library(effects)
>> aE <- allEffects(m, xlevels = list(gProt = 1:6 * 30))
>>                       # Non-default values for xlevels.
>>
>> plot(aE)                      # Fine.
>> plot(aE, x.var = "Age")       # Fine.
>> plot(aE, lines = list(multiline = TRUE))  # Fine.
>>
>> plot(aE, lines = list(multiline = TRUE),
>>        x.var = "Age")        # Nonsense.
>>
>>
>> Anybody any idea about the reason, my mistake, or a
>> workaround? Thx for any hint!
>>
>>    Regards  --  Gerrit
>>
>>
>> PS:
>>   > sessionInfo()
>> R version 4.0.2 (2020-06-22)
>> Platform: x86_64-w64-mingw32/x64 (64-bit)
>> Running under: Windows 10 x64 (build 18363)
>>
>> Matrix products: default
>>
>> locale:
>> [1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252
>> [3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
>> [5] LC_TIME=German_Germany.1252
>>
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>
>> other attached packages:
>> [1] effects_4.2-0 carData_3.0-4
>>
>> loaded via a namespace (and not attached):
>>    [1] Rcpp_1.0.5       lattice_0.20-41  MASS_7.3-53      grid_4.0.2
>> DBI_1.1.0
>>    [6] nlme_3.1-149     survey_4.0       estimability_1.3 minqa_1.2.4
>> nloptr_1.2.2.2
>> [11] Matrix_1.2-18    boot_1.3-25      splines_4.0.2    statmod_1.4.34
>> lme4_1.1-23
>> [16] tools_4.0.2      survival_3.2-3   yaml_2.2.1       compiler_4.0.2
>> colorspace_1.4-1
>> [21] mitools_2.4      insight_0.9.5    nnet_7.3-14
>>
>> ---------------------------------------------------------------------
>> Dr. Gerrit Eichner                   Mathematical Institute, Room 212
>> gerrit.eichner using math.uni-giessen.de   Justus-Liebig-University Giessen
>> Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
>> http://www.uni-giessen.de/eichner
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help