How to get number of Terms in @formula?

I think I understand now. I was digging through the model object structure and found a field that seems to count the number of variables (IVs and DVs). I am wondering if this might be what you are looking for:

nterms(model) = model.mf.schema.schema.count - 1

This counts the number of IVs which correspond to factors (I think). Using the following example from the documentation, it returns 2:

using DataFrames, CategoricalArrays, GLM

dobson = DataFrame(Counts    = [18.,17,15,20,10,21,25,13,13],
                          Outcome   = categorical([1,2,3,1,2,3,1,2,3]),
                          Treatment = categorical([1,1,1,2,2,2,3,3,3]))
gm1 = fit(GeneralizedLinearModel, @formula(Counts ~ Outcome + Treatment), dobson, Poisson())
nterms(gm1)
1 Like