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 Assistant Professor C. Kenneth and Dianne Wright Center for Clinical and Translational Research Department of Biostatistics Virginia Commonwealth University 830 E. Main St., Seventh Floor P. O. Box 980032 Richmond VA 23298-0032 U.S.A. CTSA grant: UL1TR000058 E-mail: jacobwegelin at fastmail.fm URL: http://www.people.vcu.edu/~jwegelin
> 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. WegelinHi 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
Not an answer .... But note that your several stopifnot() statements can be combined into 1. See ?stopifnot . Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Feb 12, 2016 at 8: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 > Assistant Professor > C. Kenneth and Dianne Wright Center for Clinical and Translational Research > Department of Biostatistics > Virginia Commonwealth University > 830 E. Main St., Seventh Floor > P. O. Box 980032 > Richmond VA 23298-0032 > U.S.A. CTSA grant: UL1TR000058 > E-mail: jacobwegelin at fastmail.fm URL: http://www.people.vcu.edu/~jwegelin > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.