# Inequality Key Figures - replication with R and `lissyrtools` library(lissyrtools) library(tidyverse) # 0) Load Data and Prepare it data <- lissyuse( data = "lu", # Examples: `data = c("de", "it")`; `data = c("it20", "it16")`; Run: get_countries_lis() to check other iso2 codes, or get_years_lis(c("it", "es")) to check which years are available. vars = c("dhi", "nhhmem65", "nhhmem17","sex", "relation", "hpartner") ) %>% map(~ .x %>% filter(!is.na(dhi)) %>% filter(relation == 1000) %>% mutate( new_wgt = hwgt*nhhmem, new_chld_wgt = hwgt * nhhmem17, new_eldr_wgt = hwgt * nhhmem65 ) ) %>% apply_iqr_top_bottom_coding("dhi", "hwgt", type = "type_4") %>% apply_sqrt_equivalisation("dhi") medians <- map( run_weighted_percentiles(data, "dhi", "new_wgt", type = "type_4"), "50%" ) # 4) Poverty Rates for Children by Family Type (p-level originally as we need `sex` and `relation`) poverty_line <- map(medians , ~ .x * 0.5) data_with_pov_line <- map2( data, poverty_line, ~ .x %>% mutate( line = .y, poor = if_else(dhi < line, 100, 0) ) ) # Two-parent families (50%) chld_pov_rate_2_parent <- data_with_pov_line %>% map( ~ .x %>% filter(hpartner==1 & nhhmem17>0 & nhhmem>2) ) %>% run_weighted_mean("poor", "new_chld_wgt") # Single-mother families (50%) chld_pov_rate_sing_mum <- data_with_pov_line %>% map( ~ .x %>% filter(hpartner==0 & sex==2 & nhhmem17>0) ) %>% run_weighted_mean("poor", "new_chld_wgt") # Percentage living in Single-mother households chld_percent_sing_mum <- data_with_pov_line %>% map( ~ .x %>% mutate(kidsm = if_else(sex==2 & hpartner==0 & nhhmem17>0, 100, 0, missing = 0)) ) %>% run_weighted_mean("kidsm", "new_chld_wgt") # Collect them into a list all_stats <- list( poortp = chld_pov_rate_2_parent, poorsm = chld_pov_rate_sing_mum, pkidsm = chld_percent_sing_mum ) results <- map(all_stats, ~ structure_to_plot(.x, print_columns = FALSE)) final <- imap(results , ~ .x %>% mutate(short_ikf = .y)) %>% bind_rows() %>% arrange(short_ikf, dname) %>% mutate( indicator = case_when( short_ikf == "poortp" ~ "Children Poverty Rates - Two-Parent Families (50%)", short_ikf == "poorsm" ~ "Children Poverty Rates - Single-Mother Families (50%)", short_ikf == "pkidsm" ~ "% Children Living in Single-Mother Families" ) ) %>% rename(value_ikf_R = value) write.csv(final)