# Add predictor term to formula

**URL:** <https://discourse.julialang.org/t/add-predictor-term-to-formula/62595>\
**Category:** General Usage\
**Tags:** statistics\
**Created:** [June 8, 2021, 10:02pm UTC](https://discourse.julialang.org/t/add-predictor-term-to-formula/62595 "2021-06-08T22:02:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 8, 2021, 10:02pm UTC](https://discourse.julialang.org/t/add-predictor-term-to-formula/62595/1 "2021-06-08T22:02:59Z")

</div>

I have a formula object defined by

```julia
formula = @formula(y ~ x1 + x2)

```

How can I add another predictor term to that `FormulaTerm` object after it is created?

I tried

```julia
formula.rhs += Term(:x3) # error
push!(formula.rhs, Term(:x3)) # error

```

but it gave an error.

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [June 8, 2021, 10:14pm UTC](https://discourse.julialang.org/t/add-predictor-term-to-formula/62595/2 "2021-06-08T22:14:20Z")

</div>

Hmm. Does this work?

```julia
formula = formula.lhs ~ ( formula.rhs + Term(:x3) )

```

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [June 8, 2021, 10:57pm UTC](https://discourse.julialang.org/t/add-predictor-term-to-formula/62595/3 "2021-06-08T22:57:55Z")

</div>

I’m not sure if the `lhs` and `rhs` fields are part of the public API. Usually, the specific field names are not part of the public API.

@dave.f.kleinschmidt @nalimilan Is there a public API for accessing the left-hand side and right-hand side of a `FormulaTerm`?

---

<div class="post-metadata">

**Author:** ![dave.f.kleinschmidt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dave.f.kleinschmidt/32/55_2.png) [@dave.f.kleinschmidt](https://discourse.julialang.org/u/dave.f.kleinschmidt)\
**Post date:** [June 9, 2021, 1:24pm UTC](https://discourse.julialang.org/t/add-predictor-term-to-formula/62595/4 "2021-06-09T13:24:09Z")

</div>

FWIW this is what I use in similar situations, but @dilumaluthge is right that there’s no public API. One possibility would be to add a method for `(+)(f::FormulaTerm, t::AbstractTerm) = f.rhs ~ f.rhs + t` which does that. The lack of an API is worth opening an issue about and we’d certainly consider a PR with that change…
