Ives Cavalcante
Ives Cavalcante

Reputation: 3

Code with 10 fold cross validation in machine learning

I am just starting to work with machine learning. I tried to run a 10 fold cross-validation using a C5.0 model. I asked the code to return the kappa value.

folds = createFolds(mdd.cohort1$edmsemmancomprej, k=10)
str(folds)
mdd.cohort1_train = mdd.cohort1[-folds$Fold01,]
mdd.cohort1_test = mdd.cohort1[folds$Fold01,]
library(caret)
library(C5.0)
library(irr)
set.seed(123)
folds = createFolds(mdd.cohort1$edmsemmancomprej, k=10)

cv_results = lapply(folds, function(x) 
{mdd.cohort1_train = mdd.cohort1[-x, ] 
mdd.cohort1_test = mdd.cohort1[x, ] 
mdd.cohort1_model = C5.0(edmsemmancomprej ~., data = mdd.cohort1_train) 
mdd.cohort1_pred = predict(mdd.cohort1_model, mdd.cohort1_test) 
mdd.cohort1_actual = mdd.cohort1_test$edmsemmancomprej 
kappa = kappa2(data.frame(mdd.cohort1_actual, mdd.cohort1_pred))$value    return(kappa)})

Gives the following error menssage:

Error: unexpected symbol in:
"mdd.cohort1_actual = mdd.cohort1_test$edmsemmancomprej
kappa = kappa2(data.frame(mdd.cohort1_actual, mdd.cohort1_pred))$value return"

Does anyone knows what happened? Thank you so much in advance!

Upvotes: 0

Views: 1204

Answers (1)

JackStat
JackStat

Reputation: 1653

It is a bit difficult without a reproducible example but I think that the return sharing that last line is the cause. I reformatted your code a bit for readability

library(caret)
library(C5.0)
library(irr)   

folds = createFolds(mdd.cohort1$edmsemmancomprej, k=10)

str(folds)

mdd.cohort1_train = mdd.cohort1[-folds$Fold01,]
mdd.cohort1_test = mdd.cohort1[folds$Fold01,]

set.seed(123)
folds = createFolds(mdd.cohort1$edmsemmancomprej, k=10)

cv_results = lapply(folds, function(x) {
  mdd.cohort1_train = mdd.cohort1[-x, ] 
  mdd.cohort1_test = mdd.cohort1[x, ] 
  mdd.cohort1_model = C5.0(edmsemmancomprej ~., data = mdd.cohort1_train) 
  mdd.cohort1_pred = predict(mdd.cohort1_model, mdd.cohort1_test) 
  mdd.cohort1_actual = mdd.cohort1_test$edmsemmancomprej 
  kappa = kappa2(data.frame(mdd.cohort1_actual, mdd.cohort1_pred))$value    
  return(kappa)
  })

Upvotes: 1

Related Questions