# 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%" ) # Distribution of Children by Income Group (h-level data) for (i in c(0.5, 0.75, 1.5)) { assign("poverty_line", map(medians , ~ .x * i )) # multiply the median by (0.5, 0.75, or 1.5), thus defining a poverty line data_chld <- map2( data, poverty_line, ~ .x %>% mutate( line = .y, poor = if_else(dhi < line, 100, 0) ) ) assign(paste0("perc_poor_", i), run_weighted_mean(data_chld, "poor", "new_chld_wgt")) } dist_child_75_50 <- map2(perc_poor_0.75, perc_poor_0.5, ~ .x - .y) dist_child_150_75 <- map2(perc_poor_1.5, perc_poor_0.75, ~ .x - .y) dist_child_above_150 <- map(perc_poor_1.5, ~ (100 - .x)) # Collect them into a list all_stats <- list( d5075 = dist_child_75_50, d75150 = dist_child_150_75, d150 = dist_child_above_150 ) 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 == "d5075" ~ "Distribution of Children by Income Group (50 75%)", short_ikf == "d75150" ~ "Distribution of Children by Income Group (75 150%)", short_ikf == "d150" ~ "Distribution of Children by Income Group (above 150%)" ) ) %>% rename(value_ikf_R = value) write.csv(final)