FixedEffectModels.jl icon indicating copy to clipboard operation
FixedEffectModels.jl copied to clipboard

Setting base level ignored when interacting categorical variables.

Open miguelborrero5 opened this issue 2 months ago • 1 comments

High level description

reg ignores contrasts base setting for a particular categorical variable if this variable is interacted with some other regressor.

MWE

df = DataFrame(y = rand(100), x1 = categorical(rand(1:3, 100)), x2 = categorical(rand(1:3, 100)))
# Regression of y ~ x1 using x1 = 2 as base
reg(df, @formula(y ~ x1); contrasts = Dict(:x1 => DummyCoding(base = 2)))
# Regression of y ~ x1&x2 using x1 = 2 as base 
reg(df, @formula(y ~ x1&x2); contrasts = Dict(:x1 => DummyCoding(base = 2)))

The first regression does set x1=2 as the base level, as expected. However, the second regression just sets the interaction of the highest value for each each x1 and x2 as the base level. One could always re-normalize to the desired based level but shouldn't be an easy way to set a base level in the latter case too?

miguelborrero5 avatar Oct 02 '25 15:10 miguelborrero5

Thanks for opening an issue. It seems like the issue directly comes from StatsModels. Could you open an issue on that repository? Here is your MWE with GLM, which people on StatsModels repository may be more familiar with

using DataFrames, CategoricalArrays, GLM
df = DataFrame(y = rand(100), x1 = categorical(rand(1:3, 100)), x2 = categorical(rand(1:3, 100)))
lm(@formula(y ~ x1), df; contrasts = Dict(:x1 => DummyCoding(base = 2)))
lm(@formula(y ~ x1&x2), df; contrasts = Dict(:x1 => DummyCoding(base = 2)))

matthieugomez avatar Oct 06 '25 17:10 matthieugomez