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 <- lisTopBottom(dsh,'dhi','ey','wt') dsh$poor <- as.numeric(dsh$ey < (wNtile(dsh,"ey","wt",0.5)*0.5)) return(dsh) } #---------------------------# # KEY FIGURES INDICATORS # #---------------------------# ds <- c('lu10') for (i in ds) { df <- setups(i) print(toupper("DISTRIBUTION OF CHILDREN BY INCOME GROUP")) print(paste("Children by Income Group (50-75%) : ", round(percPop(df,"ct",maxline=0.75,minline=0.5) ,digits=3) , sep="")) print(paste("Children by Income Group (75-150%): ", round(percPop(df,"ct",maxline=1.5 ,minline=0.75),digits=3) , sep="")) print(paste("Children by Income Group (150%+) : ", round(percPop(df,"ct",maxline=Inf ,minline=1.5) ,digits=3) , sep="")) }