Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用“显示推理树节点值”;“打印”;_R_Party - Fatal编程技术网

R 使用“显示推理树节点值”;“打印”;

R 使用“显示推理树节点值”;“打印”;,r,party,R,Party,如果我打断了这个问题,我会提前道歉,因为我对R和一般的统计分析非常陌生 我使用参与方库生成了一个条件推理树。 当我绘制(我的_树,type=“simple”)时,我得到如下结果: 1) SOME_VALUE <= 2.5; criterion = 1, statistic = 1306.478 2) SOME_VALUE <= -10.5; criterion = 1, statistic = 173.416 3) SOME_VALUE <= -16; criter

如果我打断了这个问题,我会提前道歉,因为我对R和一般的统计分析非常陌生

我使用
参与方
库生成了一个条件推理树。
当我
绘制(我的_树,type=“simple”)
时,我得到如下结果:

1) SOME_VALUE <= 2.5; criterion = 1, statistic = 1306.478
  2) SOME_VALUE <= -10.5; criterion = 1, statistic = 173.416
    3) SOME_VALUE <= -16; criterion = 1, statistic = 19.385
      4)*  weights = 275 
    3) SOME_VALUE > -16
      5)*  weights = 261 
  2) SOME_VALUE > -10.5
    6) SOME_VALUE <= -2.5; criterion = 1, statistic = 24.094
      7) SOME_VALUE <= -6.5; criterion = 0.974, statistic = 4.989
        8)*  weights = 346 
      7) SOME_VALUE > -6.5
        9)*  weights = 563 
    6) SOME_VALUE > -2.5
      10)*  weights = 442 
1) SOME_VALUE > 2.5
  11) SOME_VALUE <= 10; criterion = 1, statistic = 225.148
    12) SOME_VALUE <= 6.5; criterion = 1, statistic = 18.789
      13)*  weights = 648 
    12) SOME_VALUE > 6.5
      14)*  weights = 473 
  11) SOME_VALUE > 10
    15) SOME_VALUE <= 16; criterion = 1, statistic = 51.729
      16)*  weights = 595 
    15) SOME_VALUE > 16
      17) SOME_VALUE <= 23.5; criterion = 0.997, statistic = 8.931
        18)*  weights = 488 
      17) SOME_VALUE > 23.5
        19)*  weights = 365 
1) SOME_VALUE <= 2.5; criterion = 1, statistic = 1306.478
  2) SOME_VALUE <= -10.5; criterion = 1, statistic = 173.416
    3) SOME_VALUE <= -16; criterion = 1, statistic = 19.385
      4)*  weights = 275; y = (0.96, 0.04)
    3) SOME_VALUE > -16
      5)*  weights = 261; y = (0.831, 0.169)
  2) SOME_VALUE > -10.5
...

当我
打印(我的树)
时,我得到如下结果:

1) SOME_VALUE <= 2.5; criterion = 1, statistic = 1306.478
  2) SOME_VALUE <= -10.5; criterion = 1, statistic = 173.416
    3) SOME_VALUE <= -16; criterion = 1, statistic = 19.385
      4)*  weights = 275 
    3) SOME_VALUE > -16
      5)*  weights = 261 
  2) SOME_VALUE > -10.5
    6) SOME_VALUE <= -2.5; criterion = 1, statistic = 24.094
      7) SOME_VALUE <= -6.5; criterion = 0.974, statistic = 4.989
        8)*  weights = 346 
      7) SOME_VALUE > -6.5
        9)*  weights = 563 
    6) SOME_VALUE > -2.5
      10)*  weights = 442 
1) SOME_VALUE > 2.5
  11) SOME_VALUE <= 10; criterion = 1, statistic = 225.148
    12) SOME_VALUE <= 6.5; criterion = 1, statistic = 18.789
      13)*  weights = 648 
    12) SOME_VALUE > 6.5
      14)*  weights = 473 
  11) SOME_VALUE > 10
    15) SOME_VALUE <= 16; criterion = 1, statistic = 51.729
      16)*  weights = 595 
    15) SOME_VALUE > 16
      17) SOME_VALUE <= 23.5; criterion = 0.997, statistic = 8.931
        18)*  weights = 488 
      17) SOME_VALUE > 23.5
        19)*  weights = 365 
1) SOME_VALUE <= 2.5; criterion = 1, statistic = 1306.478
  2) SOME_VALUE <= -10.5; criterion = 1, statistic = 173.416
    3) SOME_VALUE <= -16; criterion = 1, statistic = 19.385
      4)*  weights = 275; y = (0.96, 0.04)
    3) SOME_VALUE > -16
      5)*  weights = 261; y = (0.831, 0.169)
  2) SOME_VALUE > -10.5
...

如何实现这一点?

可以使用
partykit
包(party的继承者)实现这一点,但即使在那里,它也需要一些黑客攻击。原则上,
print()
函数可以通过内部节点和终端节点等的面板函数进行自定义。但即使对于像这样看似简单的任务,它们看起来也不是很好

当你使用一个具有二元响应的树时,让我们考虑这个简单的(虽然不是很有意义的)可重复的例子:

library("partykit")
airq <- subset(airquality, !is.na(Ozone))
ct <- ctree(Ozone + Wind ~ ., data = airq)
对于终端节点,我们希望显示观察次数(假设未使用
权重
)和平均响应。两者都在小表格中预先计算,然后通过每个节点的
$id
访问:

n <- table(ct$fitted[["(fitted)"]])
m <- aggregate(ct$fitted[["(response)"]], list(ct$fitted[["(fitted)"]]), mean)
m <- apply(m[, -1], 1, function(x) paste(round(x, digits = 3), collapse = ", "))
names(m) <- names(n)

这很有希望接近您想要做的,并且应该为您提供一个模板以供进一步修改。

这并不容易,因为所有的
print.BinaryTree
方法基本上都是打印
my_tree@tree
在上面添加一些注释时。是否有方法简单地提取值?如果有必要,我可以稍后手动将其重新连接在一起。我只是在寻找一些编程方法来获取它们,而不是从绘图中手动复制。是的,请参阅我的答案,我将在其中详细说明如何实际实现这一点。另请参见软件包创建者本人(Zeileis教授)关于如何使用较新的
partykit
软件包实现这一点的另一个答案。太棒了!这正是我要找的!
print.party(ct, inner_panel = ip, terminal_panel = tp)
## [1] root
## |   [2] Temp <= 82 [p = 0.0044842]
## |   |   [3] Temp <= 77: n = 52, y = (18.615, 11.562)
## |   |   [4] Temp > 77: n = 27, y = (41.815, 9.737)
## |   [5] Temp > 82: n = 37, y = (75.405, 7.565)