In R formula syntax, occasionally a user will specify a very simple model that only has an intercept, e.g.
fit = lm(Response ~ 1, data)
These models allow for simplification relative to more complex models, e.g. lm(Response ~ A + B + A:B, ...) and I would like to have an easy way to detect when the RHS of the equation only contains a 1 and no other terms. Text manipulations seem possible, but are there any other ways to do this using the R formula class or other methods?
The answer with
termsis probably the canonical answer, but you can also use subsetting to drop the LHS of the formula and test it against~1:(~,Response,1): the first element is the operator, the second element is the first argument (the LHS), and the right element is the second argument (the RHS).[-2]drops the second element and makes the formula into a one-sided formula.@G.Grothendieck offers a slightly less hacky variant (no need to know or understand the internal structure of formula objects) in the comments, using
updateto overwrite the response variable with 0: