Learn R Programming

partykit (version 1.2-16)

partynode-methods: Methods for Node Objects

Description

Methods for computing on partynode objects.

Usage

is.partynode(x)
as.partynode(x, ...)
# S3 method for partynode
as.partynode(x, from = NULL, recursive = TRUE, ...)
# S3 method for list
as.partynode(x, ...)
# S3 method for partynode
as.list(x, ...)
# S3 method for partynode
length(x)
# S3 method for partynode
[(x, i, ...)
# S3 method for partynode
[[(x, i, ...)
is.terminal(x, ...)
# S3 method for partynode
is.terminal(x, ...)
# S3 method for partynode
depth(x, root = FALSE, ...)
width(x, ...)
# S3 method for partynode
width(x, ...)
# S3 method for partynode
print(x, data = NULL, names = NULL,
    inner_panel = function(node) "", 
    terminal_panel = function(node) " *",
    prefix = "", first = TRUE, digits = getOption("digits") - 2, 
    ...)
# S3 method for partynode
nodeprune(x, ids, ...)

Arguments

x

an object of class partynode or list.

from

an integer giving the identifier of the root node.

recursive

a logical, if FALSE, only the id of the root node is checked against from. If TRUE, the ids of all nodes are checked.

i

an integer specifying the kid to extract.

root

a logical. Should the root count be counted in depth?

data

an optional data.frame.

names

a vector of names for nodes.

terminal_panel

a panel function for printing terminal nodes.

inner_panel

a panel function for printing inner nodes.

prefix

lines start with this symbol.

first

a logical.

digits

number of digits to be printed.

ids

a vector of node ids to be pruned-off.

...

additional arguments.

Details

is.partynode checks if the argument is a valid partynode object. is.terminal is TRUE for terminal nodes and FALSE for inner nodes. The subset methods return the partynode object corresponding to the ith kid.

The as.partynode and as.list methods can be used to convert flat list structures into recursive partynode objects and vice versa. as.partynode applied to partynode objects renumbers the recursive nodes starting with root node identifier from.

length gives the number of kid nodes of the root node, depth the depth of the tree and width the number of terminal nodes.

Examples

Run this code
## a tree as flat list structure
nodelist <- list(
  # root node
  list(id = 1L, split = partysplit(varid = 4L, breaks = 1.9), 
      kids = 2:3),
  # V4 <= 1.9, terminal node
  list(id = 2L),
  # V4 > 1.9
  list(id = 3L, split = partysplit(varid = 1L, breaks = 1.7), 
      kids = c(4L, 7L)),
  # V1 <= 1.7
  list(id = 4L, split = partysplit(varid = 4L, breaks = 4.8), 
      kids = 5:6),
  # V4 <= 4.8, terminal node
  list(id = 5L),
  # V4 > 4.8, terminal node
  list(id = 6L),
  # V1 > 1.7, terminal node
  list(id = 7L)
)

## convert to a recursive structure
node <- as.partynode(nodelist)

## print raw recursive structure without data
print(node)

## print tree along with the associated iris data
data("iris", package = "datasets")
print(node, data = iris)

## print subtree
print(node[2], data = iris)

## print subtree, with root node number one
print(as.partynode(node[2], from = 1), data = iris)

## number of kids in root node
length(node)

## depth of tree
depth(node)

## number of terminal nodes
width(node)

## convert back to flat structure
as.list(node)

Run the code above in your browser using DataLab