随机森林模型中递归特征消除的特征选择错误

Error in Feature selection with Recursive feature elimination in random forest model

我有几百个样本,我已经将它们分为四个不同的类(簇)。现在,我有兴趣确定将样本分类为不同 类 的最佳基因集。

我想应用具有递归特征消除的随机森林并检测基因(特征)。我的数据如下所示。只是在这里发布一些示例数据。

以上数据只是一个例子:我的原始数据在数据框df中,第一列有100个样本,第二列有4个类,第3列到第1002列总共有1000个具有表达值的基因。

我正在使用以下代码,但发现有错误。

library(caret)
library(mlbench)
library(Hmisc)
library(randomForest)

# define the control using a random forest selection function
control <- rfeControl(functions=rfFuncs, method="cv", number=10)

# run the RFE algorithm
results <- rfe(df[,3:1002], df[,2], sizes = df[,1:1002], rfeControl=control)

出现错误:我觉得我哪里做错了。

Error in summary.connection(connection) : invalid connection

这里我给出的是上述数据的dput

df <-structure(list(Samples = structure(c(1L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("Sample1", 
"Sample10", "Sample11", "Sample12", "Sample13", "Sample14", "Sample15", 
"Sample2", "Sample3", "Sample4", "Sample5", "Sample6", "Sample7", 
"Sample8", "Sample9"), class = "factor"), Class = structure(c(1L, 
2L, 3L, 1L, 2L, 4L, 2L, 1L, 1L, 4L, 1L, 3L, 4L, 1L, 1L), .Label = c("Class1", 
"Class2", "Class3", "Class4"), class = "factor"), Gene1 = c(1.030078784, 
0.944152632, 0.140700452, 0.013432323, 0.265233165, -0.084496727, 
4.835469554, 0.089434913, -0.433436179, 1.462895475, -0.116005356, 
1.007868422, 0.244881864, -1.495666899, 0.364368654), Gene2 = c(1.407236415, 
1.229003431, -0.322221459, -1.361955252, 0.310963955, 0.80115063, 
4.27765356, 0.872413223, -0.568249851, 1.187873069, -0.255284575, 
1.878058722, -0.767371822, -0.859697473, 0.057304769), Gene3 = c(0.200772234, 
-0.048349737, 1.224274924, 0.492396142, 0.500786902, -0.731802706, 
1.853246564, 1.611995455, 0.287088678, 0.509235514, 2.031735375, 
3.074950771, 2.069407179, 0.886158642, 1.736798303), Gene4 = c(1.23309207, 
1.321282889, 2.403301108, 0.748860637, 1.019200751, 1.393254607, 
2.667976275, 1.158136576, 1.89503732, 2.178257717, 0.747697632, 
2.834410716, 0.028594536, -0.411039831, 1.100167946), Gene5 = c(0.883005616, 
0.570786704, 0.72649548, 4.705893892, 0.086345885, 0.502530136, 
2.681497202, 0.640362079, 0.327319762, 2.086767741, 1.853085301, 
1.001799748, 0.126208601, 0.911621722, 0.671191951), Gene6 = c(2.590519025, 
3.076688902, 1.77414005, 1.014363629, 1.134652225, 2.71957962, 
4.696379063, -0.301828123, 1.214261665, 2.413881644, -0.470794827, 
0.520494891, 0.194511306, 0.075331863, 2.315680177), Gene7 = c(0.088929673, 
0.472549468, -0.125630236, -0.069648505, -0.715250242, 0.068554966, 
4.131662998, -0.075265565, -1.234425917, 0.343350342, 0.190414782, 
1.153495806, 0.210317581, -0.475603641, 0.294299351), Gene8 = c(2.112231178, 
2.780100532, 2.423828553, 1.569215682, 1.018119196, 2.583413401, 
6.483053565, 2.215201821, 1.893325529, 2.342058862, 4.001423142, 
4.221704757, 1.978211867, 1.452633851, 2.556589741)), class = "data.frame", row.names = c(NA, 
-15L))

谁能告诉我如何使用上述数据并应用随机森林来了解哪些基因将样本分类为不同的 类。谢谢

sizes 指的是您想尝试保留的功能数量,它应该是数字,但您在 df[,1:1002] 中提供了一些奇怪的东西。

看下面类似的内容,我在其中模拟数据集并正确设置大小以确保它运行以选择最佳数量的特征(根据您提供的特征):

library(caret)
library(mlbench)
library(Hmisc)
library(randomForest) 

set.seed(101)
df = data.frame(samples=paste0("Samples",1:99),
                Class=paste0("Class",rep(1:3,33)),
                matrix(rnorm(99*1000),ncol=1000))

colnames(df)[3:ncol(df)]=paste0("Gene",1:1000)

# we create like 100 informative genes for Class1 and Class2
df[df$Class=="Class1",3:103] = df[df$Class=="Class1",3:103] + rpois(33*100,1.5)
df[df$Class=="Class2",104:203] = df[df$Class=="Class2",104:203] + rpois(33*100,1.5)

control <- rfeControl(functions=rfFuncs, method="cv", number=2)

# run the RFE algorithm
results <- rfe(df[,3:1002], df[,2], sizes = c(50,100,200), 
               rfeControl=control)

从上面,我要求 50,100 或 200 个信息特征,我得到:

results
Recursive feature selection

Outer resampling method: Cross-Validated (2 fold) 

Resampling performance over subset size:

 Variables Accuracy  Kappa AccuracySD KappaSD Selected
        50   0.9792 0.9688    0.02946 0.04419         
       100   0.9896 0.9844    0.01473 0.02210         
       200   1.0000 1.0000    0.00000 0.00000        *
      1000   1.0000 1.0000    0.00000 0.00000         

The top 5 variables (out of 200):
   Gene94, Gene198, Gene137, Gene136, Gene158

> results$optsize
[1] 200