Many thanks, Greg. It works like a dream. Best wishes,
Pete From: Greg Snow <[email protected]> To: Peter Morgan <[email protected]>, "[email protected]" <[email protected]> Date: 03/08/2011 19:06 Subject: Re: [R] Coefficient names when using lm() with contrasts Sent by: [email protected] If you add column names to your contrast matrix (treat3) then those names will be used in the coefficient names. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [email protected] 801.408.8111 > -----Original Message----- > From: [email protected] [mailto:r-help-bounces@r- > project.org] On Behalf Of Peter Morgan > Sent: Wednesday, August 03, 2011 6:12 AM > To: [email protected] > Subject: [R] Coefficient names when using lm() with contrasts > > Dear R Users, > > Am using lm() with contrasts as below. If I skip the contrasts() > statement, I get the coefficient names to be > > names(results$coef) > [1] "(Intercept)" "VarAcat" "VarArat" "VarB" > > which are much more meaningful than ones based on integers. > > Can anyone tell me how to get R to keep the coefficient names based on > the > factor levels whilst using contrasts rather than labelling them with > integers? > > Many thanks in advance, > > Pete > > Cardiff, UK > > > dt=read.table("testreg.txt",sep=",",header=T) > > dt > ID VarA VarB VarC > 1 1 cat 2 23 > 2 2 dog 3 56 > 3 3 rat 5 35 > 4 4 cat 2 43 > 5 5 cat 7 51 > 6 6 dog 3 31 > 7 7 dog 4 65 > 8 8 rat 1 18 > 9 9 rat 6 49 > 10 10 dog 3 28 > > dt$VarA=relevel(dt$VarA,ref="dog") > > treat3=matrix(-1/3,ncol=2,nrow=3); for (i in 1:2) {treat3[i+1,i]=2/3} > > contrasts(dt$VarA)=treat3 > > levels(dt$VarA) > [1] "dog" "cat" "rat" > > results=lm(formula=VarC~VarA+VarB, data=dt) > > names(results$coef) > [1] "(Intercept)" "VarA1" "VarA2" "VarB" > [[alternative HTML version deleted]] > > ______________________________________________ > [email protected] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

