Skip to content

Instantly share code, notes, and snippets.

@rubenarslan
Created May 30, 2022 09:21
Show Gist options
  • Save rubenarslan/34a114c1ac2f19b5decbf37fc67a35a5 to your computer and use it in GitHub Desktop.
Save rubenarslan/34a114c1ac2f19b5decbf37fc67a35a5 to your computer and use it in GitHub Desktop.
n <- 1000
people <- tibble(
children = rpois(n, 1.4),
# for childless people, sat with childcare is not applicable
sat_with_child_care = if_else(children > 0, rnorm(n), NA_real_),
sat_with_housing = rnorm(n),
happiness = rnorm(n) + sat_with_housing +
if_else(children > 0, sat_with_child_care, 0),
)
# I can kick out the childless subsample
summary(lm(happiness ~ sat_with_housing + sat_with_child_care, data = people))
people %>%
# I can discretize it and add dummies, but I lose information this way
mutate(sat_with_child_care = factor(case_when(
children == 0 ~ "none",
sat_with_child_care < -1 ~ "low",
sat_with_child_care > 1 ~ "high",
TRUE ~ "medium"), levels = c("none", "low", "medium", "high"))) %>%
lm(happiness ~ sat_with_housing + sat_with_child_care, data = .) %>%
summary()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment