md_compute_bins: Compute bins

View source: R/md_compute_bins.R

md_compute_binsR Documentation

Compute bins

Description

Compute bins for microdata.

Usage

md_compute_bins(welfare, weight, nbins = 100, na.rm = FALSE, output = "simple")

Arguments

welfare

numeric: A vector of income or consumption values.

weight

numeric: A vector of weights.

nbins

numeric: Number of bins.

na.rm

logical: If TRUE it will exclude all NA in calculations

output

character: It has two varieties. 1) it could be a vector of variables to retain after calculations (variables available are "welfare", "weight", "cum_pop", "cum_prop_pop", and "bins"). 2) It could be a one of two key words, "simple" or "full". output = "simple" is equivalent to output = "bins" (which is the default). output = "full" if equivalent to a vector with all the variables available, output = c("welfare", "weight", "cum_pop", "cum_prop_pop","bins")

Details

Compute percentiles (bins) accumulating by population rather than by welfare. It differs from the common way to calculate percentiles because the categories are defined by the ideal size of the quantile rather than by the cutpoints.

This is why the function is called bins rather than quantiles because it actually divides the population in equal bins sorted by the welfare aggregate rather than calculate proper percentiles.

This function however, yields less unequally sized categories when 1) the cutpoint value is frequent, 2) when using weights high variance or 3) when the number of observations in the dataset is not a product of the number of quantiles. Keep in mind that it may not work properly on small datasets or if calculated for small groups. If the number of observations in the dataset or group is small, tabulate afterwards to check the results. you may use ⁠tp <- df[, sum(weight)]; df[, round((sum(weight)/(tp))*100, digits = 2) , by = bins]⁠.

Value

data.table

Examples

wbpip:::md_compute_bins(welfare = 1:2000, weight = rep(1, 2000))
wbpip:::md_compute_bins(
  welfare = 1:2000, weight = rep(1, 2000),
  output = "full"
)
wbpip:::md_compute_bins(
  welfare = 1:2000, weight = rep(1, 2000),
  output = c("cum_pop", "cum_prop_pop")
)

PIP-Technical-Team/wbpip documentation built on Nov. 29, 2024, 6:57 a.m.