[R] Odds ratios for categorical variable

Thomas Lumley tlumley at u.washington.edu
Fri Dec 5 21:06:12 CET 2003


On Fri, 5 Dec 2003, Sabrina Russo wrote:

> Dear R-users:
> How does one calculate in R  the odds ratios for a CATEGORICAL predictor
> variable that has 4 levels.  I see r-help inquiries regarding odds ratios
> for what looked like a continuous predictor variable.  I was wondering how
> to get the pairwise odds ratios for comparisons of levels of a categorical
> predictor variable.  I can't seem to get the correct output using:
>  > sp.glm=glm(cohort$logreg~cohort$hab, family=binomial)
>  > summary(sp.glm)
>

If the predictor isn't already coded as a factor, use

   sp.glm=glm(cohort$logreg~factor(cohort$hab), family=binomial)

This will give contrasts with the first level of the factor.  A tidier way
to specify the same thing is

  sp.glm <- glm(logreg~factor(hab), data=cohort, family=binomial)


	-thomas




More information about the R-help mailing list