2015-10-26 79 views
2

如果我把這個問題作爲我對R和一般的統計分析非常陌生的話,我會提前道歉。用「print」顯示推理樹節點值

我使用party庫生成了條件推理樹。
當我我plot(my_tree, type = "simple")得到這樣一個結果:

R tree plot

當我print(my_tree)我得到這樣一個結果:

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 

我喜歡的print輸出,但它似乎是缺少y = (0.96, 0.04)值。

理想情況下,我想我的輸出是這個樣子:

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 
... 

我如何去實現呢?

+0

它不會那麼容易,因爲所有'print.BinaryTree'方法基本上都是在打印'my_tree @ tree'的同時添加上面的註釋。 –

+0

有沒有簡單提取這些值的方法?如果必須的話,我會很好的手動加入它。我只是尋找一些編程方法來獲得它們,而不是從一個繪圖手動複製。 –

+0

是的,請參閱我的回答[here](http://stats.stackexchange.com/a/171317/42632),其中我準確地展示瞭如何實現這一目標。另請參閱軟件包創建者自己(Zeileis教授)就如何使用新的'partykit'軟件包來做到這一點的其他答案。 –

回答

3

可以使用partykit程序包(party的後繼程序)執行此操作,但即使在那裏它也需要一些黑客程序。原則上,print()函數可以通過用於內部和終端節點等的面板函數進行定製。但即使看似簡單的任務,它們看起來也不是很好。

正如你似乎已經使用樹與二元響應,讓我們看看這個簡單的(雖然不是非常有意義的)重複的例子:

library("partykit") 
airq <- subset(airquality, !is.na(Ozone)) 
ct <- ctree(Ozone + Wind ~ ., data = airq) 

對於內部節點假設我們只是想表明在p值在每個節點的$info中很容易獲得。我們可以通過格式化這個:

ip <- function(node) formatinfo_node(node, 
    prefix = " ", 
    FUN = function(info) paste0("[p = ", format.pval(info$p.value), "]") 
) 

因爲我們想要展示的觀測數(假定沒有weights已使用)和平均響應終端節點。在小表兩者都預先計算並然後訪問經由每個節點的$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) 

面板功能然後由下式定義:

tp <- function(node) formatinfo_node(node, 
    prefix = ": ", 
    FUN = function(info) paste0(
    "n = ", n[as.character(node$id)], 
    ", y = (", m[as.character(node$id)], ")" 
) 
) 

要在print()方法我們需要應用此直接致電print.party(),因爲目前print.constparty()未正確傳遞此信息。 (我們將在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) 

這是希望接近你想做的事,應該給你進一步修改的模板。

+0

太棒了!這正是我正在尋找的! –