[R] confirm family==binomial and link==logistic

Marc Schwartz marc_schwartz at me.com
Fri Feb 12 17:50:29 CET 2016


> On Feb 12, 2016, at 10:33 AM, Jacob Wegelin <jacobwegelin at fastmail.fm> wrote:
> 
> To check that a regression object comes from logistic regression, I employ the following two lines:
> 
> 	stopifnot(glmObject$family$family=="binomial")
> 
> 	stopifnot(glmObject$family$link=="logit")
> 
> For instance:
> 
> toyfunction<-function(glmObject) {
> 	stopifnot(inherits(glmObject, "glm"))
> 	stopifnot(glmObject$family$family=="binomial")
> 	stopifnot(glmObject$family$link=="logit")
> 	cat("okay, I guess\n")
> 	glmObject
> }
> 
> mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
> 
> someobject<- glm(admit~gre+gpa, data=mydata)
> 
> toyfunction(someobject)
> 
> someobject<- glm(admit~gre+gpa, data=mydata, family="binomial")
> 
> toyfunction(someobject)
> 
> But Doug Bates once stated that it's preferable to use extractor functions (and perhaps other ready-made functions?) rather than "deconstructing" an object (his term), as I do here.
> 
> Accordingly, is there a smarter way to perform the check that I perform inside toyfunction?
> 
> Thanks for any insight
> 
> Jacob A. Wegelin


Hi Jacob,

The rationale behind Doug's comment is that if there is a pre-existing extractor function, you are at less risk due to future possible changes in the underlying object structure, than if you try to access an object element directly. 

If the underlying object structure should change in the future, you never know what result you might get by accessing the element directly, if you get one at all. 

If you use the extractor function, that would be (or should be) modified to reflect the change in the underlying object.

For your examples above, ?family should work, but returns more than just the 'family' and 'link' components, which print.family() displays:

someobject<- glm(admit~gre+gpa, data=mydata)
> family(someobject)

Family: gaussian 
Link function: identity 


someobject<- glm(admit~gre+gpa, data=mydata, family="binomial")
> family(someobject)

Family: binomial 
Link function: logit 


So, you could feasibly use:

  family(someobject)$family
  family(someobject)$link

and so forth, to perform your checks. If you look at the output of str(family(someobject)), you will see the other elements contained.

Regards,

Marc Schwartz



More information about the R-help mailing list