Add predictor term to formula

I have a formula object defined by

formula = @formula(y ~ x1 + x2)

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

I tried

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

but it gave an error.

Hmm. Does this work?

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

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?

2 Likes

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…

2 Likes