[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

Hi,

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
appropriate

## 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)
mod1
summary(mod1)

# 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)
mod2
summary(mod2)

G
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

```