> From: Victor Moreno <V.Moreno@ico.scs.es>
>
> After some thought, this is a quick solution to changing the
> reference category of a factor in a model. I have done minimal
> testing and some bugs may remain, so please use with caution.
>
> Thanks to Prof. Ripley, and S.D.Byers for answering previous query.
This is not what I suggested, seems needlessly complicated and gives
ugly (IMHO) output. Compare this with
> library(MASS)
> options(contrasts=c("contr.treatment", "contr.poly"))
> summary(glm(Days ~ Age, family=poisson, data=quine))
Coefficients:
Value Std. Error t value
(Intercept) 2.6981275 0.04987727 54.095333
AgeF1 -0.2864900 0.06657498 -4.303268
AgeF2 0.3487780 0.06059414 5.755970
AgeF3 0.2777121 0.06349276 4.373918
Quine <- quine
Quine$Age <- factor(quine$Age, levels=c("F3", "F0", "F1", "F2"))
summary(glm(Days ~ Age, family=poisson, data=Quine))
> Coefficients:
Value Std. Error t value
(Intercept) 2.97583958 0.03928853 75.743220
AgeF0 -0.27771209 0.06349276 -4.373918
AgeF1 -0.56420205 0.05905992 -9.553044
AgeF2 0.07106596 0.05222543 1.360754
You could use
relevel <- function(x, ref, ...) UseMethod("relevel")
relevel.default <- function(x, ref, ...) stop("relevel only for factors")
relevel.ordered <- function(x, ref, ...) stop("relevel only for factors")
relevel.factor <- function(x, ref, ...)
{
lev <- levels(x); nlev <- length(lev)
if(is.character(ref)) ref <- match(ref, lev)
if(is.na(ref)) stop("ref must be an existing level")
lev <- levels(x); nlev <- length(x)
if(ref < 1 || ref > nlev) stop(paste("ref =", ref, "must be in 1 :", nlev))
factor(x, levels = lev[c(ref, seq(along=lev)[-ref])] )
}
Quine <- quine
Quine$Age <- relevel(quine$Age, "F3") # or relevel(quine$Age, 4)
You could even use
summary(glm(Days ~ relevel(Age, "F3"), family=poisson, data=quine))
--
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
|