首页 > 解决方案 > 如何加快我的逻辑回归自举 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')})

    }

标签: rperformanceloopsclassificationregression

解决方案


推荐阅读