scieee AI-readable full text Open interactive document viewer

Missing Data Treatment

Montero Mercadé, Lídia

Full text

Missing Data Treatment DATA ANALYSIS IN TRANSPORT SYSTEMS –MASTER URBAN MOBILITY (MUM) Course 2024-25 Term 1 Author: Lídia Montero 1 Outliers What is an outlier? Definition of Douglas Hawkins: “An outlier is an observation which deviates so much from the other observations as to arouse suspicions that it was generated by a different mechanism” Statistics-based intuition. Normal data follow a “normal generating data mechanism”, e.g. some given statistical process. Outlying data may be a: –very unlikely events for the normal generating mechanism –data following a different generating mechanism 2 if X~N(0,1) Prob(xX) 1 0.1586553 2 0.02275013 3 0.001349898 4 3.167124e -05 5 2.866516e -07 outliers?? Example of an Outlier in data •The data set of N= 90 ordered observations as shown below is examined for outliers: 30, 171, 184, 201, 212, 250, 265, 270, 272, 289, 305, 306, 322, 322, 336, 346, 351, 370, 390, 404, 409, 411, 436, 437, 439, 441, 444, 448, 451, 453, 470, 480, 482, 487, 494, 495, 499, 503, 514, 521, 522, 527, 548, 550, 559, 560, 570, 572, 574, 578, 585, 592, 592, 607, 616, 618, 621, 629, 637, 638, 640, 656, 668, 707, 709, 719, 737, 739, 752, 758, 766, 792, 792, 794, 802, 818, 830, 832, 843, 858, 860, 869, 918, 925, 953, 991, 1000, 1005, 1068, 1441 ADEI course. Bachelor in Informatics Engineering. Session 2. Teaching: Lidia Montero 3 Univariant vs multivariant outliers 1. Data is usually multivariate, i.e., multi-dimensional, whereas => basic model is assumed to be univariate, i.e., 1-dimensional 2. There is usually more than one generating mechanism/statistical process underlying the “normal” data; => basic model assumes only one “normal” generating mechanism, where outliers are rare observations. Outliers may represent a different class (generating mechanism) of objects, so there may be a large class of similar objects that are the outliers. 4 Outliers are multivariate Univariate detection of outliers doesn’t imply multivariate detection Univariate detection of outliers. The Boxplot The Boxplot (Tukey, 1977) is a graphical display for exploratory data analysis, where the outliers appear tagged. Two types of outliers are distinguished: mild outliers and extreme outliers. An observation xis declared an extreme outlier, if it lies outside of the interval (Q1-3 × IQR, Q3+3 × IQR),where IQR=Q3-Q1 is called the Interquartile Range. An observation xis declared a mild outlier if it lies outside of the interval (Q1-1.5 × IQR, Q3+1.5 × IQR). The numbers 1.5 and 3are chosen by comparison with a normal distribution. If x ~ Normal : Prob(X  Q3+1.5 × IQR)= 0.003488302 Prob(X  Q3+3 × IQR)= 1.170971e-06 5 Practice of detecting outliers •To obtain unbiased results in any statistical/learning algorithm. Including outliers in the training data may invalidate the results. •Once we have detected outliers, what we should do? –Eliminate them (but we loose information of the eliminated individuals) and deleting outliers is not the best solution, since outliers are recursive. –Weight the individuals inversely to outlying degree of individuals, to diminish its importance (but statistical/learning methods would need to had implemented a weighing option of individuals). – Make robust estimation of the parameters of the “normal generating mechanism”, for instance with a given percentage of the “central” individuals. –Declare univariant outliers as “missing values” and treat them as missing data. • Detecting “rare” events: –Fraud detection, –Detecting network intrusion – Detecting changes in the behavior (sales, claims, connections, waiting time, …) 6 Multivariate outliers But outliers are multivariate –Detection tools to be applied on complete data sets Univariate detection of outliers doesn’t imply multivariate detection Then, detection of outliers is based in computing distances to the central point of data, by means an Iterative algorithm 7 21 ( , ) ( ) ( ) M i i D i G x G V x G −  = − − Mahalanobis distance Mahalanobis Distance vs. Euclidean distance 8 22 dim space ( , ) M D i G   = If generating mechanism is Normal: 2 5   = Short distances occur more often Detection of multivariate outliers Take a value of h(size of data assumed not containing outliers), h must be > p(number of variables). Usual values = 0.95n (at most 5% of outliers) Initialization of an estimation of Gand V: G= mean of variables. V= matrix of variances 1. Compute the Mahalanobis distances D2M (i,G) for each point i. 2. Rank the D2M (i,G) and retain the hindividuals with lower D2M (i,G) 3. Update Gand Vtill convergence. 9 Plot the final “robustified” Mahalanobis distances with the initial Mahalanobis distances to detect the outliers: •mvoutlier library and method aq.plot() •chemometrics library and method Moutlier Treatment of missing values •Stochastic imputation (imputare = to fill in) Simulate actual data Stochastic regression imputation 16 ( / ) imputed y f y X  =+ 2 ˆ_ (0, ) imputed iresid y y random draw N s=+ Treatment of missing values Knn –K nearest neighbor imputation (easy to implement) •For every individual containing a missing value in a specific variable, we find another individual with minimal distance to the previous one and with complete information. •Then transfer (copy) the value of the specific variable, of the second individual to the first one. 17 knn function in R with only x as covariate with xand many other covariates (age, BMI, sex, …) Knn imputation 18 ? i Find the closest individual to i, according all variables except y kyk yk Copy the ykvalue in the i individual Complete data cases with ymissing MissMDA package in R: •imputePCA(X) for numeric variables only. •imputeMCA(X) for qualitative variables only. Knn imputation: y qualitative variable 19 i Find the closest individual to i, according all available variables except factor y kyk Yu? yucategory for uindividual would be the red one –category 1 u case with ymissing Find for each missing case, the most frequent category in the complete data set for closest neighbours. Easy to calculate in R Example: SwissLabor data in AER library participation Factor. Did the individual participate in the labor force? income Logarithm of nonlabor income. age Age in decades (years divided by 10). education Years of formal education. youngkids Number of young children (under 7 years of age). oldkids Number of older children (over 7 years of age). foreign Factor. Is the individual a foreigner (i.e., not Swiss)? 20 Usage data("SwissLabor") Format A data frame containing 872 observations on 7 variables. levels(SwissLabor$participation)<- paste("Parti.",sep="",levels(SwissLabor$participation)) levels(SwissLabor$foreign)<- paste("Foreign.",sep="",levels(SwissLabor$foreign)) Example: SwissLabor data in AER library library(mvoutlier) vout<-aq.plot(SwissLabor[,2:4], delta=qchisq(0.95, df=ncol(x)),alpha=0.05) 21 Example: SwissLabor data in AER library > library(chemometrics) > dis <- Moutlier(SwissLabor[,2:4], quantile = 0.995) > plot(dis$md,dis$rd, type="n") > text(dis$md,dis$rd,labels=rownames(SwissLabor[,2:4])) > abline(h=qchisq(0.995, col(SwissLabor[,2:4])),col="red",lwd=2) > str(dis) # List of 3 $ md : Named num [1:872] 1.203 0.667 0.927 1.332 1.272 ... ..- attr(*, "names")= chr [1:872] "1" "2" "3" "4" ... $ rd : Named num [1:872] 1.207 0.763 1.072 1.575 1.431 ... ..- attr(*, "names")= chr [1:872] "1" "2" "3" "4" ... $ cutoff: num 3.58 > SwissLabor$mout<-0 > sel<-which(dis$rd>12) > SwissLabor[sel,"mout"]<-1 22 Example: SwissLabor in AER library - Imputation > llista<-sample(1:nrow(SwissLabor),40);llista > df<-SwissLabor > df[llista,"age"]<-NA > library(missMDA) # Numeric imputation > vars_con<-names(df)[2:6] > summary(df[,vars_con]) > res.input<-imputePCA(df[,vars_con],ncp=4) > summary(res.input$completeObs) > par(mfrow=c(1,3)) > hist(df$age,col="red") > hist(SwissLabor$age,col="green") > hist(res.input$completeObs[,2],col="blue") > quantile(df$age,seq(0,1,0.1),na.rm=T) > quantile(SwissLabor$age,seq(0,1,0.1),na.rm=T) > round(quantile(res.input$completeObs[,2],seq(0,1,0.1),na.rm=T),dig=1) 23 Example: SwissLabor data in AER library - Imputation 24 > quantile(df$age,seq(0,1,0.1),na.rm=T) 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% 2.0 2.6 3.0 3.3 3.6 3.9 4.3 4.6 5.0 5.5 6.2 > quantile(SwissLabor$age,seq(0,1,0.1),na.rm=T) 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% 2.0 2.6 3.0 3.3 3.6 3.9 4.3 4.6 5.0 5.5 6.2 > round( quantile(res.input$completeObs[,2],seq(0,1,0.1),na.rm=T),dig=1) 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% 2.0 2.6 3.0 3.3 3.6 4.0 4.3 4.6 4.9 5.5 6.2 > > Example: SwissLabor data in AER library - Imputation R Code imputeMCA() > llista<- sample(1:nrow(SwissLabor),40);llista > df<-SwissLabor > df[llista,“participation"]<-NA > library(missMDA) # Categorical imputation > vars_dis<-names(df)[c(1,7)] > summary(df[,vars_dis]) > nb <- estim_ncpMCA(df[, vars_dis],ncp.max=25) > res.input<-imputeMCA(df[,vars_dis],ncp=10) > summary(res.input$completeObs) Results •Check category frequences •For the given example, with such a few factors, the example code does not work 25