# [R] A Tip: lm, glm, and retained cases

From: Ted Harding <Ted.Harding_at_manchester.ac.uk>
Date: Wed, 27 Aug 2008 00:45:50 +0100 (BST)

Say (for example) you want to do a logistic regression of a binary response Y on variables X1, X2, X3, X4:

GLM <- glm(Y ~ X1 + X2 + X3 + X4)

Say there are 1000 cases in the data. Because of missing values
(NAs) in the variables, the number of complete cases retained
for the regression is, say, 600. glm() does this automatically.

QUESTION: Which cases are they?

You can of course find out "by hand" on the lines of

ix <- which( (!is.na(Y))&(!is.na(X1))&...&(!is.na(X4)) )

but one feels that GLM already knows -- so how to get it to talk?

ix <- as.integer(names(GLM\$fit))

Reason: When glm(Y~X1+...) picks up the data passed to it, it assigns[*] to each element of Y a name which is its integer position in the variable, expressed as a character string
("1", "2", "3", ... ).
[*] Assuming (as is usually the case) that the elements didn't have names in the first place. Otherwise these names are used; modify the above approach accordingly.

These names are retained during the computation, and when incomplete cases are dropped the retained complete cases retain their original names. Thus, any per-case series of computed values (such as \$fit) has the names of the retained cases the values correspond to. These can be discovered by

names(GLM\$fit)

but you don't want them as character strings, so convert them to integers:

as.integer(names(GLM\$fit))

Done! I hope this helps some people.
Ted.

E-Mail: (Ted Harding) <Ted.Harding_at_manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 27-Aug-08                                       Time: 00:45:47
------------------------------ XFMail ------------------------------

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 26 Aug 2008 - 23:49:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 27 Aug 2008 - 00:34:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.