3

我是 R 新手。我发现以下代码用于对一组变量进行单变量逻辑回归。我想做的是针对因变量对变量列表运行卡方检验,类似于下面的逻辑回归代码。我发现其中几个涉及创建变量的所有可能组合,但我无法让它工作。理想情况下,我希望变量 (X) 之一相同。

在 R 中使用 for 循环进行卡方分析

lapply(c("age","sex","race","service","cancer",
         "renal","inf","cpr","sys","heart","prevad",
         "type","frac","po2","ph","pco2","bic","cre","loc"),

       function(var) {

         formula    <- as.formula(paste("status ~", var))
         res.logist <- glm(formula, data = icu, family = binomial)

         summary(res.logist)
       })
4

1 回答 1

2

你确定你在向量中的字符串在数据集lapply的列名中吗?icu

当我下载icu数据时,它对我有用:

system("wget http://course1.winona.edu/bdeppa/Biostatistics/Data%20Sets/ICU.TXT")
icu <- read.table('ICU.TXT', header=TRUE)

并将其更改status为. 这是您的一些变量的示例:STAicu

my.list <- lapply(c("Age","Sex","Race","Ser","Can"),         
       function(var) {
         formula    <- as.formula(paste("STA ~", var))
         res.logist <- glm(formula, data = icu, family = binomial)
         summary(res.logist)
       })

这给了我一个包含summary.glm对象的列表。例子:

lapply(my.list, coefficients)
[[1]]
               Estimate Std. Error   z value     Pr(>|z|)
(Intercept) -3.05851323 0.69608124 -4.393903 1.113337e-05
Age          0.02754261 0.01056416  2.607174 9.129303e-03

[[2]]
              Estimate Std. Error    z value     Pr(>|z|)
(Intercept) -1.4271164  0.2273030 -6.2784758 3.419081e-10
Sex          0.1053605  0.3617088  0.2912855 7.708330e-01

[[3]]
              Estimate Std. Error    z value   Pr(>|z|)
(Intercept) -1.0500583  0.4983146 -2.1072198 0.03509853
Race        -0.2913384  0.4108026 -0.7091933 0.47820450

[[4]]
              Estimate Std. Error   z value     Pr(>|z|)
(Intercept) -0.9465961  0.2310559 -4.096827 0.0000418852
Ser         -0.9469461  0.3681954 -2.571858 0.0101154495

[[5]]
                 Estimate Std. Error       z value     Pr(>|z|)
(Intercept) -1.386294e+00  0.1863390 -7.439638e+00 1.009615e-13
Can          7.523358e-16  0.5892555  1.276756e-15 1.000000e+00

如果要进行卡方检验:

my.list <- lapply(c("Age","Sex","Race","Ser","Can"),function(var)chisq.test(icu$STA, icu[,var]))

或对所有变量组合进行卡方检验:

my.list.all <- apply(combn(colnames(icu), 2), 2, function(x)chisq.test(icu[,x[1]], icu[,x[2]]))

这行得通吗?

于 2013-09-17T13:15:28.557 回答