首页 > 解决方案 > R会话崩溃,当gbm()应用于因子响应变量时?请指教

问题描述

下面是代码的摘录,我正在为德国信用数据集尝试。

我正在尝试为我的闪亮仪表板制作一个用于集成技术的通用函数。

问题出在gbm上。如果响应变量未转换为因子,R 会话将崩溃。

如果将响应变量转换为因子,则 RandomForest 将不会在其输出组件中生成 OOB 错误率和混淆矩阵。

请指教。

响应变量是“默认”。在应用模型之前,响应变量被视为,

    ## load the dataset
    data_x = read.csv("credit.csv")

   ## Preprocessing the dataset

   data_x$default <- ifelse(data_x$default == "yes", 1, 0)



  ##Loading packages

  pacman::p_load(shiny,shinydashboard,gbm, 
  randomForest,ggplot2,ipred,caret,ROCR,dplyr,ModelMetrics)

 user defined function
 model = function(algo =gbm ,distribution = 'bernoulli', 
         type = 'response', set ='AUC',n.trees =10000){

 ## Fit the model

  model<- algo(formula = default ~ ., 
           distribution = distribution,
           data = train,
           n.trees = n.trees,
           cv.fold= 3)

    ## Generate the prediction on the test set

    pred<- predict(object = model,
             newdata = test,
             n.trees = n.trees,
             type = type)

   ## Generate the test set AUCs using the pred

   AUC<- auc(actual = test$default, predicted = pred)


    if (set == 'AUC'){
     return(AUC)
     }

    if (set == 'predictions'){
     return(pred)
     }
    if (set == 'model'){
    return(model)

    }
    else
   return(NULL)

     }

    now call different model
    List of different models
    get_model<- function(algo,type = 'response', ntrees = 10000){
    z= model(algo = algo, type= type, set = 'model')

     }

   Bag_model<- get_model(algo = bagging, type='prob')
   RF_model<- get_model(algo = randomForest)
   GBM_model<- get_model(algo = gbm)

标签: rrandom-forestgbmensemble-learning

解决方案


推荐阅读