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.34100.1265-2.700.00700.5683
urban: Y0.39320.08534.61<1e-05
anych: true0.60650.10455.80<1e-08
age-0.01290.0112-1.160.2463
abs2(age)-0.00560.0008-6.67<1e-10
anych: true & age0.03320.01282.590.0095

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.469840.286495-2.03136-0.90832
2-14true-1.286360.257772-1.79158-0.781137
3-13true-1.114130.231084-1.56705-0.661216
4-12true-0.9531570.206513-1.35792-0.548398
5-11true-0.8034360.18416-1.16438-0.442489
6-10true-0.6649670.164143-0.98668-0.343254
7-9true-0.5377510.146595-0.825072-0.25043
8-8true-0.4217880.131654-0.679826-0.163751
9-7true-0.3170790.119432-0.55116-0.0829969
10-6true-0.2236220.10997-0.439158-0.00808519
11-5true-0.1414180.10319-0.3436660.060831
12-4true-0.07046680.0988569-0.2642230.123289
13-3true-0.01076870.0965823-0.2000670.178529
14-2true0.03767630.0958796-0.1502440.225597
15-1true0.07486840.0962412-0.1137610.263498
160true0.1008080.0972067-0.0897140.291329
171true0.1154940.0984011-0.0773690.308356
182true0.1189270.099549-0.07618560.314039
193true0.1111070.100472-0.08581420.308028
204true0.09203440.101081-0.1060810.29015
215true0.06170880.101373-0.1369780.260396
226true0.02013010.101423-0.1786550.218916
237true-0.03270150.101391-0.2314250.166022
248true-0.0967860.10152-0.2957610.102189
259true-0.1721240.102133-0.37230.0280529
2610true-0.2587140.103625-0.461815-0.0556128
2711true-0.3565570.106432-0.565161-0.147954
2812true-0.4656540.110985-0.68318-0.248128
2913true-0.5860030.117653-0.816599-0.355408
3014true-0.7176060.126702-0.965936-0.469275
3115true-0.8604610.138274-1.13147-0.589449
3216true-1.014570.152405-1.31328-0.715862
3317true-1.179930.169053-1.51127-0.848594
3418true-1.356540.188134-1.72528-0.987808
3519true-1.544410.20955-1.95512-1.1337
3620true-1.743530.233203-2.2006-1.28646
37-15false-1.685630.205916-2.08922-1.28204
38-14false-1.568630.185618-1.93244-1.20483
39-13false-1.462890.167768-1.79171-1.13407
40-12false-1.36840.152686-1.66766-1.06914
41-11false-1.285160.140742-1.56101-1.00931
42-10false-1.213170.132297-1.47247-0.953878
43-9false-1.152440.127603-1.40254-0.902346
44-8false-1.102960.1267-1.35129-0.854636
45-7false-1.064740.129366-1.31829-0.811185
46-6false-1.037760.135163-1.30268-0.772851
47-5false-1.022040.143557-1.30341-0.740678
48-4false-1.017580.154038-1.31949-0.715669
49-3false-1.024360.16618-1.35007-0.698656
50-2false-1.04240.179667-1.39454-0.690262
51-1false-1.071690.19428-1.45248-0.690913
520false-1.112240.209882-1.5236-0.700878
531false-1.164040.226398-1.60777-0.720306
542false-1.227090.243795-1.70492-0.749258
553false-1.301390.262076-1.81505-0.787733
564false-1.386950.281261-1.93821-0.835686
575false-1.483760.301389-2.07447-0.893046
586false-1.591820.322506-2.22392-0.95972
597false-1.711140.344663-2.38666-1.03561
608false-1.84170.367913-2.5628-1.12061
619false-1.983530.392311-2.75244-1.21461
6210false-2.13660.41791-2.95569-1.31751
6311false-2.300930.444759-3.17264-1.42922
6412false-2.476510.472906-3.40339-1.54963
6513false-2.663340.502395-3.64802-1.67867
6614false-2.861430.533266-3.90661-1.81625
6715false-3.070770.565555-4.17923-1.9623
6816false-3.291360.599296-4.46596-2.11676
6917false-3.52320.634518-4.76684-2.27957
7018false-3.76630.671248-5.08192-2.45068
7119false-4.020650.70951-5.41127-2.63004
7220false-4.286260.749324-5.7549-2.81761
eff_prob = effects(design, gm1; eff_col="use", level=0.95, invlink=AutoInvLink())
72×6 DataFrame
Rowageanychuseerrlowerupper
Int64BoolFloat64Float64Float64Float64
1-15true0.1869670.04355020.101610.272324
2-14true0.216470.04372080.1307780.302161
3-13true0.2471010.04299140.162840.331363
4-12true0.278250.04147350.1969640.359537
5-11true0.3092910.03934210.2321820.3864
6-10true0.3396250.03681380.2674710.411779
7-9true0.3687110.03412190.3018330.435589
8-8true0.3960890.0314920.3343660.457812
9-7true0.4213880.02911980.3643140.478462
10-6true0.4443260.02715150.391110.497542
11-5true0.4647040.02566890.4143940.515015
12-4true0.4823910.02468360.4340120.53077
13-3true0.4973080.02414490.4499850.544631
14-2true0.5094180.02396140.4624550.556381
15-1true0.5187080.02402660.4716170.5658
160true0.5251810.024240.4776710.57269
171true0.5288410.02451840.4807860.576897
182true0.5296970.02479950.4810910.578303
193true0.5277480.02504060.478670.576827
204true0.5229920.02521690.4735680.572417
215true0.5154220.02531910.4657980.565047
226true0.5050320.02535320.4553410.554724
237true0.4918250.0253410.4421580.541493
248true0.4758220.02532060.4261950.52545
259true0.4570750.0253450.40740.50675
2610true0.435680.02547750.3857450.485615
2711true0.4117930.025780.3612650.462321
2812true0.3856450.02629490.3341080.437182
2913true0.3575520.02702590.3045830.410522
3014true0.327920.02792360.2731910.38265
3115true0.2972430.0288840.2406310.353855
3216true0.2660870.02976230.2077540.32442
3317true0.2350650.03039720.1754870.294642
3418true0.2048020.03063920.1447510.264854
3519true0.1758950.03037560.116360.23543
3620true0.1488650.02954780.09095220.206778
37-15false0.1563520.02716150.1031160.209587
38-14false0.1724120.02648510.1205020.224322
39-13false0.1880260.02561350.1378250.238228
40-12false0.2028790.02469230.1544830.251275
41-11false0.2166730.02388760.1698540.263492
42-10false0.229140.02336820.1833390.274941
43-9false0.2400430.02327760.194420.285666
44-8false0.2491850.02370460.2027250.295645
45-7false0.2564050.02466510.2080620.304748
46-6false0.2615820.02610750.2104120.312751
47-5false0.2646290.02793630.2098750.319384
48-4false0.26550.03003890.2066240.324375
49-3false0.2641780.03230350.2008650.327492
50-2false0.2606870.03462710.1928190.328554
51-1false0.2550810.0369160.1827270.327435
520false0.2474540.03908430.170850.324058
531false0.2379350.04105080.1574760.318393
542false0.2266920.04273790.1429270.310456
553false0.2139310.04407180.1275520.30031
564false0.1998950.04498420.1117280.288063
575false0.184860.04541550.09584780.273873
586false0.1691280.04531970.08030290.257953
597false0.1530160.04466910.06546660.240566
608false0.136850.04345860.05167250.222027
619false0.1209430.0417090.03919530.202692
6210false0.105590.03946770.02823470.182945
6311false0.09104610.03680680.01890610.163186
6412false0.07752150.03381840.01123860.143804
6513false0.06517150.0306080.005180930.125162
6614false0.05409360.02728590.0006143080.107573
6715false0.04432930.0239593-0.002630030.0912887
6816false0.03586880.020725-0.004751420.076489
6917false0.02865920.0176636-0.005960840.0632792
7018false0.02261420.0148365-0.006464730.0516932
7119false0.0176250.0122847-0.006452590.0417026
7220false0.01356960.0100301-0.006088940.0332282

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.340860.221173
20.00204757Yfalse-0.5544280.229911
30.00204757Ntrue-0.1276810.112241
40.00204757Ytrue0.6587550.149687
empairs(gm1; dof=Inf)
6×8 DataFrame
Rowageurbananychuse: YerrdoftPr(>|t|)
Float64StringAnyFloat64Float64Float64Float64Float64
10.00204757N > Yfalse-0.7864350.319024Inf-2.465130.0136965
20.00204757Nfalse > true-1.213180.248024Inf-4.89141.0012e-6
30.00204757N > Yfalse > true-1.999620.267065Inf-7.487377.02653e-14
40.00204757Y > Nfalse > true-0.4267480.255846Inf-1.667990.095318
50.00204757Yfalse > true-1.213180.274345Inf-4.422119.77439e-6
60.00204757N > Ytrue-0.7864350.187094Inf-4.203412.62918e-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 –>