Search code examples
regressionjuliainteractionmixed-models

Interaction of random slopes in mixed-effects models in Julia


Is it possible to have interactions of random effects in LMM fit in Julia?

This gives an error

model = fit!(lmm(@formula(response ~ 1 + A*B + (1+A*B|sub)), data)
ERROR: MethodError: no method matching getindex(::DataFrames.DataFrame, ::Expr)

Unpacking the terms doesn't help either.

model = fit!(lmm(@formula(response ~ 1 + A*B + (1+A+B+A&B|sub)), data)

This works

mode2 = fit!(lmm(@formula(response ~ 1 + A*B + (1+A+B|sub)), data)

Note that there are no issues when you have an interaction for fixed effects.


Solution

  • Maybe this should be an issue in MixedModels.jl or DataFrames.jl on github. But, in any case, I've tracked down what seems to be a problem: the calculation of eterms of the @formula. So, to get it to work, I've redefined the calculation. Paste the following into the REPL and try the problematic fit!:

    function DataFrames.evt(ex::Expr)
        if ex.head != :call error("Non-call expression encountered") end
        if !(ex.args[1] in DataFrames.nonevaluation)
            trms = DataFrames.getterms(ex)
            if length(trms)>1
                return vcat(map(DataFrames.evt,trms)...)
            else
                return [trms]
            end
        end
        return filter(x->!isa(x,Number), vcat(map(DataFrames.evt, ex.args[2:end])...))
    end
    

    Now, this left another problem with a change in the Cholesky decomposition function (my Julia is v0.7) which was also fixable, but in case everything else was working, the above redefinition allows continuing past the first problem.

    Here are links to MixedModels and DataFrames github pages: