# 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%" ) # Relative Poverty Rates for the Total Population, Children and the Elderly (h-level data) for (i in c(0.4, 0.5, 0.6)) { assign("poverty_line", map(medians , ~ .x * i)) # multiply the median by (0.4, 0.5, or 0.6), thus defining a poverty line data_chld_eldr <- map2( data, poverty_line, ~ .x %>% mutate( line = .y, poor = if_else(dhi < line, 100, 0) ) ) assign(paste0("pvr_totpop_", i), # pvr_totpop_40, pvr_totpop_50, pvr_totpop_60 run_weighted_mean(data_chld_eldr, "poor", "new_wgt")) assign(paste0("pvr_chld_", i), # pvr_chld_40, pvr_chld_50, pvr_chld_60 run_weighted_mean(data_chld_eldr, "poor", "new_chld_wgt")) assign(paste0("pvr_eldr_", i), # pvr_eldr_40, pvr_eldr_50, pvr_eldr_60 run_weighted_mean(data_chld_eldr, "poor", "new_eldr_wgt")) } # Collect them into a list all_stats <- list( poorAll4 = pvr_totpop_0.4, poorAll5 = pvr_totpop_0.5, poorAll6 = pvr_totpop_0.6, poorK4 = pvr_chld_0.4, poorK5 = pvr_chld_0.5, poorK6 = pvr_chld_0.6, poorE4 = pvr_eldr_0.4, poorE5 = pvr_eldr_0.5, poorE6 = pvr_eldr_0.6 ) 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 == "poorAll4" ~ "Relative Poverty Rates - Total Population (40%)", short_ikf == "poorAll5" ~ "Relative Poverty Rates - Total Population (50%)", short_ikf == "poorAll6" ~ "Relative Poverty Rates - Total Population (60%)", short_ikf == "poorK4" ~ "Relative Poverty Rates - Children (40%)", short_ikf == "poorK5" ~ "Relative Poverty Rates - Children (50%)", short_ikf == "poorK6" ~ "Relative Poverty Rates - Children (60%)", short_ikf == "poorE4" ~ "Relative Poverty Rates - Elderly (40%)", short_ikf == "poorE5" ~ "Relative Poverty Rates - Elderly (50%)", short_ikf == "poorE6" ~ "Relative Poverty Rates - Elderly (60%)" ) ) %>% rename(value_ikf_R = value) write.csv(final)