r - 如何加快我的逻辑回归自举 R 函数?
问题描述
我通过多次循环函数并存储我的结果来引导逻辑回归。
我可以让这个运行更快吗?我在函数中有一个循环,现在已经消失但仍然不够胖。有什么建议么?
# Bootstraping a logistic regression
# First create a function which takes a random sample from our data = 1000 and predicts
# have it return the prediction for the validation set using this sampled model
boot_fun <- function(){
train_data <- bankdata[train_set,]
rows_sample <- sample(1:nrow(train_data), 1000, TRUE)
LR_model <- glm(y~., data = train_data[rows_sample,], family = 'binomial')
pred <- predict(LR_model, type = 'response', newdata = bankdata[valid_set,])
pred <- cbind(pred, rep(0,length(pred)))
pred[which(pred[,1] > 0.5),] <- 1
return(pred[,2])
}
# We new run this function as many times as we want to bootstrap
# First create an output dataframe
# A loop error handling function tryCatch had to be used. Some samples didn't include December for
# example and was generating an error when trying to predict using a model with an unexistent December
boot_distribution <- data.frame(rep(NA,length(valid_set)))
n <- 10000
for (i in 1:n) {
tryCatch({
cat('processing.....at.....', round(i/n, digits = 3)*100,' % ', '\n')
boot_distribution <- cbind(boot_distribution, boot_fun())
}, error = function(e){cat('One of the regressions was unsucessful..', conditionMessage(e), '\n')})
}
解决方案
推荐阅读
- spring - 在将类型化的 Akka Actor 与 Spring 集成时,声明一个注入器并将其作为状态参数传递是个好主意吗?
- bash - shell脚本中的while循环给出错误:[:参数太多。如何解决这个问题?
- windows - Windows 终端,拆分窗格,但新窗格与前一个窗格位于同一目录中?
- git - Git推送旧版本的“--skip-worktree”文件
- sql - 在 QGIS 中查找函数 TO_DATE oracle
- angular - 数据绑定似乎很慢 - Audio Meter - Angular 9
- bash - 在退出时终止后台作业及其子项
- c# - 如何为 UWP 应用设置离线身份验证?
- python - 如果不遵守条件,我如何禁用下一个按钮 PyQt5 QWizard?
- json - 在 PostgreSQL 中将输出生成为数组或 json agg