How to get number of real levels of InteractionTerm from fitted model (GLM) via StatsModels?

Hi! This function is not based on StatsModels, but return real number of levels for InteractionTerm:

using StatsModels
function levelnum(mf, n)
    term = mf.f.rhs.terms[n]
    isa(term, InteractionTerm) || error("This term is not InteractionTerm")
    names = Tuple(t.sym for t in term.terms)
    cols = mf.data[names]
    length(unique(zip(cols...)))
end
2 Likes