Setting base level ignored when interacting categorical variables.
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?
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)))