Can random forest detect squared terms? In Feature Engineering and Selection: A Practical Approach for Predictive Models Kuhn and Johnson mention (chapter 7) that random forest can be used to detect interaction between variables, e.g. x1*x2.
My question is. Can random forest be used to detect squared terms x1^2. If so, how would you do that?
 A: Indeed it can. Here are some simulated data with a squared relationship between the predictor and the response, and the fit from a Random Forest:

R code:
nn <- 1e4
set.seed(1)
xx <- runif(nn)
yy <- xx^2+rnorm(nn,0,0.1)

plot(xx,yy,pch=19,cex=0.6,col="lightgray")

library(randomForest)
model <- randomForest(yy~xx)

xx_pred <- seq(0,1,by=.01)
lines(xx_pred,predict(model,newdata=data.frame(xx=xx_pred)),col="red",lwd=2)

As to how the RF does this: remember that it is just a collection of classification and regression trees. Each separate tree (based on a bootstrap sample of the data, and a subset of predictors) will use a different cutoff of the predictor and output a different value for the response for low vs. high predictor values. On average, the fitted reponse for high predictor values will deviate more from the average than for low predictor values, and thus model the nonlinear relationship.
There are also RF implementations that fit linear models on the predictor in the leaves. These can of course also model nonlinearities, by fitting different slopes for different values of the predictor.
