# 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") ) %>% map(~ .x %>% filter(!is.na(dhi)) %>% mutate( new_wgt = hwgt*nhhmem ) ) %>% apply_iqr_top_bottom_coding("dhi", "hwgt", type = "type_4") %>% apply_sqrt_equivalisation("dhi") # 1) Income Inequality Measures gini <- data %>% run_weighted_gini("dhi", "new_wgt") atkinson_ep_half <- data %>% run_weighted_atkinson("dhi", "new_wgt", epsilon = 0.5) atkinson_ep_one <- data %>% run_weighted_atkinson("dhi", "new_wgt", epsilon = 1) ratio_p90_p10 <- data %>% run_weighted_ratios("dhi", "new_wgt", upper_percentile = 0.9, lower_percentile = 0.1, type = "type_4") ratio_p90_p50 <- data %>% run_weighted_ratios("dhi", "new_wgt", upper_percentile = 0.9, lower_percentile = 0.5, type = "type_4") ratio_p80_p20 <- data %>% run_weighted_ratios("dhi", "new_wgt", upper_percentile = 0.8, lower_percentile = 0.2, type = "type_4") # Collect them into a list all_stats <- list( gini = gini, atk5 = atkinson_ep_half, atk1 = atkinson_ep_one, d9010 = ratio_p90_p10, d9050 = ratio_p90_p50, d8020 = ratio_p80_p20 ) 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 == "gini" ~ "Gini Coefficient", short_ikf == "atk5" ~ "Atkinson Coefficient (epsilon=0.5)", short_ikf == "atk1" ~ "Atkinson Coefficient (epsilon=1)", short_ikf == "d9010" ~ "Percentile Ratio (90/10)", short_ikf == "d9050" ~ "Percentile Ratio (90/50)", short_ikf == "d8020" ~ "Percentile Ratio (80/20)" ) ) %>% rename(value_ikf_R = value) write.csv(final)