Skip to content

Commit

Permalink
Add tidy function for adonis2 result
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebittinger committed Mar 23, 2023
1 parent 362ec23 commit add7e4d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3method(plot,pcoaplus)
S3method(tidy,adonis)
S3method(tidy,anova.cca)
export("%>%")
export(adonisplus)
export(adonispost)
Expand Down
25 changes: 25 additions & 0 deletions R/tidy.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,28 @@ tidy.adonis <- function (x, ...) {
attr(ret, "heading") <- NULL
ret
}

#' Tidy an \code{anova.cca} object
#'
#' @param x an object returned from \code{vegan::adonis2()}.
#' @param ... Additional arguments are not used.
#' @return A \code{tibble} with the following columns:
#' \item{term}{The name of the regression term.}
#' \item{df}{Degrees of freedom used by the model.}
#' \item{sumsq}{Sum of squares explained by this term.}
#' \item{r.squared}{
#' R-squared statistic, or the percent of variation explained by the model.}
#' \item{statistic}{
#' The value of a pseudo-F-statistic to use in the permutation test.}
#' \item{p.value}{P-value from the permutation test.}
#' @export
tidy.anova.cca <- function (x, ...) {
ret <- tibble::as_tibble(x, rownames = "term")
colnames(ret) <- c(
"term", "df", "sumsq", "r.squared", "statistic", "p.value")
attr(ret, "Random.seed") <- NULL
attr(ret, "F.perm") <- NULL
attr(ret, "control") <- NULL
attr(ret, "heading") <- NULL
ret
}
27 changes: 27 additions & 0 deletions man/tidy.anova.cca.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/testthat/test-tidy.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,23 @@ test_that("tidy.adonis produces expected result", {
p.value = c(0.635, NA, NA))
expect_equal(tidy(adonis_res), expected)
})

adonis2_res <- data.frame(
Df = c(1, 9, 10),
SumOfSqs = c(0.141236772727273, 1.4724405, 1.61367727272727),
R2 = c(0.0875247951460386, 0.912475204853961, 1),
F = c(0.863281711244329, NA, NA),
`Pr(>F)` = c(0.643, NA, NA),
row.names = c("study_group", "Residual", "Total"))
class(adonis2_res) <- c("anova.cca", "anova", "data.frame")

test_that("tidy.anova.cca produces expected result", {
expected <- tibble::tibble(
term = c("study_group", "Residual", "Total"),
df = c(1, 9, 10),
sumsq = c(0.141236772727273, 1.4724405, 1.61367727272727),
r.squared = c(0.0875247951460386, 0.912475204853961, 1),
statistic = c(0.863281711244329, NA, NA),
p.value = c(0.643, NA, NA))
expect_equal(tidy(adonis2_res), expected)
})

0 comments on commit add7e4d

Please sign in to comment.