Francesco Sarracino
2012-Dec-22 01:13 UTC
[R] how to control the naming of factors in regression
Dear R listers, I am regressing an ordered variable over an ordered independent variable using polr. The output lists every level of the ordered independent variable repeating its name and adding a suffix. In my case my variable is called "o.particip" and in my regression table I get the following: o.particip.L o.particip.Q o.particip.C o.particip^4 o.particip^5 o.particip^6 Is there any way to control the way R name the levels? I'd like each level to be consistently identified (either numbers or letters) and preferably I'd prefer to see the original naming of the level (the value label - in Stata dictionary). Moreover, the little "hat" in "o.particip^4" drives latex nuts. Any ideas on how to fix these things? Thank you very much for your kind help, f. -- Francesco Sarracino, Ph.D. https://sites.google.com/site/fsarracino/ [[alternative HTML version deleted]]
William Dunlap
2012-Dec-22 01:32 UTC
[R] how to control the naming of factors in regression
The '.L', ..., '^6' are the column names from the output contr.poly(), the default contrasts function for ordered factors. You can define your own contrasts function that calls contr.poly() and renames the columns to something you like, then set options("contrasts") so your contrasts function is used for ordered factors.> contr.myPoly <- function(...) { tmp <- contr.poly(...) ; colnames(tmp) <- paste0("Degree",seq_len(ncol(tmp))) ; tmp } > options(contrasts=c("contr.treatment", "contr.myPoly")) > d <- data.frame(y=1:20, x=ordered(rep(letters[1:6],length=20))) > lm(y ~ x, data=d)Call: lm(formula = y ~ x, data = d) Coefficients: (Intercept) xDegree1 xDegree2 xDegree3 xDegree4 xDegree5 10.5000 1.3148 1.3093 0.4472 -1.1339 0.7559 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Francesco Sarracino > Sent: Friday, December 21, 2012 5:13 PM > To: r-help at r-project.org > Subject: [R] how to control the naming of factors in regression > > Dear R listers, > > I am regressing an ordered variable over an ordered independent variable > using polr. > > The output lists every level of the ordered independent variable repeating > its name and adding a suffix. In my case my variable is called "o.particip" > and in my regression table I get the following: > > o.particip.L > > o.particip.Q > > o.particip.C > > o.particip^4 > > o.particip^5 > > o.particip^6 > > > Is there any way to control the way R name the levels? I'd like each level > to be consistently identified (either numbers or letters) and preferably > I'd prefer to see the original naming of the level (the value label - in > Stata dictionary). Moreover, the little "hat" in "o.particip^4" drives > latex nuts. Any ideas on how to fix these things? > Thank you very much for your kind help, > f. > > -- > Francesco Sarracino, Ph.D. > https://sites.google.com/site/fsarracino/ > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.