Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 281
mike Avatar asked Jan 30 '26 05:01

mike


1 Answers

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:

  • https://github.com/JuliaStats/DataFrames.jl

  • https://github.com/dmbates/MixedModels.jl

like image 85
Dan Getz Avatar answered Feb 01 '26 20:02

Dan Getz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!