[R] test if all predictors in a glm object are factors

Michael Friendly friendly at yorku.ca
Wed Sep 3 23:56:03 CEST 2008


I'm trying to develop some graphic methods for glm objects, but they 
only apply for models
where all predictors are discrete factors.  How can I test for this in a 
function, given the
glm model object?

That is, I want something that will serve as an equivalent of 
is.discrete.glm() in the following
context:

myplot.glm <-
function(model, ...) {
    if (!inherits(model,"glm")) stop("requires a glm object")
    if (!is.discrete.glm(model)) stop("only factors are allowed")
...
}

A small example, for count data, a poisson glm:

GSS <- data.frame(
  expand.grid(sex=c("female", "male"), party=c("dem", "indep", "rep")),
  count=c(279,165,73,47,225,191))

mod.glm <- glm(count ~ sex + party, family = poisson, data = GSS)

So, the model terms are sex and party, both factors.  Peeking inside 
mod.glm, I
can find

 > mod.glm$xlevels
$sex
[1] "female" "male" 

$party
[1] "dem"   "indep" "rep" 

and, in str(mod.glm$model) I see

 > str(mod.glm$model)
'data.frame':   6 obs. of  3 variables:
 $ count: num  279 165 73 47 225 191
 $ sex  : Factor w/ 2 levels "female","male": 1 2 1 2 1 2
 $ party: Factor w/ 3 levels "dem","indep",..: 1 1 2 2 3 3
 - attr(*, "terms")=Classes 'terms', 'formula' length 3 count ~ sex + party
  ....

so this is a keeper.  Can someone help me improve on the following 
is.discrete.glm() function.
It works for mod.glm, but isn't very general ;-)

is.discrete.glm <- function(model) {
    TRUE
}

-- 
Michael Friendly     Email: friendly AT yorku DOT ca 
Professor, Psychology Dept.
York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street    http://www.math.yorku.ca/SCS/friendly.html
Toronto, ONT  M3J 1P3 CANADA



More information about the R-help mailing list