R 中的弹性网络问题 - check_dims(x = x, y = y) 中的错误:nrow(x) == n 不是 TRUE

问题描述

错误:nrow(x) == n 不正确

我不确定在这种情况下“n”指的是什么。这是抛出错误的代码:

# BUILD MODEL 
set.seed(9353)
elastic_net_model <- train(x = predictors,y = y,method = "glmnet",family = "binomial",preProcess = c("scale"),tuneLength = 10,metric = "ROC",# metric = "Spec",trControl = train_control)

其他人遇到此错误的主要问题是他们的 y 变量不是因子或数字。他们经常将它作为矩阵或数据框传递。我明确地将我的 y 作为一个因素,如下所示:

# Make sure that the outcome variable is a two-level factor
dfBlocksAll$trophout1 = as.factor(dfBlocksAll$trophout1)

# Set levels for dfBlocksAll$trophout1
levels(dfBlocksAll$trophout1) <- c("NoTrophy","Trophy")

# Split the data into training and test set,70/30 split
set.seed(1934)
index <- createDataPartition(y = dfBlocksAll$trophout1,p = 0.70,list = FALSE)
training  <- dfBlocksAll[index,]
testing <- dfBlocksAll[-index,]

# This step is the heart of the process
y <- dfBlocksAll$trophout1 # outcome variable - did they get a trophy or not?
predictors <- training[,which(colnames(training) != "trophout1")]

在引发错误的块之前出现的唯一其他可能相关的代码是:

train_control <- trainControl(method = "repeatedcv",number = 10,repeats = 10,# sampling = "down",classProbs = TRUE,summaryFunction = twoClassSummary,allowParallel = TRUE,savePredictions = "final",verboseIter = FALSE)

因为我的 y 已经是一个因素,我假设我的错误与 x 有关系,而不是 y。从代码中可以看出,我的 x 是一个名为“预测器”的数据框。此数据框包含 768 个 obs。 67 个变量,并用字符和数字填充。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)