Re: [S] question about (codes of) categorical data and logistic regressio n

Prof Brian Ripley (ripley@stats.ox.ac.uk)
Wed, 11 Nov 1998 17:33:02 +0000 (GMT)


> From: "Oudshoorn K." <K.Oudshoorn@pg.tno.nl>

> I have two questions:

> 1) Suppose we have a categorical variable which is coded internally by Splus
> (starting from 1, 2 etc).
> Now I want to make another vector with the SAME categories but I only have
> the codes for that
> factor. So if 1 corresponds to e.g. FEMALE , 2 corresponds to MALE and I
> have the vector
> (1, 1, 2, 1, 2, 2), what is an elegant and fast way to make the categorical
> vector
> (FEMALE, FEMALE, MALE, FEMALE,MALE,MALE)?

These variables are called factors. Use subsetting

c("FEMALE", "MALE")[c(1, 1, 2, 1, 2, 2)]

to give a character vector. If you want a factor, the best way is to use

factor(c(1, 1, 2, 1, 2, 2), levels=1:2, labels=c("FEMALE", "MALE"))

(since although there are shortcuts like

y <- c(1, 1, 2, 1, 2, 2)
levels(y) <- c("FEMALE", "MALE")
class(y) <- "factor"

these will not work unchanged with S-PLUS 5.x.)

> 2) Does anyone know how a categorical variable (with two categories off
> course) is handled
> in glm.fit? Does 0 correspond with the category with the smallest code?

It is handled in the initialize code for the family, not glm.fit.
I presume you are talking about the binomial family. Look at:

> binomial()$initialize
expression({
if(is.matrix(y)) {
if(dim(y)[2] > 2)
stop("only binomial response matrices (2 columns)")
n <- drop(y %*% c(1, 1))
y <- y[, 1]
} else {
if(is.category(y)) y <- y != levels(y)[1]
else y <- as.vector(y)
n <- rep(1, length(y))
}
w <- w * n
n[n == 0] <- 1
y <- y/n
mu <- y + (0.5 - y)/n
}

so the first level is coded as FALSE (0) and the others as TRUE (1).
Thus a binomial glm with a factor response fits the probability of
getting a level other than the first. [A little fact that should have
been in V&R{1,2} and will be in V&R3.]

-- 
Brian D. Ripley,                  ripley@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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

----------------------------------------------------------------------- This message was distributed by s-news@wubios.wustl.edu. To unsubscribe send e-mail to s-news-request@wubios.wustl.edu with the BODY of the message: unsubscribe s-news