# Using @formula() on a dataframe with a categorical variable for Linear Mixed Models

**URL:** <https://discourse.julialang.org/t/using-formula-on-a-dataframe-with-a-categorical-variable-for-linear-mixed-models/11976>\
**Category:** General Usage\
**Tags:** statistics\
**Created:** [June 26, 2018, 6:44pm UTC](https://discourse.julialang.org/t/using-formula-on-a-dataframe-with-a-categorical-variable-for-linear-mixed-models/11976 "2018-06-26T18:44:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarah-ji](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sarah-ji](https://discourse.julialang.org/u/sarah-ji)\
**Post date:** [June 26, 2018, 6:44pm UTC](https://discourse.julialang.org/t/using-formula-on-a-dataframe-with-a-categorical-variable-for-linear-mixed-models/11976/1 "2018-06-26T18:44:29Z")

</div>

Hi,

I’m trying to find a way to run a Linear Mixed Model with certain fixed effects using the :

> μ = @formula(T ~ X\_cts + X\_categorical)  
> mixed\_effect\_model = MixedEffectModel(μ, Σ, IdentityLink(), NormalResponse(0.000001))

But for whatever reason Julia doesn’t seem to like my X\_categorical variable…

When I do the @formula without this categorical variable and just the continuous variable, μ = @formula(T ~ X\_cts), I have no issues.

I have the following:

> typeof(dataframe[:X\_categorical])  
> CategoricalArrays.CategoricalArray{Int64,1,UInt32,Int64,CategoricalArrays.CategoricalValue{Int64,UInt32},Union{}}

If anybody might have any insight I would be so appreciative.

-Sarah

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [June 27, 2018, 1:22am UTC](https://discourse.julialang.org/t/using-formula-on-a-dataframe-with-a-categorical-variable-for-linear-mixed-models/11976/2 "2018-06-27T01:22:58Z")

</div>

What is the error you’re getting? I got some errors I found quite confusing when I had overlapping data or when I had Missings etc. In other words, it might not be due to the fact that it’s categorical, but rather to something about the underlying data

---

<div class="post-metadata">

**Author:** ![sarah-ji](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sarah-ji](https://discourse.julialang.org/u/sarah-ji)\
**Post date:** [June 27, 2018, 7:38pm UTC](https://discourse.julialang.org/t/using-formula-on-a-dataframe-with-a-categorical-variable-for-linear-mixed-models/11976/3 "2018-06-27T19:38:48Z")

</div>

Hi @kevbonham, first of all thanks for the response. I really appreciate it!

The error I get is:

" Failed to evaluate: dataframe[:X\_cts] + dataframe[:X\_categorical] "

I’m actually using this to simulate traits using the “TraitSimulation.jl” package, ([https://huwenboshi.github.io/TraitSimulation.jl/](https://huwenboshi.github.io/TraitSimulation.jl/)), but I can’t seem to get it to work…

model = FixedEffectModel(@formula(T ~ X\_cts + X\_categorical), IdentityLink(), NormalResponse(1.0))  
y\_fixed = simulate(model, dataframe)

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [June 27, 2018, 8:52pm UTC](https://discourse.julialang.org/t/using-formula-on-a-dataframe-with-a-categorical-variable-for-linear-mixed-models/11976/4 "2018-06-27T20:52:57Z")

</div>

Hmm - that error doesn’t look familiar, sorry! You might want to try to produce a minimum (not) working example (in other words, the smallest amount of code that will reproduce the error) and post that. I’m not sure I’ll be able to help, but you’re more likely to get a useful response from someone else if you have something that people can run.

---

<div class="post-metadata">

**Author:** ![sarah-ji](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sarah-ji](https://discourse.julialang.org/u/sarah-ji)\
**Post date:** [June 28, 2018, 1:12am UTC](https://discourse.julialang.org/t/using-formula-on-a-dataframe-with-a-categorical-variable-for-linear-mixed-models/11976/5 "2018-06-28T01:12:21Z")

</div>

Sorry I’m new to discourse… Thanks @kevbonham !

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [June 28, 2018, 1:39am UTC](https://discourse.julialang.org/t/using-formula-on-a-dataframe-with-a-categorical-variable-for-linear-mixed-models/11976/6 "2018-06-28T01:39:42Z")

</div>

No sweat! This is a common ask when it comes to programming issues (you’ll see endless discussions about it on places like stack overflow). It might seem annoying, but the amazing thing is that in the process of trying to make an example, you often end up answering your own question 🙂

Good luck!
