Last active
May 9, 2017 11:17
-
-
Save HughParsonage/a04eaec6338bab2dce9a30cb3c042519 to your computer and use it in GitHub Desktop.
HILDA-household-stats-for-hhs-retiree-super-exceeds-1.6-million
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(testthat) | |
library(dplyr) | |
library(dtplyr) | |
library(data.table) | |
library(hildaData) | |
library(hildaExtra) | |
library(sih.hilda.1314) | |
library(taxstats) | |
library(magrittr) | |
write_clipboard <- function(tbl){ | |
write.table(tbl, "clipboard", sep = "\t", row.names = FALSE) | |
} | |
wave14 <- as.data.table(Combined_n140c) | |
enumerate_weights_by_hhid <- | |
wave14 %>% | |
select(xwaveid, | |
id_hh = nhhrhid, | |
wt_enumerated = nhhwte) %>% | |
.[, .(n_persons = sum(wt_enumerated)), keyby = id_hh] | |
enumerate_weight_by_xwaveid <- | |
wave14 %>% | |
select(xwaveid, | |
wt_enumerated = nhhwte) | |
retirees_super_balances <- | |
wave14 %>% | |
select(xwaveid, | |
id_hh = nhhrhid, | |
Age = nhgage, | |
Super_balance_retirees = npwsupri, | |
Super_balance_workers = npwsupwi) %>% | |
mutate(Super_balance = Super_balance_retirees + Super_balance_workers) %>% | |
select(-Super_balance_retirees, -Super_balance_workers) %>% | |
.[(Age >= 65)] %>% | |
# To see households together | |
.[order(id_hh)] | |
retirees_with_balances_over <- function(balance) { | |
retirees_super_balances[Super_balance > balance] %>% | |
enumerate_weight_by_xwaveid[., on = "xwaveid"] | |
} | |
net_wealth_by_idhh <- | |
svy_id_wealthPartition %>% | |
.[survey == "hilda"] %>% | |
.[, .(net_wealth = sum(asset_value)), keyby = id_hh] | |
gross_home_value_by_idhh <- | |
wave14 %>% | |
select(id_hh = nhhrhid, | |
home_value = nhsvalui) %>% | |
# refer only to person | |
setkey(id_hh) %>% | |
unique(by = key(.)) | |
net_home_value_by_idhh <- | |
svy_id_wealthPartition %>% | |
.[survey == "hilda"] %>% | |
.[asset_type == "Owner-occupied housing (net of liabilities)"] %>% | |
select(id_hh, net_home_value = asset_value) %>% | |
setkey(id_hh) | |
# Check of number of persons | |
test_that("Number of persons in HILDA within benchmark (15%) of 2013-14 sample file", { | |
n_HILDA <- | |
retirees_with_balances_over(1.0e6) %>% | |
filter(Age >= 65) %$% | |
sum(wt_enumerated) | |
n_ATO <- | |
sample_file_1314 %>% | |
copy %>% | |
merge(age_range_decoder, by = "age_range") %>% | |
filter(age_range_description >= "65 to 69") %$% | |
sum(MCS_Ttl_Acnt_Bal > 1.0e6) * 50 | |
expect_lte(abs(1 - n_HILDA / n_ATO), 0.15) | |
}) | |
retirees_with_balances_over(1.6e6) %>% | |
setkey(id_hh) %>% | |
net_wealth_by_idhh[.] %>% | |
net_home_value_by_idhh[.] %>% | |
gross_home_value_by_idhh[.] %>% | |
.[, .( | |
avg_home_value = weighted.mean(home_value, wt_enumerated), | |
n_persons = sum(wt_enumerated), | |
avg_net_home_value = weighted.mean(net_home_value, wt_enumerated), | |
avg_hh_net_wealth = weighted.mean(net_wealth, wt_enumerated) | |
)] %>% | |
write_clipboard | |
retirees_with_balances_over(1.3e6) %>% | |
setkey(id_hh) %>% | |
net_wealth_by_idhh[.] %>% | |
net_home_value_by_idhh[.] %>% | |
gross_home_value_by_idhh[.] %>% | |
.[, .( | |
avg_home_value = weighted.mean(home_value, wt_enumerated), | |
n_persons = sum(wt_enumerated), | |
avg_net_home_value = weighted.mean(net_home_value, wt_enumerated), | |
avg_hh_net_wealth = weighted.mean(net_wealth, wt_enumerated) | |
)] %>% | |
write_clipboard | |
retirees_with_balances_over(-Inf) %>% | |
setkey(id_hh) %>% | |
net_wealth_by_idhh[.] %>% | |
net_home_value_by_idhh[.] %>% | |
gross_home_value_by_idhh[.] %>% | |
.[, .( | |
avg_home_value = weighted.mean(home_value, wt_enumerated), | |
n_persons = sum(wt_enumerated), | |
avg_net_home_value = weighted.mean(net_home_value, wt_enumerated), | |
avg_hh_net_wealth = weighted.mean(net_wealth, wt_enumerated) | |
)] %>% | |
write_clipboard | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment