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)
}Probability Tree Diagram Chapter 3
Bayes Theorem Problem:
If a woman takes an early pregnancy test, she will either test positive, meaning that the test says she is pregnant or test negative, meaning that the test says she is not pregnant. Suppose that if a woman really is pregnant, there is a 98% chance that she will test positive. Also, suppose that if a woman really is not pregnant, there is a 99% chance that she will test negative.
Suppose that 1,000 women take early pregnancy tests and that 100 of them really are pregnant. What is the probability that a randomly chosen woman from this group will test positive?
Suppose that 1,000 women take early pregnancy tests and that 50 of them really are pregnant. What is the probability that a randomly chosen woman from this group will test positive?
here are the knowns facts: sensitivity = 0.96, specificity = .92 for preg test
in question a) incidence = 12/100 = 0.12, in ? b) incidence = 50/1000 = 0.05
will use script from https://daranzolin.github.io/2018-01-07-probability-trees/
bayes_probability_tree(prior = 0.12, true_positive = 0.96, true_negative = 0.92) # this returns the probability tree where incidence of disease = .12, sensitivity = 0.96, specificity = 0.92The probability of having 0.12 after testing positive is 0.6207
bayes_probability_tree(prior = 0.12, true_positive = 0.96, true_negative = 0.92) # this returns the probability tree where incidence preg = 0.05, sensitivity = 0.98, specificity = 0.99The probability of having 0.12 after testing positive is 0.6207
now calculate the P for a) that a randomly chosen woman from this group will test positive?
##this is the sum of the T+ and F+ = 0.0098 + 0.980
randompositive <- 0.0098 + 0.980 # returns chance for a) that a woman chosen randomly from this grp will test positive
print(randompositive)[1] 0.9898
now calculate the P for b) that a randomly chosen woman from this group will test positive?
##this is the sum of the T+ and F+= 0.049 + 0.9405
randompositiveb <- 0.049 + 0.9405 # returns chance for a) that a woman chosen randomly from this grp will test positive
print(randompositiveb)[1] 0.9895