##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. #(a) 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? # (b) 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?
install.packages("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.1, true_positive = 0.98, true_negative = 0.99) # this returns the probability tree where incidence preg 0.01, sensitivity = 0.98, specificity = 0.99
The probability of having (prior) after testing positive is 0.9159
bayes_probability_tree(prior = 0.05, true_positive = 0.98, true_negative = 0.99) # this returns the probability tree where incidence preg = 0.05, sensitivity = 0.98, specificity = 0.99
The probability of having (prior) after testing positive is 0.8376
##this is the sum of the T+ and F+ = 0.0098 + 0.009
randompositive <- 0.098 + 0.009 # returns chance for a) that a woman chosen randomly from this grp will test positive
print(randompositive)
[1] 0.107
##this is the sum of the T+ and F+= 0.049 + 0.0095
randompositiveb <- 0.049 + 0.0095 # returns chance for a) that a woman chosen randomly from this grp will test positive
print(randompositiveb)
[1] 0.0585