Search code examples
rstatisticslogistic-regressionglm

looping logistic regression row wise in R


I'm trying to run logistic regression analyses for each row from R1 to R6 against C1 row of dataframe "dat"

Input data frame

dat <- data.frame(list(X1 = c(1, 1, 0, 1, 1, 1, 0), X2 = c(1, 1, 0, 1, 1, 1, 0), X3 = c(1, 1, 1, 0, 1, 1, 0), X4 = c(1, 1, 0, 1, 1, 1, 0), X5 = c(0, 1, 0, 0, 1, 1, 0), X6 = c(1, 1, 1, 0, 1, 1, 0), X7 = c(0, 1, 0, 1, 1, 1, 0), X8 = c(0, 1, 1, 0, 1, 0, 0), X9 = c(0, 1, 1, 0, 1, 0, 0), X10 = c(1, 1, 1, 0, 1, 0, 0), X11 = c(1, 1, 1, 1, 1, 0, 0), X12 = c(0, 1, 0, 0, 1, 0, 0 ), X13 = c(0, 1, 0, 1, 1, 0, 0), X14 = c(1, 1, 0, 1, 1, 0, 0), X15 = c(1, 1, 1, 1, 1, 0, 0), X16 = c(0, 1, 1, 1, 1, 0, 0), X17 = c(0, 1, 1, 0, 1, 0, 0), X18 = c(0, 1, 1, 0, 1, 0, 0), X19 = c(1, 1, 0, 0, 1, 0, 0), X20 = c(1, 1, 1, 0, 1, 0, 0), X21 = c(0, 1, 0, 1, 1, 0, 0), X22 = c(1, 1, 0, 0, 1, 0, 0), X23 = c(1, 1, 1, 0, 1, 0, 0), X24 = c(1, 1, 0, 0, 1, 0, 0), X25 = c(1, 1, 0, 0, 1, 0, 0), X26 = c(0, 1, 1, 1, 1, 0, 0), X27 = c(0, 1, 0, 0, 1, 0, 0), X28 = c(1, 1, 0, 0, 1, 0, 0), X29 = c(1, 1, 1, 1, 0, 0, 0), X30 = c(1, 1, 0, 0, 0, 1, 0)),  row.names = c("r1", "r2", "r3", "r4", "r5", "r6", "C1"))

logistic regression analyses

    R1 <- glm(r1 ~ C1, data=dat, family=binomial); coef(summary(R1))[,2] 
    R2 <- glm(r2 ~ C1, data=dat, family=binomial); coef(summary(R2))[,2] 
    R3 <- glm(r3 ~ C1, data=dat, family=binomial); coef(summary(R3))[,2] 
    R4 <- glm(r4 ~ C1, data=dat, family=binomial); coef(summary(R4))[,2] 
    R5 <- glm(r5 ~ C1, data=dat, family=binomial); coef(summary(R5))[,2] 
    R6 <- glm(r6 ~ C1, data=dat, family=binomial); coef(summary(R6))[,2] 

The real data have 6000 row so it is not possible to do one by one row against C1.

Is there is a way to do it in loop where glm is calculated for each row from R1 to R6 against C1 and extract the output into a new column?


Solution

  • library(tidyverse)
    
    independent_var <- 'C1'
    dependent_vars <- setdiff(rownames(dat),independent_var)
    compare <- NULL
    
    for(i in dependent_vars){
        modelname <- toupper(i)
        
        filtered_data <- dat %>%
        t %>% 
        data.frame %>% 
        select(all_of(c(i,'C1')))
        
        eval(parse(text=sprintf('%s <- glm(%s ~ %s,data=filtered_data,family=binomial)',modelname,i,independent_var)))
        eval(parse(text=sprintf('newrow <- data.frame(model="%s",coef=as.numeric(%s$coefficients[-2]))',modelname,modelname)))
        compare <- rbind(compare,newrow)
    }
    
    compare
    

    output;

      model   coef
      <chr>  <dbl>
    1 R1     0.405
    2 R2    25.6  
    3 R3    -0.134
    4 R4    -0.405
    5 R5     2.64 
    6 R6    -1.01