Additional Functionality in Other Packages

Several packages extend the functionality of MixedModels.jl, both in ways specific to mixed models and in ways applicable to more general regression models. In the following, we will use the models from the previous sections to showcase this functionality.

using MixedModels
progress = isinteractive()
false
insteval = MixedModels.dataset("insteval")
ie1 = fit(MixedModel,
          @formula(y ~ 1 + studage + lectage + service + (1|s) + (1|d) + (1|dept)),
          insteval; progress)
Est.SEzpσ_sσ_dσ_dept
(Intercept)3.29080.0324101.45<1e-990.32640.51060.0787
studage: 40.05190.02322.240.0249
studage: 60.07210.02403.010.0026
studage: 80.13630.02645.17<1e-06
lectage: 2-0.08080.0154-5.25<1e-06
lectage: 3-0.11020.0167-6.59<1e-10
lectage: 4-0.18920.0196-9.65<1e-21
lectage: 5-0.16440.0214-7.68<1e-13
lectage: 6-0.24600.0205-12.01<1e-32
service: Y-0.07270.0135-5.40<1e-07
Residual1.1762
ie2 = fit(MixedModel,
          @formula(y ~ 1 + studage + lectage + service +
                      (1 | s) +
                      (1 + service | d) +
                      (1 + service | dept)),
          insteval; progress)
Est.SEzpσ_sσ_dσ_dept
(Intercept)3.29850.0308107.27<1e-990.32420.51600.0642
studage: 40.05020.02322.160.0306
studage: 60.05730.02422.370.0180
studage: 80.11280.02684.21<1e-04
lectage: 2-0.07870.0156-5.03<1e-06
lectage: 3-0.10360.0169-6.14<1e-09
lectage: 4-0.18370.0199-9.21<1e-19
lectage: 5-0.15030.0217-6.94<1e-11
lectage: 6-0.22320.0209-10.66<1e-25
service: Y-0.02810.0498-0.560.5731 0.39060.1640
Residual1.1698
sleepstudy = MixedModels.dataset("sleepstudy")
ss1 = fit(MixedModel, @formula(reaction ~ 1 + days + (1|subj)), sleepstudy; progress)
Est.SEzpσ_subj
(Intercept)251.40519.506226.45<1e-9936.0121
days10.46730.801713.06<1e-38
Residual30.8954
ss2 = fit(MixedModel, @formula(reaction ~ 1 + days + (1 + days|subj)), sleepstudy; progress)
Est.SEzpσ_subj
(Intercept)251.40516.632337.91<1e-9923.7807
days10.46731.50226.97<1e-115.7169
Residual25.5918
using DataFrames
contra = DataFrame(MixedModels.dataset("contra"))
contra[!, :anych] .= contra[!, :livch] .!= "0"
contrasts = Dict(:livch => EffectsCoding(; base="0"),
                 :urban => HelmertCoding(),
                 :anych => HelmertCoding())
gm1 = fit(MixedModel,
          @formula(use ~ 1 + urban + anych * age + abs2(age) + (1 | dist & urban)),
          contra,
          Bernoulli();
          contrasts,
          progress)
Est.SEzpσ_dist & urban
(Intercept)-0.34150.1265-2.700.00690.5685
urban: Y0.39360.08534.61<1e-05
anych: true0.60760.10465.81<1e-08
age-0.01300.0112-1.170.2440
abs2(age)-0.00560.0008-6.67<1e-10
anych: true & age0.03330.01282.600.0094

MixedModelsExtras.jl

https://palday.github.io/MixedModelsExtras.jl/v2

MixedModelsExtras.jl is a collection of odds-and-ends that may be useful when working with mixed effects models, but which we do not want to include in MixedModels.jl at this time. Some functions may one day migrate to MixedModels.jl, when we are happy with their performance and interface (e.g. vif), but some are intentionally omitted from MixedModels.jl (e.g. r2, adjr2).

using MixedModelsExtras
r2(ss2; conditional=true)
0.8263135094239904
r2(ss2; conditional=false)
0.28647139510771014
icc(ie2)
0.2885292866744761
icc(ie2, :dept)
0.016119194950382547
vif(ie1)
9-element Vector{Float64}:
 1.5141903374729053
 1.7354060220617624
 1.7822316986634812
 1.449378975046207
 1.4380891514780725
 1.5948966178188415
 1.4634020911560608
 1.8267103207894357
 1.0161785415795814
DataFrame(; coef=fixefnames(ie1)[2:end], VIF=vif(ie1))
9×2 DataFrame
RowcoefVIF
StringFloat64
1studage: 41.51419
2studage: 61.73541
3studage: 81.78223
4lectage: 21.44938
5lectage: 31.43809
6lectage: 41.5949
7lectage: 51.4634
8lectage: 61.82671
9service: Y1.01618
gvif(ie1)
3-element Vector{Float64}:
 1.3110872223511254
 1.325731162491792
 1.0161785415795814
DataFrame(; term=termnames(ie1)[2][2:end], GVIF=gvif(ie1))
3×2 DataFrame
RowtermGVIF
StringFloat64
1studage1.31109
2lectage1.32573
3service1.01618

RegressionFormulae.jl

https://github.com/kleinschmidt/RegressionFormulae.jl

RegressionFormulae.jl provides a few extensions to the somewhat more restricted variant of the Wilkinson-Roger notation found in Julia. In particular, it adds / for nested designs within the fixed effects and ^ for computing interactions only up to a certain order.

using RegressionFormulae

fit(MixedModel,
          @formula(y ~ 1 + service / (studage + lectage) +
                      (1 | s) +
                      (1 | d) +
                      (1 | dept)),
          insteval; progress)
Est.SEzpσ_sσ_dσ_dept
(Intercept)3.27880.034994.06<1e-990.32660.50990.0799
service: Y-0.04880.0275-1.780.0758
service: N & studage: 40.09040.02753.280.0010
service: Y & studage: 40.00930.02850.330.7442
service: N & studage: 60.07540.02752.740.0062
service: Y & studage: 60.06480.03082.100.0354
service: N & studage: 80.13980.03054.58<1e-05
service: Y & studage: 80.13490.03344.04<1e-04
service: N & lectage: 2-0.05110.0197-2.600.0093
service: Y & lectage: 2-0.11390.0233-4.89<1e-05
service: N & lectage: 3-0.10650.0211-5.06<1e-06
service: Y & lectage: 3-0.10230.0267-3.830.0001
service: N & lectage: 4-0.17970.0252-7.14<1e-12
service: Y & lectage: 4-0.19390.0294-6.61<1e-10
service: N & lectage: 5-0.20790.0283-7.34<1e-12
service: Y & lectage: 5-0.11800.0312-3.770.0002
service: N & lectage: 6-0.27120.0264-10.27<1e-24
service: Y & lectage: 6-0.22680.0293-7.74<1e-14
Residual1.1759
fit(MixedModel,
          @formula(y ~ 1 + (studage + lectage + service)^2 +
                      (1 | s) +
                      (1 | d) +
                      (1 | dept)),
          insteval; progress)
Est.SEzpσ_sσ_dσ_dept
(Intercept)3.22850.036887.85<1e-990.32640.50920.0800
studage: 40.12800.03403.770.0002
studage: 60.15250.03434.45<1e-05
studage: 80.23260.03995.83<1e-08
lectage: 20.05540.03021.840.0662
lectage: 3-0.02730.0640-0.430.6702
lectage: 4-0.13020.0724-1.800.0720
lectage: 5-0.08850.0807-1.100.2728
lectage: 6-0.17070.0836-2.040.0411
service: Y-0.03640.0278-1.310.1912
studage: 4 & lectage: 2-0.11170.0400-2.800.0052
studage: 6 & lectage: 2-0.16380.0397-4.13<1e-04
studage: 8 & lectage: 2-0.16830.0469-3.590.0003
studage: 4 & lectage: 3-0.11050.0694-1.590.1112
studage: 6 & lectage: 3-0.12950.0688-1.880.0599
studage: 8 & lectage: 3-0.08110.0714-1.140.2557
studage: 4 & lectage: 40.04200.07650.550.5833
studage: 6 & lectage: 4-0.12730.0770-1.650.0983
studage: 8 & lectage: 4-0.10950.0797-1.370.1694
studage: 4 & lectage: 5-0.17940.0964-1.860.0627
studage: 6 & lectage: 5-0.14000.0831-1.680.0921
studage: 8 & lectage: 5-0.17290.0864-2.000.0453
studage: 4 & lectage: 60.04910.09730.500.6137
studage: 6 & lectage: 6-0.08340.0853-0.980.3282
studage: 8 & lectage: 6-0.18210.0867-2.100.0358
studage: 4 & service: Y-0.08410.0314-2.670.0075
studage: 6 & service: Y-0.00680.0333-0.210.8376
studage: 8 & service: Y0.01570.03640.430.6652
lectage: 2 & service: Y-0.08410.0301-2.790.0053
lectage: 3 & service: Y-0.00310.0342-0.090.9277
lectage: 4 & service: Y-0.03500.0379-0.930.3547
lectage: 5 & service: Y0.06510.04161.560.1176
lectage: 6 & service: Y0.01370.03760.370.7150
Residual1.1755

BoxCox.jl

https://palday.github.io/BoxCox.jl/v0.3/

BoxCox.jl implements a the Box-Cox transformation in an efficient way. Via package extensions, it supports specializations for MixedModels.jl and several plotting functions, but does not incur a dependency penalty for this functionality when MixedModels.jl or Makie.jl are not loaded.

using BoxCox

bc = fit(BoxCoxTransformation, ss2)
Box-Cox transformation

estimated λ: -1.0740
resultant transformation:

 y^-1.1 - 1
------------
    -1.1
using CairoMakie
boxcoxplot(bc; conf_level=0.95)
Example block output

The estimated λ is very close to -1, i.e. the reciprocal of reaction time, which has a natural interpretation as speed. In other words, the Box-Cox transformation suggests that we should consider modelling the sleepstudy data as speed (reaction per unit time) instead of reaction time:

fit(MixedModel, @formula(1000 / reaction ~ 1 + days + (1 + days|subj)), sleepstudy)
Est.SEzpσ_subj
(Intercept)3.96580.105637.55<1e-990.4190
days-0.11100.0151-7.37<1e-120.0566
Residual0.2698

(We multiply by 1000 to get the responses per second instead of the responses per millisecond.)

Tip

BoxCox.jl also works with classical linear models.

Effects.jl

https://beacon-biosignals.github.io/Effects.jl/v1.2/

Effects.jl provides a convenient method to compute effects, i.e. predictions and associated prediction intervals computed at points on a reference grid. For models with a nonlinear link function, Effects.jl will also compute appropriate errors on the response scale based on the difference method.

For MixedModels.jl, the predictions are computed based on the fixed effects only.

The functionality of Effects.jl was inspired by the effects and emmeans packages in R and the methods within are based on @fox:effect:2003.

using Effects
design = Dict(:age => -15:1:20,
              :anych => [true, false])

eff_logit = effects(design, gm1; eff_col="use", level=0.95)
72×6 DataFrame
Rowageanychuseerrlowerupper
Int64BoolFloat64Float64Float64Float64
1-15true-1.47020.28653-2.03179-0.908608
2-14true-1.286640.257803-1.79192-0.781351
3-13true-1.114330.231113-1.5673-0.66136
4-12true-0.9532860.206539-1.3581-0.548476
5-11true-0.8034960.184184-1.16449-0.442503
6-10true-0.6649640.164164-0.98672-0.343208
7-9true-0.5376890.146615-0.82505-0.250329
8-8true-0.4216720.131673-0.679747-0.163597
9-7true-0.3169110.11945-0.551029-0.0827933
10-6true-0.2234080.109988-0.438981-0.00783577
11-5true-0.1411630.103209-0.3434480.0611226
12-4true-0.07017410.0988759-0.2639670.123619
13-3true-0.01044290.0966018-0.1997790.178893
14-2true0.0380310.0958996-0.1499290.225991
15-1true0.07524760.0962618-0.1134220.263917
160true0.1012070.0972276-0.08935550.29177
171true0.1159090.0984223-0.07699510.308813
182true0.1193540.0995703-0.07580040.314508
193true0.1115410.100493-0.08542170.308504
204true0.09247160.101102-0.1056850.290629
215true0.06214460.101394-0.1365830.260872
226true0.02056030.101443-0.1782650.219386
237true-0.03228130.101411-0.2310430.166481
248true-0.09638010.101539-0.2953930.102632
259true-0.1717360.102151-0.3719490.0284764
2610true-0.258350.103643-0.461486-0.0552138
2711true-0.356220.106449-0.564857-0.147584
2812true-0.4653480.111001-0.682907-0.24779
2913true-0.5857340.117669-0.816361-0.355106
3014true-0.7173760.126718-0.965738-0.469014
3115true-0.8602760.13829-1.13132-0.589232
3216true-1.014430.152422-1.31317-0.715691
3317true-1.179850.169071-1.51122-0.848474
3418true-1.356520.188155-1.7253-0.987742
3519true-1.544450.209573-1.9552-1.13369
3620true-1.743630.233229-2.20075-1.28651
37-15false-1.685450.205954-2.08912-1.28179
38-14false-1.568550.185652-1.93242-1.20468
39-13false-1.462910.167798-1.79178-1.13403
40-12false-1.368520.152712-1.66783-1.06921
41-11false-1.285390.140766-1.56128-1.00949
42-10false-1.213520.132319-1.47286-0.954174
43-9false-1.15290.127626-1.40304-0.902756
44-8false-1.103540.126728-1.35192-0.855159
45-7false-1.065440.129399-1.31906-0.811821
46-6false-1.038590.135204-1.30359-0.7736
47-5false-1.023010.143608-1.30447-0.74154
48-4false-1.018680.1541-1.32071-0.716647
49-3false-1.02560.166255-1.35146-0.699752
50-2false-1.043790.179754-1.3961-0.691478
51-1false-1.073230.194381-1.45421-0.692252
520false-1.113930.209998-1.52552-0.702343
531false-1.165890.226528-1.60988-0.7219
542false-1.22910.243942-1.70722-0.750985
553false-1.303570.262238-1.81755-0.789596
564false-1.38930.28144-1.94091-0.837689
575false-1.486290.301585-2.07738-0.895192
586false-1.594530.322718-2.22705-0.962014
597false-1.714030.344892-2.39001-1.03806
608false-1.844790.368159-2.56637-1.12321
619false-1.98680.392575-2.75624-1.21737
6210false-2.140080.418191-2.95971-1.32044
6311false-2.30460.445057-3.1769-1.43231
6412false-2.480390.473222-3.40789-1.55289
6513false-2.667440.502728-3.65276-1.68211
6614false-2.865740.533615-3.9116-1.81987
6715false-3.075290.565922-4.18448-1.96611
6816false-3.296110.59968-4.47146-2.12076
6917false-3.528180.634919-4.7726-2.28376
7018false-3.771510.671666-5.08796-2.45507
7119false-4.02610.709945-5.41757-2.63464
7220false-4.291950.749776-5.76148-2.82241
eff_prob = effects(design, gm1; eff_col="use", level=0.95, invlink=AutoInvLink())
72×6 DataFrame
Rowageanychuseerrlowerupper
Int64BoolFloat64Float64Float64Float64
1-15true0.1869130.04354580.1015640.272261
2-14true0.2164230.04371930.1307340.302111
3-13true0.2470640.04299230.1628010.331328
4-12true0.2782250.04147630.1969320.359517
5-11true0.3092780.03934620.2321610.386395
6-10true0.3396250.03681870.2674620.411789
7-9true0.3687250.03412720.3018370.435613
8-8true0.3961170.03149740.3343830.457851
9-7true0.4214290.02912510.3643440.478513
10-6true0.4443790.02715670.3911530.497605
11-5true0.4647680.0256740.4144480.515088
12-4true0.4824640.02468860.4340750.530852
13-3true0.4973890.02414980.4500570.544722
14-2true0.5095070.02396620.4625340.55648
15-1true0.5188030.02403140.4717020.565904
160true0.525280.02424480.4777610.572799
171true0.5289450.02452310.480880.577009
182true0.5298030.02480410.4811880.578418
193true0.5278560.02504530.4787690.576944
204true0.5231010.02522170.4736680.572535
215true0.5155310.02532390.4658970.565165
226true0.505140.02535820.4554390.554841
237true0.491930.02534620.4422530.541608
248true0.4759240.02532580.4262860.525561
259true0.4571710.02535040.4074850.506857
2610true0.4357690.02548310.3858240.485715
2711true0.4118750.02578560.3613360.462414
2812true0.3857180.02630060.334170.437266
2913true0.3576140.02703170.3046330.410595
3014true0.3279710.02792940.273230.382712
3115true0.2972820.02888960.2406590.353904
3216true0.2661130.02976760.207770.324457
3317true0.235080.03040190.1754930.294666
3418true0.2048070.03064310.1447470.264866
3519true0.175890.03037820.1163490.23543
3620true0.1488520.0295490.0909370.206767
37-15false0.1563750.02716980.1031230.209626
38-14false0.1724230.02649130.1205010.224345
39-13false0.1880230.02561780.1378130.238233
40-12false0.2028590.02469470.1544580.25126
41-11false0.2166340.02388860.1698140.263455
42-10false0.229080.02336790.1832790.27488
43-9false0.239960.02327640.1943390.285581
44-8false0.2490770.02370280.202620.295534
45-7false0.2562720.0246630.2079330.30461
46-6false0.2614210.02610520.2102560.312587
47-5false0.2644420.02793360.2096930.319191
48-4false0.2652850.03003550.2064170.324154
49-3false0.2639370.0322990.2006320.327242
50-2false0.2604190.03462090.1925640.328275
51-1false0.2547890.03690740.1824520.327126
520false0.2471390.03907240.1705580.323719
531false0.2375990.04103470.1571730.318026
542false0.2263390.04271650.1426160.310061
553false0.2135640.04404410.1272390.299889
564false0.1995190.04494920.1114210.287618
575false0.184480.04537250.09555120.273408
586false0.1687470.04526820.08002340.257472
597false0.1526420.04460910.06520950.240074
608false0.1364860.04339040.05144240.22153
619false0.1205960.04163340.03899550.202196
6210false0.1052620.03938610.0280670.182458
6311false0.09074230.03672090.01877080.162714
6412false0.07724430.03373010.01113450.143354
6513false0.06492250.03051940.005105640.124739
6614false0.05387360.0271990.0005644590.107183
6715false0.04413790.0238761-0.00265840.0909342
6816false0.03570490.020647-0.00476250.0761722
6917false0.02852090.017592-0.005958780.0630005
7018false0.02249930.014772-0.006453320.051452
7119false0.01753090.0122278-0.006435130.041497
7220false0.01349370.00998073-0.006068170.0330556

Effects are particularly nice for visualizing the model fit and its predictions.

using AlgebraOfGraphics # like ggplot2, but an algebra instead of a grammar
using CairoMakie

plt1 = data(eff_logit) * mapping(:age; color=:anych) *
      (mapping(:use) * visual(Lines) +
       mapping(:lower, :upper) * visual(Band; alpha=0.3))
draw(plt1)
Example block output
plt2 = data(eff_prob) * mapping(:age; color=:anych) *
      (mapping(:use) * visual(Lines) +
       mapping(:lower, :upper) * visual(Band; alpha=0.3))
draw(plt2)
Example block output
using Statistics: mean
contra_by_age = transform(contra,
                          :age => ByRow(x -> round(Int, x)),
                          :use => ByRow(==("Y"));
                          renamecols=false)
contra_by_age = combine(groupby(contra_by_age, [:age, :anych]),
                        :use => mean => :use)
plt3 = plt2 +
       data(contra_by_age) *
       mapping(:age, :use;
               color=:anych => "children") * visual(Scatter)

draw(plt3;
     axis=(; title="Estimated contraceptive use by age and children",
            limits=(nothing, (0, 1)) # ylim=0,1, xlim=auto
            ))
Example block output

Effects and estimated marginal (least squares) means are closely related and partially concepts. Effects.jl provides convenience function emmeans and empairs for computing EM means and pairwise differences of EM means.

emmeans(gm1)
4×5 DataFrame
Rowageurbananychuse: Yerr
Float64StringBoolFloat64Float64
10.00204757Nfalse-1.342790.221291
20.00204757Yfalse-0.5555470.23002
30.00204757Ntrue-0.1275160.112267
40.00204757Ytrue0.6597270.149722
empairs(gm1; dof=Inf)
6×8 DataFrame
Rowageurbananychuse: YerrdoftPr(>|t|)
Float64StringAnyFloat64Float64Float64Float64Float64
10.00204757N > Yfalse-0.7872430.319185Inf-2.466410.0136473
20.00204757Nfalse > true-1.215270.24814Inf-4.897539.70503e-7
30.00204757N > Yfalse > true-2.002520.267183Inf-7.494946.63291e-14
40.00204757Y > Nfalse > true-0.4280320.255955Inf-1.672290.0944673
50.00204757Yfalse > true-1.215270.274456Inf-4.427949.5137e-6
60.00204757N > Ytrue-0.7872430.187138Inf-4.206762.59062e-5
Tip

Effects.jl will work with any package that supports the StatsAPI.jl-based RegressionModel interface.

StandardizedPredictors.jl

https://beacon-biosignals.github.io/StandardizedPredictors.jl/v1/

StandardizedPredictors.jl provides a convenient way to express centering, scaling, and z-standardization as a "contrast" via the pseudo-contrasts Center, Scale, ZScore. Because these use the usual contrast machinery, they work well with any packages that use that machinery correctly (e.g. Effects.jl). The default behavior is to empirically compute the center and scale, but these can also be explicitly provided, either as a number or as a function (e.g. median to use the median for centering.)

using StandardizedPredictors

contrasts = Dict(:days => Center())
fit(MixedModel,
    @formula(reaction ~ 1 + days + (1 + days|subj)), sleepstudy;
    contrasts)
Est.SEzpσ_subj
(Intercept)298.50798.795033.94<1e-9936.4259
days(centered: 4.5)10.46731.50226.97<1e-115.7168
Residual25.5918
Tip

StandardizedPredictors.jl will work with any package that supports the StatsModels.jl-based @formula and contrast machinery.

RCall.jl and JellyMe4.jl

https://juliainterop.github.io/RCall.jl/stable/

https://github.com/palday/JellyMe4.jl/

RCall.jl provides a convenient interface for interoperability with R from Julia. JellyMe4.jl extends the functionality of RCall so that MixedModels.jl-fitted models and lme4-fitted models can be translated to each other. In practical terms, this means that you can enjoy the speed of Julia for model fitting, but use all the extra packages you love from R's larger ecosystem.

<!– MixedModelsSerializtion.jl MixedModelsSim.jl –>