Get startedGet started for free

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.

This exercise is part of the course

Handling Missing Data with Imputations in R

View Course

Exercise instructions

  • 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.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

calc_gender_coef <- function(data, indices) {
  # Get bootstrap sample
  data_boot <- data[___, ]
  # Impute with kNN imputation
  data_imp <- ___
  # Fit linear regression
  linear_model <- ___
  # Extract and return gender coefficient
  gender_coefficient <- coef(linear_model)[2]
  return(gender_coefficient)
}
Edit and Run Code