I am using partykit
and noticed a possible varid
mismatch (unless I misunderstood something). Below is the example code.
The root node as returned by nodeapply
shows variable 5 as the split variable.
Also the first element of the explicitly generated list has split$varid
5. If we look at the iris
data frame then the 5th column is Species
, and Petal.Width
is 4th column which should be the varid
for the root node as shown by the j48_party
object.
It seems like the varid
are actual feature used +1, is this intentional?
> library(partykit)
> library(RWeka)
> data("iris")
> j48 <- J48(Species~., data=iris)
> j48_party <- as.party(j48)
> j48_party
Model formula:
Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
Fitted party:
[1] root
| [2] Petal.Width <= 0.6: setosa (n = 50, err = 0.0%)
| [3] Petal.Width > 0.6
| | [4] Petal.Width <= 1.7
| | | [5] Petal.Length <= 4.9: versicolor (n = 48, err = 2.1%)
| | | [6] Petal.Length > 4.9
| | | | [7] Petal.Width <= 1.5: virginica (n = 3, err = 0.0%)
| | | | [8] Petal.Width > 1.5: versicolor (n = 3, err = 33.3%)
| | [9] Petal.Width > 1.7: virginica (n = 46, err = 2.2%)
Number of inner nodes: 4
Number of terminal nodes: 5
> colnames(iris)
[1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
> nodeapply(j48_party)
$`1`
[1] root
| [2] V5 <= 0.6 *
| [3] V5 > 0.6
| | [4] V5 <= 1.7
| | | [5] V4 <= 4.9 *
| | | [6] V4 > 4.9
| | | | [7] V5 <= 1.5 *
| | | | [8] V5 > 1.5 *
| | [9] V5 > 1.7 *
> nodes <- as.list(j48_party$node)
> nodes[[1]]$split$varid
[1] 5