AMR/inst/tinytest/test-pca.R

76 lines
3.4 KiB
R
Raw Normal View History

2021-05-15 21:36:22 +02:00
# ==================================================================== #
# TITLE #
2022-10-05 09:12:22 +02:00
# AMR: An R Package for Working with Antimicrobial Resistance Data #
2021-05-15 21:36:22 +02:00
# #
# SOURCE #
# https://github.com/msberends/AMR #
# #
2022-10-05 09:12:22 +02:00
# CITE AS #
# Berends MS, Luz CF, Friedrich AW, Sinha BNM, Albers CJ, Glasner C #
# (2022). AMR: An R Package for Working with Antimicrobial Resistance #
# Data. Journal of Statistical Software, 104(3), 1-31. #
# doi:10.18637/jss.v104.i03 #
# #
2022-12-27 15:16:15 +01:00
# Developed at the University of Groningen and the University Medical #
# Center Groningen in The Netherlands, in collaboration with many #
# colleagues from around the world, see our website. #
2021-05-15 21:36:22 +02:00
# #
# 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 the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
# ==================================================================== #
2022-08-28 10:31:50 +02:00
resistance_data <- structure(list(
order = c("Bacillales", "Enterobacterales", "Enterobacterales"),
genus = c("Staphylococcus", "Escherichia", "Klebsiella"),
AMC = c(0.00425, 0.13062, 0.10344),
CXM = c(0.00425, 0.05376, 0.10344),
CTX = c(0.00000, 0.02396, 0.05172),
TOB = c(0.02325, 0.02597, 0.10344),
TMP = c(0.08387, 0.39141, 0.18367)
),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -3L),
groups = structure(list(
order = c("Bacillales", "Enterobacterales"),
.rows = list(1L, 2:3)
),
row.names = c(NA, -2L),
class = c("tbl_df", "tbl", "data.frame"),
.drop = TRUE
)
)
2021-05-15 21:36:22 +02:00
pca_model <- pca(resistance_data)
expect_inherits(pca_model, "pca")
pdf(NULL) # prevent Rplots.pdf being created
2021-05-21 20:20:51 +02:00
if (AMR:::pkg_is_available("ggplot2")) {
2021-05-15 21:36:22 +02:00
ggplot_pca(pca_model, ellipse = TRUE)
ggplot_pca(pca_model, arrows_textangled = FALSE)
}
2021-10-05 09:58:08 +02:00
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) {
2022-08-28 10:31:50 +02:00
resistance_data <- example_isolates %>%
group_by(
order = mo_order(mo),
genus = mo_genus(mo)
) %>%
2021-05-15 21:36:22 +02:00
summarise_if(is.rsi, resistance, minimum = 0)
2022-08-28 10:31:50 +02:00
pca_result <- resistance_data %>%
pca(AMC, CXM, CTX, CAZ, GEN, TOB, TMP, "SXT")
2021-05-15 21:36:22 +02:00
expect_inherits(pca_result, "prcomp")
2022-08-28 10:31:50 +02:00
2021-05-21 20:20:51 +02:00
if (AMR:::pkg_is_available("ggplot2")) {
2021-05-15 21:36:22 +02:00
ggplot_pca(pca_result, ellipse = TRUE)
ggplot_pca(pca_result, ellipse = FALSE, arrows_textangled = FALSE, scale = FALSE)
}
}