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.0307107.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.1639
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.34060.1264-2.690.00710.5682
urban: Y0.39320.08534.61<1e-05
anych: true0.60610.10455.80<1e-08
age-0.01290.0111-1.160.2472
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.8263135090639308
r2(ss2; conditional=false)
0.2864713951077099
icc(ie2)
0.28852801934725547
icc(ie2, :dept)
0.016117901466392092
vif(ie1)
9-element Vector{Float64}:
 1.5141903373605303
 1.735406021956399
 1.7822316985578348
 1.449378975108032
 1.4380891515108385
 1.5948966180649566
 1.4634020913401908
 1.8267103210543985
 1.0161785415502533
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.3110872223681633
 1.3257311624888224
 1.0161785415502536
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.07<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.0710
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.469610.286482-2.0311-0.908116
2-14true-1.286150.25776-1.79135-0.780952
3-13true-1.113950.231073-1.56684-0.66105
4-12true-0.9529920.206504-1.35773-0.548251
5-11true-0.8032890.184151-1.16422-0.442359
6-10true-0.6648390.164135-0.986537-0.343141
7-9true-0.5376410.146588-0.824948-0.250334
8-8true-0.4216950.131648-0.67972-0.163671
9-7true-0.3170010.119425-0.55107-0.0829322
10-6true-0.2235590.109963-0.439083-0.00803501
11-5true-0.1413690.103184-0.3436060.0608673
12-4true-0.07043120.0988507-0.2641750.123312
13-3true-0.01074530.0965759-0.2000310.17854
14-2true0.03768860.0958731-0.1502190.225596
15-1true0.07487040.0962346-0.1137460.263487
160true0.10080.0971999-0.08970810.291309
171true0.1154780.0983943-0.07737120.308327
182true0.1189040.0995421-0.07619510.314003
193true0.1110780.100465-0.08583020.307985
204true0.09199930.101074-0.1061030.290101
215true0.0616690.101366-0.1370050.260342
226true0.02008660.101416-0.1786860.218859
237true-0.03274770.101385-0.2314580.165963
248true-0.09683410.101513-0.2957970.102128
259true-0.1721730.102127-0.3723370.0279924
2610true-0.2587630.103619-0.461853-0.0556726
2711true-0.3566060.106427-0.565199-0.148012
2812true-0.46570.11098-0.683217-0.248183
2913true-0.5860470.117649-0.816633-0.35546
3014true-0.7176450.126697-0.965967-0.469323
3115true-0.8604960.13827-1.1315-0.589492
3216true-1.01460.1524-1.3133-0.715899
3317true-1.179950.169048-1.51128-0.848625
3418true-1.356560.188129-1.72529-0.987834
3519true-1.544420.209545-1.95512-1.13372
3620true-1.743530.233197-2.20059-1.28647
37-15false-1.685710.205903-2.08927-1.28215
38-14false-1.568660.185607-1.93244-1.20488
39-13false-1.462870.167758-1.79167-1.13407
40-12false-1.368330.152678-1.66757-1.06908
41-11false-1.285040.140735-1.56087-1.0092
42-10false-1.2130.132289-1.47228-0.953715
43-9false-1.152210.127595-1.40229-0.902131
44-8false-1.102680.126691-1.35099-0.854368
45-7false-1.06440.129354-1.31792-0.810866
46-6false-1.037370.135147-1.30225-0.772482
47-5false-1.021590.143538-1.30292-0.740259
48-4false-1.017060.154014-1.31892-0.7152
49-3false-1.023790.166151-1.34944-0.698138
50-2false-1.041770.179632-1.39384-0.689693
51-1false-1.0710.194239-1.4517-0.690294
520false-1.111480.209836-1.52275-0.700208
531false-1.163210.226345-1.60684-0.719584
542false-1.22620.243736-1.70391-0.748484
553false-1.300440.26201-1.81397-0.786906
564false-1.385930.281189-1.93705-0.834806
575false-1.482670.301311-2.07323-0.892112
586false-1.590660.32242-2.2226-0.958732
597false-1.709910.34457-2.38526-1.03456
608false-1.840410.367814-2.56131-1.11951
619false-1.982160.392205-2.75087-1.21345
6210false-2.135160.417797-2.95403-1.31629
6311false-2.299420.44464-3.17089-1.42794
6412false-2.474920.47278-3.40155-1.54829
6513false-2.661680.502262-3.6461-1.67727
6614false-2.859690.533126-3.9046-1.81478
6715false-3.068950.565408-4.17713-1.96077
6816false-3.289470.599143-4.46377-2.11517
6917false-3.521240.634358-4.76456-2.27792
7018false-3.764250.671082-5.07955-2.44896
7119false-4.018530.709337-5.4088-2.62825
7220false-4.284050.749145-5.75234-2.81575
eff_prob = effects(design, gm1; eff_col="use", level=0.95, invlink=AutoInvLink())
72×6 DataFrame
Rowageanychuseerrlowerupper
Int64BoolFloat64Float64Float64Float64
1-15true0.1870020.04355450.1016370.272367
2-14true0.2165050.04372390.1308080.302202
3-13true0.2471360.04299350.162870.331402
4-12true0.2782840.04147460.1969950.359572
5-11true0.3093220.03934250.2322120.386432
6-10true0.3396530.03681360.26750.411807
7-9true0.3687360.03412120.301860.435613
8-8true0.3961110.0314910.334390.457832
9-7true0.4214070.02911860.3643350.478478
10-6true0.4443420.02715020.3911280.497555
11-5true0.4647160.02566750.4144090.515024
12-4true0.4823990.0246820.4340240.530775
13-3true0.4973140.02414330.4499940.544634
14-2true0.5094210.02395980.4624610.556381
15-1true0.5187090.0240250.4716210.565797
160true0.5251790.02423840.4776720.572685
171true0.5288370.02451670.4807860.576889
182true0.5296910.02479780.4810880.578294
193true0.5277410.02503890.4786650.576816
204true0.5229840.02521520.4735630.572405
215true0.5154120.02531740.4657910.565034
226true0.5050210.02535150.4553330.55471
237true0.4918140.02533940.442150.541478
248true0.475810.02531890.4261860.525435
259true0.4570630.02534340.4073910.506735
2610true0.4356680.0254760.3857360.4856
2711true0.4117820.02577850.3612570.462306
2812true0.3856340.02629340.33410.437169
2913true0.3575420.02702460.3045750.41051
3014true0.3279120.02792230.2731850.382638
3115true0.2972360.02888270.2406270.353845
3216true0.2660810.0297610.207750.324411
3317true0.2350610.0303960.1754860.294636
3418true0.20480.03063810.144750.26485
3519true0.1758940.03037460.1163610.235427
3620true0.1488650.02954710.09095410.206776
37-15false0.1563410.02715820.1031120.20957
38-14false0.1724070.02648290.1205020.224313
39-13false0.1880290.02561230.137830.238228
40-12false0.202890.02469190.1544950.251286
41-11false0.2166940.0238880.1698750.263514
42-10false0.2291710.02336910.1833680.274974
43-9false0.2400850.02327890.194460.285711
44-8false0.2492390.02370620.2027750.295702
45-7false0.256470.02466690.2081240.304817
46-6false0.2616590.02610950.2104850.312832
47-5false0.2647180.02793850.209960.319477
48-4false0.26560.03004140.206720.32448
49-3false0.264290.03230650.2009710.32761
50-2false0.2608090.03463090.1929340.328685
51-1false0.2552140.0369210.182850.327577
520false0.2475950.03909070.1709790.324212
531false0.2380840.0410590.157610.318558
542false0.2268470.04274830.1430620.310633
553false0.2140910.04408490.1276870.300496
564false0.2000590.04500020.111860.288258
575false0.1850250.04543480.0959740.274075
586false0.169290.04534240.08042110.25816
597false0.1531750.04469510.06557450.240776
608false0.1370030.04348780.05176840.222237
619false0.1210890.0417410.0392780.2029
6210false0.1057260.03950190.02830370.183148
6311false0.09117130.03684240.01896150.163381
6412false0.0776350.03385470.0112810.143989
6513false0.06527270.03064410.005211390.125334
6614false0.05418250.0273210.0006343850.107731
6715false0.04440620.0239927-0.002618640.091431
6816false0.03593420.0207561-0.004746930.0766154
6917false0.0287140.0176919-0.005961570.0633896
7018false0.02265950.0148618-0.006469130.0517882
7119false0.01766190.012307-0.006459320.0417831
7220false0.01359920.0100493-0.006096930.0332954

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.340120.221127
20.00204757Yfalse-0.5536240.229867
30.00204757Ntrue-0.1277060.112232
40.00204757Ytrue0.6587910.149677
empairs(gm1; dof=Inf)
6×8 DataFrame
Rowageurbananychuse: YerrdoftPr(>|t|)
Float64StringAnyFloat64Float64Float64Float64Float64
10.00204757N > Yfalse-0.7864970.31896Inf-2.465810.0136703
20.00204757Nfalse > true-1.212410.247978Inf-4.889211.01244e-6
30.00204757N > Yfalse > true-1.998910.267021Inf-7.485987.10151e-14
40.00204757Y > Nfalse > true-0.4259180.255802Inf-1.665030.0959075
50.00204757Yfalse > true-1.212410.274303Inf-4.419999.87058e-6
60.00204757N > Ytrue-0.7864970.187081Inf-4.204052.62177e-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 –>