## Package and options library("LorenzRegression") library("ineq") options(prompt = "R> ", continue = "+ ", width = 70, useFancyQuotes = FALSE, digits = 3) ## Colorblind-friendly Okabe-Ito palette cbf_palette <- palette.colors() ## For saving plots: require("grDevices") # dir.create("Figures") ## Data data("Ilocos", package = "ineq") # table(Ilocos$sex) # min(Ilocos$family.size) # max(Ilocos$family.size) # round(mean(Ilocos$family.size), 2) # table(Ilocos$urbanity) # table(Ilocos$province) # 0. Inequality measurement ---- Gi <- Gini.coef(Ilocos$income) Gi CI.mean <- Gini.coef(Ilocos$income, Ilocos$family.size) CI.mean # pdf(file = "Figures/CI_boxplot.pdf", width = 4.5, height = 4) par(mar = c(1,3,1,1)) CI.random <- sapply(1:1000, function(i) Gini.coef(Ilocos$income, Ilocos$family.size, ties.method = "random", seed = i)) boxplot(CI.random) abline(h = CI.mean) # dev.off() LC <- Lorenz.curve(Ilocos$income) p <- seq(0, 1, length.out = 5) LC(p) # pdf(file = "Figures/Lorenz_curve.pdf", width = 5.5, height = 4) Lorenz.graphs(income ~ 1, data = Ilocos) # dev.off() # pdf(file = "Figures/Lorenz_curve_diff.pdf", width = 5.5, height = 4) Lorenz.graphs(income ~ 1, data = Ilocos, difference = TRUE) # dev.off() # pdf(file = "Figures/Lorenz_graphs.pdf", width = 5.5, height = 4) Lorenz.graphs(income ~ family.size, data = Ilocos, palette = cbf_palette) # dev.off() # 1. Non-penalized Lorenz regression ---- # 1.1. NPLR > Lorenz.Reg() ---- LR0 <- Lorenz.Reg(income ~ family.size, data = Ilocos, ties.Gini = "mean") coef(LR0) ineqExplained(LR0) LR <- Lorenz.Reg(income ~ sex*family.size + sex*urbanity + province, data = Ilocos, ties.Gini = "mean", seed.GA = 123, seed.random = 456) coef(LR) ineqExplained(LR, type = "Gini.explained") ineqExplained(LR, type = "Lorenz-R2") summary(LR) pred_data <- data.frame( "sex" = rep(c("male","female"), 4), "family.size" = rep(c(2,6,2,6), each = 2), "urbanity" = rep(c("urban","rural"), each = 4), "province" = rep("Pangasinan", 8) ) pred_data$rank_pred <- rank(predict(LR, newdata = pred_data)) pred_data <- pred_data[order(pred_data$rank_pred),] pred_data pred_data$pred_loess <- predict(LR, newdata = pred_data, type = "response", span = 0.25) smooth_spline_method <- list( fit_fun = function(y, x, ...) { smooth.spline(x = x, y = y, ...) }, predict_fun = function(fit, newdata) { predict(fit, x = newdata[,1])$y } ) pred_data$pred_spline <- predict(LR, newdata = pred_data, type = "response", method = smooth_spline_method) pred_data # pdf(file = "Figures/Lorenz_graphs2.pdf", width = 5.5, height = 4) Ilocos$index_LR <- fitted(LR) Lorenz.graphs(income ~ family.size + index_LR, Ilocos, palette = cbf_palette) # dev.off() # pdf(file = "Figures/Lorenz_residuals.pdf", width = 5.5, height = 4) plot(LR, type = "residuals", method = smooth_spline_method) # dev.off() # 1.2. NPLR > Lorenz.boot() ---- RNGkind(kind = "L'Ecuyer-CMRG") time1 <- proc.time() set.seed(123) LR_boot <- Lorenz.boot(LR, R = 500, store_LC = TRUE, parallel = "multicore", ncpus = 6) time2 <- proc.time() time2 - time1 # R> time2 - time1 # user system elapsed # 809 116 226 summary(LR_boot) confint(LR_boot) confint(LR_boot, parm = "theta", level = 0.9, type = "perc") # pdf(file = "Figures/Lorenz_graphs3.pdf", width = 5.5, height = 4) plot(LR_boot, palette = cbf_palette) # dev.off() # 2. Penalized Lorenz regression ---- # 2.1. PLR > Lorenz.Reg() ---- PLR <- Lorenz.Reg(income ~ sex*family.size + sex*urbanity + province, data = Ilocos, penalty = "SCAD", grid.arg = "h", grid.value = c(0.5,1,2)*nrow(Ilocos)^(-1/5.5)) coef.mat <- data.frame("NPLR" = coef(LR), "PLR_BIC" = coef(PLR)) coef.mat ineqExplained(LR) - ineqExplained(PLR) # 2.2. PLR > Lorenz.boot() ---- time3 <- proc.time() set.seed(123) PLR_boot <- Lorenz.boot(PLR, R = 500, parallel = "multicore", ncpus = 6) time4 <- proc.time() time4 - time3 # R> time4 - time3 # user system elapsed # 1226.7 38.4 227.8 coef.mat$PLR_Boot <- coef(PLR_boot, pars.idx = "Boot") coef.mat ineqExplained(PLR_boot, pars.idx = "Boot") - ineqExplained(PLR) confint(PLR_boot) confint(PLR_boot, pars.idx = "Boot") # 2.3. PLR > PLR.CV ---- PLR_boot_CV <- PLR.CV(PLR_boot, k = 5, seed.CV = 123) summary(PLR_boot_CV) # pdf(file = "Figures/Lorenz_graphs4.pdf", width = 5.5, height = 4) plot(PLR_boot_CV, pars.idx = "Boot", palette = cbf_palette) # dev.off() Ilocos$index_PLR_BIC <- fitted(PLR_boot_CV, pars.idx = "BIC") Ilocos$index_PLR_Boot <- fitted(PLR_boot_CV, pars.idx = "Boot") Ilocos$index_PLR_CV <- fitted(PLR_boot_CV, pars.idx = "CV") # pdf(file = "Figures/Lorenz_graphs5.pdf", width = 5.5, height = 4) Lorenz.graphs(income ~ family.size + index_LR + index_PLR_BIC + index_PLR_Boot + index_PLR_CV, Ilocos, difference = TRUE, palette = cbf_palette) # dev.off() pred_data2 <- pred_data[,1:4] pred_data2$LR <- predict(LR, newdata = pred_data2, type = "response") pred_data2$PLR_BIC <- predict(PLR_boot_CV, newdata = pred_data2, pars.idx = "BIC", type = "response") pred_data2$PLR_Boot <- predict(PLR_boot_CV, newdata = pred_data2, pars.idx = "Boot", type = "response") pred_data2$PLR_CV <- predict(PLR_boot_CV, newdata = pred_data2, pars.idx = "CV", type = "response") pred_data2 # pdf(file = "Figures/Lorenz_residuals2.pdf", width = 5.5, height = 4) plot(PLR_boot_CV, type = "residuals", pars.idx = "Boot") # dev.off() # 2.4. PLR > diagnostics ---- # pdf(file = "Figures/Lorenz_diag.pdf", width = 6.5, height = 4) plot(PLR_boot_CV, type = "diagnostic", palette = cbf_palette) # dev.off() diagnostic.PLR(PLR_boot_CV, tol = 0.99, method = "BIC")$best diagnostic.PLR(PLR_boot_CV, tol = 0.95, method = "intersect")$best confint(PLR_boot_CV, pars.idx = c(3,61))