1.Suppose a medical test has a 96% chance of detecting a disease if the person has it and a 92% chance of correctly indicating that the disease is absent if the person really does not have the disease. Assume 12% of a particular population has the disease.

#a. (4 pts) Create a probability tree diagram to show probabilities of outcomes. Indicate on your diagram with labels the following: sensitivity, specificity, false positive, false negative.

library(DiagrammeR)

bayes_probability_tree <- function(prior, true_positive, true_negative) {
  
  if (!all(c(prior, true_positive, true_negative) > 0) && !all(c(prior, true_positive, true_negative) < 1)) {
    stop("probabilities must be greater than 0 and less than 1.",
         call. = FALSE)
  }
  c_prior <- 1 - prior
  c_tp <- 1 - true_positive
  c_tn <- 1 - true_negative
  
  round4 <- purrr::partial(round, digits = 4)
  
  b1 <- round4(prior * true_positive)
  b2 <- round4(prior * c_tp)
  b3 <- round4(c_prior * c_tn)
  b4 <- round4(c_prior * true_negative)
  
  bp <-  round4(b1/(b1 + b3))
  
  labs <- c("X", prior, c_prior, true_positive, c_tp, true_negative, c_tn, b1, b2, b4, b3)
  
  tree <-
    create_graph() %>%
    add_n_nodes(
      n = 11,
      type = "path",
      label = labs,
      node_aes = node_aes(
        shape = "circle",
        height = 1,
        width = 1,
        x = c(0, 3, 3, 6, 6, 6, 6, 8, 8, 8, 8),
        y = c(0, 2, -2, 3, 1, -3, -1, 3, 1, -3, -1))) %>% 
    add_edge(
      from = 1,
      to = 2,
      edge_aes = edge_aes(
        label = "Prior"
      )
    ) %>% 
    add_edge(
      from = 1, 
      to = 3,
      edge_aes = edge_aes(
        label = "Complimentary Prior"
      )
    ) %>% 
    add_edge(
      from = 2,
      to = 4,
      edge_aes = edge_aes(
        label = "True Positive"
      )
    ) %>% 
    add_edge(
      from = 2,
      to = 5,
      edge_aes = edge_aes(
        label = "False Negative"
      )
    ) %>% 
    add_edge(
      from = 3,
      to = 7,
      edge_aes = edge_aes(
        label = "False Positive"
      )
    ) %>% 
    add_edge(
      from = 3,
      to = 6,
      edge_aes = edge_aes(
        label = "True Negative"
      )
    ) %>% 
    add_edge(
      from = 4,
      to = 8,
      edge_aes = edge_aes(
        label = "="
      )
    ) %>% 
    add_edge(
      from = 5,
      to = 9,
      edge_aes = edge_aes(
        label = "="
      )
    ) %>% 
    add_edge(
      from = 7,
      to = 11,
      edge_aes = edge_aes(
        label = "="
      )
    ) %>% 
    add_edge(
      from = 6,
      to = 10,
      edge_aes = edge_aes(
        label = "="
      )
    ) 
  message(glue::glue("The probability of having {prior} after testing positive is {bp}"))
  print(render_graph(tree))
  invisible(tree)
}
bayes_probability_tree(prior = 0.12, true_positive = 0.96, true_negative = 0.92) # this returns the probability tree where incidence 0.12, sensitivity = 0.96, specificity = 0.92
## The probability of having 0.12 after testing positive is 0.6207
knitr::include_graphics("tree_prob.png")

#b. What is the probability that a randomly chosen person will test positive? (Show calculations). this is the sum of Pr Tp + FP

Prrandom_pos <- (0.1152 + 0.0704)
print(Prrandom_pos)
## [1] 0.1856

#interpretation - there is an 18.5% chance a randomly choosen subject will have a + test result.

#c. Suppose that a randomly chosen person does test positive. What is the probability that this person really has the disease? here we want to calculate the Positive Predictive value of the test where PPD = TP/TP+FP (bayes theorem conditinal probabilty)

PPD <- (0.1152/(0.1152+0.0704))
print(PPD)
## [1] 0.6206897

#Interpretation - there is 62.0% a randomly chosen subject who test + has the disease.

2. The prevalence of mild myopia in adults over age 40 is 23% in the U.S. Let random variable Y denote the number with myopia out of a random sample of six.

#Complete the table to the right (3 pts).

getwd()
## [1] "/Users/Darwinbeliever01/Desktop/math 217/week 4/chapter 3 quiz"
knitr::include_graphics("prob_table.png")

#find Pr{Y>=3}

PrYgreaterequal3 <-1 - pbinom(2,6,0.23)
print(PrYgreaterequal3)
## [1] 0.139102

#interpretation : there is 0.139 Pr of greater that 3 out 6 subjects might have mild myopia

#find Pr{Y<=2}

pbinom(2,6,0.23)
## [1] 0.860898

#interpretation : there is 0.861 Pr that at least 2 out 6 subjects might have mild myopia