Loop for glm model with changing number of variables

前端 未结 1 1330
故里飘歌
故里飘歌 2021-01-25 18:29

I have a dataset with 1-3 versions of the dependent variable, and 10-15 independent variables. I\'d like to run a glm command for the model, but would like it to loop for ALL po

1条回答
  •  时光说笑
    2021-01-25 19:28

    Maybe something like that:

    dep_vars <- c("DepVar1", "DepVar2") 
    ind_vars <- c("IndVar1", "IndVar2", "IndVar3")
    
    # create all combinations of ind_vars
    ind_vars_comb <- 
      unlist( sapply( seq_len(length(ind_vars)), 
              function(i) {
                   apply( combn(ind_vars,i), 2, function(x) paste(x, collapse = "+"))
              }))
    
    # pair with dep_vars:
    var_comb <- expand.grid(dep_vars, ind_vars_comb ) 
    
    # formulas for all combinations
    formula_vec <- sprintf("%s ~ %s", var_comb$Var1, var_comb$Var2)
    
    # create models
    glm_res <- lapply( formula_vec, function(f)   {
        fit1 <- glm( f, data = dfPRAC, family = binomial("logit"))
        fit1$coefficients <- coef( summary(fit1))
        return(fit1)
    })
    names(glm_res) <- formula_vec
    
    # get model for specific formula
    glm_res[["DepVar1 ~ IndVar1"]] 
    
    # coefficients for var1 ~ var1
    coef(glm_res[["DepVar1 ~ IndVar1"]])
    
    # p-values for var1 ~ var2
    coef(glm_res[["DepVar1 ~ IndVar2"]])[,"Pr(>|z|)"]
    
    # p-values in a data.frame
    p_values <- 
      cbind(formula_vec, as.data.frame ( do.call(rbind,
            lapply(glm_res, function(x) {
              coefs <- coef(x)
              rbind(c(coefs[,4] , rep(NA, length(ind_vars) - length(coefs[,4]) + 1)))
            })
      )))
    

    Result:

                             formula_vec (Intercept)    IndVar1         V3        V4
    1                  DepVar1 ~ IndVar1  1.00000000 1.00000000         NA        NA
    2                  DepVar2 ~ IndVar1  0.65526203 0.29437334         NA        NA
    3                  DepVar1 ~ IndVar2  0.29307777 0.19121066         NA        NA
    4                  DepVar2 ~ IndVar2  0.07298241 0.03858791         NA        NA
    5                  DepVar1 ~ IndVar3  0.99950535 0.99940963         NA        NA
    6                  DepVar2 ~ IndVar3  0.52105212 0.44715614         NA        NA
    7          DepVar1 ~ IndVar1+IndVar2  0.31112860 0.76310468 0.18416266        NA
    8          DepVar2 ~ IndVar1+IndVar2  0.06488501 0.08833369 0.03031766        NA
    9          DepVar1 ~ IndVar1+IndVar3  0.99952006 0.99999188 0.99940957        NA
    10         DepVar2 ~ IndVar1+IndVar3  0.38508258 0.29593637 0.45010697        NA
    11         DepVar1 ~ IndVar2+IndVar3  0.28167430 0.15753070 0.54363164        NA
    12         DepVar2 ~ IndVar2+IndVar3  0.22644873 0.04654188 0.84059019        NA
    13 DepVar1 ~ IndVar1+IndVar2+IndVar3  0.27858393 0.71600105 0.14812808 0.5222330
    14 DepVar2 ~ IndVar1+IndVar2+IndVar3  0.15634739 0.08611677 0.02889574 0.7449513
    

    0 讨论(0)
提交回复
热议问题