Table of contents for Data preparation for data mining using SAS / Mamdouh Refaat.

Bibliographic record and links to related information available from the Library of Congress catalog.

Note: Contents data are machine generated based on pre-publication provided by the publisher. Contents may have variations from the printed book or be incomplete or contain other coding.


Counter
Contents
1 Introduction 17
1.1 The Data Mining Process . . . . . . . . . . . . . . . . . . . . . 17
1.2 Methodologies of Data Mining . . . . . . . . . . . . . . . . . . . 17
1.3 The Mining View . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.4 Scoring View . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.5 Notes on Data Mining Software . . . . . . . . . . . . . . . . . . 21
2 Tasks and Data Flow 23
2.1 Data Mining Tasks . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2 Data Mining Competencies . . . . . . . . . . . . . . . . . . . . . 25
2.3 The Data Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.4 Types of Variables . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.5 The Mining View and the Scoring View . . . . . . . . . . . . . . 28
2.6 Steps of Data Preparation . . . . . . . . . . . . . . . . . . . . . 29
3 Review of Data Mining Modeling Techniques 31
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.2 Regression Models . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.2.1 Linear Regression . . . . . . . . . . . . . . . . . . . . . . 32
3.2.2 Logistic Regression . . . . . . . . . . . . . . . . . . . . . 35
3.3 Decision trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.4 Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.5 Cluster Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.6 Association Rules . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.7 Time Series Analysis . . . . . . . . . . . . . . . . . . . . . . . . 44
3.8 Support Vector Machines . . . . . . . . . . . . . . . . . . . . . . 44
4 SAS Macros: A Quick Start 45
4.1 Introduction: Why Macros . . . . . . . . . . . . . . . . . . . . . 45
4.2 The Basics - The Macro and Its Variables . . . . . . . . . . . . 46
4.3 Doing Calculations . . . . . . . . . . . . . . . . . . . . . . . . . 48
4.4 Programming Logic . . . . . . . . . . . . . . . . . . . . . . . . . 49
4.5 Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . 51
4.6 Macros that Call Other Macros . . . . . . . . . . . . . . . . . . 52
4.7 Common Macro Patterns and Caveats . . . . . . . . . . . . . . 54
4.7.1 Generating a List of Macro Variables . . . . . . . . . . . 54
4.7.2 Double Coding . . . . . . . . . . . . . . . . . . . . . . . 55
4.7.3 Using Local Variables . . . . . . . . . . . . . . . . . . . . 56
4.7.4 From a DATA Step to Macro Variables . . . . . . . . . . 58
4.8 Where to Go From Here . . . . . . . . . . . . . . . . . . . . . . 59
5 Data Acquisition and Integration 61
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.2 Sources of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.3 Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
5.4 Data Roll Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
5.5 Roll Up With Sums, Averages and Counts . . . . . . . . . . . . 73
5.6 Calculation of the Mode . . . . . . . . . . . . . . . . . . . . . . 74
5.7 Data Integration . . . . . . . . . . . . . . . . . . . . . . . . . . 76
5.7.1 Merging . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
5.7.2 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . 78
6 Integrity Checks 81
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
6.2 Comparing Datasets . . . . . . . . . . . . . . . . . . . . . . . . 84
6.3 Dataset Schema Checks . . . . . . . . . . . . . . . . . . . . . . 85
6.3.1 Dataset Variables . . . . . . . . . . . . . . . . . . . . . . 85
6.3.2 Variable Types . . . . . . . . . . . . . . . . . . . . . . . 88
6.4 Nominal Variables . . . . . . . . . . . . . . . . . . . . . . . . . 89
6.4.1 Testing the Presence of All Categories . . . . . . . . . . 89
6.4.2 Testing Similarity Between Ratios . . . . . . . . . . . . . 92
6.5 Continuous Variables . . . . . . . . . . . . . . . . . . . . . . . . 96
6.5.1 Comparing Measure from two datasets . . . . . . . . . . 97
6.5.2 Comparing the means, standard deviations and the variance 98
6.5.3 The Confidence Level Calculations Assumptions . . . . . 100
6.5.4 Comparison of Other Measures . . . . . . . . . . . . . . 100
7 Exploratory Data Analysis 103
7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
7.2 Common EDA Procedures . . . . . . . . . . . . . . . . . . . . . 103
7.3 Univariate Statistics . . . . . . . . . . . . . . . . . . . . . . . . 105
7.4 Variable Distribution . . . . . . . . . . . . . . . . . . . . . . . . 106
7.5 Detection of Outliers . . . . . . . . . . . . . . . . . . . . . . . . 107
7.5.1 Identification of Outliers Using Ranges . . . . . . . . . . 108
7.5.2 Identification of Outliers Using Model Fitting . . . . . . 112
7.5.3 Identification of Outliers Using Clustering . . . . . . . . 114
7.5.4 Notes on Outliers . . . . . . . . . . . . . . . . . . . . . . 116
7.6 Testing Normality . . . . . . . . . . . . . . . . . . . . . . . . . . 117
7.7 Cross-tabulation . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
7.8 Investigating Data Structures . . . . . . . . . . . . . . . . . . . 118
8 Sampling and Partitioning 119
8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
8.2 Contents of Samples . . . . . . . . . . . . . . . . . . . . . . . . 120
8.3 Random Sampling . . . . . . . . . . . . . . . . . . . . . . . . . 121
8.3.1 Constraints on Sample Size . . . . . . . . . . . . . . . . 121
8.3.2 SAS Implementation . . . . . . . . . . . . . . . . . . . . 121
8.4 Balanced Sampling . . . . . . . . . . . . . . . . . . . . . . . . . 125
8.4.1 Constraints on Sample Size . . . . . . . . . . . . . . . . 125
8.4.2 SAS Implementation . . . . . . . . . . . . . . . . . . . . 126
8.5 Minimum Sample Size . . . . . . . . . . . . . . . . . . . . . . . 130
8.5.1 Continuous and Binary Variables . . . . . . . . . . . . . 131
8.5.2 Sample Size for a Nominal Variable . . . . . . . . . . . . 132
8.6 Checking Validity of Sample . . . . . . . . . . . . . . . . . . . . 133
9 Data Transformations 135
9.1 Raw and Analytical Variables . . . . . . . . . . . . . . . . . . . 135
9.2 Scope of Data Transformations . . . . . . . . . . . . . . . . . . 137
9.3 Creation of New Variables . . . . . . . . . . . . . . . . . . . . . 139
9.3.1 Renaming Variables . . . . . . . . . . . . . . . . . . . . . 141
9.3.2 Automatic Generation of Simple Analytical Variables . . 144
9.4 Mapping of Nominal Variables . . . . . . . . . . . . . . . . . . . 147
9.5 Normalization of Continuous Variables . . . . . . . . . . . . . . 151
9.6 Changing the Variable Distribution . . . . . . . . . . . . . . . . 152
9.6.1 Rank Transformations . . . . . . . . . . . . . . . . . . . 152
9.6.2 Box-Cox Transformations . . . . . . . . . . . . . . . . . 154
9.6.3 Spreading the Histogram . . . . . . . . . . . . . . . . . . 159
10 Binning and Reduction of Cardinality 161
10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
10.2 Cardinality Reduction . . . . . . . . . . . . . . . . . . . . . . . 162
10.2.1 The Main Questions . . . . . . . . . . . . . . . . . . . . 162
10.2.2 Structured Grouping Methods . . . . . . . . . . . . . . . 164
10.2.3 Splitting a Dataset . . . . . . . . . . . . . . . . . . . . . 165
10.2.4 The Main Algorithm . . . . . . . . . . . . . . . . . . . . 166
10.2.5 Reduction of Cardinality Using Gini Measure . . . . . . 168
10.2.6 Limitations and Modifications . . . . . . . . . . . . . . . 177
10.3 Binning of Continuous Variables . . . . . . . . . . . . . . . . . . 178
10.3.1 Equal Width Binning . . . . . . . . . . . . . . . . . . . . 179
10.3.2 Equal Height Binning . . . . . . . . . . . . . . . . . . . . 181
10.3.3 Optimal Binning . . . . . . . . . . . . . . . . . . . . . . 186
11 Treatment of Missing Values 195
11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
11.2 Simple Replacement . . . . . . . . . . . . . . . . . . . . . . . . 198
11.2.1 Nominal Variables . . . . . . . . . . . . . . . . . . . . . 198
11.2.2 Continuous and ordinal Variables . . . . . . . . . . . . . 201
11.3 Imputing Missing Values . . . . . . . . . . . . . . . . . . . . . . 204
11.3.1 Basic Issues in Multiple Imputation . . . . . . . . . . . . 204
11.3.2 Patterns of Missingness . . . . . . . . . . . . . . . . . . . 205
11.4 Imputation Methods and Strategy . . . . . . . . . . . . . . . . . 207
11.5 SAS Macros for Multiple Imputation . . . . . . . . . . . . . . . 211
11.5.1 Extracting the Pattern of Missing Values . . . . . . . . . 211
11.5.2 Reordering Variables . . . . . . . . . . . . . . . . . . . . 216
11.5.3 Checking Missing Pattern Status . . . . . . . . . . . . . 221
11.5.4 Imputing to a Monotone Missing Pattern . . . . . . . . . 224
11.5.5 Imputing Continuous Variables . . . . . . . . . . . . . . 225
11.5.6 Combining Imputed Values of Continuous Variables . . . 227
11.5.7 Imputing Nominal and Ordinal Variables . . . . . . . . . 230
11.5.8 Combining Imputed Values of Ordinal and Nominal Variables. . . 231
11.6 Predicting Missing Values . . . . . . . . . . . . . . . . . . . . . 232
12 Predictive Power and Variable Reduction I 235
12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
12.2 Metrics of Predictive Power . . . . . . . . . . . . . . . . . . . . 237
12.3 Methods of Variable Reduction . . . . . . . . . . . . . . . . . . 238
12.4 Variable Reduction: before or during modeling . . . . . . . . . 239
13 Analysis of Nominal and Ordinal Variables 241
13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
13.2 Contingency Tables . . . . . . . . . . . . . . . . . . . . . . . . . 241
13.3 Notation and Definitions . . . . . . . . . . . . . . . . . . . . . . 242
13.4 Contingency Tables for Binary Variables . . . . . . . . . . . . . 244
13.4.1 Difference in Proportion . . . . . . . . . . . . . . . . . . 245
13.4.2 The Odds Ratio . . . . . . . . . . . . . . . . . . . . . . . 248
13.4.3 The Pearson Statistic . . . . . . . . . . . . . . . . . . . . 251
13.4.4 The Likelihood-Ratio Statistic . . . . . . . . . . . . . . . 254
13.5 Contingency Tables for Multi - Category Variables . . . . . . . . 256
13.6 Analysis of Ordinal Variables . . . . . . . . . . . . . . . . . . . 258
13.7 Implementation Scenarios . . . . . . . . . . . . . . . . . . . . . 261
14 Analysis of Continuous Variables 263
14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
14.2 When is Binning Necessary? . . . . . . . . . . . . . . . . . . . . 263
14.3 Measures of Association . . . . . . . . . . . . . . . . . . . . . . 264
14.3.1 Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
14.3.2 The F Test . . . . . . . . . . . . . . . . . . . . . . . . . 266
14.3.3 Gini and Entropy Variances . . . . . . . . . . . . . . . . 266
14.4 Correlation Coefficients . . . . . . . . . . . . . . . . . . . . . . . 269
15 Principal Component Analysis (PCA) 277
15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
15.2 Mathematical Formulations . . . . . . . . . . . . . . . . . . . . 278
15.3 Implementing and Using PCA . . . . . . . . . . . . . . . . . . . 279
15.4 Comments on Using PCA . . . . . . . . . . . . . . . . . . . . . 285
15.4.1 Number of Principal Components . . . . . . . . . . . . . 285
15.4.2 Success of PCA . . . . . . . . . . . . . . . . . . . . . . . 285
15.4.3 Nominal Variables . . . . . . . . . . . . . . . . . . . . . 286
15.4.4 Dataset Size and Performance . . . . . . . . . . . . . . . 286
16 Factor Analysis 289
16.1 Introduction to Factor Analysis . . . . . . . . . . . . . . . . . . 289
16.1.1 Basic Model . . . . . . . . . . . . . . . . . . . . . . . . . 289
16.1.2 Factor Rotation . . . . . . . . . . . . . . . . . . . . . . . 291
16.1.3 Estimation Methods . . . . . . . . . . . . . . . . . . . . 291
16.1.4 Variable Standardization . . . . . . . . . . . . . . . . . . 292
16.1.5 Illustrative Example . . . . . . . . . . . . . . . . . . . . 292
16.2 Relationship between PCA and FA . . . . . . . . . . . . . . . . 296
16.3 Implementation of Factor Analysis . . . . . . . . . . . . . . . . 297
16.3.1 Obtaining the Factors . . . . . . . . . . . . . . . . . . . 297
16.3.2 Factor Scores . . . . . . . . . . . . . . . . . . . . . . . . 299
17 Predictive Power and Variable Reduction II 301
17.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
17.2 Data with Binary Dependent Variables . . . . . . . . . . . . . . 302
17.2.1 Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
17.2.2 Nominal IV?s . . . . . . . . . . . . . . . . . . . . . . . . 302
17.2.3 Numeric Nominal IV?s . . . . . . . . . . . . . . . . . . . 307
17.2.4 Ordinal IV?s . . . . . . . . . . . . . . . . . . . . . . . . . 307
17.2.5 Continuous IV?s . . . . . . . . . . . . . . . . . . . . . . . 308
17.3 Data with Continuous Dependent Variables . . . . . . . . . . . 309
17.3.1 Nominal IV?s . . . . . . . . . . . . . . . . . . . . . . . . 309
17.3.2 Ordinal IV?s . . . . . . . . . . . . . . . . . . . . . . . . . 309
17.3.3 Continuous IV?s . . . . . . . . . . . . . . . . . . . . . . . 309
17.4 Variable Reduction Strategies . . . . . . . . . . . . . . . . . . . 310
18 Putting it All Together 313
18.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
18.2 The Process of Data Preparation . . . . . . . . . . . . . . . . . 313
18.2.1 The Procedure . . . . . . . . . . . . . . . . . . . . . . . 313
18.3 Case Study: The Bookstore . . . . . . . . . . . . . . . . . . . . 315
18.3.1 The Business Problem . . . . . . . . . . . . . . . . . . . 315
18.3.2 Project Tasks . . . . . . . . . . . . . . . . . . . . . . . . 316
18.3.3 The Data Preparation Code . . . . . . . . . . . . . . . . 318
Appendix - Listing of SAS Macros 335
A.1 Copyright and Software License . . . . . . . . . . . . . . . . . . 335
A.2 Dependencies between Macros . . . . . . . . . . . . . . . . . . . 336
A.3 Data Acquisition and Integration . . . . . . . . . . . . . . . . . 337
A.3.1 Macro TBRollup() . . . . . . . . . . . . . . . . . . . . . 337
A.3.2 Macro ABRollup() . . . . . . . . . . . . . . . . . . . . . 339
A.3.3 Macro VarMode() . . . . . . . . . . . . . . . . . . . . . . 342
A.3.4 Macro MergeDS() . . . . . . . . . . . . . . . . . . . . . . 342
A.3.5 Macro ContcatDS() . . . . . . . . . . . . . . . . . . . . . 343
A.4 Integrity Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
A.4.1 Macro SchCompare() . . . . . . . . . . . . . . . . . . . . 343
A.4.2 Macro CatCompare() . . . . . . . . . . . . . . . . . . . . 345
A.4.3 Macro ChiSample() . . . . . . . . . . . . . . . . . . . . . 346
A.4.4 Macro VarUnivar1() . . . . . . . . . . . . . . . . . . . . 347
A.4.5 Macro CVLimits() . . . . . . . . . . . . . . . . . . . . . 348
A.4.6 Macro CompareTwo() . . . . . . . . . . . . . . . . . . . 349
A.5 Exploratory Data Analysis . . . . . . . . . . . . . . . . . . . . . 350
A.5.1 Macro Extremes1() . . . . . . . . . . . . . . . . . . . . . 350
A.5.2 Macro Extremes2() . . . . . . . . . . . . . . . . . . . . . 351
A.5.3 Macro RobRegOL() . . . . . . . . . . . . . . . . . . . . . 352
A.5.4 MacroClustOL() . . . . . . . . . . . . . . . . . . . . . . 352
A.6 Sampling and Partitioning . . . . . . . . . . . . . . . . . . . . . 353
A.6.1 Macro RandomSample() . . . . . . . . . . . . . . . . . . 353
A.6.2 Macro R2Samples() . . . . . . . . . . . . . . . . . . . . . 353
A.6.3 Macro B2Samples() . . . . . . . . . . . . . . . . . . . . . 356
A.7 Data Transformations . . . . . . . . . . . . . . . . . . . . . . . 359
A.7.1 Macro NorList() . . . . . . . . . . . . . . . . . . . . . . . 359
A.7.2 Macro NorVars() . . . . . . . . . . . . . . . . . . . . . . 360
A.7.3 Macro AutoInter() . . . . . . . . . . . . . . . . . . . . . 361
A.7.4 Macro CalcCats() . . . . . . . . . . . . . . . . . . . . . . 363
A.7.5 Macro MapCats() . . . . . . . . . . . . . . . . . . . . . . 363
A.7.6 Macro CalcLL() . . . . . . . . . . . . . . . . . . . . . . . 365
A.7.7 Macro BoxCox() . . . . . . . . . . . . . . . . . . . . . . 365
A.8 Binning and Reduction of Cardinality . . . . . . . . . . . . . . . 367
A.8.1 Macro GRedCats() . . . . . . . . . . . . . . . . . . . . . 367
A.8.2 Macro GSplit() . . . . . . . . . . . . . . . . . . . . . . . 372
A.8.3 Macro AppCatRed() . . . . . . . . . . . . . . . . . . . . 374
A.8.4 Macro BinEqW() . . . . . . . . . . . . . . . . . . . . . . 375
A.8.5 Macro BinEqW2() . . . . . . . . . . . . . . . . . . . . . 375
A.8.6 Macro BinEqW3() . . . . . . . . . . . . . . . . . . . . . 376
A.8.7 Macro BinEqH() . . . . . . . . . . . . . . . . . . . . . . 377
A.8.8 Macro GBinBDV() . . . . . . . . . . . . . . . . . . . . . 380
A.8.9 Macro AppBins() . . . . . . . . . . . . . . . . . . . . . . 385
A.9 Treatment of Missing Values . . . . . . . . . . . . . . . . . . . . 386
A.9.1 Macro ModeCat() . . . . . . . . . . . . . . . . . . . . . . 386
A.9.2 Macro SubCat() . . . . . . . . . . . . . . . . . . . . . . . 386
A.9.3 Macro SubCont() . . . . . . . . . . . . . . . . . . . . . . 387
A.9.4 Macro MissPatt() . . . . . . . . . . . . . . . . . . . . . . 389
A.9.5 Macro ReMissPat() . . . . . . . . . . . . . . . . . . . . . 393
A.9.6 Macro CheckMono() . . . . . . . . . . . . . . . . . . . . 395
A.9.7 Macro MakeMono() . . . . . . . . . . . . . . . . . . . . . 397
A.9.8 Macro ImpReg() . . . . . . . . . . . . . . . . . . . . . . 397
A.9.9 Macro AvgImp() . . . . . . . . . . . . . . . . . . . . . . 397
A.9.10 Macro NOrdImp() . . . . . . . . . . . . . . . . . . . . . 398
A.10 Analysis of Nominal and Ordinal Variables . . . . . . . . . . . . 399
A.10.1 Macro ContinMat() . . . . . . . . . . . . . . . . . . . . . 399
A.10.2 Macro PropDiff() . . . . . . . . . . . . . . . . . . . . . . 399
A.10.3 Macro OddsRatio() . . . . . . . . . . . . . . . . . . . . . 400
A.10.4 Macro PearChi() . . . . . . . . . . . . . . . . . . . . . . 401
A.10.5 Macro LikeRatio() . . . . . . . . . . . . . . . . . . . . . 402
A.10.6 Macro ContPear() . . . . . . . . . . . . . . . . . . . . . 402
A.10.7 Macro ContSpear() . . . . . . . . . . . . . . . . . . . . . 403
A.10.8 Macro ContnAna() . . . . . . . . . . . . . . . . . . . . . 404
A.11 Analysis of Continuous Variables . . . . . . . . . . . . . . . . . 405
A.11.1 Macro ContGrF() . . . . . . . . . . . . . . . . . . . . . . 405
A.11.2 Macro VarCorr() . . . . . . . . . . . . . . . . . . . . . . 407
A.12 Principal Component Analysis . . . . . . . . . . . . . . . . . . . 407
A.12.1 Macro PrinComp1() . . . . . . . . . . . . . . . . . . . . 407
A.12.2 Macro PrinComp2() . . . . . . . . . . . . . . . . . . . . 408
A.13 Factor Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
A.13.1 Macro Factor() . . . . . . . . . . . . . . . . . . . . . . . 410
A.13.2 Macro FactScore() . . . . . . . . . . . . . . . . . . . . . 410
A.13.3 Macro FactRen() . . . . . . . . . . . . . . . . . . . . . . 411
A.14 Predictive Power and Variable Reduction II . . . . . . . . . . . 411
A.14.1 Macro GiniCatBDV() . . . . . . . . . . . . . . . . . . . 411
A.14.2 Macro EntCatBDV() . . . . . . . . . . . . . . . . . . . . 413
A.14.3 Macro PearSpear() . . . . . . . . . . . . . . . . . . . . . 414
A.14.4 Macro PowerCatBDV() . . . . . . . . . . . . . . . . . . . 415
A.14.5 Macro PowerOrdBDV() . . . . . . . . . . . . . . . . . . 417
A.14.6 Macro PowerCatNBDV() . . . . . . . . . . . . . . . . . . 419
A.15 Other Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
A.15.1 ListToCol() . . . . . . . . . . . . . . . . . . . . . . . . . 421

Library of Congress Subject Headings for this publication:

Data mining.
SAS (Computer file).