test if all predictors in a glm object are factors
On Wed, 3 Sep 2008, Marc Schwartz wrote:
on 09/03/2008 04:56 PM Michael Friendly wrote:
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
Is an ordered factor a 'discrete factor'? I suspect it is, so this needs
to be
is.discrete.glm <- function(model)
all(attr(terms(model), "dataClasses")[-1] %in% c("factor", "ordered"))
(removing redundant braces).
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,
How about something like this:
is.discrete.glm <- function(model) {
all(attr(terms(model), "dataClasses")[-1] == "factor")
}
Essentially, take the output of terms(model), check the 'dataClasses'
attribute, except for the first element, which is the DV.
is.discrete.glm(mod.glm)
[1] TRUE HTH, Marc Schwartz
______________________________________________ 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.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595