[R] covariate in a glm

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Jan 15 11:29:08 CET 2008

On Tue, 2008-01-15 at 11:15 +0100, LA SPINA, MICHELANGELO wrote:
> Hello mailing list!
> I would like to know, how I can introduce a covariate in a glm, I've
> two factors and a covariate.
> Thank you very much!
> ____________________________________________________________________
> Michelangelo La Spina 


I'm not clear on the distinction between a factor and a covariate (I my
way of thinking, a covariate is a variable that might predict an outcome
or response variable). If you want to use the "covariate" as the
response variable in a GLM then something like the following may be

## dummy data
fac1 <- sample(gl(4, 25))
fac2 <- sample(gl(2, 50))
covar <- rnorm(100)
my.dat <- data.frame(fac1 = fac1, fac2 = fac2, covar = covar)
mod1 <- glm(covar ~ fac1 + fac2, data = my.dat)

# or if you already have a response variable and you want to include the
covariate on the RHS of the formula, then one simple adds it in the same
manner as we added the factors to the RHS above

## new dummy response
my.dat$resp <- rt(100, df = 3)
## new model
mod2 <- glm(resp ~ fac1 + fac2 + covar, data = my.dat)

Does this answer your question?

 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk

More information about the R-help mailing list