用文本/自定义控件打印决策树[r]
我想很好地打印文本中的决策树。例如,我可以打印树对象本身:用文本/自定义控件打印决策树[r],r,decision-tree,rpart,party,R,Decision Tree,Rpart,Party,我想很好地打印文本中的决策树。例如,我可以打印树对象本身: library(rpart) f = as.formula('Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + Species') fit = rpart(f, data = iris, control = rpart.control(xval = 3)) fit 屈服 n= 150 node), split, n, deviance, yval *
library(rpart)
f = as.formula('Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + Species')
fit = rpart(f, data = iris, control = rpart.control(xval = 3))
fit
屈服
n= 150
node), split, n, deviance, yval
* denotes terminal node
1) root 150 102.1683000 5.843333
2) Petal.Length< 4.25 73 13.1391800 5.179452
4) Petal.Length< 3.4 53 6.1083020 5.005660
8) Sepal.Width< 3.25 20 1.0855000 4.735000 *
9) Sepal.Width>=3.25 33 2.6696970 5.169697 *
... # omitted
Model formula:
Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + Species
Fitted party:
[1] root
| [2] Petal.Length < 4.25
| | [3] Petal.Length < 3.4
| | | [4] Sepal.Width < 3.25: 4.735 (n = 20, err = 1.1)
| | | [5] Sepal.Width >= 3.25: 5.170 (n = 33, err = 2.7)
| | [6] Petal.Length >= 3.4: 5.640 (n = 20, err = 1.2)
...# omitted
Number of inner nodes: 6
Number of terminal nodes: 7
屈服
n= 150
node), split, n, deviance, yval
* denotes terminal node
1) root 150 102.1683000 5.843333
2) Petal.Length< 4.25 73 13.1391800 5.179452
4) Petal.Length< 3.4 53 6.1083020 5.005660
8) Sepal.Width< 3.25 20 1.0855000 4.735000 *
9) Sepal.Width>=3.25 33 2.6696970 5.169697 *
... # omitted
Model formula:
Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + Species
Fitted party:
[1] root
| [2] Petal.Length < 4.25
| | [3] Petal.Length < 3.4
| | | [4] Sepal.Width < 3.25: 4.735 (n = 20, err = 1.1)
| | | [5] Sepal.Width >= 3.25: 5.170 (n = 33, err = 2.7)
| | [6] Petal.Length >= 3.4: 5.640 (n = 20, err = 1.2)
...# omitted
Number of inner nodes: 6
Number of terminal nodes: 7
模型公式:
萼片长~萼片宽+花瓣长+花瓣宽+种
第三方:
[1] 根
|[2]花瓣长度<4.25
||[3]花瓣长度<3.4
|| |[4]萼片宽度<3.25:4.735(n=20,err=1.1)
|| |[5]萼片宽度>=3.25:5.170(n=33,err=2.7)
||[6]花瓣长度>=3.4:5.640(n=20,err=1.2)
…#省略
内部节点数:6
终端节点数:7
有没有办法让我有更多的控制权?例如,我不想打印
n
和err
,也不想打印标准偏差而不是err
。这不是一个非常优雅的答案,但如果您只想摆脱n=
和err=
,您可以捕获输出并编辑它
CO = capture.output(print(as.party(fit)))
CO2 = sub("\\(.*\\)", "", CO)
cat(paste(CO2, collapse="\n"))
Model formula:
Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + Species
Fitted party:
[1] root
| [2] Petal.Length < 4.25
| | [3] Petal.Length < 3.4
| | | [4] Sepal.Width < 3.25: 4.735
| | | [5] Sepal.Width >= 3.25: 5.170
| | [6] Petal.Length >= 3.4: 5.640
| [7] Petal.Length >= 4.25
CO=capture.output(打印(作为第三方(fit)))
CO2=sub(“\\(.\\)”,“,CO)
cat(粘贴(CO2,塌陷=“\n”))
模型公式:
萼片长~萼片宽+花瓣长+花瓣宽+种
第三方:
[1] 根
|[2]花瓣长度<4.25
||[3]花瓣长度<3.4
|| |[4]萼片宽度<3.25:4.735
|| |[5]萼片宽度>=3.25:5.170
||[6]花瓣长度>=3.4:5.640
|[7]花瓣长度>=4.25
我不确定您要插入什么标准偏差,但我希望您可以用同样的方式编辑它 party对象的print()
方法非常灵活,可以通过各种面板功能和定制进行控制。有关概述,请参见《打印方》。不过,文档有点简短和技术性
在您的案例中,最简单的解决方案是设置响应y
、案例权重w
(在您的案例中默认为全部1)和所需的位数的函数:
myfun <- function(y, w, digits = 2) {
n <- sum(w)
m <- weighted.mean(y, w)
s <- sqrt(weighted.mean((y - m)^2, w) * n/(n - 1))
sprintf("%s (serr = %s)",
round(m, digits = digits),
round(s, digits = digits))
}