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?
-
4$\begingroup$ 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? $\endgroup$– Tim ♦Apr 5, 2015 at 20:35
-
1$\begingroup$ 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. $\endgroup$– FredrikApr 6, 2015 at 17:48
-
3$\begingroup$ 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. $\endgroup$– Tim ♦Apr 6, 2015 at 18:03
-
3$\begingroup$ 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. $\endgroup$– mpiktasAug 20, 2015 at 6:25
3 Answers
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.
-
1$\begingroup$ 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? $\endgroup$ Oct 21, 2017 at 9:25
-
$\begingroup$ If one uses the x, y specification in
train
having thena.action = na.pass
option set will cause the following error:Something is wrong; all the RMSE metric values are missing
$\endgroup$ Jul 22, 2019 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, ...)
from http://www.inside-r.org/packages/cran/caret/docs/predict.train
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,
)