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

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

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

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/), 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)

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.

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

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 :slight_smile:

Good luck!