# How to include interaction terms in R/tree model?

I have read at many places that tree is good for uncovering complex dependencies among predictor variables. From Tree models in R:

The recursive structure of CART models is ideal for uncovering complex dependencies among predictor variables. If the effect of, say, soil moisture content depends strongly on soil texture in nonlinear fashion, CART models of species occurrences have a better shot at detecting this than interaction terms in GLMs or even GAMs.

However the tree() function from the tree package doesn't seem to accept the interaction term and reports error “trees cannot handle interaction terms”. Is there a way to include interaction term in tree?

> tree(cover~(elev+plotsize+disturb),data=dat)
node), split, n, deviance, yval
* denotes terminal node

1) root 8971 39740 4.006
2) elev < 1157.5 6875 29610 3.868 *
3) elev > 1157.5 2096  9567 4.460 *
> tree(cover~(elev+plotsize+disturb)^2,data=dat)
Error in tree(cover ~ (elev + plotsize + disturb)^2, data = dat) :
trees cannot handle interaction terms

You don't add interaction terms in the model formula, the nature of the tree structure itself allows for interactions without specifying a variable that is the interaction.

In R an interaction term in a formula is converted to a variable in the model matrix. For example the interaction a:b would become a variable in the model matrix that takes values $ab = a \times b$. R does this for you behind the scenes.

In a tree interactions are formed not by explicit operations on the variables but through the tree structure. Consider this example using the famous Edgar Anderson Iris data set

data(iris)
require(rpart)
mod <- rpart(Species ~ ., data = iris)
plot(mod)
text(mod)

Produces

In this simple case, the interaction is local; the variable Petal.Width only has an effect in the model for the subset of data for which Petal.Length is greater than or equal to 2.45. In other words, the interaction only affects observations that end up going down the right hand branch of the tree after the first split.

In contrast, interactions of the sort you specified are global; in a:b the interaction has an effect for any value of a or b. `

• This related answer by Rob Hyndman highlights "all terms are assumed to interact" as one of the disadvantages of regression trees. Jan 30, 2017 at 23:21