Manipulation of formulas (place of interaction in the correct order)

I am trying to create a model validation tool in which I follow a direct selection approach, so assuming my model is model<- y~a*b+c*d+e , I can use the term function in R attributes(terms(model))$term.labels to find out all the predicates in my model, but the problem with this approach is that the interaction terms always end. I want a:b to be after a and b , and not at the end, but also for c: d. Is there a way to order terms of interaction? Can anybody help?

+6
source share
1 answer

The easiest way is to use keep.order in terms.formula()

 model <- y ~ a * b + c * d + e labels(terms(model, keep.order = TRUE)) # [1] "a" "b" "a:b" "c" "d" "c:d" "e" 

To find the help file, you will want to use ?terms.formula , since this method is not shown in ?terms . But terms() will send the formula method. In addition, labels() is a shorthand way for getting terminal labels from terms() .

+8
source

All Articles