Last active
October 13, 2016 10:49
-
-
Save AngelBerihuete/fdb11a7dc3ece81bcf5d6261a49af440 to your computer and use it in GitHub Desktop.
R function to calculate elasticity of the dual Lorenz curve
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
EmpiricalDualElasticity <- function(dataset){ | |
# ########################################################################### | |
# R function to calculate elasticity of the dual Lorenz curve | |
# --------------------------------------------------------- | |
# | |
# Authors: Berihuete, A.; Ramos, C.D; Sordo, M.A. and Ramos, H. | |
# | |
# input: a dataset formatted with rtip package (https://cran.r-project.org/web/packages/rtip/) | |
# | |
# output: abscissas and ordinates of the elasticiy of the dual Lorenz curve. | |
# | |
# example: Estonia (EE). You will need EU-SILC 2010 csv files. | |
# | |
# library(rtip) | |
# dataset10 <- loadEUSILC("UDB_c10D.csv", "UDB_c10H.csv") | |
# dataEE <- setupDataset(dataset = dataset10, country = "EE") | |
# delas <- EmpiricalDualElasticity(dataEE) | |
# plot(delas) | |
# | |
############################################################################# | |
dataset_aux <- dataset[order(dataset[,'ipuc']),] | |
dataset_aux <- subset(dataset_aux, ipuc > 0) | |
dataset_aux$cum1_w <- cum1_w <- cumsum(dataset_aux$wHX040) | |
dataset_aux$p <- dataset_aux$cum1_w/dataset_aux$cum1_w[length(dataset_aux$cum1_w)] | |
dataset_aux$invp <- 1-dataset_aux$p | |
dataset_aux <- dataset_aux[order(dataset_aux[,'invp']),] | |
dataset_aux$cum2_w <- cumsum(dataset_aux$wHX040) | |
dataset_aux$cum3_w <- cumsum(dataset_aux$wHX040*dataset_aux$ipuc) | |
delas <- c() | |
for(i in 1:(dim(dataset_aux)[1]-1)){ | |
index <- which(dataset_aux$cum2_w > cum1_w[i])[1] | |
if(index == 1){ | |
delas[i] <- 1 | |
}else{ | |
num.delas <- dataset_aux$ipuc[index] | |
dem.delas <- (dataset_aux$cum3_w[index-1] + dataset_aux$ipuc[index]*(cum1_w[i]-dataset_aux$cum2_w[index-1]))/cum1_w[i] | |
delas[i] <- num.delas/dem.delas | |
} | |
} | |
p <- cum1_w/cum1_w[length(cum1_w)] | |
p <- p[-length(p)] | |
results <- data.frame(p = p, delas = delas) | |
return(results) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment