# Imputing missing values on a testing set

I'm a newbie to machine learning so forgive me if the answer to this question is obvious. I have been working on a binary prediction problem using logistic regression. Using a selection of categorical and continuous I have been able to predict accuracy on a testing set with an AUC of about $0.7$. I have been comparing multiple data pre-processing approaches where I carry out combinations of various filtering steps which are:

• no data filtering
• removing mean based outliers without replacement
• removing mean based outliers with mean replacement & additionally replacing NA's with the mean.
• removing median absolute deviation outliers without replacement
• removing median absolute deviation outliers with mean replacement & additionally replacing NA's with the mean.
• repeating the above 5 procedures on a data set that has all of the NA's removed.

I find that my model is the most predictive on a testing set whenever I remove all median absolute deviation outliers and replace them with the mean and additionally replace pre-exisiting NA's with the mean.

Is it OK to impute mean based missing values with the mean whenever implementing the model?

Thanks!

Is it ok to impute mean based missing values with the mean whenever implementing the model?

Yes, as long as you use the mean of your training set---not the mean of the testing set---to impute. Likewise, if you remove values above some threshold in the test case, make sure that the threshold is derived from the training and not test set.

You might also consider holding out two "test" sets and trying all of the methods described above on one of them (using this set to "select" a method) and using the second to estimate error of the method that works best (using this set to "evaluate" the selected method). You would then have a train-validation-test split, which is good practice.

• This is copied and pasted from a comment I made above above however it applies to your answer and I would be interested in a response.... wouldn't the use of the training mean to impute for both/either or missing values and and outliers on the testing set be a kind of data leakage to the test set? Then the model would of course perform better than it should because there is data in the testing set that is based upon training data (the same data used to create said model)? – steve zissou Sep 5 '17 at 7:39

Yes.

It is fine to perform mean imputation, however, make sure to calculate the mean (or any other metrics) only on the train data to avoid data leakage to your test set.

• Many thanks for your response. However, wouldn't the use of the training mean to impute for both/either or missing values and and outliers on the testing set be a kind of data leakage to the test set? Then the model would of course perform better than it should because there is data in the testing set that is based upon training data (the same data used to create said model)? – steve zissou Sep 5 '17 at 7:38
• All your training data shoud be available during the test phase (and in fact is implicitly used in your model). Just note that in case of k-fold CV the train and test might switch roles numerous times – Dimgold Sep 5 '17 at 15:20