使用caret训练lasso模型时,列不可用

8

我遇到了一个奇怪的错误

Error in `[.data.frame`(data, , lvls[1]) : undefined columns selected

在使用caret训练glmnet模型时,我遇到了一条消息。我基本上使用相同的代码和相同的预测变量来创建序数模型(只是使用不同的因子y),并且它可以正常工作。由于计算需要400个核心小时,所以我无法在此处展示它。

#Source a small subset of data
source("https://gist.githubusercontent.com/FredrikKarlssonSpeech/ebd9fccf1de6789a3f529cafc496a90c/raw/efc130e41c7d01d972d1c69e59bf8f5f5fea58fa/voice.R")
trainIndex <- createDataPartition(notna$RC, p = .75, 
                                  list = FALSE, 
                                  times = 1)


training <- notna[ trainIndex[,1],] %>%
  select(RC,FCoM_envel:ATrPS_freq,`Jitter->F0_abs_dif`:RPDE)
testing  <- notna[-trainIndex[,1],] %>%
  select(RC,FCoM_envel:ATrPS_freq,`Jitter->F0_abs_dif`:RPDE)

fitControl <- trainControl(## 10-fold CV
  method = "CV",
  number = 10,
  allowParallel=TRUE,
  savePredictions="final",
  summaryFunction=twoClassSummary)

vtCVFit <- train(x=training[-1],y=training[,"RC"], 
                  method = "glmnet", 
                  trControl = fitControl,
                  preProcess=c("center", "scale"),
                  metric="Kappa"
)

我没有发现数据中明显的问题。没有缺失值。

table(is.na(training))

FALSE 
43166

我不认为它会尝试索引超出列数的内容。有什么建议吗?

我已将您的标签“caret”更改为“r-caret”。由于您的问题的解决方案相当简单,我相信如果您使用了正确的标签,您本可以更快地获得答案。 - missuse
2个回答

5

你需要在trainControl()中删除summaryFunction=twoClassSummary。这对我有效。

fitControl <- trainControl(## 10-fold CV
 method = "CV",
 number = 10,
 allowParallel=TRUE,
 savePredictions="final")

vtCVFit <- train(x=training[-1],y=training[,"RC"], 
method = "glmnet", 
 trControl = fitControl,
preProcess=c("center", "scale"),
metric="Kappa")

 print(vtCVFit)

#glmnet 

#113 samples
#381 predictors
#  2 classes: 'NVT', 'VT' 

#Pre-processing: centered (381), scaled (381) 
#Resampling: Bootstrapped (25 reps) 
#Summary of sample sizes: 113, 113, 113, 113, 113, 113, ... 
#Resampling results across tuning parameters:

#  alpha  lambda      Accuracy   Kappa    
#  0.10   0.01113752  0.5778182  0.1428393
#  0.10   0.03521993  0.5778182  0.1428393
#  0.10   0.11137520  0.5778182  0.1428393
#  0.55   0.01113752  0.5778182  0.1428393
#  0.55   0.03521993  0.5748248  0.1407333
#  0.55   0.11137520  0.5749980  0.1136131
#  1.00   0.01113752  0.5815391  0.1531280
#  1.00   0.03521993  0.5800217  0.1361240
#  1.00   0.11137520  0.5939621  0.1158007

#Kappa was used to select the optimal model using the largest value.
#The final values used for the model were alpha = 1 and lambda = 0.01113752.

你好,Fredrik。如果你的问题已经得到解决,请随时确认答案。最好的问候。 - Alex Yahiaoui Martinez
是的。对不起,我的错! - Fredrik Karlsson

2

通过以下代码将您的因素更改为字符,然后查看是否有效:

      training <- data.frame(lapply(training , as.character), stringsAsFactors=FALSE)

我原本想把这个建议留作评论,但由于我的声望不足50分,所以无法这样做。


我不确定我理解你的解决方案。我只有一个因素(RC),将其更改为字符对我来说没有任何帮助。我仍然得到相同的错误。 - Fredrik Karlsson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接