[R] Constrain coefs. in linear model to sum to 0

Bill.Venables at csiro.au Bill.Venables at csiro.au
Tue Aug 8 07:52:20 CEST 2006


 
Gorjanc Gregor asks:

> 
> Hello!
> 
> I would like to use constrain to sum coeficients of a factor to 0
instead
> of classical corner contraint i.e. I would like to fit a model like
> 
> lm(y ~ 1 + effectA + effectB)
> 
> and say get parameters
> 
> intercept
> effectA_1
> effectA_2
> effectB_1
> effectB_2
> effectB_3
> 
> where effectA_1 represents deviation of level A_1 from intercept and 
> sum(effectA_1, effectA_2) = 0 and the same for factor B.
> 
> Is this possible to do?

Yes, but not quite as simply as you would like.  If you set

options(contrasts = c("contr.sum", "contr.poly"))

for example, then factor models are parametrised as you wish above,
BUT you don't get all the effects directly

In your case above, for example, if fm is the fitted model object, then

coef(fm)

Will give you intercept, effectA_2, effectB_2, effectB_3.  The
remaining effects*_1 you will need to calculate as the negative of the
sum of all the others.

This gets a bit more complicated when you have crossed terms, a*b, but
the same principle applies.

Bill Venables.


> 
> Lep pozdrav / With regards,
>     Gregor Gorjanc
> 
> ----------------------------------------------------------------------
> University of Ljubljana     PhD student
> Biotechnical Faculty        URI: http://www.bfro.uni-lj.si/MR/ggorjan
> Zootechnical Department     mail: gregor.gorjanc <at> bfro.uni-lj.si
> Groblje 3                   tel: +386 (0)1 72 17 861
> SI-1230 Domzale             fax: +386 (0)1 72 17 888
> Slovenia, Europe
> ----------------------------------------------------------------------
> "One must learn by doing the thing; for though you think you know it,
>  you have no certainty until you try." Sophocles ~ 450 B.C.


Well, now's your chance!



More information about the R-help mailing list