R Return 带有 glm 的分类自变量的 p 值

R Return p-values for categorical independent variables with glm

我最近问了一个 关于为自变量的所有可能组合循环 glm 命令的问题。另一位用户提供了一个很好的答案,可以运行所有可能的模型,但是我不知道如何生成所有可能的 p 值的 data.frame。

上一个问题中建议的代码适用于二进制自变量(粘贴在下面)。但是,我的几个变量是绝对的。有什么方法可以调整代码,以便我可以为每个可能的模型生成一个 table 的所有 p 值(有 2,046 个可能的模型和 10 个自变量...)?

# 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)))
        })
  )))

一个自变量的示例是 "Bedrock",其中可能的类别包括:"till," "silt," 和 "glacial deposit." 为这些变量分配数值是不可行的,这是问题的一部分。如有任何建议,我们将不胜感激。

在附加分类变量 IndVar4(因子 a、b、c)的情况下,系数 table 可能不止一行更长。添加变量 IndVar4:

              Estimate Std. Error    z value  Pr(>|z|)
(Intercept) -1.7548180  1.4005800 -1.2529223 0.2102340
IndVar1     -0.2830926  1.2076534 -0.2344154 0.8146625
IndVar2      0.1894432  0.1401217  1.3519903 0.1763784
IndVar3      0.1568672  0.2528131  0.6204867 0.5349374
IndVar4b     0.4604571  1.0774018  0.4273773 0.6691045
IndVar4c     0.9084545  1.0943227  0.8301523 0.4064527

最大行数小于所有变量 + 所有类别:

max_values <- length(ind_vars) +
  sum(sapply( dfPRAC, function(x) pmax(length(levels(x))-1,0)))

所以新修正的函数是:

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, max_values - length(coefs[,4]) + 1)))
        })
  )))

但结果不如连续变量那么干净。我认为 Metrics 将每个分类变量转换为 (levels-1) 虚拟变量的想法给出了相同的结果,并且可能更清晰的呈现。

数据:

dfPRAC <- structure(list(DepVar1 = c(0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 
                                     1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1), DepVar2 = c(0, 1, 0, 0, 
                                                                                      1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1), 
                         IndVar1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 
                                     0, 0, 0, 1, 0, 0, 0, 1, 0), 

                         IndVar2 = c(1, 3, 9, 1, 5, 1, 
                                                                             1, 8, 4, 6, 3, 15, 4, 1, 1, 3, 2, 1, 10, 1, 9, 9, 11, 5), 
                         IndVar3 = c(0.500100322564443, 1.64241601558441, 0.622735778490702, 
                                     2.42429812749226, 5.10055213237027, 1.38479786027561, 7.24663629203007, 
                                     0.5102348706939, 2.91566510995229, 3.73356170379198, 5.42003495939846, 
                                     1.29312896116503, 3.33753833987496, 0.91783513806083, 4.7735736131668, 
                                     1.17609362602233, 5.58010703426296, 5.6668754863739, 1.4377813063642, 
                                     5.07724130837643, 2.4791994535923, 2.55100067348583, 2.41043629522981, 
                                     2.14411703944206)), .Names = c("DepVar1", "DepVar2", "IndVar1", 
                                                                    "IndVar2", "IndVar3"), row.names = c(NA, 24L), class = "data.frame")

dfPRAC$IndVar4 <- factor(rep(c("a", "b", "c"),8))
dfPRAC$IndVar5 <- factor(rep(c("d", "e", "f", "g"),6))

设置模型:

dep_vars <- c("DepVar1", "DepVar2") 
ind_vars <- c("IndVar1", "IndVar2", "IndVar3", "IndVar4", "IndVar5")

# 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