1

Since the model (package fastNaiveBayes) that I am using is not in the built-in library of the caret package, I am trying to make a k-fold cross validation in R without using the caret package. Does anyone have a solution to this?

Edit: Here is my code so far from what I learned on how to do cv without caret. I am very certain something is wrong here.

library(fastNaiveBayes)


k<- 10
outs <- NULL
proportion <- 0.8
for (i in 1:10)
{
  split <- sample(1:nrow(data), round(proportion*nrow(data)))
  traindata <- data[split,]
  testdata <- data[-split,]

y <- traindata$Label

x <- traindata[,0 - 15:ncol(traindata)]

model <- fnb.train(x, y=y, priors = NULL, laplace=0, 
               distribution = fnb.detect_distribution(x, nrows = nrow(x)))
model




test1 <- testdata[,0 - 15:ncol(testdata)]
pred <- predict(model, newdata = test1)


cm<- table(testdata$Label, pred)


print(confusionMatrix(cm))

}

It gave me 10 different results and I think that's not how it cross validation is supposed to work. I'm an entry-level R learner and I appreciate so much to receive enlightenment from this

  • You could do it with `vfold_cv()` from the `rsample` package, which is particularly useful if you're working in a tidy framework. Otherwise, you could sample observation numbers for the groups using `sample(), then loop over the groups to create the cross-validation error. – DaveArmstrong Feb 25 '22 at 14:35
  • I answered a question not that long ago about K-fold validation. [See if this helps](https://stackoverflow.com/questions/71208263/preparing-test-train-sets-for-cross-validaton-in-a-loop/71213227#71213227). If it doesn't help or doesn't work for you, provide an abbreviated data sample (like the output from `dput(head(dataObject))` and provide the implementation method (with arguments). That way, it can be set up properly. Welcome to the community! If you want great answers, make your questions reproducible. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). – Kat Feb 25 '22 at 14:51
  • Thank you for your comments! I edited my question and included my code, would be really happy to know how it should be done instead :) – Charles Darwin Feb 25 '22 at 15:00

0 Answers0