Search code examples
rglm

How to make a double loop for glm, using two data frames (one for dependent and another for independent variables)?


I feel that I am just little bit off with my code but cannot figure out how to make it work. I am trying to use all the columns in one data frame as an independent variable and all the columns in another as dependent (to run multiple single variable models) I'll greatly appreciate any suggestions.

A <- as.data.frame( matrix(rnorm(1:(250*4)), ncol = 4) )
colnames(a) <- paste0("A", 1:ncol(a))
B <- as.data.frame( matrix(rnorm(1:(250*6)), ncol = 6) )
model_<-list()
results_<-list()
for (i in 1:ncol(A)){
  for (j in 1:ncol(B)){
    model_<-glm(A[,i]~B[,j], family=quasipoisson){
      results_<-lapply(model_, function(x) anova(x, test="F"))
    }
  }
}

Solution

  • You can initialise the list with fixed length. Keep track of an index to store data in a list.

    A <- as.data.frame( matrix(abs(rnorm(1:(250*4))), ncol = 4) )
    colnames(A) <- paste0("A", 1:ncol(A))
    B <- as.data.frame( matrix(abs(rnorm(1:(250*6))), ncol = 6) )
    model_<- vector('list', ncol(A) * ncol(B))
    results_<- vector('list', ncol(A) * ncol(B))
    k <- 1
    
    for (i in 1:ncol(A)){
      for (j in 1:ncol(B)){
        model_[[k]] <-glm(A[,i]~B[,j], family=quasipoisson)
        results_[[k]] <-anova(model_[[k]], test="F")
        k <- k + 1
      }
    }