E-Book

\$70.99

# Statistical Data Cleaning with Applications in R

ISBN: 978-1-118-89713-3 February 2018 320 Pages

E-Book
\$70.99
Hardcover
\$88.50
O-Book

## Description

A comprehensive guide to automated statistical data cleaning

The production of clean data is a complex and time-consuming process that requires both technical know-how and statistical expertise. Statistical Data Cleaning brings together a wide range of techniques for cleaning textual, numeric or categorical data. This book examines technical data cleaning methods relating to data representation and data structure. A prominent role is given to statistical data validation, data cleaning based on predefined restrictions, and data cleaning strategy.

Key features:

• Focuses on the automation of data cleaning methods, including both theory and applications written in R.
• Enables the reader to design data cleaning processes for either one-off analytical purposes or for setting up production systems that clean data on a regular basis.
• Explores statistical techniques for solving issues such as incompleteness, contradictions and outliers, integration of data cleaning components and quality monitoring.
• Supported by an accompanying website featuring data and R code.

This book enables data scientists and statistical analysts working with data to deepen their understanding of data cleaning as well as to upgrade their practical data cleaning skills. It can also be used as material for a course in data cleaning and analyses.

Foreword xi

1 Data Cleaning 1

1.1 The Statistical Value Chain 1

1.1.1 Raw Data 2

1.1.2 Input Data 2

1.1.3 Valid Data 3

1.1.4 Statistics 3

1.1.5 Output 3

1.2 Notation and Conventions Used in this Book 3

2 A Brief Introduction to R 5

2.1 R on the Command Line 5

2.1.1 Getting Help and Learning R 6

2.2 Vectors 7

2.2.1 Computing with Vectors 9

2.2.2 Arrays and Matrices 10

2.3 Data Frames 11

2.3.1 The Formula-Data Interface 12

2.3.2 Selecting Rows and Columns; Boolean Operators 12

2.3.3 Selection with Indices 13

2.3.4 Data Frame Manipulation:The dplyr Package 14

2.4 Special Values 15

2.4.1 Missing Values 17

2.5 Getting Data into and out of R 18

2.5.1 File Paths in R 19

2.5.2 Formats Provided by Packages 20

2.5.3 Reading Data from a Database 20

2.5.4 Working with Data External to R 21

2.6 Functions 21

2.6.1 Using Functions 22

2.6.2 Writing Functions 22

2.7 Packages Used in this Book 23

3 Technical Representation of Data 27

3.1 Numeric Data 28

3.1.1 Integers 28

3.1.2 Integers in R 30

3.1.3 Real Numbers 31

3.1.4 Double Precision Numbers 31

3.1.5 The Concept of Machine Precision 33

3.1.6 Consequences ofWorking with Floating Point Numbers 34

3.1.7 Dealing with the Consequences 35

3.1.8 Numeric Data in R 37

3.2 Text Data 38

3.2.1 Terminology and Encodings 38

3.2.2 Unicode 39

3.2.3 Some Popular Encodings 40

3.2.4 Textual Data in R: Objects of Class Character 43

3.2.5 Encoding in R 44

3.2.6 Reading andWriting of Data with Non-Local Encoding 46

3.2.7 Detecting Encoding 48

3.2.8 Collation and Sorting 49

3.3 Times and Dates 50

3.3.1 AIT, UTC, and POSIX Seconds Since the Epcoch 50

3.3.2 Time and Date Notation 52

3.3.3 Time and Date Storage in R 54

3.3.4 Time and Date Conversion in R 55

3.3.5 Leap Days, Time Zones, and Daylight Saving Times 57

3.4 Notes on Locale Settings 58

4 Data Structure 61

4.1 Introduction 61

4.2 Tabular Data 61

4.2.1 data.frame 61

4.2.2 Databases 62

4.2.3 dplyr 64

4.3 Matrix Data 65

4.4 Time Series 66

4.5 Graph Data 68

4.6 Web Data 69

4.6.1 Web Scraping 69

4.6.2 Web API 70

4.7 Other Data 72

4.8 Tidying Tabular Data 72

4.8.1 Variable Per Column 74

4.8.2 Single Observation Stored in Multiple Tables 75

5 Cleaning Text Data 77

5.1 Character Normalization 78

5.1.1 Encoding Conversion and Unicode Normalization 78

5.1.2 Character Conversion and Transliteration 80

5.2 Pattern Matching with Regular Expressions 81

5.2.1 Basic Regular Expressions 82

5.2.2 Practical Regular Expressions 85

5.2.3 Generating Regular Expressions in R 92

5.3 Common String Processing Tasks in R 93

5.4 Approximate Text Matching 98

5.4.1 String Metrics 100

5.4.2 String Metrics and Approximate Text Matching in R 109

6 Data Validation 119

6.1 Introduction 119

6.2 A First Look at the validate Package 120

6.2.1 Quick Checks with check_that 120

6.2.2 The BasicWorkflow: validator and confront 122

6.2.3 A Little Background on validate and DSLs 124

6.3 Defining Data Validation 125

6.3.1 Formal Definition of Data Validation 126

6.3.2 Operations on Validation Functions 128

6.3.3 Validation and Missing Values 130

6.3.4 Structure of Validation Functions 131

6.3.5 Demarcating Validation Rules in validate 132

6.4 A Formal Typology of Data Validation Functions 134

6.4.1 A Closer Look at Measurement 134

6.4.2 Classification of Validation Rules 135

6.5 Validating Data with the validate Package 137

6.5.1 Validation Rules in the Console and the validator Object 137

6.5.2 Validating in the Pipeline 139

6.5.3 Raising Errors orWarnings 140

6.5.4 Tolerance for Testing Linear Equalities 140

6.5.5 Setting and Resetting Options 141

6.5.6 Importing and Exporting Validation Rules from and to File 142

6.5.7 Checking Variable Types and Metadata 145

6.5.8 Checking Value Ranges and Code Lists 146

6.5.9 Checking In-Record Consistency Rules 146

6.5.10 Checking Cross-Record Validation Rules 148

6.5.11 Checking Functional Dependencies 149

6.5.12 Cross-Dataset Validation 150

6.5.13 Macros, Variable Groups, Keys 152

6.5.14 Analyzing Output: validation Objects 152

6.5.15 Output Dimensionality and Output Selection 155

7 Localizing Errors in Data Records 157

7.1 Error Localization 157

7.2 Error Localization with R 160

7.2.1 The Errorlocate Package 160

7.3 Error Localization as MIP-Problem 163

7.3.1 Error Localization and Mixed-Integer Programming 163

7.3.2 Linear Restrictions 164

7.3.3 Categorical Restrictions 165

7.3.4 Mixed-Type Restrictions 167

7.4 Numerical Stability Issues 170

7.4.1 A Short Overview of MIP Solving 170

7.4.2 Scaling Numerical Records 172

7.4.3 Setting NumericalThreshold Values 173

7.5 Practical Issues 174

7.5.1 Setting ReliabilityWeights 174

7.5.2 Simplifying Conditional Validation Rules 176

7.6 Conclusion 180

8 Rule Set Maintenance and Simplification 183

8.1 Quality of Validation Rules 183

8.1.1 Completeness 183

8.1.2 Superfluous Rules and Infeasibility 184

8.2 Rules in the Language of Logic 184

8.2.1 Using Logic to Rewrite Rules 185

8.3 Rule Set Issues 186

8.3.1 Infeasible Rule Set 186

8.3.2 Fixed Value 187

8.3.3 Redundant Rule 188

8.3.4 Nonrelaxing Clause 189

8.3.5 Nonconstraining Clause 189

8.4 Detection and Simplification Procedure 190

8.4.1 Mixed-Integer Programming 190

8.4.2 Detecting Feasibility 191

8.4.3 Finding Rules Causing Infeasibility 191

8.4.4 Detecting Conflicting Rules 191

8.4.5 Detect Partial Infeasibility 192

8.4.6 Detect Fixed Values 192

8.4.7 Detect Nonrelaxing Clauses 192

8.4.8 Detecting Nonconstraining Clauses 193

8.4.9 Detecting Redundant Rules 193

8.5 Conclusion 194

9 Methods Based on Models for Domain Knowledge 195

9.1 Correction with Data Modifying Rules 195

9.1.1 Modifying Functions 196

9.1.2 A Class of Modifying Functions on Numerical Data 201

9.2 Rule-Based Correction with dcmodify 205

9.2.1 Reading Rules from File 206

9.2.2 Modifying Rule Syntax 207

9.2.3 Missing Values 208

9.2.4 Sequential and Sequence-Independent Execution 208

9.2.5 Options Settings Management 209

9.3 Deductive Correction 209

9.3.1 Correcting Typing Errors in Numeric Data 209

9.3.2 Deductive Imputation Using Linear Restrictions 213

10.1 Missing Data 219

10.1.1 Missing Data Mechanisms 219

10.1.2 Visualizing and Testing for Patterns in Missing Data Using R 220

10.2 Model-Based Imputation 224

10.3 Model-Based Imputation in R 226

10.3.1 Specifying ImputationMethods with simputation 226

10.3.2 Linear Regression-Based Imputation 227

10.3.3 M-Estimation 230

10.3.4 Lasso, Ridge, and Elasticnet Regression 231

10.3.5 Classification and Regression Trees 232

10.3.6 Random Forest 235

10.4 Donor Imputation with R 236

10.4.1 Random and Sequential Hot Deck Imputation 237

10.4.2 k Nearest Neighbors and Predictive Mean Matching 238

10.5 Other Methods in the simputation Package 239

10.6 Imputation Based on the EM Algorithm 240

10.6.1 The EM Algorithm 241

10.6.2 EM Imputation Assuming the Multivariate Normal Distribution 243

10.7 Sampling Variance under Imputation 244

10.8 Multiple Imputations 246

10.8.1 Multiple Imputation Based on the EM Algorithm 248

10.8.2 The Amelia Package 249

10.8.3 Multivariate Imputation with Chained Equations (Mice) 252

10.8.4 Imputation with the mice Package 254

10.9 Analytic Approaches to Estimate Variance of Imputation 256

10.9.1 Imputation as Part of the Estimator 256

10.10 Choosing an ImputationMethod 257

10.11.1 Formal Description 259

10.11.2 Application to Imputed Data 262

10.11.3 Adjusting Imputed Values with the rspa Package 263

11 Example: A Small Data-Cleaning System 265

11.1 Setup 266

11.1.1 DeterministicMethods 266

11.1.2 Error Localization 269

11.1.3 Imputation 269

11.2 Monitoring Changes in Data 273

11.2.1 Data Diff (Daff) 274

11.2.2 Summarizing Cell Changes 275

11.2.3 Summarizing Changes in Conformance to Validation Rules 277

11.2.4 Track Changes in Data Automatically with lumberjack 278

11.3 Integration and Automation 282

11.3.1 Using RScript 283

11.3.2 The docopt Package 283

11.3.3 Automated Data Cleaning 285

References 287

Index 297