[R] test if all predictors in a glm object are factors
Marc Schwartz
marc_schwartz at comcast.net
Thu Sep 4 00:15:52 CEST 2008
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
> 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
More information about the R-help
mailing list