[R] glht: Problem with symbolic contrast for factors withnumber-levels

William Dunlap wdunlap at tibco.com
Fri Mar 4 20:33:45 CET 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Ulrich Halekoh
> Sent: Friday, March 04, 2011 11:04 AM
> To: r-help at r-project.org
> Subject: [R] glht: Problem with symbolic contrast for factors 
> withnumber-levels
> 
> Using a factor with  'number'  levels the straightforward 
> symbolic formulation of a contrast in 'glht' of
> the 'multcomp' package fails.
> 
> How can this problem be resolved without having to redefine 
> the factor levels?
> 
> Example:
> #A is a factor with  'number' levels
> #B similar factor with 'letter' levels
> dat<-data.frame(y=1:4,A=factor(c(1,1,2,2)),
>                       B=factor(c('e','e','f','f')) )
> motA<-lm(y~A,data=dat)
> motB<-lm(y~B,data=dat)
> library(multcomp)
> #does not work
> glht(motA,linfct=mcp(A=c("2 - 1 = 0 ")))
> 
> #the error message is
> # Error in coefs(ex[[3]]) : 
> #   cannot interpret expression '1' as linear function

Try putting backquotes around the factor levels so
the R parser makes names out of them.

  > glht(motA,linfct=mcp(A=c("`2` - `1` = 0 ")))
  
           General Linear Hypotheses

  Multiple Comparisons of Means: User-defined Contrasts
  
  
  Linear Hypotheses:
                 Estimate
  `2` - `1` == 0        2

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
> 
> #works
> glht(motB,linfct=mcp(B=c("f - e  = 0")))
> 
> regards
> 
> Ulrich Halekoh
> Aarhus University
> e-mail: Ulrich.Halekoh at agrsci.dk
> 
> I use R.2.12.2, package version of multcomp : 1.2-5
> 
> ______________________________________________
> 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.
> 



More information about the R-help mailing list