繁体   English   中英

在插入符号中使用 LmFuncs(线性回归)进行递归特征消除:如何修复“x 和 y 中的样本数相同”错误?

[英]Using LmFuncs (Linear Regression) in Caret for Recursive Feature Elimination: How do I fix "same number of samples in x and y" error?

我是 R 新手,并试图从 247 列(246 个变量 + 1 个结果)和 800 左右行(其中每一行是一个人的数据)的数据集中分离出性能最佳的特征来创建预测模型。 我正在使用插入符号使用lmfuncs进行 RFE - 我需要使用线性回归,因为目标变量是连续的。

我使用以下内容拆分为测试/训练数据(没有引发错误)

inTrain <- createDataPartition(data$targetVar, p = .8, list = F)
train <- data[inTrain, ]
test <- data[-inTrain, ]

生成的测试和训练文件在集合中甚至包含变量。 例如 X 和 Y 包含相同数量的样本/所有列的长度相同

我的控制参数如下(也运行无错误)

control = rfeControl(functions = lmFuncs, method = "repeatedcv", repeats = 5, verbose = F, returnResamp = "all")

但是当我运行 RFE 时,我收到一条错误消息说

rfe.default(train[, -1], train[, 1], sizes = c(10, 15, 20, 25, 30), rfeControl = control) 中的错误:x 和是的

我的 RFE 代码如下,目标变量在第一列rfe_lm_profile <- rfe(train[, -1], train[, 1], sizes = c(10, 15, 20, 25, 30), rfeControl = control)

我浏览了各种论坛,但似乎没有任何效果。 这个 google.group 建议使用旧版本的 Caret - 我试过了,但得到了相同的 X/Y 错误https://groups.google.com/g/rregrs/c/qwcP0VGn4ag?pli=1其他人建议转换目标变量为因子或矩阵。 这没有帮助,并引发警告消息:在 createDataPartition(data$EBI_SUM, p = 0.8, list = F) 中:某些类在将数据划分为测试/训练时只有一条记录,并且相同的 X/Y 样本错误如果您尝试执行 RFE。

超级感谢提前:)

ps 这是目标变量 (EBI_SUM) 和几个变量的 dput

data <- structure(list(TargetVar = c(243, 243, 243, 243, 355, 355), Dosing = c(2, 
2, 2, 2, 2, 2), `QIDS_1 ` = c(1, 1, 3, 1, 1, 1), `QIDS_2 ` = c(3, 
3, 2, 3, 3, 3), `QIDS_3 ` = c(1, 2, 1, 1, 1, 2)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))
> 

您的data对象不应包含空格:

library(caret)

data <- data.frame(
  TargetVar = c(243, 243, 243, 243, 355, 355),
  Dosing = c(2, 2, 2, 2, 2, 2),
  QIDS_1 = c(1, 1, 3, 1, 1, 1),
  QIDS_2 = c(3, 3, 2, 3, 3, 3),
  QIDS_3 = c(1, 2, 1, 1, 1, 2)
)

inTrain <- createDataPartition(data$TargetVar, p = .8, list = F)
train <- data[inTrain, ]
test <- data[-inTrain, ]
control <- rfeControl(functions = lmFuncs, method = "repeatedcv", repeats = 5, verbose = F, returnResamp = "all")
rfe_lm_profile <- rfe(train[, -1], train[, 1], sizes = c(10, 15, 20, 25, 30), rfeControl = control)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM