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? 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.
"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
Bill.Venables at csiro.au
2006-Aug-08 05:52 UTC
[R] Constrain coefs. in linear model to sum to 0
Gorjanc Gregor asks:> > Hello! > > I would like to use constrain to sum coeficients of a factor to 0instead> 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!
Apparently Analagous Threads
- Regression; how to get t-values for all parameters estimates
- nlsList - Error in !unlist(lapply(coefs, is.null))
- interpretation of RCS 'coefs' and 'knots'
- Problem extracting enough coefs from gam (mgcv package)
- acts_as_paranoid --> undefined method `constrain' for class `Class'