按 R 中的最小列值排序数据帧列表

Ordering a list of dataframes by minimum column value in R

作为创建多元逻辑回归的准备,我正在进行单变量回归,并希望 select 将 p < 0.20 的变量包含在多元模型中。我可以将所需的变量映射到 glm 并获得模型的输出,但我很难按 p 值的等级对它们进行排序。

这是我目前拥有的:

predictor1 <- c(0,1.1,2.4,3.1,4.0,5.9,4.2,3.3,2.2,1.1)
predictor2 <- as.factor(c("yes","no","no","yes","yes","no","no","yes","no","no"))
predictor3 <- as.factor(c("a", "b", "c", "c", "a", "c", "a", "a", "a", "c"))
outcome <- as.factor(c("alive","dead","alive","dead","alive","dead","alive","dead","alive","dead"))
df <- data.frame(pred1 = predictor1, pred2 = predictor2, pred3 = predictor3, outcome = outcome)
predictors <- c("pred1", "pred2", "pred3")
df %>%
    select(predictors) %>%
    map(~ glm(df$outcome ~ .x, data = df, family = "binomial"))  %>%
    #Extract odds ratio, confidence interval lower and upper bounds, and p value
    map(function (x, y) data.frame(OR = exp(coef(x)), 
        lower=exp(confint(x)[,1]), 
        upper=exp(confint(x)[,2]),
        Pval = coef(summary(x))[,4]))

这段代码给出了每个模型的摘要

$pred1
                OR   lower          upper           Pval
    (Intercept) 0.711082 0.04841674 8.521697    0.7818212
    .x          1.133085 0.52179227 2.653040    0.7465663
$pred2
                OR   lower          upper           Pval
    (Intercept) 1   0.18507173  5.40331     1
    .xyes   1   0.07220425  13.84960    1
$pred3
                OR   lower          upper           Pval
    (Intercept) 0.25    0.0127798   1.689944    0.2149978
    .xb         170179249.43 0.0000000  NA  0.9961777
    .xc         12.00   0.6908931   542.678010  0.1220957 

但是我的真实数据集有很多预测变量,所以我需要一种方法来对输出进行排序。最好按每个模型中的最小(非截距)p 值。也许我为每个模型的摘要选择的数据结构不是最好的,所以任何关于如何在更灵活的数据结构中获取相同信息的建议也将是好的。

您可以只使用 do.call(rbind) 方法,然后按 p-value 排序。 [-1, ] 省略截距。

pl <- do.call(rbind, sapply(predictors, function(x) {
  fo <- reformulate(x, response="outcome")
  summary(glm(fo, data=df, family="binomial"))$coef[-1, ]
  }))
pl[order(pl[, 4]), ]
#             Estimate   Std. Error       z value  Pr(>|z|)
# pred3c  2.484907e+00    1.6072751  1.546037e+00 0.1220957
# pred1   1.249440e-01    0.3866195  3.231703e-01 0.7465663
# pred3b  1.895236e+01 3956.1804861  4.790571e-03 0.9961777
# pred2  -5.733167e-16    1.2909944 -4.440892e-16 1.0000000

数据

df <- structure(list(pred1 = c(0, 1.1, 2.4, 3.1, 4, 5.9, 4.2, 3.3, 
2.2, 1.1), pred2 = structure(c(2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 
1L, 1L), .Label = c("no", "yes"), class = "factor"), pred3 = structure(c(1L, 
2L, 3L, 3L, 1L, 3L, 1L, 1L, 1L, 3L), .Label = c("a", "b", "c"
), class = "factor"), outcome = structure(c(1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L), .Label = c("alive", "dead"), class = "factor")), class = "data.frame", row.names = c(NA, 
-10L))

predictors <- c("pred1", "pred2", "pred3")

使用 map_dfr 而不是 map,使用拦截过滤行然后执行 arrange。使用 broom 中的 tidy 而不是您的自定义函数。

library(broom)    
df %>%
   select(predictors) %>%
   map(~ glm(df$outcome ~ .x, data = df, family = "binomial")) %>%
   map_dfr(tidy, .id='Model') %>% 
   filter(term!="(Intercept)") %>% arrange(p.value)

# A tibble: 4 x 6
Model term   estimate std.error statistic p.value
<chr> <chr>     <dbl>     <dbl>     <dbl>   <dbl>
1 pred3 .xc    2.48e+ 0     1.61   1.55e+ 0   0.122
2 pred1 .x     1.25e- 1     0.387  3.23e- 1   0.747
3 pred3 .xb    1.90e+ 1  3956.     4.79e- 3   0.996
4 pred2 .xyes -5.73e-16     1.29  -4.44e-16   1.000