0

I am wondering if there is some way how to extract results from adonis function in vegan package and possibly save it by write.table?

I mean other way than print results to console and copy-past R2 value to Excel.

This can be especially useful when running adonis iteratively for multiple combinations and saving objects with results into one list as suggested in this SO answer.

tlask
  • 198
  • 9

1 Answers1

2

Here is an example on how you can extract the needed parameters from the model. I will use the linked example:

library(vegan)
data(dune)
data(dune.env)

lapply is used here instead of a loop:

results <- lapply(colnames(dune.env), function(x){
  form <- as.formula(paste("dune", x, sep="~")) 
  z <- adonis(form, data = dune.env, permutations=99)
  return(as.data.frame(z$aov.tab)) #convert anova table to a data frame
}
)

this will produce a list of data frames each having the form

> results[[1]]
#output
          Df SumsOfSqs   MeanSqs  F.Model        R2 Pr(>F)
A1         1 0.7229518 0.7229518 3.638948 0.1681666   0.01
Residuals 18 3.5760701 0.1986706       NA 0.8318334     NA
Total     19 4.2990219        NA       NA 1.0000000     NA

now you can name the list elements with the appropriate variable:

names(results) <- colnames(dune.env)

convert to a data frame:

results <- do.call(rbind, results)
#output
                      Df SumsOfSqs   MeanSqs  F.Model        R2 Pr(>F)
A1.A1                  1 0.7229518 0.7229518 3.638948 0.1681666   0.01
A1.Residuals          18 3.5760701 0.1986706       NA 0.8318334     NA
A1.Total              19 4.2990219        NA       NA 1.0000000     NA
Moisture.Moisture      3 1.7281651 0.5760550 3.585140 0.4019903   0.01
Moisture.Residuals    16 2.5708567 0.1606785       NA 0.5980097     NA
Moisture.Total        19 4.2990219        NA       NA 1.0000000     NA
Management.Management  3 1.4685918 0.4895306 2.767243 0.3416107   0.01
Management.Residuals  16 2.8304301 0.1769019       NA 0.6583893     NA
Management.Total      19 4.2990219        NA       NA 1.0000000     NA
Use.Use                2 0.5531507 0.2765754 1.255190 0.1286690   0.30
Use.Residuals         17 3.7458712 0.2203454       NA 0.8713310     NA
Use.Total             19 4.2990219        NA       NA 1.0000000     NA
Manure.Manure          4 1.5238805 0.3809701 2.059193 0.3544714   0.03
Manure.Residuals      15 2.7751414 0.1850094       NA 0.6455286     NA
Manure.Total          19 4.2990219        NA       NA 1.0000000     NA

and now you can save it to a csv or any other format you like:

write.csv(results, "res.csv")

If only R squared is needed change the lapply call to:

results <- lapply(colnames(dune.env), function(x){
  form <- as.formula(paste("dune", x, sep="~"))
  z <- adonis(form, data = dune.env, permutations=99)
  return(data.frame(name = rownames(z$aov.tab), R2 = z$aov.tab$R2))
}
)
missuse
  • 19,056
  • 3
  • 25
  • 47
  • Very elegant solution. Playing with number of columns in final table is possible as well. `return(data.frame(name = rownames(z$aov.tab), R2 = z$aov.tab$R2, P = z$aov.tab$P, Df = z$aov.tab$Df))` – tlask Jan 28 '18 at 23:15