【发布时间】: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