1

This is my first time asking a question about R, so forgive me if I leave out details that are necessary. I'll try to be as thorough but concise as possible.

My data has 4 columns: score (values of -1, 0, or 1); species (at least 10 different ones); habitat (three different ones); and site (at least 8 different ones). So each row is a unique combination of score, species, habitat, and site. See below for top rows:

score species habitat  site
0    NOCA     NAG  NAG1
0    BRTH     NAG  NAG1
0    BARS     NAG  NAG1
1    COYE     NAG  NAG1
0    HOWR     NAG  NAG1
0    SAVS     NAG  NAG1
0    CEDW     NAG  NAG1
1    CHSP     NAG  NAG1
0    EAKI     NAG  NAG1
1    MODO     NAG  NAG1
0    NOCA     NAG NAG16
0    BRTH     NAG NAG16

I ran an lme to evaluate the effect of species and habitat on score, with site being a random effect. Code was as follows:

anova(model<-lme(score~species*habitat, random=~1|site))

I then wanted to do pairwise comparisons to figure out where the significant differences are. TukeyHSD doesn't work with lme, so I used the multcomp package as follows:

summary(glht(model, linfct=mcp(habitat="Tukey")))

Which as far as I can tell would do pairwise comparisons of scores based on habitat type. I also tried substituting species in for habitat to do comparisons based on species. In both cases I got the following error:

Error in contrMat(table(mf[[nm]]), type = types[pm]) : less than two groups

I don't see how I can have less than two groups anywhere, since the minimum number of groups I have for any column is three. Maybe I am misinterpreting what "groups" R is referring to? Any suggestions on what might be going wrong and how to fix it?

EDIT As per recommendations (thank you), here is some more information about my data that might make it reproducible. First, the code I used in its entirety.

library(nlme)
library(multcomp)
null<-read.csv("baci_null_red.csv", head=TRUE)
attach(null)
anova(model<-lme(score~species*habitat, random=~1|site))
              numDF   denDF  F-value  p-value

(Intercept)         1   1392   1.929021  0.1651

species            29   1392   2.691207    <.0001

habitat             2    48    3.412485  0.0411

species:habitat    58  1392    1.239267  0.1099

summary(glht(model, linfct=mcp(habitat="Tukey")))

Error in contrMat(table(mf[[nm]]), type = types[pm]) : 
  less than two groups

Here are some summaries about the data that may help in reproducing it...it's just kind of big and I'm not sure how to make it minimal and yet still get the error.

str(null)

'data.frame':   1530 obs. of  4 variables:
 $ score  : int  0 0 -1 0 0 0 0 0 0 0 ...
 $ species: Factor w/ 30 levels "AMGO","AMRO",..: 27 5 7 2 18 12 22 28 6 13 ...
 $ habitat: Factor w/ 3 levels "CUM","IAG","NAG": 3 3 3 3 3 3 3 3 3 3 ...
 $ site   : Factor w/ 51 levels " CUM6","CUM1",..: 37 37 37 37 37 37 37 37 37 37 ..

summary(null)

 score             species     habitat        site     

 Min.   :-1.00000   AMGO   :  51   CUM:210    CUM6  :  30  
 1st Qu.: 0.00000   AMRO   :  51   IAG:660   CUM1   :  30  
 Median : 0.00000   BARS   :  51   NAG:660   CUM10  :  30  
 Mean   :-0.01569   BCCH   :  51             CUM12  :  30  
 3rd Qu.: 0.00000   BHCO   :  51             CUM2   :  30  
 Max.   : 1.00000   BLJA   :  51             CUM3   :  30  
                (Other):1224             (Other):1350  

dput(null[somerows,c("species","habitat","site")])


structure(list(species = structure(c(27L, 5L, 7L, 2L, 18L, 12L, 
22L, 28L, 6L, 13L, 15L, 23L, 4L, 10L, 14L, 11L, 20L, 30L, 9L, 
19L, 26L, 8L, 16L, 25L, 3L, 17L, 21L, 29L, 24L, 1L, 27L, 5L, 
7L, 2L, 18L, 12L, 22L, 28L, 6L, 13L, 15L, 23L, 4L, 10L, 14L, 
11L, 20L, 30L, 9L, 19L, 26L, 8L, 16L, 25L, 3L, 17L, 21L, 29L, 
24L, 1L, 27L, 5L, 7L, 2L, 18L, 12L, 22L, 28L, 6L, 13L, 15L, 23L, 
4L, 10L, 14L, 11L, 20L, 30L, 9L, 19L, 26L, 8L, 16L, 25L, 3L, 
17L, 21L, 29L, 24L, 1L, 27L, 5L, 7L, 2L, 18L, 12L, 22L, 28L, 
6L, 13L), .Label = c("AMGO", "AMRO", "BARS", "BCCH", "BHCO", 
"BLJA", "BOBO", "BRTH", "CEDW", "CSWA", "EABL", "EAKI", "EAWP", 
"FISP", "GCFL", "HOLA", "HOWR", "INBU", "KILL", "LEFL", "MODO", 
"NOCA", "RBGR", "RWBL", "SAVS", "SOSP", "VESP", "WIFL", "YSFL", 
"YWAR"), class = "factor"), habitat = structure(c(3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L), .Label = c("CUM", "IAG", "NAG"), class = "factor"), site = structure(c(37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 
46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 
46L, 46L, 46L, 46L, 46L, 46L, 46L, 47L, 47L, 47L, 47L, 47L, 47L, 
47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 
47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 48L, 48L, 
48L, 48L, 48L, 48L, 48L, 48L, 48L, 48L), .Label = c(" CUM6", 
"CUM1", "CUM10", "CUM12", "CUM2", "CUM3", "CUM8", "IAG1", "IAG10", 
"IAG13", "IAG14", "IAG15", "IAG16", "IAG18", "IAG19", "IAG21", 
"IAG22", "IAG23", "IAG24", "IAG25", "IAG26", "IAG27", "IAG28", 
"IAG3", "IAG4", "IAG5", "IAG6", "IAG8", "IAG9", "NAG10", "NAG11", 
"NAG13", "NAG14", "NAG15", "NAG18", "NAG19", "NAG2", "NAG21", 
"NAG22", "NAG23", "NAG24", "NAG25", "NAG26", "NAG27", "NAG28", 
"NAG3", "NAG4", "NAG5", "NAG6", "NAG7", "NAG8"), class = "factor")), .Names =
  c("species", "habitat", "site"), row.names = c(NA, 100L), class = "data.frame")

There you have it :s thanks for any guidance at all.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 1
    this is not [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) because there is no variation in `habitat` or `site` in your example. – user1317221_G Feb 12 '13 at 21:10
  • You really need to give more information about your data. Look around at other questions here for how to make yours reproducible. At the very minimum, give str() and summary() of your data. I made a dummy set matching your description, and it runs fine, so I expect there's something about your data that's causing the issue. – alexwhan Feb 13 '13 at 01:47
  • it wasn't me. That said: I couldn't reproduce it either (`score` wasn't included in your `dput()`). Making a reproducible example is often very hard, but it's the only way to go (did you read the link in @user1317221_G's comment?) Can you post the `baci_null_red.csv` data set, or an anonymized/randomized version of it, somewhere publicly accessible? Can you examine `table(species,habitat)` and make sure you have every species in every habitat? – Ben Bolker Mar 11 '13 at 04:04

1 Answers1

0

I just resolved this issue by avoiding to attach the dataframe. I read online that when trying to pass on a fitted model to a function, attaching the dataframe can create issues.

I suggest using a code like this instead of using attach()

summary(model<-lme(fix~treat*week, random=~1|rep/week, data=dataframename))
summary(glht(model, linfct=mcp(treat="Tukey")))
sschale
  • 5,168
  • 3
  • 29
  • 36