Skip to content
Back to formatted view

Raw Message

Message-ID: <31194FD54F3246AEB27ECF0265F98508@botanica1>
Date: 2011-09-24T20:52:41Z
From: Aitor Gastón
Subject: glm coefficients
In-Reply-To: <CAD9MWXXEN+M=-rDoQ6AMM1q_NqxbWRuLer+BdAyCaauOgsS_wQ@mail.gmail.com>

In your example there is not categorical variables and you need at least one 
for ANCOVA.
Try the following, is the same dataset using factor() to create a 
categorical variable (named CatVar):

dat <- data.frame(response = rnorm(9), size = rnorm(9),  CatVar =
factor(c(1,1,1,1,0,0,0,0,0)))
model <- glm(response ~ size * CatVar, data = dat)
coef(model)

You will get:

(Intercept)         size      CatVar1 size:CatVar1
   0.2141371   -0.7847063   -1.8409264    3.3637699

The first coefficient (labeled "Intercept") is the intercept for CatVar = 0.
The second coefficient (labeled "size") is the slope for CatVar=0
The third coefficient (labeled "CatVar1") is the difference between the 
intercept for CatVar = 0 and the intercept for CatVar = 1
The fourth coefficient ("labeled size:CatVar") is the difference between the 
slope for CatVar = 0 and the slope for CatVar = 1

You can check it plotting this:

plot(dat$size,dat$response,col=dat$CatVar)
abline(a=coef(model)[1],b=coef(model)[2],col=1)
abline(a=sum(coef(model)[c(1,3)]),b=sum(coef(model)[c(2,4)]),col=2)

This way of presenting coefficients is the default in R (treatment contrast) 
but there are other alternatives, see ?contr.treatment.

Hope it helps,

Aitor


--------------------------------------------------
From: "Scott Chamberlain" <scttchamberlain4 at gmail.com>
Sent: Friday, September 23, 2011 5:59 PM
To: <R-sig-ecology at r-project.org>
Subject: [R-sig-eco] glm coefficients

> Hello,
>
>
> Is there a way to add up coefficients from a glm model for an ANCOVA to 
> get
> the coefficients for each term?
>
> For example, in the following:
> dat <- data.frame(response = rnorm(9), size = rnorm(9),  covariate =
> c(1,1,1,1,0,0,0,0,0))
> model <- glm(response ~ size * covariate, data = dat)
>> coef(model)
>   (Intercept)           size                covariate       size:covariate
>    -0.2995964     -0.1969266      0.7158756      1.2829886
>
> we are interested in the coefficients for the intercepts for size at both
> levels of the covariate  (0 and 1), and the slopes for each line. This
> requires adding up coefficients from the above output.
>
> Are there built in base functions or in other packages that do these
> additions?
>
>
> Thanks!
> __
> Scott Chamberlain
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>