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?

> dat = read.csv("~/Downloads/treedata.csv")
> 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

1 Answer 1


You don't add interaction terms in the model formula because the nature of the tree structure itself allows for interactions without having to specify them explicitly.

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

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

Which produces the following tree:

decision tree

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.