# How to get number of Terms in @formula?

**URL:** <https://discourse.julialang.org/t/how-to-get-number-of-terms-in-formula/53864>\
**Category:** New to Julia\
**Created:** [January 24, 2021, 3:11pm UTC](https://discourse.julialang.org/t/how-to-get-number-of-terms-in-formula/53864 "2021-01-24T15:11:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PharmCat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pharmcat/32/6953_2.png) [@PharmCat](https://discourse.julialang.org/u/PharmCat)\
**Post date:** [January 24, 2021, 3:11pm UTC](https://discourse.julialang.org/t/how-to-get-number-of-terms-in-formula/53864/1 "2021-01-24T15:11:35Z")

</div>

How to get number of Terms in @formula? Is any API?

is a good way to do it?

```julia
function nterms(model)
        if isa(model.rhs, Term)
            p = 1
        else isa(model.rhs, Tuple)
            p = length(model.rhs)
        end
        p
end

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 24, 2021, 6:55pm UTC](https://discourse.julialang.org/t/how-to-get-number-of-terms-in-formula/53864/2 "2021-01-24T18:55:25Z")

</div>

I’m assuming that you are using GLM.jl or a related package. Would the following work?

`nterms(model) = length(coef(model))`

---

<div class="post-metadata">

**Author:** ![PharmCat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pharmcat/32/6953_2.png) [@PharmCat](https://discourse.julialang.org/u/PharmCat)\
**Post date:** [January 25, 2021, 1:21pm UTC](https://discourse.julialang.org/t/how-to-get-number-of-terms-in-formula/53864/3 "2021-01-25T13:21:15Z")

</div>

Hello!

No, unfortunately not. This returns a number of coefficients, not Trems/factors. For example model `responce ~ 1 + treatment` have 2 effects(I want to get this): Intercept + treatment, but it can be many levels - `length(coef(model))`

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 25, 2021, 5:19pm UTC](https://discourse.julialang.org/t/how-to-get-number-of-terms-in-formula/53864/4 "2021-01-25T17:19:59Z")

</div>

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:

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![PharmCat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pharmcat/32/6953_2.png) [@PharmCat](https://discourse.julialang.org/u/PharmCat)\
**Post date:** [January 25, 2021, 5:49pm UTC](https://discourse.julialang.org/t/how-to-get-number-of-terms-in-formula/53864/5 "2021-01-25T17:49:22Z")

</div>

@Christopher_Fisher thank you! This is solution.

Actually I use `nterms(model) = model.mf.schema.schema.count`, because `Intercept` Term was added automatically.
