Introduction

rsample can be used to create objects containing resamples of the original data. This page contains examples of how those objects can be used for data analysis.

For illustration, the attrition data is used. From the help file:

These data are from the IBM Watson Analytics Lab. The website describes the data with “Uncover the factors that lead to employee attrition and explore important questions such as ‘show me a breakdown of distance from home by job role and attrition’ or ‘compare average monthly income by education and attrition’. This is a fictional data set created by IBM data scientists.” There are 1470 rows.

The data can be accessed using

Model Assessment

Let’s fit a logistic regression model to the data with model terms for the job satisfaction, gender, and monthly income.

If we were fitting the model to the entire data set, we might model attrition using

glm(Attrition ~ JobSatisfaction + Gender + MonthlyIncome, data = attrition, family = binomial)

For convenience, we’ll create a formula object that will be used later:

mod_form <- as.formula(Attrition ~ JobSatisfaction + Gender + MonthlyIncome)

To evaluate this model, we will use 10 repeats of 10-fold cross-validation and use the 100 holdout samples to evaluate the overall accuracy of the model.

First, let’s make the splits of the data:

Now let’s write a function that will, for each resample:

  1. obtain the analysis data set (i.e. the 90% used for modeling)
  2. fit a logistic regression model
  3. predict the assessment data (the other 10% not used for the model) using the broom package
  4. determine if each sample was predicted correctly.

Here is our function:

For example:

For this model, the .fitted value is the linear predictor in log-odds units.

To compute this data set for each of the 100 resamples, we’ll use the map function from the purrr package:

Now we can compute the accuracy values for all of the assessment data sets:

Keep in mind that the baseline accuracy to beat is the rate of non-attrition, which is 0.839. Not a great model so far.

Using the Bootstrap to Make Comparisons

Traditionally, the bootstrap has been primarily used to empirically determine the sampling distribution of a test statistic. Given a set of samples with replacement, a statistic can be calculated on each analysis set and the results can be used to make inferences (such as confidence intervals).

For example, are there differences in the median monthly income between genders?

If we wanted to compare the genders, we could conduct a t-test or rank-based test. Instead, let’s use the bootstrap to see if there is a difference in the median incomes for the two groups. We need a simple function to compute this statistic on the resample:

median_diff <- function(splits) {
  x <- analysis(splits)
  median(x$MonthlyIncome[x$Gender == "Female"]) - 
      median(x$MonthlyIncome[x$Gender == "Male"])     
}

Now we would create a large number of bootstrap samples (say 2000+). For illustration, we’ll only do 500 in this document.

set.seed(353)
bt_resamples <- bootstraps(attrition, times = 500)

This function is then computed across each resample:

bt_resamples$wage_diff <- map_dbl(bt_resamples$splits, median_diff)

The bootstrap distribution of this statistic has a slightly bimodal and skewed distribution:

The variation is considerable in this statistic. One method of computing a confidence interval is to take the percentiles of the bootstrap distribution. A 95% confidence interval for the difference in the means would be:

quantile(bt_resamples$wage_diff, 
         probs = c(0.025, 0.500, 0.975))
#>  2.5%   50% 97.5% 
#>  -207   190   618

On average, there is no evidence for a difference in the genders.

Bootstrap Estimates of Model Coefficients

Unless there is already a column in the resample object that contains the fitted model, a function can be used to fit the model and save all of the model coefficients. The broom package package has a tidy function that will save the coefficients in a data frame. Instead of returning a data frame with a row for each model term, we will save a data frame with a single row and columns for each model term. As before, purrr::map can be used to estimate and save these values for each split.