diff --git a/Julie/DataCall.R b/Julie/DataCall.R new file mode 100644 index 0000000..d771ffc --- /dev/null +++ b/Julie/DataCall.R @@ -0,0 +1,36 @@ +library(tidyverse) +library(gridExtra) +library(Hmisc) +library(reldist) + +# nipa_income + +nipa <- read_csv("data-hackaton/nipa_income.csv", col_names = TRUE, ) + + +# Sample A + +sampA <- read_csv("data-hackaton/sample_A.csv", + col_names = TRUE, + col_types = cols( + new_hrs = col_double(), + new_wage = col_double() + )) + +# Sample B + +sampB <- read_csv("data-hackaton/sample_B.csv", + col_names = TRUE, + col_types = cols( + new_hrs = col_double(), + new_wage = col_double() + )) + +# Sample C + +sampC <- read_csv("data-hackaton/sample_C.csv", + col_names = TRUE, + col_types = cols( + new_hrs = col_double(), + new_wage = col_double() + )) \ No newline at end of file diff --git a/Julie/Figure 9.R b/Julie/Figure 9.R new file mode 100644 index 0000000..931c005 --- /dev/null +++ b/Julie/Figure 9.R @@ -0,0 +1,26 @@ +quant = sampC %>% select(year, hh_earnings_equiv, hh_wgt) + +sampgender = function(data){ + + wage1 = data %>% + filter(!is.na(hh_earnings_equiv)) %>% + group_by(year) %>% + summarise(wage = mean(hh_earnings_equiv)) %>% + + + B = wage2 %>% summarise(bias = mean(bias)) + b = B$bias + + final1 = wage1 %>% + mutate(wage1 = wage - b) %>% + select(year, wage1) + + final2 = wage2 %>% + select(year, new_wage) %>% + filter(year>1975) + + data = bind_rows(final1, final2) %>% + mutate(wage = ifelse(year<1976, wage1, new_wage)) + + return(data) +} diff --git a/Julie/Figure1.R b/Julie/Figure1.R new file mode 100644 index 0000000..21b91dd --- /dev/null +++ b/Julie/Figure1.R @@ -0,0 +1,35 @@ +inc = sampA %>% select(year, labor_income, pre_tax_income, cpi, wgt) %>% + mutate(true_year = year - 1, + real_income = labor_income/cpi * 100, + real_pre_tax = pre_tax_income/cpi * 100) %>% + group_by(true_year) %>% + summarise(real_income = log(weighted.mean(real_income, wgt)), + real_pre_tax = log(weighted.mean(real_pre_tax, wgt)), + cpi = mean(cpi)) %>% + mutate(type = "CPS") %>% + select(true_year, real_income, real_pre_tax, type) + +nipa = nipa %>% + mutate(real_income = log(salary_nipa), + real_pre_tax = log(total_income_nipa), + type = "NIPA") %>% + select(true_year, real_income, real_pre_tax, type) + +g1 = ggplot(data = nipa, aes(x = true_year, y = real_income)) + + geom_point(color = "red") + + geom_line(data = inc, color = "blue") + + theme_bw() + + labs(title = "Labor Income Per Capita", + x = "Year", + y = "") + +g2 = ggplot(data = nipa, aes(x = true_year, y = real_pre_tax)) + + geom_point(color = "red") + + geom_line(data = inc, color = "blue", ) + + theme_bw() + + labs(title = "Pre-Tax Income Per Capita", + x = "Year", + y = "") + +grid.arrange(g1, g2, ncol = 2) + diff --git a/Julie/Figure2.R b/Julie/Figure2.R new file mode 100644 index 0000000..84fe880 --- /dev/null +++ b/Julie/Figure2.R @@ -0,0 +1,115 @@ +sampgender = function(g){ + +data2 = sampA %>% filter(between(age, 25, 60)) %>% + select(sex, year, wage, new_wage) + +wage1 = data2 %>% + select(year, wage, new_wage, sex) %>% + filter(!is.na(wage), year < 1976) %>% + filter(sex == g) %>% + group_by(year) %>% + summarise(wage = mean(wage)) %>% + mutate(new_wage = NA) + +wage2 = data2 %>% + select(year, wage, new_wage, sex) %>% + filter(!is.na(wage), year > 1975) %>% + filter(!is.na(new_wage)) %>% + filter(sex == g) %>% + group_by(year) %>% + summarise(wage = mean(wage), + new_wage = mean(new_wage)) %>% + mutate(bias = wage - new_wage) + +B = wage2 %>% summarise(bias = mean(bias)) +b = B$bias + +final1 = wage1 %>% + mutate(wage1 = wage - b) %>% + select(year, wage1) + +final2 = wage2 %>% + select(year, new_wage) %>% + filter(year>1975) + +data = bind_rows(final1, final2) %>% + mutate(wage = ifelse(year<1976, wage1, new_wage)) + +return(data) +} + +wagem = sampgender(1) +wagef = sampgender(2) + +wm = ggplot(wagem, aes(x = year, y = wage)) + geom_line() + + labs(title = "Average Male Wage", + x = "Year", + y = "") + +wf = ggplot(wagef, aes(x = year, y = wage)) + geom_line() + + labs(title = "Average Female Wage", + x = "Year", + y = "") + +sampgender2 = function(g){ + + data2 = sampA %>% filter(between(age, 25, 60)) %>% + select(sex, year, hrs, new_hrs) + + hrs1 = data2 %>% + select(year, hrs, new_hrs, sex) %>% + filter(!is.na(hrs), year < 1976) %>% + filter(sex == g) %>% + group_by(year) %>% + summarise(hrs = mean(hrs)) %>% + mutate(new_hrs = NA) + + hrs2 = data2 %>% + select(year, hrs, new_hrs, sex) %>% + filter(!is.na(hrs), year > 1975) %>% + filter(!is.na(new_hrs)) %>% + filter(sex == g) %>% + group_by(year) %>% + summarise(hrs = mean(hrs), + new_hrs = mean(new_hrs)) %>% + mutate(bias = hrs - new_hrs) + + B = hrs2 %>% summarise(bias = mean(bias)) + b = B$bias + + final1 = hrs1 %>% + mutate(hrs1 = hrs - b) %>% + select(year, hrs1) + + final2 = hrs2 %>% + select(year, new_hrs) %>% + filter(year>1975) + + data = bind_rows(final1, final2) %>% + mutate(hrs = ifelse(year<1976, hrs1, new_hrs)) + + return(data) +} + +hours1 = sampgender2(1) +hours2 = sampgender2(2) + +hm = ggplot(hours1, aes(x = year, y = hrs)) + geom_line() + + labs(title = "Average Male Annual hours", + x = "Year", + y = "") + +hf = ggplot(hours2, aes(x = year, y = hrs)) + geom_line() + + labs(title = "Average Female Annual hours", + x = "Year", + y = "") + +grid.arrange(wm, wf, hm, hf, ncol = 2) + + + + + + + + \ No newline at end of file diff --git a/Julie/Figure4.R b/Julie/Figure4.R new file mode 100644 index 0000000..4bfaedc --- /dev/null +++ b/Julie/Figure4.R @@ -0,0 +1,163 @@ +data3 = sampA %>% select(year, wage, new_wage, sex, wgt) + +variance = function(data, g){ + + wage1 = data %>% + filter(!is.na(wage), year < 1976) %>% + filter(sex == g) %>% + group_by(year) %>% + mutate(log = log(wage)) %>% + dplyr::summarise(wage = wtd.var(log, wgt)) %>% + mutate(new_wage = NA) + + wage2 = data %>% + filter(!is.na(wage), year > 1975) %>% + filter(!is.na(new_wage)) %>% + filter(sex == g) %>% + group_by(year) %>% + mutate(log = log(wage), + logn = log(new_wage)) %>% + dplyr::summarise(wage = wtd.var(log, wgt), + new_wage = wtd.var(logn, wgt)) %>% + mutate(bias = wage - new_wage) + + B = wage2 %>% dplyr::summarise(bias = mean(bias)) + b = B$bias + + final1 = wage1 %>% + mutate(wage1 = wage - b) %>% + select(year, wage1) + + final2 = wage2 %>% + select(year, new_wage) %>% + filter(year>1975) + + data = bind_rows(final1, final2) %>% + mutate(wage = ifelse(year<1976, wage1, new_wage)) + + return(data) +} + +var1 = variance(data3, 1) %>% mutate(gndr = "Male") +var2 = variance(data3, 2) %>% mutate(gndr = "Female") +var = bind_rows(var1, var2) + +gvar = ggplot(var, aes(x = year, y = wage)) + + geom_line(aes(linetype = gndr, colour = gndr)) + + labs(title = "Variance of log hourly wages", + x = "Year", + y = "") + +quant = function(data, g, q, t){ + + wage1 = data %>% + filter(!is.na(wage), year < 1976) %>% + filter(sex == g) %>% + group_by(year) %>% + dplyr::summarise(wage50 = wtd.quantile(wage, probs = c(q)), + wage10 = wtd.quantile(wage, probs = c(t))) %>% + mutate(wage = wage50/wage10, + w_wage = NA) + + wage2 = data %>% + filter(!is.na(wage), year > 1975) %>% + filter(!is.na(new_wage)) %>% + filter(sex == g) %>% + group_by(year) %>% + dplyr::summarise(wage50 = wtd.quantile(wage, wgt, probs = c(q)), + wage10 = wtd.quantile(wage, wgt, probs = c(t)), + nwage50 = wtd.quantile(new_wage, wgt, probs = c(q)), + nwage10 = wtd.quantile(new_wage, wgt, probs = c(t))) %>% + mutate(wage = wage50/wage10, + nwage = wage50/wage10, + bias = wage-nwage) + + B = wage2 %>% dplyr::summarise(bias = mean(bias)) + b = B$bias + + final1 = wage1 %>% + mutate(wage1 = wage - b) %>% + select(year, wage1) + + final2 = wage2 %>% + select(year, nwage) %>% + filter(year>1975) + + data = bind_rows(final1, final2) %>% + mutate(wage = ifelse(year<1976, wage1, nwage)) + + return(data) +} + +quant1 = quant(data3, 1, 0.5, 0.1) %>% mutate(gndr = "Male") +quant2 = quant(data3, 2 ,0.5, 0.1) %>% mutate(gndr = "Female") +quant = bind_rows(quant1, quant2) + +gquant1 = ggplot(quant, aes(x = year, y = wage)) + + geom_line(aes(linetype = gndr, colour = gndr)) + + labs(title = "P50 - P10 ratio of hourly wages", + x = "Year", + y = "") + +quant11 = quant(data3, 1, 0.9, 0.5) %>% mutate(gndr = "Male") +quant21 = quant(data3, 2 ,0.9, 0.5) %>% mutate(gndr = "Female") +QUant = bind_rows(quant11, quant21) + +gquant2 = ggplot(QUant, aes(x = year, y = wage)) + + geom_line(aes(linetype = gndr, colour = gndr)) + + labs(title = "P90 - 510 ratio of hourly wages", + x = "Year", + y = "") + + +gini = function(data, g){ + + wage1 = data %>% + filter(!is.na(wage), year < 1976) %>% + filter(sex == g) %>% + group_by(year) %>% + dplyr::summarise(wage = reldist::gini(wage, wgt)) %>% + mutate(new_wage = NA) + + wage2 = data %>% + filter(!is.na(wage), year > 1975) %>% + filter(!is.na(new_wage)) %>% + filter(sex == g) %>% + group_by(year) %>% + dplyr::summarise(wage = reldist::gini(wage, wgt), + new_wage = reldist::gini(new_wage, wgt)) %>% + mutate(bias = wage - new_wage) + + B = wage2 %>% dplyr::summarise(bias = mean(bias)) + b = B$bias + + final1 = wage1 %>% + mutate(wage1 = wage - b) %>% + select(year, wage1) + + final2 = wage2 %>% + select(year, new_wage) %>% + filter(year>1975) + + data = bind_rows(final1, final2) %>% + mutate(wage = ifelse(year<1976, wage1, new_wage)) + + return(data) +} + +gini1 = gini(data3, 1) %>% mutate(gndr = "Male") +gini2 = gini(data3, 2) %>% mutate(gndr = "Female") +gini = bind_rows(gini1, gini2) + +gini = ggplot(gini, aes(x = year, y = wage)) + + geom_line(aes(linetype = gndr, colour = gndr)) + + labs(title = "Gini Coefficient of hourly wages", + x = "Year", + y = "") + +grid.arrange(gvar, gini,gquant1, gquant2, ncol = 2) + + + + + diff --git a/Julie/Table2.R b/Julie/Table2.R new file mode 100644 index 0000000..a89b9fa --- /dev/null +++ b/Julie/Table2.R @@ -0,0 +1,53 @@ + +sampB = sampB %>% mutate(yind = "PROB") + + + +sampB$yind = ifelse(between(sampB$year, 1968, 1980), "1967-1979", + ifelse(between(sampB$year, 1981, 1990), "1980-1989", + ifelse(between(sampB$year, 1991, 2000), "1990-1999", + ifelse(sampB$year > 2000, "from 2000", sampB$yind)))) + +age = sampB %>% group_by(yind, sex) %>% + summarise(av = weighted.mean(age,wgt)) %>% + spread(key = yind, value = av) %>% + mutate(name = c("Avg male age", "Avg female age")) %>% + select(-sex) + +edu16m = sampB %>% select(yind, edu, sex) %>% + filter(sex == 1 & edu >= 16) %>% + group_by(yind) %>% + summarise(n = n()) %>% + mutate(eff = c(nrow(filter(sampB, yind == "1967-1979" & sex == 1)), + nrow(filter(sampB, yind == "1980-1989" & sex == 1)), + nrow(filter(sampB, yind == "1990-1999" & sex == 1)), + nrow(filter(sampB, yind == "from 2000" & sex == 1)))) %>% + mutate(edu16 = n * 100 / eff) %>% + select(yind, edu16) %>% + spread(key = yind, value = edu16) %>% + mutate(name = c("% male > 16 years edu")) + +edu16f = sampB %>% select(yind, edu, sex) %>% + filter(sex == 2 & edu >= 16) %>% + group_by(yind) %>% + summarise(n = n()) %>% + mutate(eff = c(nrow(filter(sampB, yind == "1967-1979" & sex == 2)), + nrow(filter(sampB, yind == "1980-1989" & sex == 2)), + nrow(filter(sampB, yind == "1990-1999" & sex == 2)), + nrow(filter(sampB, yind == "from 2000" & sex == 2)))) %>% + mutate(edu16 = n * 100 / eff) %>% + select(yind, edu16) %>% + spread(key = yind, value = edu16) %>% + mutate(name = c("% female > 16 years edu")) + +table2 = bind_rows(age, edu16m, edu16f) %>% select(name,"1967-1979", "1980-1989", + "1990-1999", "from 2000") +table2 + + + + + + + +