You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Caret包gafs函数报错:promise already under evaluation问题求助

解决caret包gafs函数递归参数报错问题

问题场景

使用caret包的gafs()函数通过遗传算法进行特征选择,目标变量y为clin.info数据框的subtype列,clin.info的行名与met.deconv.relative的列名一一对应。

原执行代码

library(caret)
obj <- gafs(x = met.deconv.relative, 
            y = as.factor(clin.info[,"subtype"]),
            iters = 100)

报错信息

Error in gafs.default(x = met.deconv.relative, y = as.factor(clin.info[,  : 
  promise already under evaluation: recursive default argument reference or earlier problems?

输入数据结构

met.deconv.relative(前10行10列)

> dput(met.deconv.relative[1:10,1:10])
structure(list(TCGA.Y8.A8S1.01 = c(0.550828247772734, 0, 0.331193755024188, 
0.0397937067092402, 0, 0, 0.0123106244532931, 0.0429490913967326, 
0.020395397358106, 0), TCGA.Y8.A8RZ.01 = c(0, 0, 0, 0, 0, 0, 
0.00723028292077139, 0.414997365852491, 0.577772351226738, 0), 
    TCGA.Y8.A8RY.01 = c(0.867244835975172, 0, 0, 0.0180277687859282, 
    0, 0, 0.0733546839226409, 0.0413727113162587, 0, 0), TCGA.Y8.A897.01 = c(0.200014611553106, 
    0, 0.307165518738796, 0.243888136397938, 0, 0, 0.24893173331016, 
    0, 0, 0), TCGA.Y8.A896.01 = c(0.699516108255348, 0, 0, 0, 
    0, 0, 0.300483891744652, 0, 0, 0), TCGA.Y8.A895.01 = c(0.688926101620937, 
    0, 0.19723212494978, 0, 0.0058672819613848, 0, 0.0704367767769461, 
    0.0375377146909525, 0, 0), TCGA.Y8.A894.01 = c(0, 0.481688485576903, 
    0, 0.356287482154964, 0, 0, 0.162024032268133, 0, 0, 0), 
    TCGA.WN.A9G9.01 = c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0), TCGA.V9.A7HT.01 = c(0.721893680326815, 
    0, 0, 0.141791379325406, 0.0210361494183095, 0, 0.11527879092947, 
    0, 0, 0), TCGA.UZ.A9Q0.01 = c(0.403189710486653, 0.0991244095085273, 
    0, 0.142388711545978, 0, 0, 0.355297168458842, 0, 0, 0)), row.names = c("Monocytes", 
"Dendritic Cells", "Macrophages", "Neutrophils", "Eosinophils", 
"Regulatory T cells", "Naive T cells", "Memory T cells", "CD8 T cells", 
"NK cells"), class = "data.frame")

clin.info(前10行的14-15列)

> dput(clin.info[1:10,14:15])
structure(list(subtype = c("2a", "1a", "1a", "1a", "1b", "2b", 
"1a", "2a", "1a", "1c"), type = c("2", "1", "1", "1", "1", "2", 
"1", "2", "1", "1")), row.names = c("TCGA.Y8.A8S1.01", "TCGA.Y8.A8RZ.01", 
"TCGA.Y8.A8RY.01", "TCGA.Y8.A897.01", "TCGA.Y8.A896.01", "TCGA.Y8.A895.01", 
"TCGA.Y8.A894.01", "TCGA.WN.A9G9.01", "TCGA.V9.A7HT.01", "TCGA.UZ.A9Q0.01"), class = "data.frame")

解决方案及原因分析

核心问题

gafs()函数要求输入的x是样本为行,特征为列的数据结构,但你的met.deconv.relative是特征为行、样本为列的格式,这会导致函数内部参数匹配出现递归引用错误。此外,未明确指定遗传算法的控制参数和基础模型,默认参数配置不全也会触发此类报错。

修正步骤

  • 转置x矩阵:将met.deconv.relative转置,使样本作为行,特征作为列,同时确保样本顺序与y一致。
  • 明确配置gafs控制参数:指定遗传算法的迭代次数、种群规模等,同时指定基础分类模型(比如rf随机森林,需提前加载randomForest包)。
  • 提前转换y为因子:避免在函数参数内直接转换,减少潜在的参数解析问题。

修正后的代码

library(caret)
library(randomForest)

# 转置特征矩阵,确保样本为行、特征为列
x_transposed <- t(met.deconv.relative)
# 确保x和y的样本顺序完全匹配
x_transposed <- x_transposed[rownames(clin.info), ]
y_factor <- as.factor(clin.info$subtype)

# 定义gafs控制参数
gafs_ctrl <- gafsControl(functions = rfGA, # 使用随机森林作为基础模型
                         method = "cv", # 交叉验证方式
                         number = 5, # 5折交叉验证
                         verbose = TRUE)

# 运行遗传算法特征选择
obj <- gafs(x = x_transposed, 
            y = y_factor,
            iters = 100, # 遗传算法迭代次数
            gafsControl = gafs_ctrl)

补充说明

  • 若使用其他基础模型,需替换rfGA为对应模型的GA函数(比如ldaGA对应线性判别分析),并确保加载相应包。
  • 若样本顺序已经完全匹配,x_transposed <- x_transposed[rownames(clin.info), ]这一步可以省略,但建议保留以避免潜在的样本错位问题。

内容的提问来源于stack exchange,提问作者Anon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 05:34:52