This is an extension of the tidytuesday assignment you have already done. Complete the questions below, using the screencast you chose for the tidytuesday assigment.
library(tidyverse)
set.seed(2018)
simulations <- crossing(trial = 1:1e5,
weekday = 1:5,
commute = c("Morning", "Evening")) %>%
arrange(trial, weekday, desc(commute)) %>%
mutate(rain = rbinom(n(), 1, ifelse(commute == "Morning", .5, .4)),
home_change = case_when(
commute == "Morning" & rain ~ -1,
commute == "Evening" & rain ~ 1,
TRUE ~ 0),
office_change = -home_change) %>%
group_by(trial) %>%
mutate(home = 2 + cumsum(home_change),
office = 1 + cumsum(office_change))
This data describes the variables which will be used in the graph below. The data shows .5 in the mornings and .4 in the evenings, suggesting that it is more likely to rain in the mornings than in the evenings. It lists the weekdays as 1:5, which represents Monday to Friday, Monday being 1, and so on until Friday is 5. The data also shows the number of trials performed to estimate how frequently it will rain and on which days at which times.
Hint: One graph of your choice.
simulations %>%
summarize(dry = !any(home < 0 | office < 0)) %>%
summarize(dry = mean(dry))
## # A tibble: 1 x 1
## dry
## <dbl>
## 1 0.693
## # A tibble: 1 x 1
## dry
## <dbl>
## 1 0.693
days <- c("Mon", "Tue", "Wed", "Thu", "Fri")
simulations %>%
ungroup() %>%
filter(home < 0 | office < 0) %>%
distinct(trial, .keep_all = TRUE) %>%
count(weekday, commute, sort = TRUE) %>%
mutate(weekday = factor(days[weekday], levels = days),
commute = fct_relevel(commute, "Morning")) %>%
ggplot(aes(weekday, n / 1e5, fill = commute)) +
geom_col(position = "dodge") +
scale_y_continuous(labels = scales::percent_format())
The graph represents each day of the week (between Monday and Friday) that the man commutes from his home to his office in the mornings and from his office back home in the evenings. Each of the bars in the graph represent the probability of the man getting wet from the rain because he does not have an umbrella. The results of the graph show that he is most likely to get rained on on Thursday and Friday mornings, and least likely to get rained on on Friday evenings. The overall probability of him getting wet during the work week is 69.3%