knitr::opts_chunk$set( collapse = TRUE, cache = TRUE, comment = "#>" ) library(iterake)
iterake's main utility is creating row weights using a process called iterative raking. Iterative raking (also known as rim weighting), is one of several methods used to correct the deviation between the marginal proportions in a sample and a known population, or, universe as it was first referred to (Deming & Stephan 1940) for a given set of variables.
iterake is designed with speed and simplicity in mind. The weighting algorithm is powered by data.table and takes advantage of its fast grouping and joining.
The weighting process with iterake
is fairly straightforward, we suggest:
universe()
function to build your population. category()
function.inherit_category()
. Just make sure the name given to the category matches the existing data and the data you intend to weight.compare_margins()
function. iterake()
. compare_margins()
again to verify that the weighted proportions in your sample now match the population. weight_stats()
. ``` {r, eval = FALSE}
install.packages("remotes") remotes::install_github("ttrodrigz/iterake")
## Motivating Example Say you have conducted a study by randomly sampling 400 individuals from a population. You were dilligent in monitoring the responses to make sure the makeup of the sample adequately reflected the population. But, due to chance, slightly too many males and individuals under 50 years of age entered the sample. You know from experts in your field that 60% of the population from which you sampled are female, and 20% of the population are less than 50 years old. Let's build a data set to use as an example: ```r library(tibble) N <- 400 set.seed(101) df <- tibble( id = 1:N, Sex = sample( x = c("Male", "Female"), size = N, replace = TRUE, prob = c(0.42, 0.58) ), Under50 = sample( x = c(T, F), size = N, replace = TRUE, prob = c(0.22, 0.78) ) ) df
Simply supply the data you intend on weighting, and build weighting categories by using the category()
function.
library(iterake) uni <- universe( data = df, category( name = "Sex", buckets = c("Male", "Female"), targets = c(0.4, 0.6) ), category( name = "Under50", buckets = c(TRUE, FALSE), targets = c(0.2, 0.8) ) )
This is the time to inspect the differences in proportions between the sample and the population. A large discrepancy will require extreme weights, and in some cases the algorithm may not even converge. Before you decide to weight, keep in mind that weighting the data decreases accuracy. In some cases it is best to deal with the fact your sample doesn't perfectly match the population.
compare_margins(universe = uni)
If weighting is necessary, pass the universe object to iterake()
.
df_wgt <- iterake(universe = uni) df_wgt
````r compare_margins( universe = uni, data = df_wgt, weight = weight, plot = TRUE )
#### **Step 5:** Inspect weights Again, weights much higher or lower than 1 are undesirable, check the output with `weight_stats()` to inspect the quality of the weights. Details about what each of the statistics mean can be found in the documentation. ```r weight_stats(df_wgt[["weight"]])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.