Creating a loop that will run a Logistic regression across all Independent variables

StackOverflow https://stackoverflow.com/questions/22281753

  •  11-06-2023
  •  | 
  •  

Вопрос

I would like to run the dependent variable of a logistic regression (in my data set it's : dat$admit) with all available variables, each regression with its own Independent variable vs dependent variable. The outcome that I wanted to get is a list of each regression summary. Using the data set submitted below there should be 3 regressions.

Here is a sample data set (where admit is the logistic regression dependent variable) :

dat <- read.table(text = "
+ female  apcalc    admit       num
+ 0        0        0         7
+ 0        0        1         1
+ 0        1        0         3
+ 0        1        1         7
+ 1        0        0         5
+ 1        0        1         1
+ 1        1        0         0
+ 1        1        1         6",
+                   header = TRUE)

I got an example for simple linear regression but When i tried to change the function from lm to glm I got "list()" as a result.
Here is the original code - for the iris dataset where "Sepal.Length" is the dependent variable :

sapply(names(iris)[-1], 
       function(x) lm.fit(cbind(1, iris[,x]), iris[,"Sepal.Length"])$coef)

How can I create the right function for a logistic regression?

Это было полезно?

Решение

dat <- read.table(text = "
female  apcalc    admit       num
0        0        0         7
0        0        1         1
0        1        0         3
0        1        1         7
1        0        0         5
1        0        1         1
1        1        0         0
1        1        1         6",
               header = TRUE)

This is perhaps a little too condensed, but it does the job. Of course, the sample data set is too small to get any sensible answers ...

t(sapply(setdiff(names(dat),"admit"),
     function(x) coef(glm(reformulate(x,response="admit"),
                          data=dat,family=binomial))))
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top