# MixedModels and Random Interactions

**URL:** <https://discourse.julialang.org/t/mixedmodels-and-random-interactions/5315>\
**Category:** Statistics\
**Tags:** question, package, statistics, error\
**Created:** [August 10, 2017, 12:32am UTC](https://discourse.julialang.org/t/mixedmodels-and-random-interactions/5315 "2017-08-10T00:32:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kei](https://avatars.discourse-cdn.com/v4/letter/k/ac8455/32.png) [@kei](https://discourse.julialang.org/u/kei)\
**Post date:** [August 10, 2017, 12:32am UTC](https://discourse.julialang.org/t/mixedmodels-and-random-interactions/5315/1 "2017-08-10T00:32:27Z")

</div>

Hello, everyone.

I am attempting to use MixedModels to produce a model that would (in R using lme4) otherwise look like this:

(Edited formula):  
model = lmer( y ~ x \* cond1 \* cond2  
+ (1+x \* cond1 \* cond2 | region )  
, data = rawdata )

Although this is an oversimplification of the full model, this model does work within the context of R.

I have attempted the following in Julia, but have received errors:

pool!(rawdata, [:region])  
(region column is originally string, and so converion to PooledDataArray is needed?)

model = fit!(lmm(@formula( y ~ x & cond1 & cond2 + (1+ x & cond1 & cond2 | region) ), rawdata ))  
(returns:) MethodError: no method matching getindex(::DataFrames.DataFrame, ::Expr)

Oddly enough, removing the interaction specification from the random term allowed the model to work fine:  
model = fit!(lmm(@formula( y ~ x & cond1 & cond2 + (1+ x | region) ), rawdata ))

Is there a way to specify random interactions within MixedModels similar to lme4? Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [August 11, 2017, 1:29am UTC](https://discourse.julialang.org/t/mixedmodels-and-random-interactions/5315/2 "2017-08-11T01:29:24Z")

</div>

It might be an issue in the implementation for handling that part of the formula.  
I would suggest you open an issue at the [package’s repository](https://github.com/dmbates/MixedModels.jl/issues).

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [August 11, 2017, 6:03am UTC](https://discourse.julialang.org/t/mixedmodels-and-random-interactions/5315/3 "2017-08-11T06:03:24Z")

</div>

I believe opening an issue is the best chance get a response directly from @dmbates

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [August 11, 2017, 7:21pm UTC](https://discourse.julialang.org/t/mixedmodels-and-random-interactions/5315/4 "2017-08-11T19:21:37Z")

</div>

I believe the issue is in constructing the model matrix from the formula.

I have a vague recollection of some work being done on that construction but I’m not sure if it was in the [`StatsModels`](https://github.com/JuliaStats/StatsModels.jl) package (which conflicts with [`DataFrames`](https://github.com/JuliaStats/DataFrames.jl), which conflicts with [`DataTables`](https://github.com/JuliaStats/DataTables.jl), which …) or in `DataFrames`, which is what is used to create model matrices in `MixedModels`.

Could you check out the development version of the package

```julia
Pkg.checkout("MixedModels")

```

and see if the problem still occurs there? If not I will create a new release (and then arm wrestle with Tony about my habit of defining methods for generics from `Base` for `Base` types).

Could I ask why you would be interested in this formula? It is quite unusual to define a model with what is in effect a fourth-order interaction (`x:cond1:cond2:region`) and not have the nested second and third order interactions.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [August 11, 2017, 8:29pm UTC](https://discourse.julialang.org/t/mixedmodels-and-random-interactions/5315/5 "2017-08-11T20:29:34Z")

</div>

> [@dmbates](#):
>
> I have a vague recollection of some work being done on that construction

Was that in [GitHub - kleinschmidt/StreamModels.jl: Stream yer models](https://github.com/kleinschmidt/StreamModels.jl)?

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [August 11, 2017, 9:29pm UTC](https://discourse.julialang.org/t/mixedmodels-and-random-interactions/5315/6 "2017-08-11T21:29:54Z")

</div>

> [@dmbates](#):
>
> and see if the problem still occurs there? If not I will create a new release (and then arm wrestle with Tony about my habit of defining methods for generics from Base for Base types).

The proposed formula is a bit unusual, but one can get the same error even with more reasonable formulas where all the interaction terms are included:

```julia
julia> fit!(lmm(@formula(z ~ x*y + (x*y|s)), df))
ERROR: MethodError: no method matching getindex(::DataFrames.DataFrame, ::Expr)
Stacktrace:
 [1] collect_to!(::Array{DataArrays.DataArray{Float64,1},1}, ::Base.Generator{Array{Any,1},DataFrames.##125#126{DataFrames.DataFrame}}, ::Int64, ::Int64) at ./array.jl:474
 [2] _collect(::Array{Any,1}, ::Base.Generator{Array{Any,1},DataFrames.##125#126{DataFrames.DataFrame}}, ::Base.EltypeUnknown, ::Base.HasShape) at ./array.jl:455
 [3] #ModelFrame#124(::Dict{Any,Any}, ::Type{T} where T, ::DataFrames.Terms, ::DataFrames.DataFrame) at /Users/pietro/.julia/v0.6/DataFrames/src/statsmodels/formula.jl:320
 [4] (::Core.#kw#Type)(::Array{Any,1}, ::Type{DataFrames.ModelFrame}, ::DataFrames.Terms, ::DataFrames.DataFrame) at ./<missing>:0
 [5] #ModelFrame#127(::Array{Any,1}, ::Type{T} where T, ::DataFrames.Formula, ::DataFrames.DataFrame) at /Users/pietro/.julia/v0.6/DataFrames/src/statsmodels/formula.jl:333
 [6] (::Core.#kw#Type)(::Array{Any,1}, ::Type{DataFrames.ModelFrame}, ::DataFrames.Formula, ::DataFrames.DataFrame) at ./<missing>:0
 [7] #lmm#32(::Array{Any,1}, ::Dict{Any,Any}, ::Distributions.Normal{Float64}, ::Function, ::DataFrames.Formula, ::DataFrames.DataFrame) at /Users/pietro/.julia/v0.6/MixedModels/src/pls.jl:90
 [8] lmm(::DataFrames.Formula, ::DataFrames.DataFrame) at /Users/pietro/.julia/v0.6/MixedModels/src/pls.jl:90

```

I get a similar error both in release and master of MixedModels (this stacktrace is from master).

---

<div class="post-metadata">

**Author:** ![kei](https://avatars.discourse-cdn.com/v4/letter/k/ac8455/32.png) [@kei](https://discourse.julialang.org/u/kei)\
**Post date:** [August 17, 2017, 5:31am UTC](https://discourse.julialang.org/t/mixedmodels-and-random-interactions/5315/7 "2017-08-17T05:31:11Z")

</div>

Thank you, everyone, for responding to my inquiry. Apologies for my delay – I have been dealing with some post-operative unpleasantries this past week.

I will shamelessly blame my initial formulation on the meds, Dr. Bates. Yes, I was hoping for the interaction specification to expand, but that’s what the asterisk is for, not the colon (&s in Julia). Either way, as piever notes, specifying any interaction within the random term results in an error. I had speculated that this was due to the parser not understanding ampersands and asterisks. I have not yet tried out the development version, although piever seems to indicate that I should expect similar outcomes.
