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) } 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","dhi","hwgt","hpartner"), 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') pfile <- paste(df,'p',sep='') dsp <- read.LIS(pfile, vars=c("hid","relation","sex"), subset="relation == 1000", labels=FALSE) ds <- merge(dsh,dsp,by="hid") rm(list=c('dsh','dsp')) ds$kidsm <- as.numeric(ds$sex == 2 & ds$hpartner == 200 & ds$nhhmem17 > 0) ds$twoPa <- as.numeric(ds$hpartner < 200 & ds$nhhmem > 2 & ds$nhhmem17 > 0) ds$poor <- as.numeric(ds$ey < (wNtile(ds,"ey","wt",0.5)*0.5)) ds <- ds[,!(names(ds) %in% c("hwgt","cumwt","sex","relation"))] return(ds) } #---------------------------# # KEY FIGURES INDICATORS # #---------------------------# ds <- c('lu10') for (i in ds) { df <- setups(i) print(toupper("POVERTY RATES FOR CHILDREN")) print(paste("% Child Poverty Two-Parent 50%: ", round(100*(sum((df$poor*df$ct)[c(which(df$twoPa==1))]/sum(df$ct[c(which(df$twoPa==1))]))),digits=3), sep="")) print(paste("% Child Poverty Single-Mother 50%: ", round(100*(sum((df$poor*df$ct)[c(which(df$kidsm==1))]/sum(df$ct[c(which(df$kidsm==1))]))),digits=3), sep="")) print(paste("% Child in Single-Mother Family : ", round(100*(sum(df$kidsm*df$ct/sum(df$ct))), digits=3) , sep="")) }