wNtile <- function(df,var,weight,split) { df <- df[order(df[paste(var)]),] df$cumwt <- cumsum(df[,paste(weight)])/sum(df[,paste(weight)]) res <- df[,paste(var)][Find(function(y) df$cumwt[y]>split, seq_along(df$cumwt))] return(res) } percPop <- function(df,weight="wt",maxline=0.5,minline=0) { res <- 100 * (sum((df$ey< maxline*wNtile(df,"ey","wt",0.5) & df$ey >= minline*wNtile(df,"ey","wt",0.5)) * df[,paste(weight)]) / sum(df[,paste(weight)])) return(res) } lisTopBottom <- function(df,var,evar,weight) { df <- df[which(!is.na(df[paste(var)])),] df <- df[order(df[paste(var)]),] df$cumwt <- cumsum(df[,paste(weight)]) / sum(df[,paste(weight)]) topline <- 10 * df[,paste(var)][Find(function(y) df$cumwt[y]>0.5,seq_along(df$cumwt))] botline <- 0.01 * sum(df[,paste(var)]*df[,paste(weight)] / sum(df[,paste(weight)])) df[,paste(evar)] <- ifelse(df[,paste(evar)] < botline, botline ,df[,paste(evar)]) df[,paste(evar)] <- ifelse(df[,paste(var)] > topline, topline/sqrt(df$nhhmem),df[,paste(evar)]) return(df) } setups <- function(df) { hfile <- paste(df,'h',sep='') dsh <- read.LIS(hfile, vars=c("did","hid","nhhmem","nhhmem17","nhhmem65","dhi","hwgt"), subset="!is.na(dhi) & dhi !=0 & !is.na(hwgt)",labels=FALSE) dsh$ey <- dsh$dhi / sqrt(dsh$nhhmem) dsh$wt <- dsh$hwgt * dsh$nhhmem dsh$ct <- dsh$hwgt * dsh$nhhmem17 dsh$et <- dsh$hwgt * dsh$nhhmem65 dsh <- lisTopBottom(dsh,'dhi','ey','wt') return(dsh) } #---------------------------# # KEY FIGURES INDICATORS # #---------------------------# ds <- c('lu10') for (i in ds) { df <- setups(i) print(toupper("POVERTY")) print(paste("Poverty rate Total (40%) : ", round(percPop(df,maxline=0.4),digits=3) , sep="")) print(paste("Poverty rate Total (50%) : ", round(percPop(df),digits=3) , sep="")) print(paste("Poverty rate Total (60%) : ", round(percPop(df,maxline=0.6),digits=3) , sep="")) print(paste("Poverty rate Child (40%) : ", round(percPop(df,weight="ct",maxline=0.4),digits=3), sep="")) print(paste("Poverty rate Child (50%) : ", round(percPop(df,weight="ct",maxline=0.5),digits=3), sep="")) print(paste("Poverty rate Child (60%) : ", round(percPop(df,weight="ct",maxline=0.6),digits=3), sep="")) print(paste("Poverty rate Elder (40%) : ", round(percPop(df,weight="et",maxline=0.4),digits=3), sep="")) print(paste("Poverty rate Elder (50%) : ", round(percPop(df,weight="et",maxline=0.5),digits=3), sep="")) print(paste("Poverty rate Elder (60%) : ", round(percPop(df,weight="et",maxline=0.6),digits=3), sep="")) }