使用 partykit:mob() 对象的并排节点模型生成 table
Generate table with side-by-side node models of `partykit:mob()` object
假设我使用 partykit:mob()
拟合了一个模型。之后,我想生成一个包含所有节点(包括使用整个样本拟合的模型)的并排 table。在这里,我尝试使用 stargazer()
来做到这一点,但也欢迎使用其他方式。
下面是一个示例并尝试获取 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.- 提取 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.- 提取 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.- 不太优雅,我知道:强制 class 模拟 glm 对象。
## 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
一个相当务实的解决方案是重新拟合恢复 partykit:mob()
找到的规则的模型,然后对它们使用 stargaze()
,但我肯定在这里遗漏了一些东西。提前致谢。
我的错,这是一个小的差异,使它工作。这是一个解决方案,不确定是否是最好的方法,但它确实有效。-
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)
最好提取(或重新调整)每个节点的模型对象列表,然后应用选择的 table 包。就个人而言,我不太喜欢 stargazer
而更喜欢使用 modelsummary
或有时使用旧的 memisc
.
如果树在 $info
中包含模型 $objects
(至于 pid_tree
),您可以对所有 nodeids()
使用 nodeapply()
来提取这些:
pid_models <- nodeapply(pid_tree, ids = nodeids(pid_tree), FUN = function(x) x$info$object)
如果您只想提取树的终端节点(叶子)的拟合模型,那么您可以通过设置 ids = nodeids(pid_tree, terminal = TRUE)
.
来实现
或者,尤其是当模型对象未存储时,您可以通过以下方式轻松地重新调整它们:
pid_models <- refit.modelparty(pid_tree)
在这里,您还可以包括 node = nodeids(pid_tree, terminal = TRUE)
以仅改装终端节点模型。
在所有情况下您随后都可以使用
msummary(pid_models)
生成模型摘要table。它支持多种输出格式,当然您可以进一步调整列表以更改结果,例如,通过更改名称等。默认输出如下所示:
假设我使用 partykit:mob()
拟合了一个模型。之后,我想生成一个包含所有节点(包括使用整个样本拟合的模型)的并排 table。在这里,我尝试使用 stargazer()
来做到这一点,但也欢迎使用其他方式。
下面是一个示例并尝试获取 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.- 提取 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.- 提取 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.- 不太优雅,我知道:强制 class 模拟 glm 对象。
## 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
一个相当务实的解决方案是重新拟合恢复 partykit:mob()
找到的规则的模型,然后对它们使用 stargaze()
,但我肯定在这里遗漏了一些东西。提前致谢。
我的错,这是一个小的差异,使它工作。这是一个解决方案,不确定是否是最好的方法,但它确实有效。-
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)
最好提取(或重新调整)每个节点的模型对象列表,然后应用选择的 table 包。就个人而言,我不太喜欢 stargazer
而更喜欢使用 modelsummary
或有时使用旧的 memisc
.
如果树在 $info
中包含模型 $objects
(至于 pid_tree
),您可以对所有 nodeids()
使用 nodeapply()
来提取这些:
pid_models <- nodeapply(pid_tree, ids = nodeids(pid_tree), FUN = function(x) x$info$object)
如果您只想提取树的终端节点(叶子)的拟合模型,那么您可以通过设置 ids = nodeids(pid_tree, terminal = TRUE)
.
或者,尤其是当模型对象未存储时,您可以通过以下方式轻松地重新调整它们:
pid_models <- refit.modelparty(pid_tree)
在这里,您还可以包括 node = nodeids(pid_tree, terminal = TRUE)
以仅改装终端节点模型。
在所有情况下您随后都可以使用
msummary(pid_models)
生成模型摘要table。它支持多种输出格式,当然您可以进一步调整列表以更改结果,例如,通过更改名称等。默认输出如下所示: