I'd like to make a less than full rank design using dummy variables
for factors. Here is some example data:
when <- data.frame(time = c("afternoon", "night",
"afternoon",
"morning", "morning",
"morning",
"morning", "afternoon",
"afternoon"),
day = c("Monday", "Monday",
"Monday",
"Wednesday", "Wednesday",
"Friday",
"Saturday", "Saturday",
"Friday"))
For a single factor, I can do this this using
> head(model.matrix(~time -1, data = when))
timeafternoon timemorning timenight
1 1 0 0
2 0 0 1
3 1 0 0
4 0 1 0
5 0 1 0
6 0 1 0
but this breakdown muti-variable formulas such as "time + day" or
"time + dat + time:day".
I've looked for alternate contrast functions to do this and I haven't
figured out a way to coerce existing functions to get the desired
output. Hopefully I haven't missed anything obvious.
Thanks,
Max
> sessionInfo()
R version 2.11.1 Patched (2010-09-11 r52910)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Have you tried setting singular.ok=TRUE in the call to lm? This will start with the full set of contrasts, but only fit those that it is able to. Otherwise you can set specific contrasts or subsets using the C (note case) or contrasts functions. -- 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 Max Kuhn > Sent: Monday, December 06, 2010 8:35 AM > To: r-help at r-project.org > Subject: [R] less than full rank contrast methods > > I'd like to make a less than full rank design using dummy variables > for factors. Here is some example data: > > when <- data.frame(time = c("afternoon", "night", "afternoon", > "morning", "morning", "morning", > "morning", "afternoon", "afternoon"), > day = c("Monday", "Monday", "Monday", > "Wednesday", "Wednesday", "Friday", > "Saturday", "Saturday", "Friday")) > > For a single factor, I can do this this using > > > head(model.matrix(~time -1, data = when)) > timeafternoon timemorning timenight > 1 1 0 0 > 2 0 0 1 > 3 1 0 0 > 4 0 1 0 > 5 0 1 0 > 6 0 1 0 > > but this breakdown muti-variable formulas such as "time + day" or > "time + dat + time:day". > > I've looked for alternate contrast functions to do this and I haven't > figured out a way to coerce existing functions to get the desired > output. Hopefully I haven't missed anything obvious. > > Thanks, > > Max > > > sessionInfo() > R version 2.11.1 Patched (2010-09-11 r52910) > Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) > > locale: > [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > ______________________________________________ > 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.
Given a non-singular fit, the contrast function in the rms package will allow you to request multi-dimensional contrasts some of which are redundant. These singular contrasts are automatically ignored. One use for this is to test for differences in longitudinal trends between two of three treatment groups, where the time trend is a multiple degree of freedom parameterization such as cubic splines. You don't have to stop and think about how many time points to test; just test as many as you'd like and get the right degrees of freedom according to the number of spline terms (main effects + interactions). Frank ----- Frank Harrell Department of Biostatistics, Vanderbilt University -- View this message in context: http://r.789695.n4.nabble.com/less-than-full-rank-contrast-methods-tp3074688p3075771.html Sent from the R help mailing list archive at Nabble.com.