Я загружаю логистическую регрессию, многократно повторяя функцию и сохраняя свои результаты.
Могу ли я сделать это быстрее? У меня был цикл внутри функции, который теперь исчез, но все еще не достаточно жира. Есть предложения?
# 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')})
}