Libraries

library(tidyverse)
library(readxl)
library(knitr)
library(janitor)
library(data.tree)
library(DiagrammeR)

Students

students <- 
  read_excel("G:/My Drive/homework/Victor W/From Victor/M3-expert-data3-students.xls")

kable(students)
Question Science, Technology, Engineering and Math Social/Behavioral Sciences Arts and humanities Business
What percentage of the student body is this college 0.19 0.32 0.28 0.21
What percentage of the students in this college use marijuana? 0.15 0.40 0.65 0.32
# as_tabyl(students) %>% adorn_totals(where = c("row", "col")) %>% kable()

# modifed from class example
students.tree <- Node$new("Proportion of Students")
STEM <- students.tree$AddChild("STEM")
SocBehav <- students.tree$AddChild("Social/Behavioral Sciences")
ArtsHuman <- students.tree$AddChild("Arts and humanities")
Business <- students.tree$AddChild("Business")

STEM$p <- 0.19
SocBehav$p <- 0.32
ArtsHuman$p <- 0.28
Business$p <- 0.21

STEM_yes <- STEM$AddChild("Marijuana: Yes (0.15)")
STEM_no <- STEM$AddChild("Marijuana: No (0.85)")
SocBehav_yes <- SocBehav$AddChild("Marijuana: Yes (0.4)")
SocBehav_no <- SocBehav$AddChild("Marijuana: No (0.6)")
ArtsHuman_yes <- ArtsHuman$AddChild("Marijuana: Yes (0.65)")
ArtsHuman_no <- ArtsHuman$AddChild("Marijuana: No (0.35)")
Business_yes <- Business$AddChild("Marijuana: Yes (0.32)")
Business_no <- Business$AddChild("Marijuana: No (0.68)")

STEM_yes$p <- .019 * 0.15
STEM_no$p <- 0.19 * 0.85
SocBehav_yes$p <- 0.32 * 0.4
SocBehav_no$p <- 0.32 * 0.6
ArtsHuman_yes$p <- 0.28 * 0.65 
ArtsHuman_no$p <- 0.28 * 0.35
Business_yes$p <- 0.21 * 0.32
Business_no$p <- 0.21 * 0.68 

Version 1

print(students.tree, "p")
##                         levelName       p
## 1  Proportion of Students              NA
## 2   ¦--STEM                       0.19000
## 3   ¦   ¦--Marijuana: Yes (0.15)  0.00285
## 4   ¦   °--Marijuana: No (0.85)   0.16150
## 5   ¦--Social/Behavioral Sciences 0.32000
## 6   ¦   ¦--Marijuana: Yes (0.4)   0.12800
## 7   ¦   °--Marijuana: No (0.6)    0.19200
## 8   ¦--Arts and humanities        0.28000
## 9   ¦   ¦--Marijuana: Yes (0.65)  0.18200
## 10  ¦   °--Marijuana: No (0.35)   0.09800
## 11  °--Business                   0.21000
## 12      ¦--Marijuana: Yes (0.32)  0.06720
## 13      °--Marijuana: No (0.68)   0.14280

Version 2

# code from class example
GetNodeLabel <- function(node) paste0('Prob\n', node$p)
GetEdgeLabel <- function(node) {
  label = node$name
  return (label)
}

SetEdgeStyle(students.tree, fontname = 'helvetica', label = GetEdgeLabel)

SetNodeStyle(students.tree,
             fontname = 'helvetica',
             label = GetNodeLabel,
             shape = "circle")

SetGraphStyle(students.tree, rankdir = "LR")

plot(students.tree)