SplitNode from the party package to a data.tree structure.R/node_conversion_party.R
as.Node.BinaryTree.RdConvert a a SplitNode from the party package to a data.tree structure.
# S3 method for class 'BinaryTree'
as.Node(x, ...)library(party)
#> Loading required package: grid
#> Loading required package: mvtnorm
#> Loading required package: modeltools
#> Loading required package: stats4
#> Loading required package: strucchange
#> Loading required package: zoo
#>
#> Attaching package: ‘zoo’
#> The following objects are masked from ‘package:base’:
#>
#> as.Date, as.Date.numeric
#> Loading required package: sandwich
#>
#> Attaching package: ‘party’
#> The following object is masked from ‘package:ape’:
#>
#> where
airq <- subset(airquality, !is.na(Ozone))
airct <- ctree(Ozone ~ ., data = airq,
controls = ctree_control(maxsurrogate = 3))
tree <- as.Node(airct)
tree
#> levelName
#> 1 1
#> 2 ¦--2
#> 3 ¦ ¦--3
#> 4 ¦ °--4
#> 5 ¦ ¦--5
#> 6 ¦ °--6
#> 7 °--7
#> 8 ¦--8
#> 9 °--9
print(tree,
"label",
criterion = function(x) round(x$criterion$maxcriterion, 3),
statistic = function(x) round(max(x$criterion$statistic), 3)
)
#> levelName label criterion statistic
#> 1 1 Temp <= 82 1.000 56.086
#> 2 ¦--2 Wind <= 6.9 0.998 12.969
#> 3 ¦ ¦--3 weights = 10 0.000 0.000
#> 4 ¦ °--4 Temp > 77 0.997 11.599
#> 5 ¦ ¦--5 weights = 48 0.936 6.141
#> 6 ¦ °--6 weights = 21 0.891 5.182
#> 7 °--7 Wind > 10.3 0.997 11.712
#> 8 ¦--8 weights = 30 0.675 3.159
#> 9 °--9 weights = 7 0.000 0.000
FindNode(tree, 6)$path
#> [1] "1" "2" "4" "6"