Created
July 25, 2017 14:30
-
-
Save HughParsonage/3c7d7201c9b56d7dde013db3db99d016 to your computer and use it in GitHub Desktop.
Use HILDA to calculate Gini by household
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(data.table) | |
library(hildaData) | |
library(magrittr) | |
library(grattanCharts) | |
library(ggplot2) | |
#' @source acid package (but it's chock full of unnecessary dependencies) | |
weighted_gini <- function (x, w = NULL) { | |
if (is.null(w)) | |
w <- rep(1, length(x)) | |
x.sort <- sort(x) | |
x.order <- order(x) | |
x <- x.sort | |
n <- length(x) | |
w <- w[x.order]/sum(w) | |
w.c <- cumsum(w) | |
xw.c <- cumsum(w * x) | |
xw.c <- xw.c/xw.c[n] | |
Gini <- t(xw.c[-1]) %*% w.c[-n] - t(xw.c[-n]) %*% w.c[-1] | |
Gini[1, 1] | |
} | |
get_disposable_incomes <- function(year) { | |
wave <- letters[year - 2000] | |
get(paste0("Combined_", wave, "150c")) %>% | |
as.data.table %>% | |
.[, .SD, .SDcols = c(#paste0(wave, "hhrhid"), | |
paste0(wave, "hhwth"), | |
grep("hifdit[pn]", names(.), value = TRUE))] %>% | |
setnames(paste0(wave, "hhwth"), "WEIGHT") %>% | |
setnames(paste0(wave, "hifditp"), "positive_income") %>% | |
setnames(paste0(wave, "hifditn"), "negative_income") %>% | |
make_negatives_NA %>% | |
.[, Disposable_Income := positive_income - negative_income] %>% | |
.[, Year := year] | |
} | |
2001:2015 %>% | |
lapply(get_disposable_incomes) %>% | |
rbindlist %>% | |
.[, .(Gini = weighted_gini(Disposable_Income, WEIGHT)), keyby = Year] %>% | |
grplot(aes(x = Year, y = Gini)) + | |
geom_line() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment