Skip to content

Commit

Permalink
Merge pull request #311 from r-causal/if_else
Browse files Browse the repository at this point in the history
`ifelse()` -> `if_else()`
  • Loading branch information
malcolmbarrett authored Jan 9, 2025
2 parents a9fe112 + 6176497 commit 7a542b1
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 39 deletions.
34 changes: 17 additions & 17 deletions chapters/03-po-counterfactuals.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) |>
Expand Down Expand Up @@ -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),
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"
)
)
Expand Down
14 changes: 7 additions & 7 deletions chapters/05-not-just-a-stats-problem.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
) +
Expand All @@ -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)
) +
Expand All @@ -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)
) +
Expand All @@ -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)
) +
Expand Down Expand Up @@ -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)
) +
Expand All @@ -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)
) +
Expand Down Expand Up @@ -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)
) +
Expand Down
8 changes: 4 additions & 4 deletions chapters/06-stats-models-ci.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)",
Expand Down
2 changes: 1 addition & 1 deletion chapters/07-prep-data.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions chapters/11-outcome-model.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions chapters/15-missingness-and-measurement.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions chapters/16-sensitivity.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
) +
Expand Down

0 comments on commit 7a542b1

Please sign in to comment.