Created
May 25, 2020 14:13
-
-
Save mschnetzer/77fbc33c4c8f5addb40c9abc71a69617 to your computer and use it in GitHub Desktop.
Selfassessment in net wealth distirbution (https://twitter.com/matschnetzer/status/1264836003119927296)
This file contains 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(tidyverse) | |
library(survey) | |
library(msthemes) | |
library(gganimate) | |
library(Hmisc) | |
# Load HFCS and Non-core data | |
load("hfcs2017AT.rda") | |
load("non-core-at.rda") | |
# Calculate real decile and merge with estimated decile | |
data <- hfcs2017hat %>% group_by(im0100) %>% | |
mutate(realdec = as.numeric(cut(dn3001, wtd.quantile(dn3001, weights=hw0010, | |
probs = seq(0,1,0.1), na.rm=T), | |
include.lowest = T, labels = 1:10))) %>% | |
left_join(hnc %>% select(sa0010, im0100, estdec = aha0500)) | |
# Mean of real and estimated decile over the imputations | |
data <- data %>% group_by(sa0010) %>% summarise_at(vars(hw0010, realdec, estdec), list(~mean(.))) %>% mutate_at(vars(realdec,estdec), list(~round(.,0))) | |
# Calculate population share of all movements from one to another decile | |
data <- data %>% mutate(popsh = hw0010/sum(hw0010)*100) %>% | |
group_by(realdec,estdec) %>% | |
summarise(sumpopsh = sum(popsh)) %>% | |
mutate_at(vars(sumpopsh), list(~round(.*10,0))) # Multiply by 10 to get 1000 obs | |
# Replicate the combinations by the weights | |
plotdat <- data %>% uncount(sumpopsh) | |
plotdat <- plotdat %>% ungroup() %>% | |
mutate(id = row_number(), count = 1, color = factor(realdec)) %>% | |
gather(state, decile, estdec:realdec) %>% | |
mutate(state = factor(state, levels = c("realdec","estdec"))) | |
labdf <- tibble(state=c("realdec","estdec"), | |
label=c("Tatsächliche Position","Selbsteinschätzung")) | |
plot <- plotdat %>% ggplot(aes(x = decile, y = count, color = color)) + | |
geom_point(position = position_jitter(width = 0.2, height = 0.2), size=2) + | |
scale_color_viridis_d() + | |
scale_x_continuous(breaks=1:10, minor_breaks = seq(0.5,10.5,1)) + | |
theme_ms() + | |
geom_label(aes(x=10,y=1.25, label=label, hjust=0.8), | |
color=msc_palette[1], data = labdf, size=4) + | |
theme_ms() + | |
theme(legend.position = "none", | |
axis.text.y = element_blank(), | |
axis.title.x = element_text(hjust=0), | |
panel.grid.minor.x = element_line(size=0.3,color="gray"), | |
panel.grid.major = element_blank(), | |
plot.caption = element_text(size=10)) + | |
labs(x=expression("Nettovermögensdezil (arm" %->% "reich)"), y="", | |
title="Reiche Menschen vermuten sich in Mittelschicht", | |
subtitle ="Realität und Selbsteinschätzung bei Vermögen in Österreich", | |
caption="Daten: HFCS 2017, OeNB. Grafik: @matschnetzer") + | |
transition_states(state, transition_length = 15, state_length = 20) | |
anim <- animate(plot, height=4, width=8, nframes = 100, res=150, unit = "in") | |
anim_save("hfcs_2017.gif", anim) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, first, time I read a .r code
Is it the R language for statistics ?