【问题标题】:how can i apply Cross validation for Random Forest我如何为随机森林应用交叉验证
【发布时间】:2019-09-24 05:14:36
【问题描述】:

我想为随机森林回归做交叉验证,但我真的不确定如何做。到目前为止,这是我的代码:

library(rfUtilities)
# Read Data
base <- readxl::read_xlsx(c:\ File)

# Pull columns to use in the model
base_cl <- select(base, 
                  Id = PLA_WTWPartyID, 
                  Ind =Global_reference_Industry, 
                  Num__Ind =NumInd,
                  Retention = Retention_AL,
                  Limit = Limit_AL,
                  Exposure = Exposure_AL,
                  #RL_Exposure = Risk_level_Exposure,
                  LPremium = Liab_Premuim_AL,
                  Haz_Gp = HazardGp_AL,
                  LPick =Loss_Pick_AL,
                  #RL_LPick = Level_Loss_Pick,
                  Rate = Rate_AL,
                  lob = AL_R,
                  Date = AL_R_Date) 

#Clean Data
base_cl$_Ind[is.na(base_cl$_Ind)] <- "Other"
base_cl$Limit[base_cl$Limit == "0"] <- NA
base_cl$Exposure[base_cl$Exposure == "0"] <- NA

#Remove Rate outliers
base_cl$Rate <- remove_outliers(base_cl$Rate)

base_cl <- base_cl %>%
  filter(lob == "1") %>%
  filter(Date == "1") %>%
  drop_na(Limit)%>%
  drop_na(Exposure) %>%
  drop_na(LPremium) %>%
  drop_na(Retention) %>%
  drop_na(Rate)     
output.forest <- randomForest(Formula_3, base_cl, ntree = 400, keep.forest = T,
                              importance = T, localImp = T, mtry = 6)

print(output.forest)
rf.regression.fit(output.forest)
varImpPlot(output.forest, sort = TRUE)    
RF_CV_2 <- rfcv(trainx = base_cl[, 4:9], trainy = base_cl[[10]], p = .2,
                normalize = T,bootstrap = T, trace = T,step = 3, method = "cv")

最后我有一个错误

RF <- rf.crossValidation(output.forest, base_cl, p = 0.1, n = 99, seed = NULL,
                         normalize = FALSE, bootstrap = FALSE, trace = FALSE, ntree = 400)

sample.int(length(x), size, replace, prob) 中的错误:找不到对象“sample.sizes”

...我不知道如何修复它以运行。你能帮我构建一个函数或修复我的代码以运行交叉验证吗,也许 k= 5 或 10。

【问题讨论】:

标签: r random-forest cross-validation


【解决方案1】:

在 Google 上搜索:

 rf.crossValidation "Error in sample.int(length(x), size, replace, prob) : object 'sample.sizes' not found" 

...我们发现该错误已在 2 月修复,但您需要从 Github 安装开发版本。请参阅错误报告和回复:https://github.com/jeffreyevans/rfUtilities/issues/4

【讨论】:

    猜你喜欢
    • 2014-04-16
    • 2018-09-03
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 2019-09-15
    • 2013-11-14
    • 2021-06-05
    相关资源
    最近更新 更多