2016-05-06 1 views
1

たとえば、回答者がいて病気があるかどうか聞いてみましょう。そこから私は彼女/彼の父親が病気にかかっているかどうか尋ねます。後者の質問に「はい」と答えた場合は、父親が現在治癒しているかどうかを尋ねます。父親が病気にかかっていない場合、その質問は適用されません。Rの意思決定ツリーとして複数の変数を持つ不測の事態の表をどのように視覚的に表現できますか?

Rにこのような「決定木」を作成することができますか?

ここ

は0が「なし」を意味し、1つの手段「yes」の場合は、使用可能なデータである:

person_disease <- c(rep(1, 10), rep(0, 20)) 

father_disease <- c(rep(1, 7), rep(0,18), rep(1,5)) 

father_cured <- c(rep(0, 4), rep(1,3), rep(NA,18),rep(1,5) ) 

## 
df <- data.frame(person_disease, father_disease, father_cured) 

enter image description here

+0

は、おそらくパッケージpartykit(https://cran.r-project.org/web/packages/partykit/index.html)が役立ちます。 –

答えて

1

。あなたが望むことをする方法はたくさんあります。たとえば:

person_disease <- c(rep(1, 10), rep(0, 20)) 
father_disease <- c(rep(1, 7), rep(0,18), rep(1,5)) 
father_cured <- c(rep(0, 4), rep(1,3), rep(NA,18),rep(1,5) ) 
df <- data.frame(person_disease, father_disease, father_cured) 

library(data.tree) 

#here, the tree is constructed "manually" 
#however, depending on your data and your needs, you might want to generate the tree directly from the data 
#many examples for this are available in the vignettes, see browseVignettes("data.tree") 
disease <- Node$new("Disease", data = df) 
father_disease_yes <- disease$AddChild("Father Disease Yes", label = "Father Disease", edge = "yes", condition = function(df) df[df$person_disease == 1,]) 
father_cured_yes <- father_disease_yes$AddChild("Father Cured Yes", label = "Father Cured", edge = "yes", condition = function(df) df[df$father_cured == 1,]) 
father_disease_no <- disease$AddChild("Father Disease No", label = "Father Disease", edge = "no", condition = function(df) df[df$person_disease == 0,]) 


#data filter (pre-order) 
#an alternative would be to do this recursively 
disease$Do(function(node) { 
    for (child in node$children) { 
    child$data <- child$condition(node$data) 
    } 
}) 

print(disease, total = function(node) nrow(node$data)) 


#plotting 
#(many more options are available, see ?plot.Node) 
SetEdgeStyle(disease, 
      fontname = "helvetica", 
      arrowhead = "none", 
      label = function(node) paste0(node$edge, "\n", "total = ", nrow(node$data))) 

SetNodeStyle(disease, 
      fontname = "helvetica", 
      label = function(node) node$label) 

plot(disease) 

enter image description here

1

確かにありますが、グラフィックの種類を作成するために使用できるパッケージそれは、便利に十分に、ダイヤグラムと呼ばれています。

barplot()qplot()のような自動化されたグラフ作成プロセスではありませんが、あなたが作成しようとしているダイアグラムの種類を正確に作成するために使用できるものです。

あなたが訓練されている場合は、特定のデータや状況に対してプロセスをより自動化するコードを書くことができます。

パッケージはダイアグラムと呼ばれます。あなたはこのpdfでそれについてもっと知ることができます。あなたはそのためdata.treeパッケージを使用することができます

diagram pdf.

関連する問題