# Mob model tree algorithm

I am trying to figure out the inner workings of the mob function in the party package. I can't figure out how the splitting variable is selected when it is a categorical variable.

In the publications by the authors, they say that it is done by a chisquare test of association between the residual deviances and the categorical variable. I can't see how this works.

In the toy example below, the mob function works fine. The variable X is the splitting variable.

b <- (1:1000) / 100 - 5
a <- c(b,-b)
b <- c(b, b)
X <- as.factor(c(rep(FALSE, 1000), rep(TRUE, 1000)))
mob(a ~ b |  X)


However, if I fit a linear model without splitting, there is (by construction) no association between the residuals and the values of the splitting variable:

res <- sign(residuals(lm(a ~ b)))
table(res, X)


The resulting table is:

X
res  FALSE TRUE
-1   499  500
1    501  500


This table does not show that the X variable is important, still the algorithm can figure it out. How is that done?

The parameter stability tests in mob() are based not only on residuals but the entire score (aka gradient aka estimating function). For a linear regression model with an intercept, the residual is the first component of the score but there are additional components for each regressor (= residual * regressor). In your example, the second score has the information because only the slope but not the intercept is affected by the change:

library("sandwich")
res <- sign(estfun(lm(a ~ b))[,2])
table(res, X)


which yields

    X
res  FALSE TRUE
-1     0  999
0      1    1
1    999    0


Two further comments: (1) The test not only uses the sign of the score, though, but a full chi-squared statistics. (2) A more flexible and improved reimplementation of mob() is available in package partykit which also provides dedicated functions lmtree() and glmtree().