【问题标题】:Columns not available for when training lasso model using caret使用插入符号训练套索模型时,列不可用
【发布时间】:2019-02-09 18:54:21
【问题描述】:

我遇到了一个奇怪的错误

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

当我使用插入符号训练 glmnet 模型时的消息。我对序数模型使用了基本相同的代码和相同的预测变量(只是使用了不同的因子ythen)并且效果很好。计算需要 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"
)

我找不到任何明显的数据错误。没有 NA

table(is.na(training))

FALSE 
43166

并且不明白为什么它会尝试在列数之外建立索引。

有什么建议吗?

【问题讨论】:

  • 我已将您的标签 caret 更改为 r-caret。由于您的问题的解决方案相当简单,我相信只要您使用正确的标签,您就可以更快地获得它。

标签: r r-caret training-data glmnet


【解决方案1】:

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

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

我会将此建议作为评论留下,但我无法做到(因为我的声望不到 50!)

【讨论】:

  • 不确定我是否理解您的解决方案。我只有一个因素(RC),将其更改为角色并不能解决任何问题。我仍然遇到同样的错误。
【解决方案2】:

您必须在 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,如果您的问题已经得到阐明,请随时验证答案。最好的问候。
  • 是的。很抱歉!
猜你喜欢
  • 2019-10-01
  • 2018-05-28
  • 2019-08-30
  • 2014-11-06
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 2015-12-30
  • 2017-08-19
相关资源
最近更新 更多