Book contents

STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 27

Data Cleaning and Preprocessing

Before performing statistical analyses, data must be organised, consistent and free of errors. Data cleaning and preprocessing involve identifying and correcting issues such as missing values, outliers, inconsistent coding and formatting problems. This chapter outlines strategies and tools for preparing data using R, Python and Julia.

Missing Data

Missing values arise from nonresponse, data entry errors or sensor failures. Key steps include:

Outliers

Outliers are extreme values that may indicate errors or true variability. Detect outliers using boxplots, z–scores or robust statistics. Investigate whether they result from measurement error or reflect genuine variation. Options include:

Inconsistent Coding and Formatting

Survey data often include inconsistent codes (e.g., "male", "Male", "M") or date formats. Standardise categories and formats:

Data Transformation

Transformations prepare variables for analysis. Common transformations include:

Exercises

Practice

  1. Load a messy dataset (e.g., survey data) with missing values, inconsistent categories and outliers. Clean the data using appropriate methods in R or Python. Document each step.

  2. Simulate a dataset with 5% missing values and compare mean imputation, median imputation and complete–case analysis for estimating the mean and variance. Discuss biases.

  3. For a continuous variable with outliers, compare results of parametric (mean) and robust (median, trimmed mean) estimators before and after log transformation.

  4. Standardise the variables in a dataset and perform a principal component analysis. Explain why standardisation is necessary.

References