rpart创建一个表,该表指示观察值是否属于一个节点

时间:2019-01-18 19:45:01

标签: r rpart

下图显示了我想做什么:

  1. 使用rpart为某些数据集种植一棵树
  2. 创建一个表,该表在原始数据集中每个观测值一行,在树中每个节点一行一列,外加一个ID。如果观测值属于该节点,则节点列的值应为1,否则为零。

enter image description here

这是我写的一些代码:

library(rpart)
  library(rattle)
  data <- kyphosis
  fit <- rpart(Age ~ Number + Start, data = kyphosis)
  fancyRpartPlot(fit)

  nodeNumbers <- as.numeric(rownames(fit$frame))

  paths <- path.rpart(fit, nodeNumbers)

  for(i in 1:length(nodeNumbers)){
    nodeNumber <- nodeNumbers[i]
    data[,paste0('gp', nodeNumber)] <- NA
    path <- paths[[i]]
    if(length(path) == 1) # i.e. we're at the root
      data[,paste0('gp', nodeNumber)] <- 1 else
        print('help')
  }
  data

是否有一个包装可以满足我的需要?我唯一想到的方法就是为paths对象添加一些正则表达式魔术。我的猜测/希望是这样做有更简单的方法。

1 个答案:

答案 0 :(得分:1)

  

有没有可以做我需要的包裹?

AFAIK,否,但只能在rpart 4.1.13版中使用

# function to get the binary matrix OP wants given the leaf index
get_nodes <- function(object, where){
  rn <- row.names(object$frame)
  edges <- descendants(as.numeric(rn))
  o <- t(edges)[where, , drop = FALSE]
  colnames(o) <- paste0("GP", rn)
  o
}
environment(get_nodes) <- environment(rpart)

# use function 
nodes <- get_nodes(fit, fit$where)
head(nodes, 9)
#R       GP1   GP2   GP3   GP6   GP7  GP14  GP15
#R [1,] TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE
#R [2,] TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
#R [3,] TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE
#R [4,] TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
#R [5,] TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
#R [6,] TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE
#R [7,] TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE
#R [8,] TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE
#R [9,] TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE

# compare with
head(data, 9)
#R   Kyphosis Age Number Start
#R 1   absent  71      3     5
#R 2   absent 158      3    14
#R 3  present 128      4     5
#R 4   absent   2      5     1
#R 5   absent   1      4    15
#R 6   absent   1      2    16
#R 7   absent  61      2    17
#R 8   absent  37      3    16
#R 9   absent 113      2    16

这是适合模型的完整代码,创建了一个可以获取新数据集的末页叶子的函数,并创建并使用了上面的函数

# do as OP
library(rpart)
library(rattle)
data <- kyphosis
fit <- rpart(Age ~ Number + Start, data = kyphosis)
fancyRpartPlot(fit)

enter image description here

# function that gives us the leaf index
get_where <- function(object, newdata, na.action = na.pass){
  if (is.null(attr(newdata, "terms"))) {
    Terms <- delete.response(object$terms)
    newdata <- model.frame(Terms, newdata, na.action = na.action, 
                           xlev = attr(object, "xlevels"))
    if (!is.null(cl <- attr(Terms, "dataClasses"))) 
      .checkMFClasses(cl, newdata, TRUE)
  }
  pred.rpart(object, rpart.matrix(newdata))
}
environment(get_where) <- environment(rpart)

# check that we get the correct value
where <- get_where(fit, data)
stopifnot(isTRUE(all.equal(
  fit$frame$yval[where], unname(predict(fit, newdata = data)))))

# function to get the binary matrix OP wants given the leaf index
get_nodes <- function(object, where){
  rn <- row.names(object$frame)
  edges <- descendants(as.numeric(rn))
  o <- t(edges)[where, , drop = FALSE]
  colnames(o) <- paste0("GP", rn)
  o
}
environment(get_nodes) <- environment(rpart)

# use function 
nodes <- get_nodes(fit, where)
head(nodes, 9)
#R       GP1   GP2   GP3   GP6   GP7  GP14  GP15
#R [1,] TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE
#R [2,] TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
#R [3,] TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE
#R [4,] TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
#R [5,] TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
#R [6,] TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE
#R [7,] TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE
#R [8,] TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE
#R [9,] TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE

# compare with
head(data, 9)
#R   Kyphosis Age Number Start
#R 1   absent  71      3     5
#R 2   absent 158      3    14
#R 3  present 128      4     5
#R 4   absent   2      5     1
#R 5   absent   1      4    15
#R 6   absent   1      2    16
#R 7   absent  61      2    17
#R 8   absent  37      3    16
#R 9   absent 113      2    16

代码来自rpart:::predict.rpartrpart::path.rpart。当然,您可以根据需要合并get_whereget_nodes函数。