# R caret and NAs

I very much prefer caret for its parameter tuning ability and uniform interface, but I have observed that it always requires complete datasets (i. e. without NAs) even if the applied "naked" model allows NAs. That is very bothersome, regarding that one should apply laborous imputation methods, which are not necessary in the first place. How could one evade the imputation and still use caret advantages?

• You always have to do something with missing values. I must say I don't really understand your question - you are looking for some one-size-fit-all approach..? If you don't want to imput NAs, then what do you want to do with them? Delete? – Tim Apr 5 '15 at 20:35
• I want to leave NAs there and leave it to the model to cope with NAs. If I do it with a C5.0 function in C50, for example, it could cope with NAs itself, but in this case I cannot use caret, because caret's train function allows no NAs in datasets even when I want to use the C5.0 function of C50 mentioned above. – Fredrik Apr 6 '15 at 17:48
• But what "model" does is it either ignores (deletes) this data leaving you with smaller sample; it estimates (imputs) those values; or it predicts the "NA" category (e.g. in some tree based models). What else would you like your "model" to do? Some software does those things for you automatically, but imagine that your coffee machine gave you the "default" coffee... Some software makes the "default coffee" out of NAs, but it is not the best you can get. – Tim Apr 6 '15 at 18:03
• Are you sure that caret does not allow NAs? I've tried introducing NA with the default example in train help page and with method C5.0 train worked just fine. It failed with random forest. – mpiktas Aug 20 '15 at 6:25

To the train function in caret, you can pass the parameter na.action = na.pass, and no preprocessing (do not specify preProcess, leave it as its default value NULL). This will pass the NA values unmodified directly to the prediction function (this will cause prediction functions that do not support missing values to fail, for those you would need to specify preProcess to impute the missing values before calling the prediction function). For example:

train(formula,
dataset,
method = "C5.0",
na.action = na.pass)


In this case, C5.0 will handle missing values by itself.

• This is an interesting discussion. What would be the pitfall of adding NA as another level to a categorical predictor? If the NAs cannot be modelled or imputed, i.e. the presence of an absence is actually informative, it would seem that simply making NA an additional level makes sense? – Seanosapien Oct 21 '17 at 9:25
• If one uses the x, y specification in train having the na.action = na.pass option set will cause the following error: Something is wrong; all the RMSE metric values are missing – user29609 Jul 22 at 19:23

Have you tried recoding the NAs? Something >3 standard deviations outside your data (e.g. -12345) should encourage C5.0 to predict them separately, like it does with NAs.

I think your solution would be to impute the values while using the predict() function.

See ?predict.train for more details.

You can use na.omit to allow caret to impute values. For example:

    ## S3 method for class 'train':
predict((object, newdata = NULL, type = "raw", na.action = na.omit, ...)


Another solution would be to impute while preprocessing the data:

    ## S3 method for class 'default':
preProcess(x,
method = "knnImpute",   # or *bagImpute* / *medianImpute*
pcaComp = 10,
na.remove = TRUE,
k = 5,
knnSummary = mean,
outcome = NULL,
fudge = .2,
numUnique = 3,
verbose = TRUE,
)