简体   繁体   中英

Storing output for nested loop

I'm trying to do a nested loop for logistic regression.

I'm trying to run a loop for the discretization value and for each class. Here's the code so far... I'm unable to get an output for each different iteration.

class <- c(1,2,3,4,5)
discretization_value <- seq(0.25, 0.75, by =0.05)
output<-data.frame(matrix(nrow=500, ncol=5))
names(output)=c("discretization_value", "class", "var1_coef", "var2_coef", "var3_coef")

for (i in discretization_value){
  for (j in class) {
  df$discretization_value <- ifelse(df$score >= i,1,0)
  result <- (glm(discretization_value ~ 
                   var1 + var2 + var3, 
                 data = df[df$class == j,], family= "binomial"))
  output[i,1] <- i
  output[i,2] <- j
  output[i,3] <- coef(summary(result))[c("var1"),c("Estimate")]
  output[i,4] <- coef(summary(result))[c("var2"),c("Estimate")]
  output[i,5] <- coef(summary(result))[c("var3"),c("Estimate")]
  }
}

a snippet of my df

class score var1 var2 var3
1 0.3 0.18 0.33 356
1 0.5 0.22 0.55 33
1 0.6 0.77 0.44 35
2 0.9 0.99 0.55 2
3 0 0 0 0
3 0.4 0.5 0.11 5
4 0 0.6 0 7
4 0 0.6 0 9
4 0.6 0.2 0.1 6

Could this be the problem? data = df[df$class == j, ], family= "binomial"))

I would try to remove the comma before the squared parenthesis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM