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


i nested loop not returning values expect. new nested loops please bear me. want calculate new independent variable logistic regression model based upon different calculations of original variables. specifically, have 6 variables "x1...x6", , create 3 new variables (newvar1, newvar2, newvar3) extracting percentile pairs of original variables. these 3 new variables combine them via subtraction form final new variable forms independent variable logistic regression model. value of final variable evaluated aic of logistic regression model.

i need determine optimal combination of percentile values form newvar2, newvar2, , newvar3 gives me best logistic regression model. have attempted create 3 level nested 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 (i in 0.1:n){   (j in 0.1:n){     (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 provide sequence of 11 values (0, 0.1, 0.2....0.9,1) "probs" argument of quantile function, , aic each of possible quantile parameter estimations (11*11*11). aic variable in end should numeric vector of 121 values. however, when run above code empty numeric value aic. how can code run , supply me values possible 121 models?

thanks!

edit: isn't solution provides part of answer think. in previous code "n" less 1 performing single iteration, (obviously) "n" needs greater one. reason less 1 before "probs" argument quantile requires value betwee 0 , 1. on come this, parameter passed argument probs divided 10. aic[1] can vector of 10, still don"t understand how full 10*10*10 (or 11*11*11) representing combinations.

new code:

 n = 10  aic = null  (i in 1:n){    (j in 1:n){      (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, aicis r function i've changed name aic. second, in code's innermost loop index i only, when have 3 indices. maybe need.

n = 10 aic = array(0, dim = c(n, n, n))  # changed for(...)     for(...)         for(...){             [...]             aic[i, j, k] <- as.numeric(model$aic)  # changed         } 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -