r - 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)
解决方案
推荐阅读
- node.js - 有没有办法在不使用 next.js(或至少不使用 SSR)的情况下开发 Shopify 应用程序
- swift - 结构:Swift Playground 不打印用户名
- proxy - Haproxy Https 和 Vhost
- javascript - 当应用程序未打开时,Expo Linking 不会重定向到特定路径
- javascript - 在画廊中展开的盒子
- javascript - 如何将产品添加到收藏夹?
- python - django 的 authenticate() 不能正常工作
- amazon-ec2 - 通过 CDK 在需要的包中配置 EC2 机器
- python - 如何将两个字符串与元素列表连接起来?
- javascript - 使用钩子将值插入数组(反应)