Im trying to use the train() function from the Caret package for fitting a K Nearest Neighbor model. This gives me an error. My code is:
"%+%" <- function(x,y) paste(x, y, sep = "")
set.seed(28)
ContEnt <- trainControl(method = "repeatedcv", number = 10, repeats = 3)
EducKnn <- train(as.formula("pp04b_cod ~ " %+% paste(VarEduc[!VarEduc %in% NoRel], collapse
= " + ")), EducPrueba, method = "knn", trctrl = ContEnt,
tuneLength = 10)
This gives me in return:
Warning: predictions failed for Resample01: k= 5 Error in knn3Train(train = structure(c(0.569069692629571, 0.569069692629571, :
unused argument (trctrl = list("cv", 10, NA, "grid", 0.75, NULL, 1, TRUE, 0, FALSE, TRUE, "final", FALSE, FALSE, function (data, lev = NULL, model = NULL)
{
if (is.character(data$obs)) data$obs <- factor(data$obs, levels = lev)
postResample(data[, "pred"], data[, "obs"])
}, "best", list(0.95, 3, 5, 19, 10, 0.9), NULL, NULL, NULL, NULL, 0, c(FALSE, FALSE), NA, list(5, 0.05, "gls", TRUE), FALSE, TRUE))
Many similar warning messeges, and at the end:
Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info =
trainInfo, :
There were missing values in resampled performance measures.
Something is wrong; all the Accuracy metric values are missing:
Accuracy Kappa
Min. : NA Min. : NA
1st Qu.: NA 1st Qu.: NA
Median : NA Median : NA
Mean :NaN Mean :NaN
3rd Qu.: NA 3rd Qu.: NA
Max. : NA Max. : NA
NA's :10 NA's :10
Error: Stopping
>
Setting parameters to avoid cross validation, doesnt seems to fix it.
ContEnt <- trainControl(method = "none")
Also, setting na.action = na.omit in train() gives the same results. Interestingly, the knn() function of the class package, works flawlessly, with a .75 training set, over the same set of variables.
Entre <- createDataPartition(EducPrueba$pp04b_cod, 1, 0.75, list = FALSE)
EducKnn <- knn(train = EducPrueba[Entre, VarEduc[!VarEduc %in% NoRel]], test = EducPrueba[-Entre,
VarEduc[!VarEduc %in% NoRel]], cl = EducPrueba$pp04b_cod[Entre], k = 5)
Finally, to be sure, EducPrueba has no NAs or NaNs:
> any(is.na(EducPrueba))
[1] FALSE
> any(unlist(lapply(EducPrueba, is.nan)))
[1] FALSE
The varibles in VarEduc are already centered and scaled. Does anyone know how to make it work? Im using R Portable 3.5.2 with RStudio. Packages caret 6.0-81 and class 7.3-15. I dont know how to upload the dataframe, so this can be a reproducible example.