[R] Constrain coefs. in linear model to sum to 0
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Mon Aug 7 11:16:57 CEST 2006
"Gorjanc Gregor" <Gregor.Gorjanc at bfro.uni-lj.si> writes:
> 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?
This is what contr.sum does. There are multiple ways to set this
instead of the default contr.treatment:
(a) options(contrast=....)
(b) contrasts(effectA) <- contr.sum
(c) lm(y~C(effectA,contr.sum)+.....)
Notice though that you only get parameters for a linearly independent
subset of groups (i.e. the first n-1 of them). The last one is defined
as -sum(the other levels).
--
O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list