【问题标题】:Logistic regression without using for loop不使用 for 循环的逻辑回归
【发布时间】:2018-04-23 17:07:21
【问题描述】:

我有 202×500 矩阵,其中包含 500 人中 200 个变量的数据(data1.txt)。 y 是二进制数据(0=控制,1=案例)

 case y x_1 x_2 X_3 x_4・・・・x_200
 1   1  
 2   0
 3   1
 4   0
500  0 

我想用 R 对 x_1 到 X_200(只有一个变量,没有协变量)进行逻辑回归。 y~X_, data=sample,family=binomial(=1・・200) 有没有不使用 for 循环的简单方法来运行此分析?

【问题讨论】:

  • for 循环有什么问题?

标签: r


【解决方案1】:

如果您不想要显式循环,则可以使用 purrr 包中的 map。这将做你想要的。以下是基于您的信息的示例代码。可能不是最好的代码,但应该让你开始。 glms 存储在my_glms 列表对象中。更多信息请阅读 purrr 和 broom 的小插曲。

library(purrr)
sample <- data.frame(Y = c(0,0,0,0,1,1,1,1),
                     x_1 = c(1,2,3,4,5,6,7,8),
                     x_2 = c(2,3,4,5,6,7,8,9))


my_glms <- map(sample[,-1], #remove Y otherwise glm also on Y
               ~ glm(Y ~ .x, data = sample, family = binomial))

#using the tidy function from broom
library(broom)
map(my_glms, tidy)

$x_1
         term   estimate std.error     statistic   p.value
1 (Intercept) -206.12214  365130.9 -0.0005645158 0.9995496
2          .x   45.80492   80643.9  0.0005679899 0.9995468

$x_2
         term   estimate std.error     statistic   p.value
1 (Intercept) -251.92706  445370.6 -0.0005656572 0.9995487
2          .x   45.80492   80643.9  0.0005679899 0.9995468

当然,所有这些都可以在 1 行代码中完成,但这显示了步骤,您可以根据需要进行调整。

【讨论】:

  • 感谢您的回答。我只想要 y~x_1,y~x_2,y~x3,···X200 每个 P 值,95%CI.not y~x_1+x_2+···x_200
  • @ko0101,你试过代码了吗?列表中的结果是 Y ~ x_1、Y ~ x_2 等。我添加了一些代码来使用 broom 包中的 tidy 显示结果。
猜你喜欢
  • 2014-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 2012-11-05
  • 2020-12-18
  • 2014-10-03
  • 1970-01-01
相关资源
最近更新 更多