Skip to content

Instantly share code, notes, and snippets.

@HughParsonage
Created February 2, 2016 23:09
Show Gist options
  • Save HughParsonage/016f80abddca218ffddb to your computer and use it in GitHub Desktop.
Save HughParsonage/016f80abddca218ffddb to your computer and use it in GitHub Desktop.
Financial stress by age group, on/off welfare, renting/not renting, from HES2009-10
library(data.table)
library(ggplot2)
library(scales)
library(grattan)
library(readxl)
library(grid)
# The Housing Expenditure Survey (STATA) file
# 2009-10
# requires authority from ABS in order to access.
hes10hh_raw <- read.dta("./HES/hes10bh.dta")
# This is a table containing the names of hes10hh_raw and
# the description of that variable.
hes.sih.vars <- read_excel("./SIH/HES_and_SIH_variable_listing.xlsx")
# See page 21 of HES 2009-10 user guide
# http://www.ausstats.abs.gov.au/ausstats/subscriber.nsf/0/1D03ACBBD40275ADCA257A3900173E85/$File/65030_1.pdf
hes.sih.vars %<>%
mutate(Definition = ifelse(HES_basic_name == "COICEXP", paste0(Definition, "_COICOP"), Definition))
hes.sih.vars %<>%
gather(GROUP, variable, -Definition) %>%
mutate(variable = gsub("[^A-Za-z0-9]", "", variable)) %>%
filter(complete.cases(.)) %>%
spread(GROUP, variable) %>%
mutate(
# replace all nonletters with _ unless they occur at the end of the name,
# in which case delete it
Definition_new = gsub("[^A-Za-z]+(?!$)", "_", Definition, perl = TRUE),
Definition_new = gsub("[^A-Za-z]+$", "", Definition_new)
)
hes10hh.names <-
hes.sih.vars$HES_basic_name[hes.sih.vars$HES_basic_name %in% names(hes10hh_raw)]
hes10hh.newnames <-
hes.sih.vars$Definition_new[hes.sih.vars$HES_basic_name %in% names(hes10hh_raw)]
hes10hh <-
hes10hh_raw %>%
# This omits some weights and PETTAX (hh consumption of fuel tax) for some reason
select_(.dots = hes10hh.names) %>%
data.table %>%
setnames(old = hes10hh.names,
new = hes10hh.newnames)
hes10hh %<>%
mutate(
Age_of_HH_reference_person = factor(gsub(" years",
"",
Age_of_HH_reference_person))
)
disp.inc.percentiles <-
hes10hh %>%
svydesign(~Unique_household_number, data = ., weights = ~Weight_HH_HES) %>%
svyquantile(~Current_weekly_HH_disposable_income # disposable income
,.
,quantiles = c(0:100)/100)
disp.inc.deciles <-
hes10hh %>%
svydesign(~Unique_household_number, data = ., weights = ~Weight_HH_HES) %>%
svyquantile(~Current_weekly_HH_disposable_income
,.
, quantiles = c(0:10)/10)
total.inc.percentiles <-
hes10hh %>%
svydesign(~Unique_household_number, data = ., weights = ~Weight_HH_HES) %>%
svyquantile(~Total_current_weekly_HH_income_from_all_sources
,.
, quantiles = c(0:100)/100)
total.inc.deciles <-
hes10hh %>%
svydesign(~Unique_household_number, data = ., weights = ~Weight_HH_HES) %>%
svyquantile(~Total_current_weekly_HH_income_from_all_sources
,.
, quantiles = c(0:10)/10)
total.inc.quintiles <-
hes10hh %>%
svydesign(~Unique_household_number, data = ., weights = ~Weight_HH_HES) %>%
svyquantile(~Total_current_weekly_HH_income_from_all_sources
,.
#, quantiles = c(0:5)/5)
,quantiles = c(0:5)/5)
hes10hh %<>%
mutate(income.percentile = factor(as.numeric(cut(Current_weekly_HH_employee_income,
breaks = disp.inc.percentiles,
include.lowest = TRUE))))
hes10_indiv_raw <-
read.dta("./HES/hes10bp.dta")
hes10hp.names <-
hes.sih.vars$HES_basic_name[hes.sih.vars$HES_basic_name %in% names(hes10_indiv_raw)]
hes10hp.newnames <-
hes.sih.vars$Definition_new[hes.sih.vars$HES_basic_name %in% names(hes10_indiv_raw)]
hes10_indiv <-
hes10_indiv_raw %>%
data.table %>%
select_(.dots = hes10hp.names) %>%
setnames(old = hes10hp.names,
new = hes10hp.newnames)
total.inc.deciles_indiv <-
hes10_indiv_raw %>%
svydesign(~ABSPID, data = ., weights = ~HESPSWT) %>%
svyquantile(~INCTSCP8 # total weekly income from all sources
,.
, quantiles = c(0:10)/10)
total.inc.quintiles_indiv <-
hes10_indiv_raw %>%
svydesign(~ABSPID, data = ., weights = ~HESPSWT) %>%
svyquantile(~INCTSCP8 # total weekly income from all sources
,.
, quantiles = c(0:5)/5)
# =====
hes10hh %>%
data.table %>%
select(Age_of_HH_reference_person,
Weekly_rent_payments_HH,
Current_weekly_HH_income_from_government_pensions_and_allowances,
Weight_HH_HES) %>%
group_by(over65 = ifelse(Age_of_HH_reference_person %in% c("65 to 69", "70 to 74", "80 and over"),
"65+", "18-64"),
renter = ifelse(Weekly_rent_payments_HH > 0, "Renting", "Not renting"),
has_welfare = ifelse(Current_weekly_HH_income_from_government_pensions_and_allowances > 0,
"Welfare recipient", "Not a welfare recipient")) %>%
summarise(n_households = sum(Weight_HH_HES)) %>%
grplot(aes(x = over65, y = renter)) +
geom_tile(aes(alpha = n_households), fill = Orange) +
geom_text(aes(label = comma(round(n_households, -2))),
size = 23/(14/5)) +
facet_grid(~has_welfare) +
scale_x_discrete(expand = c(0,0)) +
scale_y_discrete(expand = c(0,0)) +
facet_grid(~has_welfare) +
theme(panel.grid.major = element_blank(),
strip.text = element_text(size = 23),
axis.title.x = element_blank(),
strip.background = element_blank(),
plot.title = element_text(hjust = -0.5, face = "bold", size = 20)) +
ggtitle("\n")
grid::grid.text("Number of households\n",
hjust = 0, x = 0, y = 0.99, vjust = 1, gp = gpar(fontsize = 23, fontface = 2))
dev.copy2pdf(file = "Grattan_gathering/Number_of_households.pdf",
width = 11, height = 7)
# =====
hes10hh %>%
data.table %>%
select(Age_of_HH_reference_person,
Weekly_rent_payments_HH,
Current_weekly_HH_income_from_government_pensions_and_allowances,
Weight_HH_HES) %>%
group_by(over65 = ifelse(Age_of_HH_reference_person %in% c("65 to 69", "70 to 74", "80 and over"),
"65+", "18-64"),
renter = ifelse(Weekly_rent_payments_HH > 0, "Renting", "Not renting"),
has_welfare = ifelse(Current_weekly_HH_income_from_government_pensions_and_allowances > 0,
"Welfare recipient", "Not a welfare recipient")) %>%
summarise(n = n()) %>%
grplot(aes(x = over65, y = renter)) +
geom_tile(aes(alpha = n), fill = Orange) +
geom_text(aes(label = n),
size = 23/(14/5)) +
facet_grid(~has_welfare) +
scale_x_discrete(expand = c(0,0)) +
scale_y_discrete(expand = c(0,0)) +
facet_grid(~has_welfare) +
theme(panel.grid.major = element_blank(),
strip.text = element_text(size = 23),
axis.title.x = element_blank(),
strip.background = element_blank(),
plot.title = element_text(hjust = -0.5, face = "bold", size = 20)) +
ggtitle("\n")
grid::grid.text("Number of respondents\n",
hjust = 0, x = 0, y = 0.99, vjust = 1, gp = gpar(fontsize = 23, fontface = 2))
dev.copy2pdf(file = "Grattan_gathering/Number_of_respondents.pdf",
width = 11, height = 7)
# =====
stressy_regex <-
"(Could not pay)|(Spend more money than we get)|(Could not raise \\$2000 within a week)|(Sought financial help)|(Unable to heat home)|(Went without meals)|(Pawned or sold something)"
hes10hh %>%
data.table %>%
select(Age_of_HH_reference_person,
Weekly_rent_payments_HH,
Current_weekly_HH_income_from_government_pensions_and_allowances,
Weight_HH_HES,
Management_of_household_income_HES_only,
Pawned_or_sold_something_due_to_shortage_of_money_HES_only,
Sought_financial_help_from_friends_family_due_to_a_shortage_of_money_HES_only,
Unable_to_heat_home_due_to_shortage_of_money_HES_only,
Went_without_meals_due_to_shortage_of_money_HES_only,
Whether_could_not_pay_gas_electricity_telephone_bill_on_time_due_to_shortage_of_money_HES_only,
Whether_could_not_pay_registration_insurance_on_time_due_to_shortage_of_money_HES_only,
Main_source_of_households_emergency_money_HES_only,
Ability_of_household_to_raise_emergency_money_HES_only) %>%
mutate(tempid = 1:n()) %>%
gather(Financial_stressor, response, Management_of_household_income_HES_only:Ability_of_household_to_raise_emergency_money_HES_only) %>%
mutate(NotInStress = grepl("Not applicable", response),
InStress = grepl(stressy_regex, response),
InStress = ifelse(NotInStress, FALSE, InStress)) %>%
data.table %>%
group_by(tempid) %>%
mutate(n_stressors = sum(InStress)) %>%
ungroup %>%
mutate(max_stresors = max(n_stressors)) %>%
group_by(over65 = ifelse(Age_of_HH_reference_person %in% c("65 to 69", "70 to 74", "80 and over"),
"65+", "18-64"),
renter = ifelse(Weekly_rent_payments_HH > 0, "Renting", "Not renting"),
has_welfare = ifelse(Current_weekly_HH_income_from_government_pensions_and_allowances > 0,
"Welfare recipient", "Not a welfare recipient")) %>%
summarise(prop_stressors = weighted.mean(n_stressors / max_stresors, Weight_HH_HES)) %>%
grplot(aes(x = over65, y = renter)) +
geom_tile(aes(alpha = prop_stressors), fill = Orange) +
geom_text(aes(label = percent(prop_stressors)),
size = 23/(14/5)) +
facet_grid(~has_welfare) +
scale_x_discrete(expand = c(0,0)) +
scale_y_discrete(expand = c(0,0)) +
facet_grid(~has_welfare) +
theme(panel.grid.major = element_blank(),
strip.text = element_text(size = 23),
axis.title.x = element_blank(),
strip.background = element_blank(),
plot.title = element_text(hjust = -0.5, face = "bold", size = 20)) +
ggtitle("\n")
grid::grid.text("Average proportion of financial stress indicators present\n",
hjust = 0, x = 0, y = 0.99, vjust = 1, gp = gpar(fontsize = 23, fontface = 2))
dev.copy2pdf(file = "Grattan_gathering/Average_prop_fin_stress_indicators_present.pdf",
width = 11, height = 7)
# =====
hes10hh %>%
data.table %>%
select(Age_of_HH_reference_person,
Weekly_rent_payments_HH,
Current_weekly_HH_income_from_government_pensions_and_allowances,
Weight_HH_HES,
Management_of_household_income_HES_only,
Pawned_or_sold_something_due_to_shortage_of_money_HES_only,
Sought_financial_help_from_friends_family_due_to_a_shortage_of_money_HES_only,
Unable_to_heat_home_due_to_shortage_of_money_HES_only,
Went_without_meals_due_to_shortage_of_money_HES_only,
Whether_could_not_pay_gas_electricity_telephone_bill_on_time_due_to_shortage_of_money_HES_only,
Whether_could_not_pay_registration_insurance_on_time_due_to_shortage_of_money_HES_only,
Main_source_of_households_emergency_money_HES_only,
Ability_of_household_to_raise_emergency_money_HES_only) %>%
mutate(tempid = 1:n()) %>%
gather(Financial_stressor, response, Management_of_household_income_HES_only:Ability_of_household_to_raise_emergency_money_HES_only) %>%
mutate(NotInStress = grepl("Not applicable", response),
InStress = grepl(stressy_regex, response),
InStress = ifelse(NotInStress, FALSE, InStress)) %>%
data.table %>%
group_by(tempid) %>%
mutate(n_stressors = sum(InStress)) %>%
ungroup %>%
mutate(max_stresors = max(n_stressors)) %>%
group_by(over65 = ifelse(Age_of_HH_reference_person %in% c("65 to 69", "70 to 74", "80 and over"),
"65+", "18-64"),
renter = ifelse(Weekly_rent_payments_HH > 0, "Renting", "Not renting"),
has_welfare = ifelse(Current_weekly_HH_income_from_government_pensions_and_allowances > 0,
"Welfare recipient", "Not a welfare recipient")) %>%
summarise(prop_stressors = weighted.mean(n_stressors >= 2, Weight_HH_HES)) %>%
grplot(aes(x = over65, y = renter)) +
geom_tile(aes(alpha = prop_stressors), fill = Orange) +
geom_text(aes(label = percent(prop_stressors)),
size = 23/(14/5)) +
scale_x_discrete(expand = c(0,0)) +
scale_y_discrete(expand = c(0,0)) +
facet_grid(~has_welfare) +
theme(panel.grid.major = element_blank(),
strip.text = element_text(size = 23),
axis.title.x = element_blank(),
plot.title = element_text(hjust = -0.8, face = "bold", size = 20)) +
ggtitle("\n")
grid::grid.text("Proportion of households with at least 2 financial stressors",
hjust = 0, x = 0, y = 0.99, vjust = 1, gp = gpar(fontsize = 23, fontface = 2))
#dev.copy2pdf(file = "Grattan_gathering/Proportion of households with at least 2 financial stressors.pdf", width = 11, height = 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment