# Comparison formula with random effect term \[StatsModels.jl\]

**URL:** <https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489>\
**Category:** Statistics\
**Tags:** statsmodels\
**Created:** [September 20, 2021, 5:18pm UTC](https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489 "2021-09-20T17:18:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yonghee\_Lee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yonghee_lee/32/29345_2.png) [@Yonghee\_Lee](https://discourse.julialang.org/u/Yonghee_Lee)\
**Post date:** [September 20, 2021, 5:18pm UTC](https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489/1 "2021-09-20T17:18:09Z")

</div>

The follwing comparision between two formula with random effect term for MixedModels.jl gives `false`:

```julia
@formula(y ~ 1 + x + (1 | z3) ) == @formula(y ~ 1 + x + (1 | z3) ) 

```

However, a comparision formula without random effect term gives `true`, as expected.

```julia
@formula(y ~ 1 + x ) == @formula(y ~ 1 + x ) 

```

Is there any solution for this?

---

<div class="post-metadata">

**Author:** ![halleysfifthinc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halleysfifthinc/32/206280_2.png) [@halleysfifthinc](https://discourse.julialang.org/u/halleysfifthinc)\
**Post date:** [September 20, 2021, 5:33pm UTC](https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489/2 "2021-09-20T17:33:11Z")

</div>

I’m not very familiar with StatsModels, so I don’t have a complete answer, but my expectation is that the equality comparison is not (solely) comparing the structural similarity of the models, and that random effects terms from 2 different formulas can’t be guaranteed equal (they are random, after all) even if they are structurally similar.

What are you trying to achieve by comparing formulas?

---

<div class="post-metadata">

**Author:** ![Yonghee\_Lee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yonghee_lee/32/29345_2.png) [@Yonghee\_Lee](https://discourse.julialang.org/u/Yonghee_Lee)\
**Post date:** [September 20, 2021, 5:50pm UTC](https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489/3 "2021-09-20T17:50:50Z")

</div>

Thanks for your reply !  
I am doing simulation with many formula stored in a vector.  
I want delete a true model from candidates if true model is inside the candidate.

PS. I may do this by converting formula to string. But I asked this because it is not intuitive ^^.

---

<div class="post-metadata">

**Author:** ![halleysfifthinc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halleysfifthinc/32/206280_2.png) [@halleysfifthinc](https://discourse.julialang.org/u/halleysfifthinc)\
**Post date:** [September 20, 2021, 6:25pm UTC](https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489/4 "2021-09-20T18:25:06Z")

</div>

Are the formula generated or manually created? If the latter, it might be more convenient to store them in something with a named index kind of interface (e.g. `Dict`) with meaningful names for each formula.

I agree that it’s not very intuitive, and based on that my gut impression is that there might be better/more appropriate approaches than trying to directly compare formulas. Are you comparing nested models and/or doing a stepwise regression (e.g. with a likelihood ratio test)?

---

<div class="post-metadata">

**Author:** ![Yonghee\_Lee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yonghee_lee/32/29345_2.png) [@Yonghee\_Lee](https://discourse.julialang.org/u/Yonghee_Lee)\
**Post date:** [September 20, 2021, 7:39pm UTC](https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489/5 "2021-09-20T19:39:13Z")

</div>

Thanks, a lot

I now am tryng to use Dictionary or Tuple to make it work ^^.  
Simulation work is about model averaging in mixed models.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [September 20, 2021, 7:41pm UTC](https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489/6 "2021-09-20T19:41:10Z")

</div>

I think that comparison should return true. I would file an issue at MixedModels.jl.

---

<div class="post-metadata">

**Author:** ![Eric](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric/32/25565_2.png) [@Eric](https://discourse.julialang.org/u/Eric)\
**Post date:** [September 20, 2021, 11:13pm UTC](https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489/7 "2021-09-20T23:13:32Z")

</div>

Executing:

```julia
f1 = @formula(y ~ 1 + x + (1 | z3) )
dump(f1)
f2 = @formula(y ~ 1 + x + (1 | z3) )
dump(f2)

```

It looks like the FunctionTerm is different in the two formulas.  
The documentation ([API documentation · StatsModels.jl](https://juliastats.org/StatsModels.jl/stable/api/#StatsModels.FunctionTerm)) indicates that the fanon from the FunctionTerm is a generated anon function. So I would guess that since the formulas are created twice, the FunctionTerms are generated twice too, and then the comparison is unable to detect that they are the same.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [September 21, 2021, 7:55am UTC](https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489/8 "2021-09-21T07:55:42Z")

</div>

Yeah the problem is that `(1 | z3)` is interpreted as being a call to a custom function. It’s only after MixedModels calls `apply_schema` that these are detected as being a random effects term.

@dave.f.kleinschmidt Maybe `==` should ignore anonymous functions fields and only compare syntaxes?

---

<div class="post-metadata">

**Author:** ![dave.f.kleinschmidt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dave.f.kleinschmidt/32/55_2.png) [@dave.f.kleinschmidt](https://discourse.julialang.org/u/dave.f.kleinschmidt)\
**Post date:** [September 21, 2021, 2:42pm UTC](https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489/9 "2021-09-21T14:42:51Z")

</div>

Yeah that’s what should happen…the method for the FunctionTerm itself is actually there already, but the PR for general `==` of terms got bogged down so the formula term is checking `===`. I’ll have a go at updating that PR (the person who opened it seems to have deleted their github account so I’ll have to open a new one: [https://github.com/JuliaStats/StatsModels.jl/pull/241](https://github.com/JuliaStats/StatsModels.jl/pull/241)

---

<div class="post-metadata">

**Author:** ![Yonghee\_Lee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yonghee_lee/32/29345_2.png) [@Yonghee\_Lee](https://discourse.julialang.org/u/Yonghee_Lee)\
**Post date:** [September 21, 2021, 3:05pm UTC](https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489/10 "2021-09-21T15:05:39Z")

</div>

Thanks so much

---

<div class="post-metadata">

**Author:** ![dave.f.kleinschmidt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dave.f.kleinschmidt/32/55_2.png) [@dave.f.kleinschmidt](https://discourse.julialang.org/u/dave.f.kleinschmidt)\
**Post date:** [September 21, 2021, 3:07pm UTC](https://discourse.julialang.org/t/comparison-formula-with-random-effect-term-statsmodels-jl/68489/11 "2021-09-21T15:07:07Z")

</div>

thanks for the nudge 🙂 I’d forgotten about that PR
