不使用for循环的逻辑回归

时间:2021-10-16 14:57:56

I have 202×500 matrix that contains data for 200 variables in 500 people(data1.txt). y is binary data(0=control, 1= case)

我有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 

I would like to make logistic regression for x_1 to X_200(only one variable and no covariates) using R. y~X_, data=sample,family=binomial(=1・・200) Are there any easy way to run this analysis not using for loop?

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

1 个解决方案

#1


2  

If you do not want a explicit loop, you could use map from the purrr package. This will do what you want. Below is example code based on your info. Probably not the best code, but should get you started. The glms are stored in the my_glms list object. More info read the vignettes of purrr and broom.

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

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

Of course all this can be done in 1 line of code but this shows the steps and you can adjust where needed.

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

#1


2  

If you do not want a explicit loop, you could use map from the purrr package. This will do what you want. Below is example code based on your info. Probably not the best code, but should get you started. The glms are stored in the my_glms list object. More info read the vignettes of purrr and broom.

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

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

Of course all this can be done in 1 line of code but this shows the steps and you can adjust where needed.

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