Hi, I'm trying to use adonis on a subset of data from a dataframe. The actual data is in columns 5:118, and the first four columns are various factors. There are 3 levels of the factor Habitat, and I want to examine differences among only two of them. So I started with:> CoastNear = subset(gel_data, Habitat != "I")The resulting data.frame has three levels for Habitat, but only two of those levels have any records. Then I run:> adonis(CoastNear[,5:118]~Habitat, data = CoastNear,permutations=1000,+ method='jaccard') Call: adonis(formula = CoastNear[, 5:118] ~ Habitat, data = CoastNear, permutations = 1000, method = "jaccard") Df SumsOfSqs MeanSqs F.Model R2 Pr(>F) Habitat 2.0000000 0.0092966 0.0046483 2.0549327 0.0707 0.005 Residuals 54.0000000 0.1221491 0.0022620 0.9293 Total 56.0000000 0.1314457 1.0000 This appears to be wrong - with only two Habitat levels I should only have 1 Df, shouldn't I? I checked by forcibly excising the third factor level:> CoastNear$Habitat <- as.factor(as.character(CoastNear$Habitat)) > adonis(CoastNear[,5:118]~Habitat, data = CoastNear,permutations=1000,+ method='jaccard') Call: adonis(formula = CoastNear[, 5:118] ~ Habitat, data = CoastNear, permutations = 1000, method = "jaccard") Df SumsOfSqs MeanSqs F.Model R2 Pr(>F) Habitat 1.0000000 0.0092966 0.0092966 4.1859740 0.0707 0.003 Residuals 55.0000000 0.1221491 0.0022209 0.9293 Total 56.0000000 0.1314457 1.0000 This appears to be correct. Subsetting factors is something I always struggle with in R, but, based on previous experience with lda(), it seems that R generally does the right thing. Am I doing something wrong here, or is there a problem in adonis? Thanks, Tyler ps. Sorry for not supplying a reproducible bit of code. The data.frame is quite large. The general layout is:> head(gel_data) # additional data columns trimmed for emailSite Habitat Plot Concate A01 A02 A03 A04 A05 A06 A07 A08 A09 A10 1 PE C 1 PEC1 1 1 1 1 1 1 1 1 1 1 2 PE C 3 PEC3 1 1 1 1 0 1 1 1 1 1 3 PE C 4 PEC4 1 1 1 1 0 1 1 1 1 1 4 PE C 6 PEC6 1 1 1 1 0 1 1 1 1 1 5 PE C 12 PEC12 1 1 1 1 0 1 1 1 1 1 6 PE C 13 PEC13 1 1 1 1 0 1 1 1 1 1
On Thu, 2008-04-10 at 11:18 -0300, tyler wrote:> Hi, > > I'm trying to use adonis on a subset of data from a dataframe. The > actual data is in columns 5:118, and the first four columns are various > factors. There are 3 levels of the factor Habitat, and I want to examine > differences among only two of them. So I started with:Hi Tyler, This behaviour arises from the following, using the in-built dune data:> example(dune)dune> data(dune) dune> data(dune.env)> newdune.env <- subset(dune.env, Management != "NM") > newdune.env$Management[1] BF SF SF SF HF SF HF HF BF BF HF SF SF HF Levels: BF HF NM SF Notice this hasn't dropped the empty level "NM", and this is what is catching out adonis --- it is not checking for empty levels in the grouping factor, as this shows:> newdune <- dune[which(dune.env$Management != "NM"), ] > adonis(newdune ~ Management*A1, data=newdune.env, permutations=100)Call: adonis(formula = newdune ~ Management * A1, data = newdune.env, permutations = 100) Df SumsOfSqs MeanSqs F.Model R2 Pr(>F) Management 3.00000 0.57288 0.19096 1.27735 0.2694 <0.01 *** A1 1.00000 0.29851 0.29851 1.99672 0.1404 0.33 Management:A1 3.00000 0.35831 0.11944 0.79892 0.1685 0.87 Residuals 6.00000 0.89699 0.14950 0.4218 Total 13.00000 2.12669 1.0000 --- Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 For now, forcibly remove empty factor levels as per your second example, but I'll take a look at fixing adonis() --- it looks easy but that could be deceptive! I've CC'd the maintainer (Jari Oksanen) here as well, as I don't think Jari follows R-help too closely at the moment. <snip/>> > Thanks, > > Tyler > > ps. Sorry for not supplying a reproducible bit of code. The data.frame > is quite large. The general layout is:It is often instructional to use one of the in-built data sets (as I have here), even if just to prove to yourself that it isn't a problem with your data. All the best, G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%