diff --git a/chapters/03-po-counterfactuals.qmd b/chapters/03-po-counterfactuals.qmd index a935f4a..f6c4f38 100644 --- a/chapters/03-po-counterfactuals.qmd +++ b/chapters/03-po-counterfactuals.qmd @@ -209,7 +209,7 @@ data_observed <- data |> # change the exposure to randomized, generated from # a binomial distribution with a probability of 0.5 for # being in either group - exposure = ifelse( + exposure = if_else( rbinom(n(), 1, 0.5) == 1, "chocolate", "vanilla" ), observed_outcome = case_when( @@ -235,8 +235,8 @@ avg_vanilla <- data_observed |> data_observed |> mutate( - y_chocolate = ifelse(exposure == "chocolate", y_chocolate, NA), - y_vanilla = ifelse(exposure == "vanilla", y_vanilla, NA), + y_chocolate = if_else(exposure == "chocolate", y_chocolate, NA), + y_vanilla = if_else(exposure == "vanilla", y_vanilla, NA), causal_effect = NA_real_ ) |> select(-observed_outcome, -exposure) |> @@ -373,12 +373,12 @@ We've exchanged the groups by flipping their assignments, but we can still detec set.seed(11) mix_up <- function(flavor) { - ifelse(flavor == "chocolate", "vanilla", "chocolate") + if_else(flavor == "chocolate", "vanilla", "chocolate") } data_observed <- data |> mutate( - exposure = ifelse( + exposure = if_else( rbinom(n(), 1, 0.5) == 1, "chocolate", "vanilla" ), exposure = mix_up(exposure), @@ -404,13 +404,13 @@ data_observed_exch <- data |> prefer_chocolate = y_chocolate > y_vanilla, exposure = case_when( # people who like chocolate more chose that 80% of the time - prefer_chocolate ~ ifelse( + prefer_chocolate ~ if_else( rbinom(n(), 1, 0.8) == 1, "chocolate", "vanilla" ), # people who like vanilla more chose that 80% of the time - !prefer_chocolate ~ ifelse( + !prefer_chocolate ~ if_else( rbinom(n(), 1, 0.8) == 1, "vanilla", "chocolate" @@ -478,7 +478,7 @@ This is called **conditional exchangeability**: $Y(x) \perp\!\!\!\perp X \mid Z$ #| fig-cap: "The average potential outcomes by observed exposure group in the presence of confounding. We can still achieve *conditional* exchangeability within levels of the confounder. Here, we also start to see the limits of our sample size, as the potential outcomes, which would be valid in higher numbers, start to fail." #| code-fold: true data_observed_exch |> - mutate(prefer_chocolate = ifelse( + mutate(prefer_chocolate = if_else( prefer_chocolate, "prefers\nchocolate", "prefers\nvanilla" @@ -538,12 +538,12 @@ data_observed_pos <- data |> mutate( prefer_chocolate = y_chocolate > y_vanilla, exposure = case_when( - prefer_chocolate ~ ifelse( + prefer_chocolate ~ if_else( rbinom(n(), 1, 0.8) == 1, "chocolate", "vanilla" ), - !prefer_chocolate ~ ifelse( + !prefer_chocolate ~ if_else( rbinom(n(), 1, 0.8) == 1, "vanilla", "chocolate" @@ -573,7 +573,7 @@ In this case, let's say that anyone with an allergy to vanilla who is assigned v set.seed(11) data_observed_struc <- data |> mutate( - exposure = ifelse( + exposure = if_else( rbinom(n(), 1, 0.5) == 1, "chocolate", "vanilla" @@ -586,8 +586,8 @@ data_observed_struc <- data_observed_struc |> # 30% chance of allergy allergy = rbinom(n(), 1, 0.3) == 1, # in which case `y_vanilla` is impossible - exposure = ifelse(allergy, "chocolate", exposure), - y_vanilla = ifelse(allergy, NA, y_vanilla), + exposure = if_else(allergy, "chocolate", exposure), + y_vanilla = if_else(allergy, NA, y_vanilla), observed_outcome = case_when( # those with allergies always take chocolate allergy ~ y_chocolate, @@ -869,10 +869,10 @@ data <- tibble( set.seed(37) data_observed_interf <- data |> mutate( - exposure = ifelse( + exposure = if_else( rbinom(n(), 1, 0.5) == 1, "chocolate", "vanilla" ), - exposure_partner = ifelse( + exposure_partner = if_else( rbinom(n(), 1, 0.5) == 1, "chocolate", "vanilla" ), observed_outcome = case_when( @@ -921,7 +921,7 @@ data_observed_interf |> potential_outcome == "y(vanilla)" & exposure == "vanilla" ~ TRUE, TRUE ~ FALSE ), - flavor_match = ifelse(exposure == exposure_partner, "Same Flavors", "Different Flavors") # Collapse to same/different + flavor_match = if_else(exposure == exposure_partner, "Same Flavors", "Different Flavors") # Collapse to same/different ) |> ggplot(aes(happiness, exposure)) + geom_jitter( @@ -985,7 +985,7 @@ set.seed(11) ## we are now randomizing the *partnerships* not the individuals partners <- tibble( partner_id = 1:5, - exposure = ifelse( + exposure = if_else( rbinom(5, 1, 0.5) == 1, "chocolate", "vanilla" ) ) diff --git a/chapters/05-not-just-a-stats-problem.qmd b/chapters/05-not-just-a-stats-problem.qmd index 0910722..af82fc1 100644 --- a/chapters/05-not-just-a-stats-problem.qmd +++ b/chapters/05-not-just-a-stats-problem.qmd @@ -286,7 +286,7 @@ d_mbias <- dagify( p_coll <- d_coll |> tidy_dagitty() |> - mutate(covariate = ifelse(label == "c", "covariate", NA_character_)) |> + mutate(covariate = if_else(label == "c", "covariate", NA_character_)) |> ggplot( aes(x = x, y = y, xend = xend, yend = yend) ) + @@ -307,7 +307,7 @@ p_coll <- d_coll |> p_conf <- d_conf |> tidy_dagitty() |> - mutate(covariate = ifelse(label == "c", "covariate", NA_character_)) |> + mutate(covariate = if_else(label == "c", "covariate", NA_character_)) |> ggplot( aes(x = x, y = y, xend = xend, yend = yend) ) + @@ -327,7 +327,7 @@ p_conf <- d_conf |> p_med <- d_med |> tidy_dagitty() |> - mutate(covariate = ifelse(label == "c", "covariate", NA_character_)) |> + mutate(covariate = if_else(label == "c", "covariate", NA_character_)) |> ggplot( aes(x = x, y = y, xend = xend, yend = yend) ) + @@ -348,7 +348,7 @@ p_med <- d_med |> p_m_bias <- d_mbias |> tidy_dagitty() |> - mutate(covariate = ifelse(label == "c", "covariate", NA_character_)) |> + mutate(covariate = if_else(label == "c", "covariate", NA_character_)) |> ggplot( aes(x = x, y = y, xend = xend, yend = yend) ) + @@ -460,7 +460,7 @@ d_coll <- dagify( d_coll |> tidy_dagitty() |> - mutate(covariate = ifelse(name == "Z_3", "covariate\n(follow-up)", NA_character_)) |> + mutate(covariate = if_else(name == "Z_3", "covariate\n(follow-up)", NA_character_)) |> ggplot( aes(x = x, y = y, xend = xend, yend = yend) ) + @@ -482,7 +482,7 @@ d_coll |> d_coll |> tidy_dagitty() |> - mutate(covariate = ifelse(name == "Z_2", "covariate\n(baseline)", NA_character_)) |> + mutate(covariate = if_else(name == "Z_2", "covariate\n(baseline)", NA_character_)) |> ggplot( aes(x = x, y = y, xend = xend, yend = yend) ) + @@ -673,7 +673,7 @@ d_conf2 <- dagify( p_conf2 <- d_conf2 |> tidy_dagitty() |> - mutate(covariate = ifelse(name == "Q", "covariate", NA_character_)) |> + mutate(covariate = if_else(name == "Q", "covariate", NA_character_)) |> ggplot( aes(x = x, y = y, xend = xend, yend = yend) ) + diff --git a/chapters/06-stats-models-ci.qmd b/chapters/06-stats-models-ci.qmd index 9c6e975..5a187d2 100644 --- a/chapters/06-stats-models-ci.qmd +++ b/chapters/06-stats-models-ci.qmd @@ -563,7 +563,7 @@ satisfaction_randomized <- tibble( plot_estimates <- function(d) { unadj_model <- lm(satisfaction ~ update_frequency, data = d) |> tidy(conf.int = TRUE) |> - mutate(term = ifelse( + mutate(term = if_else( term == "update_frequencydaily", "update_frequency", term @@ -577,7 +577,7 @@ plot_estimates <- function(d) { data = d ) |> tidy(conf.int = TRUE) |> - mutate(term = ifelse( + mutate(term = if_else( term == "update_frequencydaily", "update_frequency", term @@ -625,8 +625,8 @@ plot_estimates <- function(d) { names_to = "statistic" ) |> mutate( - conf.low = ifelse(statistic == "std.error", NA, conf.low), - conf.high = ifelse(statistic == "std.error", NA, conf.high), + conf.low = if_else(statistic == "std.error", NA, conf.low), + conf.high = if_else(statistic == "std.error", NA, conf.high), statistic = case_match( statistic, "estimate" ~ "estimate (95% CI)", diff --git a/chapters/07-prep-data.qmd b/chapters/07-prep-data.qmd index c3a710e..6da6aa6 100644 --- a/chapters/07-prep-data.qmd +++ b/chapters/07-prep-data.qmd @@ -269,7 +269,7 @@ seven_dwarfs_9 <- seven_dwarfs_train |> wait_minutes_posted_avg, wait_minutes_actual_avg ), - \(.x) ifelse(is.nan(.x), NA, .x) + \(.x) if_else(is.nan(.x), NA, .x) )) |> # outcome definition: # only keep the average wait time between 9 and 10 diff --git a/chapters/11-outcome-model.qmd b/chapters/11-outcome-model.qmd index 37c2b1e..1861d7e 100644 --- a/chapters/11-outcome-model.qmd +++ b/chapters/11-outcome-model.qmd @@ -260,8 +260,8 @@ library(PSW) seven_dwarfs_9 <- seven_dwarfs_9 |> mutate( - park_ticket_season_regular = ifelse(park_ticket_season == "regular", 1, 0), - park_ticket_season_value = ifelse(park_ticket_season == "value", 1, 0) + park_ticket_season_regular = if_else(park_ticket_season == "regular", 1, 0), + park_ticket_season_value = if_else(park_ticket_season == "value", 1, 0) ) psw( data = seven_dwarfs_9, diff --git a/chapters/15-missingness-and-measurement.qmd b/chapters/15-missingness-and-measurement.qmd index 4d5a1f9..38ee2b5 100644 --- a/chapters/15-missingness-and-measurement.qmd +++ b/chapters/15-missingness-and-measurement.qmd @@ -80,7 +80,7 @@ edges_with_aes <- function(..., edge_color = "grey85", shadow = TRUE) { }, geom_dag_edges_link( aes(edge_color = path), - data = \(.x) mutate(.x, path = ifelse(is.na(to), NA, path)) + data = \(.x) mutate(.x, path = if_else(is.na(to), NA, path)) ) ) } @@ -259,7 +259,7 @@ y <- case_when( x == "e" ~ 5 + rnorm(n), ) -x_measured <- ifelse( +x_measured <- if_else( x %in% c("c", "d"), sample(c("c", "d"), size = n, replace = TRUE), x @@ -394,7 +394,7 @@ outcome <- exposure + confounder + rnorm(n) true_model <- lm(outcome ~ exposure * confounder) # mismeasure confounder -confounder <- ifelse( +confounder <- if_else( outcome > 0, confounder, confounder + 10 * rnorm(n) @@ -650,9 +650,9 @@ actual <- coef * posted + rnorm(365, mean = 0, sd = 2) posted_60 <- posted / 60 missing_dag_1 <- rbinom(365, 1, .3) |> as.logical() -missing_dag_2 <- ifelse(posted_60 > .50, rbinom(365, 1, .95), 0) |> +missing_dag_2 <- if_else(posted_60 > .50, rbinom(365, 1, .95), 0) |> as.logical() -missing_dag_3 <- ifelse(actual > 22, rbinom(365, 1, .99), 0) |> +missing_dag_3 <- if_else(actual > 22, rbinom(365, 1, .99), 0) |> as.logical() # the same structure, but it's `posted` that gets the resulting missingness missing_dag_4 <- missing_dag_2 @@ -702,7 +702,7 @@ dag_stats <- bind_rows( dag_stats |> mutate( - true_value = ifelse(dag == "No missingness", "True value", "Observed value"), + true_value = if_else(dag == "No missingness", "True value", "Observed value"), dag = factor(dag, levels = c(paste("DAG", 5:1), "No missingness")), stat = factor( stat, diff --git a/chapters/16-sensitivity.qmd b/chapters/16-sensitivity.qmd index 11bec52..e06c426 100644 --- a/chapters/16-sensitivity.qmd +++ b/chapters/16-sensitivity.qmd @@ -402,7 +402,7 @@ seven_dwarfs_sim2 <- seven_dwarfs_train_2018 |> mutate( u = rnorm(n(), mean = 10, sd = 3), wait_minutes_posted_avg = wait_minutes_posted_avg + u, - park_extra_magic_morning = ifelse( + park_extra_magic_morning = if_else( u > 10, rbinom(1, 1, .1), park_extra_magic_morning @@ -809,7 +809,7 @@ curvatures[5] <- 0.3 emm_wait_dag |> tidy_dagitty() |> node_status() |> - mutate(linetype = ifelse(name == "park_temperature_high", "dashed", "solid")) |> + mutate(linetype = if_else(name == "park_temperature_high", "dashed", "solid")) |> ggplot( aes(x, y, xend = xend, yend = yend, color = status, edge_linetype = linetype) ) +