Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove specific columns when doing a logistic regression on all variables

Tags:

r

I would like to perform a logistic regression on all variables but two in a large data frame.How can i ask r to refer to all variable except those two without creating a new data frame. for example:

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 have this line of code :

Model1 <- glm(admit ~.,data=dat,family = 'binomial')

and i want to take out "female" and "apcalc". Can I do it in this single line of code?

like image 463
mql4beginner Avatar asked Dec 03 '25 10:12

mql4beginner


1 Answers

EDIT

If you want to remove those columns for analysis, then either subset the data before running the model, or inside the glm call. Keep in mind the latter will slow the call to gml for larger data sets.

> dat2 <- dat[!names(dat) %in% c("female", "apcalc")]
  admit num
1     0   7
2     1   1
3     0   3
4     1   7
5     0   5
6     1   1
7     0   0
8     1   6

> glm(admit ~., data = dat2, family = 'binomial')

ORIGINAL ANSWER

If you want to extract only the coefficients for female and apcalc, then

> glm(admit ~.,data=dat,family = 'binomial')$coef[c("female", "apcalc")]
like image 79
Rich Scriven Avatar answered Dec 06 '25 00:12

Rich Scriven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!