0

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?

4

1 に答える 1

4
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)

これは少し凝縮しすぎているかもしれませんが、うまくいきます。もちろん、サンプル データ セットが小さすぎて、適切な回答を得ることができません...

t(sapply(setdiff(names(dat),"admit"),
     function(x) coef(glm(reformulate(x,response="admit"),
                          data=dat,family=binomial))))
于 2014-03-09T15:31:46.117 に答える