StatsModels: get levels and model matrix for each level of categorical term

I have dataframe (df) with categoricals factors and i make schema fs = apply_schema(someterm, StatsModels.schema(df)) (where someterm is InteractionTerm). Then I want to get a vector of levels and then get a submatrix of the model matrix for each level.
From one side I can get mx = modelcols(fs, df) and then get view(mx, :, levelrange), but how to get column range for each level? Or how to get a map of the model matrix column to levels? Or there is no such functionality in StatsModels?

1 Like

Mmm… really no way to get Term for specific model matrix column?

this can be helfull:

function lcontrast(mm::ModelMatrix, i::Int)
    n = mm.assign[end]
    if i > n || n < 1 error("Factor number out of range 1-$(n)") end
    inds = findall(x -> x==i,
    mx = zeros(length(inds), size(, 2))
    for i = 1:length(inds)
        mx[i, inds[i]] = 1