R-对每个学习者具有不同功能子集的超级学习者的建议?

时间:2020-11-09 23:29:47

标签: r ensemble-learning superlearner

我希望将每个使用不同功能和算法子集开发的学习器组合到一个SuperLearner中。我意识到这不是SuperLearning通常的工作原理,但是请相信我有我的理由。

我一直在创建自定义SL.___函数,并处理诸如超参数之类的功能子集,但是正如您将在下面看到的那样,当我尝试在CV.SuperLearner中调用它们时,这会造成混乱。

有什么建议吗?在sl3包中有更简单的方法吗?

library(tidyverse)

library(SuperLearner)
#> Loading required package: nnls
#> Super Learner
#> Version: 2.0-26
#> Package created on 2019-10-27

set.seed(123)

示例数据集

data<- data.frame(
  id = 1:600, 
  a = sample(1:1000, size = 600, replace = TRUE), 
  b = rbinom(600, 1, .8), 
  c = rbinom(600, 100, .3), 
  d = sample(c(1:5), 600, replace = TRUE), 
  e = rpois(600, 4), 
  y = rnorm(600, 70, sd=15)
)

通过删除ID列和结果“ y”来创建仅包含要素/变量的数据框

data_x<-data %>%
  select(-c("id", "y"))

创建3个学习者,每个学习者使用不同的算法/方法和功能的不同子集。第一个学习者使用glm并具有a,b和c。第二个学习者使用LASSO回归并具有特征b,d和e,第三个学习者使用具有所有默认超参数并具有a,c,d和e的随机森林。

L1 = function(...) {
  SL.glm(..., X=L1_data)
}

L1_data<-data %>%
  select("a", "b", "c")

L2 = function(...) {
  SL.glmnet(..., X=L2_data, alpha = 1)
}

L2_data<-data %>%
  select("b", "d", "e")


L3<-function(...) {
  SL.ranger(..., X=L3_data)  
}

L3_data<-data %>%
  select("a", "c", "d", "e")

不足为奇的是,在CV.SuperLearner命令中未能定义“ X”参数会产生错误。

cv.SL_1 = CV.SuperLearner(Y=data$y, family = gaussian(), 
                         V=10, 
                         SL.library = c("L1", "L2", "L3"))

#> Error in CV.SuperLearner(Y = data$y, family = gaussian(), V = 10, SL.library = c("L1", : argument "X" is missing, with no default

但是在CV.SuperLearner命令中定义X也会产生错误,因为现在X已经被定义了两次。 (出于所有人的理智,我删除了大多数重复的警告和错误。)

cv.SL_2 = CV.SuperLearner(Y=data$y, X=data_x, family = gaussian(), 
                        V=10, 
                        SL.library = c("L1", "L2", "L3"))

#> Error in SL.glm(..., X = L1_data) : 
#>   formal argument "X" matched by multiple actual arguments
#> Warning in FUN(X[[i]], ...): Error in algorithm L1 
#>   The Algorithm will be removed from the Super Learner (i.e. given weight 0)
#> Error in SL.glmnet(..., X = L2_data, alpha = 1) : 
#>   formal argument "X" matched by multiple actual arguments
#> Warning in FUN(X[[i]], ...): Error in algorithm L2 
#>   The Algorithm will be removed from the Super Learner (i.e. given weight 0)
#> Error in SL.ranger(..., X = L3_data) : 
#>   formal argument "X" matched by multiple actual arguments
#> Warning in FUN(X[[i]], ...): Error in algorithm L3 

reprex package(v0.3.0)于2020-11-09创建

1 个答案:

答案 0 :(得分:0)

没关系,我想出了如何在SuperLearner程序包中编写适当的筛选算法。下面的简短示例。

L1 <- function(X,...){
  returnCols <- rep(FALSE, ncol(X))
  returnCols[names(X) %in% c("a","b","c")] <- TRUE
  return(returnCols)
}

cv.SL_1 = CV.SuperLearner(Y=data$y, family = gaussian(), 
                         V=10, 
                         SL.library = list(c("SL.glm","L1"),
                                            ("SL.glm", "All"))
)