Setting base level ignored when interacting categorical variables.
High level description
lm ignores contrasts base setting for a particular categorical variable if this variable is interacted with some other regressor.
MWE
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)))
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?
The same happens in FixedEffectModels. The issue comes from the way the StatsModels function schema works
Another related problem is that the base level for the interaction with "&" is not omitted from the regression table.
glm(@formula(hbp ~ race & sex), hbp, Bernoulli(), LogitLink(),
contrasts = Dict(:race => StatsModels.DummyCoding(base=2))
)
StatsModels.TableRegressionModel{GeneralizedLinearModel{GLM.GlmResp{Vector{Float64}, Bernoulli{Float64}, LogitLink}, GLM.DensePredChol{Float64, LinearAlgebra.CholeskyPivoted{Float64, Matrix{Float64}, Vector{Int64}}}}, Matrix{Float64}}
hbp ~ 1 + race & sex
Coefficients:
──────────────────────────────────────────────────────────────────────────────────────
Coef. Std. Error z Pr(>|z|) Lower 95% Upper 95%
──────────────────────────────────────────────────────────────────────────────────────
(Intercept) -4.07754 1.00844 -4.04 <1e-04 -6.05404 -2.10103
race: 1 & sex: female 0.0 NaN NaN NaN NaN NaN
race: 2 & sex: female 0.329715 1.07851 0.31 0.7598 -1.78412 2.44355
race: 3 & sex: female -9.48853 64.4638 -0.15 0.8830 -135.835 116.858
race: 1 & sex: male 2.37713 1.03598 2.29 0.0218 0.346635 4.40762
race: 2 & sex: male 1.06602 1.03181 1.03 0.3015 -0.956298 3.08834
race: 3 & sex: male 0.733498 1.16707 0.63 0.5297 -1.55391 3.02091
──────────────────────────────────────────────────────────────────────────────────────