Complexity in fitting Linear Mixed Models
Linear mixed-effects models are increasingly used for the analysis of data from experiments in fields like psychology where several subjects are each exposed to each of several different items. In addition to a response, which here will be assumed to be on a continuous scale, such as a response time, a number of experimental conditions are systematically varied during the experiment. In the language of statistical experimental design the latter variables are called experimental factors whereas factors like Subject
and Item
are blocking factors. That is, these are known sources of variation that usually are not of interest by themselves but still should be accounted for when looking for systematic variation in the response.
An example data set
The data from experiment 2 in Kronmueller and Barr (2007) are available in .rds
(R Data Set) format in the file kb07_exp2_rt.rds
in the github repository provided by Dale Barr. Files in this format can be loaded using the RData package for Julia.
Loading the data in Julia
Install some packages not part of the standard nextjournal environment. The version of MixedModels
should be at least v"2.1.0".
using Pkg Pkg.add(["GLM", "MixedModels", "RData", "StatsModels"])
Attach the packages to be used.
using BenchmarkTools, CSV, DataFrames, GLM, MixedModels using RData, Statistics, StatsBase, StatsModels, StatsPlots gr(); # plot backend
kb07 = load("/kronmueller-barr-2007/kb07_exp2_rt.rds")
describe(kb07)
The blocking factors are subj
and item
with 56 and 32 levels respectively. There are three experimental factors each with two levels: spkr
(speaker), prec
(precedence), and load
(cognitive load). The response time, rt_raw
, is measured in milliseconds. A few very large values, e.g. the maximum which is nearly 16 seconds, which could skew the results, are truncated in the rt_trunc
column. In addition, three erroneously recorded responses (values of 300 ms.) have been dropped, resulting in a slight imbalance in the data.
A table of mean responses and standard deviations for combinations of the experimental factors, as shown in Table 3 of the paper and on the data repository can be reproduced as
cellmeans = by(kb07, [:spkr, :prec, :load], meanRT = :rt_trunc => mean, sdRT = :rt_trunc => std, n = :rt_trunc => length, semean = :rt_trunc => x -> std(x)/sqrt(length(x)) )
The data are slightly imbalanced because 3 unrealistically low response times were removed.
An interaction plot of the cell means shows that the main effect of prec
is the dominant effect. (Need to fix this plot to show levels of prec in different colors/symbols.)
cellmeans scatter(:load, :meanRT, group=:spkr, layout=(1,2), link=:both, xlabel="Cognitive load", ylabel="Mean response time (ms)", label="")
Loading the data in R
kb07 <- readRDS("/kronmueller-barr-2007/kb07_exp2_rt.rds") str(kb07)
The positions of the missing observations can be determined from
(subjitemtbl <- xtabs(~ subj + item, kb07))
table(subjitemtbl)
All of the experimental factors vary within subject and within item, as can be verified by examining the frequency tables for the experimental and grouping factors. For example
xtabs(~ spkr + subj, kb07)
Formulating a simple model
Installing the required R package
For R lme4 and its dependencies must be installed
if (is.na(match("lme4", rownames(installed.packages())))) { install.packages("lme4", repos="https://cloud.R-project.org") }
require(lme4, quietly=TRUE)
Formula and model for simple, scalar random effects
A simple model with main-effects for each of the experimental factors and with random effects for subject and for item is described by the formula rt_trunc ~ 1 + spkr + prec + load + (1|subj) + (1|item)
. In the MixedModels package, which uses the formula specifications from the StatsModels package, a formula must be wrapped in a call to the @formula
macro.
f1 = (rt_trunc ~ 1 + spkr + prec + load + (1|subj) + (1|item));
In R a formula can stand alone.
f1 <- rt_trunc ~ 1 + spkr + prec + load + (1|subj) + (1|item)
For the MixedModels package the model is constructed as a LinearMixedModel
then fit with a call to fit!
(By convention, the names in Julia of mutating functions, which modify the value of one or more of their arguments, end in !
as a warning to the user that arguments, usually just the first argument, can be overwritten with new values.)
m1 = fit(MixedModel, f1, kb07)
The first fit of such a model can take several seconds because the Just-In-Time (JIT) compiler must analyze and compile a considerable amount of code. (All of the code in the MixedModels package is Julia code.) Subsequent fits of this or similar models are much faster.
The comparable model fit with lme4 in R is
m1 <- lmer(f1, kb07, REML=FALSE) summary(m1, corr=FALSE)
The estimated coefficients for the fixed-effects are different from those in the Julia fit because the reference level for load
is different. Conversion to a factor (CategoricalArray) is done slightly differently.
Assigning contrasts
For two-level experimental factors, such as prec
, spkr
and load
, in a (nearly) balanced design such as this it is an advantage to use a
In R one assigns a contrasts specification to the factor itself. The "Helmert" contrast type produces a
contrasts(kb07$spkr) <- contr.helmert(2) contrasts(kb07$prec) <- contr.helmert(2) contrasts(kb07$load) <- contr.helmert(2) system.time(m1 <- lmer(f1, kb07, REML=FALSE))
summary(m1, corr=FALSE)
The change in coding results in estimated coefficients (and standard errors) for the experimental factors being half the previous estimate, because of the Intercept
) estimate now is now a "typical" response over all the conditions. It would be the sample mean if the experiment were completely balanced.
mean(kb07$rt_trunc)
In Julia the contrasts are specified in a dictionary that is passed as an argument to the model constructor.
const HC = HelmertCoding(); const contrasts = Dict(:spkr => HC, :prec => HC, :load=> HC);
m1 = fit(MixedModel, f1, kb07, contrasts=contrasts)
The model matrix for the fixed effects is
Int.(m1.X) # convert to integers for cleaner printing
An advantage of the
Int.(m1.X'm1.X)
A benchmark of this fit shows that it is quite fast - on the order of a few milliseconds.
m1bmk = fit(MixedModel, $f1, $kb07, contrasts = $contrasts)
Model construction versus model optimization
The m1
object is created in the call to the constructor function, LinearMixedModel
, then the parameters are optimized or fit in the call to fit!
. Usually the process of fitting a model will take longer than creating the numerical representation but, for simple models like this, the creation time can be a significant portion of the overall running time.
bm1construct = LinearMixedModel($f1, $kb07, contrasts=$contrasts)
bm1fit = fit!($m1)
Factors affecting the time to optimize the parameters
The optimization process is summarized in the optsum
property of the model.
m1.optsum
For this model there are two parameters to be optimized because the objective function, negative twice the log-likelihood, can be profiled with respect to all the other parameters. (See section 3 of Bates et al. 2015 for details.) Both these parameters must be non-negative (i.e. both have a lower bound of zero) and both have an initial value of one. After 28 function evaluations an optimum is declared according to the function value tolerance, either
The optimization itself has a certain amount of setup and summary time but the majority of the time is spent in the evaluation of the objective - the profiled log-likelihood.
Each function evaluation is of the form
θ1 = m1.θ; m1objective = objective(updateL!(setθ!($m1, $θ1)))
On this machine 28 function evaluations, each taking around 60 microseconds, gives the total function evaluation time of at least 1.7 ms., which is practically all of the time to fit the model.
The majority of the time for the function evaluation for this model is in the call to updateL!
m1update = updateL!($m1)
This is an operation that updates the lower Cholesky factor (often written as L
) of a blocked sparse matrix.
There are 4 rows and columns of blocks. The first row and column correspond to the random effects for subject, the second to the random effects for item, the third to the fixed-effects parameters and the fourth to the response. Their sizes and types are
describeblocks(m1)
There are two lower-triangular blocked matrices in the model representation: A
with fixed entries determined by the model and data, and L
which is updated for each evaluation of the objective function. The type of the A
block is given before the size and the type of the L
block is after the size. For scalar random effects, generated by a random-effects term like (1|G)
, the (1,1) block is always diagonal in both A
and L
. Its size is the number of levels of the grouping factor, G
.
Because subject and item are crossed, the (2,1) block of A
is dense, as is the (2,1) block of L
. The (2,2) block of A
is diagonal because, like the (1,1) block, it is generated from a scalar random effects term. However, the (2,2) block of L
ends up being dense as a result of "fill-in" in the sparse Cholesky factorization. All the blocks associated with the fixed-effects or the response are stored as dense matrices but their dimensions are (relatively) small.
Increasing the model complexity
In general, adding more terms to a model will increase the time required to fit the model. However, there is a big difference between adding fixed-effects terms and adding complexity to the random effects.
Increasing the complexity of the fixed effects
Adding the two- and three-factor interactions to the fixed-effects terms increases the time required to fit the model.
f2 = (rt_trunc ~ 1 + spkr*prec*load + (1|subj) + (1|item));
m2 = fit(MixedModel, f2, kb07, contrasts=contrasts)
(Notice that none of the interactions are statistically significant.)
m2bmk = fit(MixedModel, $f2, $kb07,contrasts=$contrasts)
In this case, the increase in fitting time is more because the number of function evaluations to determine the optimum increases than because of increased evaluation time for the objective function.
m2.optsum.feval
θ2 = m2.θ; m2objective = objective(updateL!(setθ!($m2, $θ2)))
f2 <- rt_trunc ~ 1 + spkr*prec*load + (1|subj) + (1|item) system.time(m2 <- lmer(f2, kb07, REML=FALSE))
summary(m2, corr=FALSE)
Increasing complexity of the random effects
Another way in which the model can be extended is to switch to vector-valued random effects. Sometimes this is described as having random slopes, so that a subject not only brings their own shift in the typical response but also their own shift in the change due to, say, Load
versus No Load
. Instead of just one, scalar, change associated with each subject there is an entire vector of changes in the coefficients.
A model with a random slopes for each of the experimental factors for both subject and item is specified as
f3 = ( rt_trunc ~ 1 + spkr*prec*load + (1+spkr+prec+load|subj) + (1+spkr+prec+load|item) );
m3 = fit(MixedModel, f3, kb07, contrasts=contrasts)
m3bmk = fit(MixedModel, $f3, $kb07, contrasts=$contrasts)
f3 <- rt_trunc ~ 1 + spkr*prec*load + (1+spkr+prec+load|subj) + (1+spkr+prec+load|item) system.time(m3 <- lmer(f3, kb07, REML=FALSE, control=lmerControl(calc.derivs=FALSE)))
summary(m3, corr=FALSE)
There are several interesting aspects of this model fit.
First, the number of parameters optimized directly has increased substantially. What was previously a 2-dimensional optimization has now become 20 dimensional.
m3.optsum
and the number of function evaluations to convergence has gone from under 40 to over 600.
The time required for each function evaluation has also increased considerably,
θ3 = m3.θ; m3objective = objective(updateL!(setθ!($m3, $θ3)))
resulting in much longer times for model fitting - about three-quarters of a second in Julia and over 6 seconds in R.
Notice that the estimates of the fixed-effects coefficients and their standard errors have not changed substantially except for the standard error of prec
, which is also the largest effect.
The parameters in the optimization for this model can be arranged as two lower-triangular 4 by 4 matrices.
m3.λ[1]
m3.λ[2]
which generate the covariance matrices for the random effects. The cumulative proportion of the variance in the principal components of these covariance matrices, available as
m3.rePCA
show that 93% of the variation in the random effects for subject is in the first principal direction and 99% in the first two principal directions. The random effects for item also have 99% of the variation in the first two principal directions.
Furthermore the estimates of the standard deviations of the "slope" random effects are much smaller than the those of the intercept random effects except for the prec
coefficient random effect for item
, which suggests that the model could be reduced to rt_trunc ~ 1 + spkr*prec*load + (1|subj) + (1+prec|item)
or even rt_trunc ~ 1 + spkr+prec+load + (1|subj) + (1+prec|item)
.
f4 = (rt_trunc ~ 1 + spkr+prec+load + (1|subj) + (1+prec|item));
m4 = fit(MixedModel, f4, kb07, contrasts=contrasts)
m4bmk = fit(MixedModel, $f4, $kb07, contrasts=$contrasts)
m4.optsum.feval
f4 <- rt_trunc ~ 1 + spkr+prec+load + (1+prec|item) + (1|subj) system.time(m4 <- lmer(f4, kb07, REML=FALSE, control=lmerControl(calc.derivs=FALSE)))
summary(m4, corr=FALSE)
θ4 = m4.θ; m4objective = objective(updateL!(setθ!($m4, $θ4)))
These two model fits can be compared with one of the information criteria, AIC
or BIC
, for which "smaller is better". They both indicate a preference for the smaller model, m4
.
These criteria are values of the objective, negative twice the log-likelihood at convergence, plus a penalty that depends on the number of parameters being estimated.
Because model m4
is a special case of model m3
, a likelihood ratio test could also be used. The alternative model, m3
, will always produce an objective that is less than or equal to that from the null model, m4
. The difference in these value is similar to the change in the residual sum of squares in a linear model fit. This objective would be called the deviance if there was a way of defining a saturated model but it is not clear what this should be. However, if there was a way to define a deviance then the difference in the deviances would be the same as the differences in these objectives, which is
diff(objective.([m3, m4]))
This difference is compared to a
diff(dof.([m4, m3]))
producing a p-value of about 14%.
pchisq(26.7108, 20, lower.tail=FALSE)
Going maximal
The "maximal" model as proposed by Barr et al., 2013 would include all possible interactions of experimental and grouping factors.
f5 = (rt_trunc ~ 1 + spkr*prec*load + (1+spkr*prec*load|subj) + (1+spkr*prec*load|item));
m5 = fit(MixedModel, f5, kb07, contrasts=contrasts)
m5bmk = fit(MixedModel, $f5, $kb07, contrasts=$contrasts)
As is common in models with high-dimensional vector-valued random effects, the dominant portion of the variation is in the first few principal components
m5.rePCA
For both the subjects and the items practically all the variation of these 8-dimensional random effects is in the first 4 principal components.
The dimension of
θ5 = m5.θ; length(θ5)
Of these 72 parameters, 36 are estimated from variation between items, yet there are only 32 items.
Because the dimension of the optimization problem has gotten much larger the number of function evaluations to convergence increases correspondingly.
m5.optsum.feval
Also, each function evaluation requires more time
m5objective = objective(updateL!(setθ!($m5, $θ5)))
almost all of which is for the call to updateL!
.
updateL!($m5)
This model takes a long time to fit using lme4.
f5 <- rt_trunc ~ 1 + spkr*prec*load + (1+spkr*prec*load|subj) + (1+spkr*prec*load|item) system.time(m5 <- lmer(f5, kb07, REML=FALSE, control=lmerControl(calc.derivs=FALSE)))
summary(m5, corr=FALSE)
A model of intermediate complexity
To provide more granularity in the plots of execution time shown below, fit one more model without random effects for the third-order interaction of the experimental factors.
f6 = (rt_trunc ~ 1 + spkr*prec*load + (1+spkr+prec+load+spkr&prec+spkr&load+prec&load|subj) + (1+spkr+prec+load+spkr&prec+spkr&load+prec&load|item));
m6 = fit(MixedModel, f6, kb07, contrasts=contrasts)
m6bmk = fit(MixedModel, $f6, $kb07, contrasts=$contrasts)
θ6 = m6.θ; length(θ6)
m6objective = objective(updateL!(setθ!($m6, $θ6)))
updateL!($m6)
Summary of goodness of fit
Apply the goodness of fit measures to m1
to m6
creating a data frame
const mods = [m1, m2, m3, m4, m5, m6]; gofsumry = DataFrame(dof=dof.(mods), deviance=deviance.(mods), AIC = aic.(mods), AICc = aicc.(mods), BIC = bic.(mods))
dof | deviance | AIC | AICc | BIC | |
---|---|---|---|---|---|
Int64 | Float64 | Float64 | Float64 | Float64 | |
1 | 7 | 28823.1 | 28837.1 | 28837.2 | 28875.6 |
2 | 11 | 28818.5 | 28840.5 | 28840.6 | 28900.9 |
3 | 29 | 28637.1 | 28695.1 | 28696.1 | 28854.3 |
4 | 9 | 28663.9 | 28681.9 | 28682.0 | 28731.3 |
5 | 81 | 28578.3 | 28740.3 | 28748.1 | 29185.0 |
6 | 65 | 28603.9 | 28733.9 | 28738.9 | 29090.7 |
Here dof
or degrees of freedom is the total number of parameters estimated in the model and deviance
is simply negative twice the log-likelihood at convergence, without a correction for a saturated model. All the information criteria are on a scale of "smaller is better" and all would select model 4 as "best".
map(nm -> argmin(getproperty(gofsumry,nm)), (AIC=:AIC, AICc=:AICc, BIC=:BIC))
The benchmark times are recorded in nanoseconds. The median time in seconds to fit each model is evaluated as
fits = [median(b.times) for b in [m1bmk, m2bmk, m3bmk, m4bmk, m5bmk, m6bmk]] ./ 10^9;
nfe(m) = length(coef(m)); nre1(m) = MixedModels.vsize(first(m.reterms)); nlv1(m) = MixedModels.nlevs(first(m.reterms)); nre2(m) = MixedModels.vsize(last(m.reterms)); nlv2(m) = MixedModels.nlevs(last(m.reterms)); nθ(m) = sum(MixedModels.nθ, m.reterms); nev = [m.optsum.feval for m in mods]; dimsumry = DataFrame(p = nfe.(mods), q1 = nre1.(mods), n1 = nlv1.(mods), q2 = nre2.(mods), n2 = nlv2.(mods), nθ = nθ.(mods), npar = dof.(mods), nev = nev, fitsec = fits)
In this table, p
is the dimension of the fixed-effects vector, q1
is the dimension of the random-effects for each of the n1
levels of the first grouping factor while q2
and n2
are similar for the second grouping factor. nθ
is the dimension of the parameter vector and npar
is the total number of parameters being estimated, and is equal to nθ + p + 1
.
nev
is the number of function evaluations to convergence. Because this number will depend on the number of parameters in the model and several other factors such as the setting of the convergence criteria, only its magnitude should be considered reproducible. As described above, fitsec
is the median time, in seconds, to fit the model.
Relationships between model complexity and fitting time
As would be expected, the time required to fit a model increases with the complexity of the model. In particular, the number of function evaluations to convergence increases with the number of parameters being optimized.
dimsumry scatter(:nθ, :nev, xlabel="# of parameters in the optimization", ylabel="Function evaluations to convergence", label="")
The relationship is more-or-less linear, based on this small sample.
Finally, consider the time to fit the model versus the number of parameters in the optimization. On a log-log scale these form a reasonably straight line.
dimsumry scatter(:nθ, :fitsec, xlabel="# of parameters in the optimization", ylabel = "Median benchmark time (sec) to fit model", label="", yscale=:log10, xscale=:log10)
with a slope of about 2.2
coef(lm( (log(fitsec) ~ 1 + log(nθ)), dimsumry))
or close to a quadratic relationship between time to fit and the number of parameters in the optimization. However, the number of parameters to optimize is itself quadratic in the dimension of the random effects vector in each random-effects term. Thus, increasing the dimension of the vector-valued random effects can cause a considerable increase in the amount of time required to fit the model.
Furthermore, the estimated covariance matrix for high-dimensional vector-valued random effects is singular in m3
, m5
and m6
, as is often the case.