Dear listers, I would like to compare the levels of a factor with 8 age categories (0,10] (10,20] (20,30] (30,40] (40,50] (50,60] (60,70] (70,90] (however, the factor has not been ordered yet). The default in glm is cont.treatment (for unordered factors) and that leads to compare each level to the first one. I would rather prefer to compare the 2nd to the 1st, the 3rd to the 2nd, the 4th to the 3rd, etc... My understanding is that cont.poly may make the trick, eg specified like this: mod3<-glm(AE~agecat, family=binomial,data=qinghai2, contrasts=list(agecat="contr.poly")) but I am not sure to be right. Would be grateful if a true statistician can confirm or fire me... and before definitive fire tell me how to manage with this...
Hi Patrick,>> The default in glm is cont.treatment (for unordered factors) and that >> leads to compare each level to the first one. I would rather prefer to >> compare the 2nd to the 1st, the 3rd to the 2nd, the 4th to the 3rd, >> etc...The functions ?C and ?contrasts allow you to set up your own contrast matrix. A function to carry out the type of comparisons you are interested in has been written by Venables & Ripley. See contr.sdif in package MASS. Regards, Mark. Patrick Giraudoux wrote:> > Dear listers, > > I would like to compare the levels of a factor with 8 age categories > (0,10] (10,20] (20,30] (30,40] (40,50] (50,60] (60,70] (70,90] (however, > the factor has not been ordered yet). The default in glm is > cont.treatment (for unordered factors) and that leads to compare each > level to the first one. I would rather prefer to compare the 2nd to the > 1st, the 3rd to the 2nd, the 4th to the 3rd, etc... My understanding is > that cont.poly may make the trick, eg specified like this: > > mod3<-glm(AE~agecat, family=binomial,data=qinghai2, > contrasts=list(agecat="contr.poly")) > > but I am not sure to be right. > > Would be grateful if a true statistician can confirm or fire me... and > before definitive fire tell me how to manage with this... > > ______________________________________________ > R-help at r-project.org 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. > >-- View this message in context: http://www.nabble.com/Comparison-of-age-categories-using-contrasts-tp22031426p22032444.html Sent from the R help mailing list archive at Nabble.com.
Dear listers, I would like to compare the levels of a factor with 8 age categories (0,10] (10,20] (20,30] (30,40] (40,50] (50,60] (60,70] (70,90] (however, the factor has not been ordered yet). The default in glm is cont.treatment (for unordered factors) and that leads to compare each level to the first one. I would rather prefer to compare the 2nd to the 1st, the 3rd to the 2nd, the 4th to the 3rd, etc... My understanding is that cont.poly may make the trick, eg specified like this: mod3<-glm(AE~agecat, family=binomial,data=qinghai2, contrasts=list(agecat="contr.poly")) but I am not sure to be right. Would be grateful if a true statistician can confirm or fire me... and before definitive fire tell me how to manage with this...
One approach is to create your own contrasts matrix:> mycmat <- diag(8) > mycmat[ row(mycmat) == col(mycmat) + 1 ] <- -1 > mycmati <- solve(mycmat) > contrasts(agefactor) <- mycmati[,-1]Now when you use agefactor, the intercept will be the first age group and the slopes will be the differences between the pairs of groups (make sure that the order of the levels of agefactor is correct). The difference between this method and the contr.sdif function in MASS is how the intercept will end up being interpreted (and the dimnames). Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Patrick Giraudoux > Sent: Sunday, February 15, 2009 9:23 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Comparison of age categories using contrasts > > Dear listers, > > I would like to compare the levels of a factor with 8 age categories > (0,10] (10,20] (20,30] (30,40] (40,50] (50,60] (60,70] (70,90] > (however, > the factor has not been ordered yet). The default in glm is > cont.treatment (for unordered factors) and that leads to compare each > level to the first one. I would rather prefer to compare the 2nd to the > 1st, the 3rd to the 2nd, the 4th to the 3rd, etc... My understanding is > that cont.poly may make the trick, eg specified like this: > > mod3<-glm(AE~agecat, family=binomial,data=qinghai2, > contrasts=list(agecat="contr.poly")) > > but I am not sure to be right. > > Would be grateful if a true statistician can confirm or fire me... and > before definitive fire tell me how to manage with this... > > ______________________________________________ > R-help at r-project.org 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.