Hello,

Although the example below doesn't necessary make any sense from a
statistical perspective, it is just a close enough example to hopefully get
your help upon.

For my purpose, I'm particularly interested to know if there is a way to
replace the results from the vapply() function below by a faster
alternative. Essentially, I need to repeat the same function *many* times
in my code, so if there is a computationally more efficient means to do the
same, that would save overall a lot of computing time.

### Step 1. A reproducible example

set.seed(1)
var.ind <- 1:10
y <- rnorm(100)
x <- matrix(rnorm(100), 10, 10)
df <- data.frame(y, x)
var_names <- names(df[, 2:11])

### Step 2. a) Select m variables at random from x and b) get the AIC from
a ###regression between y and each of the selected m individually

m <- 3
m.ind <- sample(var.ind, m) #select mtry variables
res <- vapply(m.ind,
         function(i)
           AIC(glm(as.formula(paste('y ~', paste(var_names[i]))),
                                    data = df)), FUN.VALUE = 0);

res
[1] 267.2759 265.9167 265.4468

Thank you,
Axel.

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to