【问题标题】:how derive standard deviation of the leaf nodes (rpart)?如何导出叶节点(rpart)的标准偏差?
【发布时间】:2019-01-07 16:04:05
【问题描述】:

我用 rpart 做了一个回归树,根据几个变量来评估老年人的步行情况。通过使用绘图,我想使用输出在另一个软件中进行进一步分析。但是我想知道是否不仅可以从叶节点导出每组的步行,还可以从叶节点导出标准偏差(就步行而言)?

image of my regression tree

#### Decision tree with rpart
modelRT <- rpart(logwalkin~.-walkinmin-walkingtime, data=trainDF,
             control=rpart.control(minsplit=25, maxdepth = 8, cp =0.00005))
rpart.plot(modelRT,type=3,digits=3,fallen.leaves=TRUE)

【问题讨论】:

    标签: r decision-tree rpart


    【解决方案1】:

    要绘制树的每个节点的标准差,您可以使用 rpart.plotnode.fun,如第 6 章所述 rpart.plot package vignette。 例如

    library(rpart.plot)
    data(iris)
    tree = rpart(Sepal.Length~., data=iris, cp=.05) # example tree
    
    # Calculate the standard deviation at each node of the tree.
    sd <- sqrt(tree$frame$dev / (tree$frame$n-1))
    
    # Append the standard deviation as an extra column to the tree frame.
    tree$frame <- cbind(tree$frame, sd)
    
    # Create a node.fun to print the standard deviation at each node.
    # See Chapter 6 of the rpart.plot vignette http://www.milbo.org/doc/prp.pdf.
    node.fun.sd <- function(x, labs, digits, varlen)
    {
        s <- round(x$frame$sd, 2) # round sd to 2 digits
        paste(labs, "\n\nsd", s)
     }
    
    # Plot the tree, using the node.fun to add the standard deviation to each node
    rpart.plot(tree, type=4, node.fun=node.fun.sd)
    

    给了

    如果您只想要叶节点的标准偏差(而不是 内部节点),您可以这样做:

    library(rpart.plot)
    data(iris)
    tree = rpart(Sepal.Length~., data=iris, cp=.05)
    sd <- sqrt(tree$frame$dev / (tree$frame$n-1))
    is.leaf <- tree$frame$var == "<leaf>" # logical vec, indexed on row in frame
    sd[!is.leaf] <- NA # change sd of non-leaf nodes to NA
    tree$frame <- cbind(tree$frame, sd)
    node.fun2 <- function(x, labs, digits, varlen)
    {
        s <- paste("\n\nsd", round(x$frame$sd, 2)) # round sd to 2 digits
        s[is.na(x$frame$sd)] <- "" # delete NAs
        paste(labs, s)
    }
    rpart.plot(tree, type=4, node.fun=node.fun2)
    

    给了

    【讨论】:

      【解决方案2】:

      我不认为你可以从图中做到这一点,但你当然可以从 rpart 模型中得出每个叶节点的标准差。由于您不提供数据,因此我将使用内置的虹膜数据进行示例。由于您对回归感兴趣,我将消除类变量(Species)并从其他变量中预测变量 Sepal.Length。

      设置

      library(rpart)
      library(rpart.plot)
      
      RP = rpart(Sepal.Length ~ ., data=iris[,-5])
      rpart.plot(as.party(RP))
      

      如您所见,节点 4、5、6、10、11、12 和 13 是叶节点。返回的结构RP$where 的一部分告诉您原始实例去了哪个叶子。所以你只需要使用这个变量进行聚合。

      SD = aggregate(iris$Sepal.Length, list(RP$where), sd)
      SD
        Group.1         x
      1       4 0.2390221
      2       5 0.2888391
      3       6 0.2500526
      4      10 0.4039577
      5      11 0.3802046
      6      12 0.3020486
      7      13 0.2279132
      

      Group.1 告诉您哪个叶节点,x 告诉您最终在该叶中的点的标准偏差。如果您希望将标准差添加到您的绘图中,您可以使用mtext 来实现。在摆弄了一些展示位置之后:

      rpart.plot(RP)
      mtext(text=round(SD$x,1), side=1, line=3.2, at=seq(0.06,1,0.1505))
      

      【讨论】:

        猜你喜欢
        • 2012-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-16
        • 2022-01-23
        • 2014-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多