Session Ready
Exercise

Wrapping imputation & modeling in a function

Whenever you perform any analysis or modeling on imputed data, you should account for the uncertainty from imputation. Running a model on a dataset imputed only once ignores the fact that imputation estimates the missing values with uncertainty. Standard errors from such a model tend to be too small. The solution to this is multiple imputation and one way to implement it is by bootstrapping.

In the upcoming exercises, you will work with the familiar biopics data. The goal is to use multiple imputation by bootstrapping and linear regression to see if, based on the data at hand, biographical movies featuring females earn less than those about males.

Let's start with writing a function that creates a bootstrap sample, imputes it, and fits a linear regression model.

Instructions
100 XP
  • Slice data to resample rows indicated by indices and assign the result to data_boot.
  • Impute the bootstrap sample data_boot with kNN imputation using 5 neighbors and assign the result to data_imp.
  • Fit a linear regression model to data_imp that explains earnings with sub_sex, sub_type and year.