From 9fc858f2082a9531c7bd08e6ee514c0a623bfbd9 Mon Sep 17 00:00:00 2001 From: "Matthijs S. Berends" Date: Sun, 8 Mar 2020 09:12:11 +0100 Subject: [PATCH] (v1.0.1.9001) PCA unit tests --- DESCRIPTION | 4 ++-- NAMESPACE | 1 + NEWS.md | 2 +- R/ggplot_pca.R | 13 ++++++++---- R/pca.R | 1 + docs/404.html | 2 +- docs/LICENSE-text.html | 2 +- docs/articles/index.html | 2 +- docs/authors.html | 2 +- docs/index.html | 2 +- docs/news/index.html | 8 ++++---- docs/reference/ggplot_pca.html | 10 ++++++--- docs/reference/index.html | 2 +- man/ggplot_pca.Rd | 4 +++- tests/testthat/test-pca.R | 37 ++++++++++++++++++++++++++++++++++ 15 files changed, 71 insertions(+), 21 deletions(-) create mode 100644 tests/testthat/test-pca.R diff --git a/DESCRIPTION b/DESCRIPTION index a7b6196f..8f626de4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 1.0.1.9000 -Date: 2020-03-07 +Version: 1.0.1.9001 +Date: 2020-03-08 Title: Antimicrobial Resistance Analysis Authors@R: c( person(role = c("aut", "cre"), diff --git a/NAMESPACE b/NAMESPACE index a4ae25e8..9567031b 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -327,6 +327,7 @@ importFrom(stats,complete.cases) importFrom(stats,glm) importFrom(stats,lm) importFrom(stats,pchisq) +importFrom(stats,prcomp) importFrom(stats,predict) importFrom(tidyr,pivot_longer) importFrom(tidyr,pivot_wider) diff --git a/NEWS.md b/NEWS.md index 349a057a..6119f607 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 1.0.1.9000 +# AMR 1.0.1.9001 ### New * Support for easy principal component analysis for AMR, using the new `pca()` function diff --git a/R/ggplot_pca.R b/R/ggplot_pca.R index b03ae9aa..d512609a 100755 --- a/R/ggplot_pca.R +++ b/R/ggplot_pca.R @@ -33,6 +33,7 @@ #' @param ellipse_prob statistical size of the ellipse in normal probability #' @param ellipse_size the size of the ellipse line #' @param ellipse_alpha the alpha (transparency) of the ellipse line +#' @param points_size the size of the points #' @param points_alpha the alpha (transparency) of the points #' @param arrows a logical to indicate whether arrows should be drawn #' @param arrows_textsize the size of the text for variable names @@ -42,7 +43,7 @@ #' @param arrows_alpha the alpha (transparency) of the arrows and their text #' @param base_textsize the text size for all plot elements except the labels and arrows #' @param ... Parameters passed on to functions -#' @source The [ggplot_pca()] function is based on the [ggbiplot()] function from the `ggbiplot` package by Vince Vu, as found on GitHub: (retrieved: 2 March 2020, their latest commit: [`7325e88`](https://github.com/vqv/ggbiplot/commit/7325e880485bea4c07465a0304c470608fffb5d9); 12 February 2015). +#' @source The [ggplot_pca()] function is based on the `ggbiplot()` function from the `ggbiplot` package by Vince Vu, as found on GitHub: (retrieved: 2 March 2020, their latest commit: [`7325e88`](https://github.com/vqv/ggbiplot/commit/7325e880485bea4c07465a0304c470608fffb5d9); 12 February 2015). #' #' As per their GPL-2 licence that demands documentation of code changes, the changes made based on the source code were: #' 1. Rewritten code to remove the dependency on packages `plyr`, `scales` and `grid` @@ -324,14 +325,18 @@ pca_calculations <- function(pca_model, sigma <- var(cbind(x$xvar, x$yvar)) mu <- c(mean(x$xvar), mean(x$yvar)) ed <- sqrt(qchisq(ellipse_prob, df = 2)) - data.frame(sweep(circle %*% chol(sigma) * ed, 2, mu, FUN = "+"), + el <- data.frame(sweep(circle %*% chol(sigma) * ed, 2, mu, FUN = "+"), groups = x$groups[1]) + names(el)[1:2] <- c("xvar", "yvar") + el })) - names(ell)[1:2] <- c("xvar", "yvar") + if (NROW(ell) == 0) { + ell <- NULL + } } else { ell <- NULL } - + list(nobs.factor = nobs.factor, d = d, u = u, diff --git a/R/pca.R b/R/pca.R index 65bd1d3a..f6a9a2e1 100755 --- a/R/pca.R +++ b/R/pca.R @@ -31,6 +31,7 @@ #' The result of the [pca()] function is a [`prcomp`] object, with an additional attribute `non_numeric_cols` which is a vector with the column names of all columns that do not contain numeric values. These are probably the groups and labels, and will be used by [ggplot_pca()]. #' @rdname pca #' @exportMethod prcomp.data.frame +#' @importFrom stats prcomp #' @export #' @examples #' # `example_isolates` is a dataset available in the AMR package. diff --git a/docs/404.html b/docs/404.html index ee5c562c..2da52a13 100644 --- a/docs/404.html +++ b/docs/404.html @@ -78,7 +78,7 @@ AMR (for R) - 1.0.1.9000 + 1.0.1.9001 diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index fd38e4e8..83237154 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -78,7 +78,7 @@ AMR (for R) - 1.0.1.9000 + 1.0.1.9001 diff --git a/docs/articles/index.html b/docs/articles/index.html index ed5d18ed..81472786 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -78,7 +78,7 @@ AMR (for R) - 1.0.1.9000 + 1.0.1.9001 diff --git a/docs/authors.html b/docs/authors.html index c40eb5aa..4ec83601 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -78,7 +78,7 @@ AMR (for R) - 1.0.1.9000 + 1.0.1.9001 diff --git a/docs/index.html b/docs/index.html index bbd5b8b7..616492cf 100644 --- a/docs/index.html +++ b/docs/index.html @@ -43,7 +43,7 @@ AMR (for R) - 1.0.1.9000 + 1.0.1.9001 diff --git a/docs/news/index.html b/docs/news/index.html index c60b1aaf..2f69e007 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -78,7 +78,7 @@ AMR (for R) - 1.0.1.9000 + 1.0.1.9001 @@ -226,9 +226,9 @@ -
+

-AMR 1.0.1.9000 Unreleased +AMR 1.0.1.9001 Unreleased

@@ -1489,7 +1489,7 @@

Contents

@@ -117,9 +117,9 @@
  • - + - Conduct Principal Component Analysis for AMR + Conduct principal component analysis for AMR
  • @@ -307,6 +307,10 @@ ellipse_alpha

    the alpha (transparency) of the ellipse line

    + + points_size +

    the size of the points

    + points_alpha

    the alpha (transparency) of the points

    diff --git a/docs/reference/index.html b/docs/reference/index.html index 9016f199..47037b36 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -78,7 +78,7 @@ AMR (for R) - 1.0.1.9000 + 1.0.1.9001
  • diff --git a/man/ggplot_pca.Rd b/man/ggplot_pca.Rd index 4f1c1624..0db9b854 100644 --- a/man/ggplot_pca.Rd +++ b/man/ggplot_pca.Rd @@ -4,7 +4,7 @@ \alias{ggplot_pca} \title{PCA biplot with \code{ggplot2}} \source{ -The \code{\link[=ggplot_pca]{ggplot_pca()}} function is based on the \code{\link[=ggbiplot]{ggbiplot()}} function from the \code{ggbiplot} package by Vince Vu, as found on GitHub: \url{https://github.com/vqv/ggbiplot} (retrieved: 2 March 2020, their latest commit: \href{https://github.com/vqv/ggbiplot/commit/7325e880485bea4c07465a0304c470608fffb5d9}{\code{7325e88}}; 12 February 2015). +The \code{\link[=ggplot_pca]{ggplot_pca()}} function is based on the \code{ggbiplot()} function from the \code{ggbiplot} package by Vince Vu, as found on GitHub: \url{https://github.com/vqv/ggbiplot} (retrieved: 2 March 2020, their latest commit: \href{https://github.com/vqv/ggbiplot/commit/7325e880485bea4c07465a0304c470608fffb5d9}{\code{7325e88}}; 12 February 2015). As per their GPL-2 licence that demands documentation of code changes, the changes made based on the source code were: \enumerate{ @@ -71,6 +71,8 @@ ggplot_pca( \item{ellipse_alpha}{the alpha (transparency) of the ellipse line} +\item{points_size}{the size of the points} + \item{points_alpha}{the alpha (transparency) of the points} \item{arrows}{a logical to indicate whether arrows should be drawn} diff --git a/tests/testthat/test-pca.R b/tests/testthat/test-pca.R new file mode 100644 index 00000000..2ad7cdc4 --- /dev/null +++ b/tests/testthat/test-pca.R @@ -0,0 +1,37 @@ +# ==================================================================== # +# TITLE # +# Antimicrobial Resistance (AMR) Analysis # +# # +# SOURCE # +# https://gitlab.com/msberends/AMR # +# # +# LICENCE # +# (c) 2018-2020 Berends MS, Luz CF et al. # +# # +# This R package is free software; you can freely use and distribute # +# it for both personal and commercial purposes under the terms of the # +# GNU General Public License version 2.0 (GNU GPL-2), as published by # +# the Free Software Foundation. # +# # +# We created this package for both routine data analysis and academic # +# research and it was publicly released in the hope that it will be # +# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. # +# Visit our website for more info: https://msberends.gitlab.io/AMR. # +# ==================================================================== # + +context("pca.R") + +test_that("PCA works", { + library(dplyr) + resistance_data <- example_isolates %>% + filter(mo %in% as.mo(c("E. coli", "K. pneumoniae", "S. aureus"))) %>% + select(mo, AMC, CXM, CTX, TOB, TMP) %>% + group_by(order = mo_order(mo), # group on anything, like order + genus = mo_genus(mo)) %>% # and genus as we do here + summarise_if(is.rsi, resistance, minimum = 0) + + expect_s3_class(pca(resistance_data), "prcomp") + expect_s3_class(prcomp(resistance_data), "prcomp") + + ggplot_pca(pca(resistance_data), ellipse = TRUE) +})