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 = false
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.34010.1264-2.690.00720.5682
urban: Y0.39350.08534.61<1e-05
anych: true0.60590.10455.80<1e-08
age-0.01290.0111-1.150.2484
abs2(age)-0.00560.0008-6.67<1e-10
anych: true & age0.03320.01282.590.0096

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.469790.286479-2.03128-0.908298
2-14true-1.286290.257758-1.79148-0.781091
3-13true-1.114040.231071-1.56693-0.661148
4-12true-0.9530480.206502-1.35779-0.548311
5-11true-0.803310.18415-1.16424-0.442382
6-10true-0.6648260.164134-0.986522-0.34313
7-9true-0.5375970.146587-0.824902-0.250291
8-8true-0.4216220.131647-0.679645-0.163598
9-7true-0.3169010.119425-0.550969-0.0828327
10-6true-0.2234340.109963-0.438957-0.00791077
11-5true-0.1412220.103183-0.3434570.0610139
12-4true-0.07026370.0988502-0.2640060.123479
13-3true-0.01055980.0965754-0.1998440.178724
14-2true0.03788970.0958724-0.1500170.225796
15-1true0.0750850.0962338-0.113530.2637
160true0.1010260.0971991-0.08948060.291533
171true0.1157130.0983933-0.07713460.30856
182true0.1191450.0995411-0.07595170.314242
193true0.1113240.100464-0.08558220.308229
204true0.09224750.101073-0.1058530.290348
215true0.06191720.101365-0.1367540.260589
226true0.02033260.101415-0.1784380.219103
237true-0.03250620.101384-0.2312150.166202
248true-0.09659930.101513-0.295560.102362
259true-0.1719470.102126-0.372110.0282169
2610true-0.2585480.103619-0.461638-0.055459
2711true-0.3564040.106427-0.564996-0.147812
2812true-0.4655140.11098-0.683031-0.247998
2913true-0.5858790.117648-0.816465-0.355292
3014true-0.7174980.126697-0.965819-0.469176
3115true-0.860370.138269-1.13137-0.589367
3216true-1.01450.1524-1.3132-0.715799
3317true-1.179880.169047-1.51121-0.848553
3418true-1.356510.188128-1.72524-0.98779
3519true-1.544410.209544-1.9551-1.13371
3620true-1.743550.233196-2.2006-1.28649
37-15false-1.686080.205902-2.08964-1.28252
38-14false-1.568940.185608-1.93273-1.20516
39-13false-1.463060.16776-1.79186-1.13426
40-12false-1.368430.15268-1.66768-1.06918
41-11false-1.285060.140737-1.5609-1.00922
42-10false-1.212940.132291-1.47222-0.953652
43-9false-1.152070.127595-1.40215-0.90199
44-8false-1.102460.126689-1.35077-0.854155
45-7false-1.06410.129349-1.31762-0.810584
46-6false-1.0370.135139-1.30187-0.772134
47-5false-1.021150.143525-1.30246-0.739848
48-4false-1.016560.153997-1.31839-0.71473
49-3false-1.023220.166129-1.34883-0.69761
50-2false-1.041130.179606-1.39315-0.689111
51-1false-1.07030.194208-1.45094-0.689659
520false-1.110720.2098-1.52192-0.699524
531false-1.16240.226304-1.60595-0.718853
542false-1.225330.24369-1.70296-0.747708
553false-1.299520.261959-1.81295-0.786087
564false-1.384960.281133-1.93597-0.833947
575false-1.481650.301249-2.07209-0.891214
586false-1.58960.322354-2.2214-0.957798
597false-1.70880.344499-2.38401-1.0336
608false-1.839260.367737-2.56001-1.11851
619false-1.980970.392124-2.74952-1.21242
6210false-2.133940.41771-2.95263-1.31524
6311false-2.298160.444548-3.16945-1.42686
6412false-2.473630.472683-3.40007-1.54719
6513false-2.660360.50216-3.64457-1.67614
6614false-2.858340.533019-3.90304-1.81364
6715false-3.067580.565297-4.17554-1.95962
6816false-3.288070.599027-4.46214-2.114
6917false-3.519810.634238-4.7629-2.27673
7018false-3.762810.670957-5.07787-2.44776
7119false-4.017070.709208-5.40709-2.62705
7220false-4.282580.749011-5.75061-2.81454
eff_prob = effects(design, gm1; eff_col="use", level=0.95, invlink=AutoInvLink())
72×6 DataFrame
Rowageanychuseerrlowerupper
Int64BoolFloat64Float64Float64Float64
1-15true0.1869750.04354930.101620.27233
2-14true0.2164820.04372020.1307920.302172
3-13true0.2471180.0429910.1628580.331379
4-12true0.2782720.04147330.1969860.359558
5-11true0.3093180.03934190.2322090.386427
6-10true0.3396560.03681350.2675030.411809
7-9true0.3687470.03412150.301870.435624
8-8true0.3961290.03149140.3344070.457851
9-7true0.4214310.0291190.3643590.478503
10-6true0.4443730.02715050.3911590.497587
11-5true0.4647530.02566770.4144450.515061
12-4true0.4824410.02468210.4340650.530817
13-3true0.497360.02414320.450040.54468
14-2true0.5094710.02395950.4625120.556431
15-1true0.5187620.02402460.4716750.56585
160true0.5252350.02423790.477730.57274
171true0.5288960.02451620.4808450.576947
182true0.5297510.02479720.481150.578353
193true0.5278020.02503830.4787280.576876
204true0.5230460.02521470.4736260.572465
215true0.5154740.02531690.4658540.565095
226true0.5050830.02535120.4553950.55477
237true0.4918740.02533930.442210.541538
248true0.4758690.0253190.4262450.525493
259true0.4571190.02534380.4074460.506792
2610true0.4357210.02547660.3857870.485654
2711true0.411830.02577930.3613040.462357
2812true0.3856780.02629450.3341420.437215
2913true0.3575810.02702580.3046110.410551
3014true0.3279440.02792370.2732150.382674
3115true0.2972620.02888410.240650.353874
3216true0.2661010.02976230.2077670.324434
3317true0.2350740.03039710.1754970.294651
3418true0.2048070.03063880.1447560.264858
3519true0.1758960.03037470.1163620.235429
3620true0.1488630.02954650.09095260.206773
37-15false0.1562920.02715130.1030770.209508
38-14false0.1723670.02647820.1204710.224264
39-13false0.1880.02560950.1378060.238193
40-12false0.2028730.02469070.154480.251266
41-11false0.2166910.02388810.1698710.26351
42-10false0.2291820.02337020.1833770.274986
43-9false0.2401110.02328070.1944820.28574
44-8false0.2492790.02370850.2028110.295747
45-7false0.2565260.02466950.2081750.304877
46-6false0.2617290.02611240.210550.312909
47-5false0.2648030.02794180.2100380.319568
48-4false0.2656980.03004520.2068110.324586
49-3false0.2644010.0323110.2010730.32773
50-2false0.2609320.03463630.1930460.328818
51-1false0.2553460.03692760.1829690.327723
520false0.2477360.03909890.1711040.324368
531false0.2382310.0410690.1577370.318725
542false0.2269990.04276050.143190.310809
553false0.2142460.04409950.1278130.30068
564false0.2002140.04501740.1119810.288446
575false0.1851780.04545470.09608860.274268
586false0.169440.04536490.08052650.258354
597false0.1533190.04472020.06566910.240969
608false0.1371390.0435150.0518510.222427
619false0.1212150.04176990.03934790.203083
6210false0.1058420.03953180.02836090.183323
6311false0.09127580.03687280.01900640.163545
6412false0.07772760.03388480.01131470.14414
6513false0.06535340.03067310.005235190.125472
6614false0.05425170.02734840.0006498170.107854
6715false0.04446460.0240181-0.002609950.0915392
6816false0.03598280.0207791-0.004743430.0767089
6917false0.02875370.0177123-0.005961810.0634692
7018false0.02269140.0148795-0.006471870.0518548
7119false0.01768720.012322-0.006463550.041838
7220false0.0136190.0100619-0.006101880.0333399

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, :use; color=:anych) *
      (visual(Lines) + mapping(; lower=:lower, upper=:upper) * visual(LinesFill))
draw(plt1)
Example block output
plt2 = data(eff_prob) *
      mapping(:age, :use; color=:anych => "children") *
      (visual(Lines) + mapping(; lower=:lower, upper=:upper) * visual(LinesFill))
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.339490.221092
20.00204757Yfalse-0.5525640.229835
30.00204757Ntrue-0.1276050.11223
40.00204757Ytrue0.6593220.149676
empairs(gm1; dof=Inf)
6×8 DataFrame
Rowageurbananychuse: YerrdoftPr(>|t|)
Float64StringAnyFloat64Float64Float64Float64Float64
10.00204757N > Yfalse-0.7869270.318913Inf-2.467530.0136051
20.00204757Nfalse > true-1.211890.247946Inf-4.88771.02021e-6
30.00204757N > Yfalse > true-1.998810.266992Inf-7.486427.07787e-14
40.00204757Y > Nfalse > true-0.4249590.255773Inf-1.661470.096619
50.00204757Yfalse > true-1.211890.274276Inf-4.418499.9391e-6
60.00204757N > Ytrue-0.7869270.187079Inf-4.206382.59497e-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 –>