steve zissou - 3 years ago 76
R Question

# Nested loop doesn't return expected values: Return model results from multiple recalculated independent variables

I would like some help with my nested loop which is not returning the values I expect. I am new to nested loops so please bear with me. I want to calculate a new independent variable for a logistic regression model which is based upon different calculations of the original variables. Specifically, I have six variables "x1...x6", and I then create three new variables (newvar1, newvar2, newvar3) by extracting a percentile from pairs of the original variables. From these three new variables I then combine them via subtraction to form a final new variable which forms the independent variable for a logistic regression model. The value of that final variable is then evaluated by the AIC of the logistic regression model.

I need to determine the optimal combination of percentile values which form newvar2, newvar2, and newvar3 which gives me the best logistic regression model. To do this I have attempted to create a three level nested like this:

``````df <- data.frame(x1 <- rnorm(100),
x2 <- rnorm(100),
x3 <- rnorm(100),
x4 <- rnorm(100),
x5 <- rnorm(100),
x6 <- rnorm(100),
y <- as.factor(runif(100)<=.70))

n = 1
AIC = NULL
for (i in 0.1:n){
for (j in 0.1:n){
for (k in 0.1:n){
df\$newvar1 <-apply(df[,1:2], 1, quantile, probs = i, na.rm = T)
df\$newvar2 <-apply(df[,3:4], 1, quantile, probs = j, na.rm = T)
df\$newvar3 <-apply(df[,5:6], 1, quantile, probs = k, na.rm = T)
df\$finalvar <- df\$newvar1 - df\$newvar2 - df\$newvar3
model <- glm(y ~ finalvar, data = df, family = "binomial")
AIC[i] <- as.numeric(model\$aic)
}
}
}
``````

I would like to provide a sequence of 11 values (0, 0.1, 0.2....0.9,1) to the "probs" argument of the quantile function, and I would like to get the AIC for each of the possible quantile parameter estimations (11*11*11). Thus the AIC variable in the end should be a numeric vector of 121 values. However, when I run the above code I get an empty numeric value for AIC. How can I get this code the run properly and supply me the values for all possible 121 models?

Thanks!

EDIT: this isn't the solution but provides part of the answer I think. in my previous code "n" was less than one so it was only performing a single iteration, (obviously) "n" needs to greater than one. The reason it was less than 1 before is that the "probs" argument to quantile requires a value betwee 0 and 1. The over come this, the parameter passed to the argument probs is now divided by 10. Now with AIC[1] i can get a vector of 10, but I still don"t understand how to get the full 10*10*10 (or 11*11*11) representing all combinations.

New code:

`````` n = 10
AIC = NULL
for (i in 1:n){
for (j in 1:n){
for (k in 1:n){
df\$newvar1 <-apply(df[,1:2], 1, quantile, probs = i/10, na.rm = T)
df\$newvar2 <-apply(df[,3:4], 1, quantile, probs = j/10, na.rm = T)
df\$newvar3 <-apply(df[,5:6], 1, quantile, probs = k/10, na.rm = T)
df\$finalvar <- df\$newvar1 - df\$newvar2 - df\$newvar3
model <- glm(y ~ finalvar, data = df, family = "binomial")
AIC[i] <- as.numeric(model\$aic)
}
}
}
``````

First of all, `AIC`is an `R` function so I've changed the name to `aic`. Second, in your code's innermost loop you index by `i` only, when you have 3 indices. So maybe this is what you really need.
``````n = 10