机器学习中具有 10 折交叉验证的代码

Code with 10 fold cross validation in machine learning

我刚刚开始使用机器学习。我尝试 运行 使用 C5.0 模型进行 10 折交叉验证。我要求代码 return kappa 值。

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)})

给出以下错误消息:

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

有谁知道发生了什么事吗?非常感谢您!

没有可重现的例子有点困难,但我认为 return 共享最后一行是原因。为了便于阅读,我重新格式化了您的代码

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)
  })