# Generate table with side-by-side node models of `partykit:mob()` object

### Tags:

#### modelsummary

Let's say I fit a model using `partykit:mob()`. Afterward, I would like to generate a side-by-side table with all the nodes (including the model fitted using the whole sample). Here I attempted to do it using `stargazer()`, but other ways are more than welcome.

Below an example and attempts to get the table.

``````library("partykit")
require("mlbench")
## Pima Indians diabetes data
data("PimaIndiansDiabetes", package = "mlbench")
## a simple basic fitting function (of type 1) for a logistic regression
logit <- function(y, x, start = NULL, weights = NULL, offset = NULL, ...) {
glm(y ~ 0 + x, family = binomial, start = start, ...)
}
## set up a logistic regression tree
pid_tree <- mob(diabetes ~ glucose | pregnant + pressure + triceps + insulin +
mass + pedigree + age, data = PimaIndiansDiabetes, fit = logit)

pid_tree
# Model-based recursive partitioning (logit)
#
# Model formula:
#   diabetes ~ glucose | pregnant + pressure + triceps + insulin +
#   mass + pedigree + age
#
# Fitted party:
#   [1] root
# |   [2] mass <= 26.3: n = 167
# |       x(Intercept)     xglucose
# |        -9.95150963   0.05870786
# |   [3] mass > 26.3
# |   |   [4] age <= 30: n = 304
# |   |       x(Intercept)     xglucose
# |   |        -6.70558554   0.04683748
# |   |   [5] age > 30: n = 297
# |   |       x(Intercept)     xglucose
# |   |        -2.77095386   0.02353582
#
# Number of inner nodes:    2
# Number of terminal nodes: 3
# Number of parameters per node: 2
# Objective function: 355.4578
``````

### 1.- Extract `summary(pid_tree, node = x)` + `stargazer()`.

``````## I want to replicate this table extracting the the nodes from partykit object.
library(stargazer)
m.glm<-   glm(diabetes ~ glucose, family = binomial,data = PimaIndiansDiabetes)

typeof(m.glm)
## [1] "list"
class(m.glm)
## [1] "glm" "lm"
stargazer(m.glm)
## ommited output.

## Extracting summary from each node
summ_full_data <- summary(pid_tree, node = 1)
summ_node_2    <- summary(pid_tree, node = 2)
summ_node_4    <- summary(pid_tree, node = 4)
summ_node_5    <- summary(pid_tree, node = 5)

## trying to create stargazer table with coefficients
stargazer(m.glm,
summ_node_2,
summ_node_4,
summ_node_5,title="MOB Results")
##Error: \$ operator is invalid for atomic vectors
``````

### 2.- Extract `pid_tree[x]` + `stargazer()`.

``````## Second Attempt (extracting modelparty objects instead)
node_2    <- pid_tree[2]
node_4    <- pid_tree[4]
node_5    <- pid_tree[5]

class(node_5)
##[1] "modelparty" "party"

stargazer(m.glm,
node_2,
node_4,
node_5,title="MOB Results")
# % Error: Unrecognized object type.
# % Error: Unrecognized object type.
# % Error: Unrecognized object type.
``````

### 3.- Not really elegant, I know: Force class to emulate the glm object.

``````## Force class of object to emulate glm one
class(m.glm)
class(summ_node_2) <- c("glm", "lm")
stargazer(summ_node_2)
##Error in if (p > 0) { : argument is of length zero
``````

A rather pragmatic solution would be just re-fit the model recovering the rules found by `partykit:mob()` and then use `stargaze()` on them, but for sure I am missing something here. Thanks in advance.

487

#### Álvaro A. Gutiérrez-Vargas

It's best to extract (or refit) the list of model objects per node and then apply the table package of choice. Personally, I don't like `stargazer` much and much rather use `modelsummary` instead or sometimes the good old `memisc`.

If the tree contains the model `\$objects` in the `\$info` (as for `pid_tree`) you can use `nodeapply()` for all `nodeids()` to extract these:

``````pid_models <- nodeapply(pid_tree, ids = nodeids(pid_tree), FUN = function(x) x\$info\$object)
``````

If you just want to extract the fitted models for the terminal nodes (leaves) of the tree, then you can do so by setting `ids = nodeids(pid_tree, terminal = TRUE)`.

Alternatively, especially when the model objects are not stored, you can easily refit them via:

``````pid_models <- refit.modelparty(pid_tree)
``````

Here, you could also include `node = nodeids(pid_tree, terminal = TRUE)` to only refit the terminal node models.

In all cases you can subsequently use

``````msummary(pid_models)
``````

to produce the model summary table. It supports a variety of output formats and of course you can tweak the list further to change the results, e.g., by changing their names etc. The default output looks like this:

87

#### Achim Zeileis

My bad, it was a small difference that makes it work. Here a solution, not sure if the best way, but it does the work.-

``````library(stargazer)
obj_node_full_sample<- pid_tree[1]\$node\$info\$object
obj_node_2<- pid_tree[2]\$node\$info\$object
obj_node_4<- pid_tree[4]\$node\$info\$object
obj_node_5<- pid_tree[5]\$node\$info\$object

stargazer(obj_node_full_sample,
obj_node_2,
obj_node_4,
obj_node_5,title="Results", align=TRUE)
``````

30