1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-27 08:46:12 +01:00
AMR/R/pca.R

127 lines
6.4 KiB
R
Raw Normal View History

2020-03-07 21:48:21 +01:00
# ==================================================================== #
# TITLE #
2020-10-08 11:16:03 +02:00
# Antimicrobial Resistance (AMR) Analysis for R #
2020-03-07 21:48:21 +01:00
# #
# SOURCE #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
2020-03-07 21:48:21 +01:00
# #
# LICENCE #
2020-12-27 00:30:28 +01:00
# (c) 2018-2021 Berends MS, Luz CF et al. #
2020-10-08 11:16:03 +02:00
# Developed at the University of Groningen, the Netherlands, in #
# collaboration with non-profit organisations Certe Medical #
# Diagnostics & Advice, and University Medical Center Groningen. #
2020-03-07 21:48:21 +01: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. #
2020-10-08 11:16:03 +02:00
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR analysis: https://msberends.github.io/AMR/ #
2020-03-07 21:48:21 +01:00
# ==================================================================== #
#' Principal Component Analysis (for AMR)
#'
2020-03-08 11:18:59 +01:00
#' Performs a principal component analysis (PCA) based on a data set with automatic determination for afterwards plotting the groups and labels, and automatic filtering on only suitable (i.e. non-empty and numeric) variables.
2020-03-14 14:05:43 +01:00
#' @inheritSection lifecycle Maturing lifecycle
2020-03-07 21:48:21 +01:00
#' @param x a [data.frame] containing numeric columns
2020-05-16 13:05:47 +02:00
#' @param ... columns of `x` to be selected for PCA, can be unquoted since it supports quasiquotation.
2020-03-07 21:48:21 +01:00
#' @inheritParams stats::prcomp
2020-03-08 11:18:59 +01:00
#' @details The [pca()] function takes a [data.frame] as input and performs the actual PCA with the \R function [prcomp()].
2020-03-07 21:48:21 +01:00
#'
2020-03-08 11:18:59 +01:00
#' 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()].
#' @return An object of classes [pca] and [prcomp]
2020-03-08 09:12:11 +01:00
#' @importFrom stats prcomp
2020-03-07 21:48:21 +01:00
#' @export
#' @examples
#' # `example_isolates` is a dataset available in the AMR package.
#' # See ?example_isolates.
#'
#' \donttest{
#'
#' if (require("dplyr")) {
#' # calculate the resistance per group first
#' resistance_data <- example_isolates %>%
#' 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) # then get resistance of all drugs
#'
#' # now conduct PCA for certain antimicrobial agents
#' pca_result <- resistance_data %>%
#' pca(AMC, CXM, CTX, CAZ, GEN, TOB, TMP, SXT)
#'
#' pca_result
#' summary(pca_result)
#' biplot(pca_result)
#' ggplot_pca(pca_result) # a new and convenient plot function
#' }
2020-05-16 13:05:47 +02:00
#' }
2020-03-08 11:18:59 +01:00
pca <- function(x,
...,
retx = TRUE,
center = TRUE,
scale. = TRUE,
tol = NULL,
rank. = NULL) {
meet_criteria(x, allow_class = "data.frame")
meet_criteria(retx, allow_class = "logical", has_length = 1)
meet_criteria(center, allow_class = "logical", has_length = 1)
meet_criteria(scale., allow_class = "logical", has_length = 1)
meet_criteria(tol, allow_class = "numeric", has_length = 1, allow_NULL = TRUE)
meet_criteria(rank., allow_class = "numeric", has_length = 1, allow_NULL = TRUE)
2020-03-07 21:48:21 +01:00
2020-03-08 11:18:59 +01:00
# unset data.table, tibble, etc.
2020-03-07 21:48:21 +01:00
# also removes groups made by dplyr::group_by
x <- as.data.frame(x, stringsAsFactors = FALSE)
x.bak <- x
2020-05-16 13:05:47 +02:00
# defuse R expressions, this replaces rlang::enquos()
dots <- substitute(list(...))
if (length(dots) > 1) {
2020-03-07 21:48:21 +01:00
new_list <- list(0)
2020-05-16 13:05:47 +02:00
for (i in seq_len(length(dots) - 1)) {
new_list[[i]] <- tryCatch(eval(dots[[i + 1]], envir = x),
2020-03-07 21:48:21 +01:00
error = function(e) stop(e$message, call. = FALSE))
if (length(new_list[[i]]) == 1) {
2020-05-16 13:05:47 +02:00
if (is.character(new_list[[i]]) & new_list[[i]] %in% colnames(x)) {
# this is to support quoted variables: df %pm>% pca("mycol1", "mycol2")
2020-05-16 13:05:47 +02:00
new_list[[i]] <- x[, new_list[[i]]]
2020-03-07 21:48:21 +01:00
} else {
2020-12-22 00:51:17 +01:00
# remove item - it's a argument like `center`
2020-03-07 21:48:21 +01:00
new_list[[i]] <- NULL
}
}
}
2020-05-16 13:05:47 +02:00
2020-03-07 21:48:21 +01:00
x <- as.data.frame(new_list, stringsAsFactors = FALSE)
if (any(vapply(FUN.VALUE = logical(1), x, function(y) !is.numeric(y)))) {
2020-11-10 16:35:56 +01:00
warning_("Be sure to first calculate the resistance (or susceptibility) of variables with antimicrobial test results, since PCA works with numeric variables only. Please see Examples in ?pca.")
2020-03-07 21:48:21 +01:00
}
2020-05-16 13:05:47 +02:00
2020-03-07 21:48:21 +01:00
# set column names
2020-05-16 13:05:47 +02:00
tryCatch(colnames(x) <- as.character(dots)[2:length(dots)],
2020-03-07 21:48:21 +01:00
error = function(e) warning("column names could not be set"))
2020-05-16 13:05:47 +02:00
2020-03-07 21:48:21 +01:00
# keep only numeric columns
x <- x[, vapply(FUN.VALUE = logical(1), x, function(y) is.numeric(y))]
2020-03-07 21:48:21 +01:00
# bind the data set with the non-numeric columns
x <- cbind(x.bak[, vapply(FUN.VALUE = logical(1), x.bak, function(y) !is.numeric(y) & !all(is.na(y))), drop = FALSE], x)
2020-03-07 21:48:21 +01:00
}
2020-12-13 20:44:32 +01:00
x <- pm_ungroup(x) # would otherwise select the grouping vars
2020-05-16 13:05:47 +02:00
x <- x[rowSums(is.na(x)) == 0, ] # remove columns containing NAs
2020-03-08 11:18:59 +01:00
pca_data <- x[, which(vapply(FUN.VALUE = logical(1), x, function(x) is.numeric(x)))]
2020-03-08 11:18:59 +01:00
2020-10-27 15:56:51 +01:00
message_("Columns selected for PCA: ", paste0(font_bold(colnames(pca_data)), collapse = "/"),
". Total observations available: ", nrow(pca_data), ".")
2020-03-08 11:18:59 +01:00
pca_model <- prcomp(pca_data, retx = retx, center = center, scale. = scale., tol = tol, rank. = rank.)
attr(pca_model, "non_numeric_cols") <- x[, vapply(FUN.VALUE = logical(1), x, function(y) !is.numeric(y) & !all(is.na(y))), drop = FALSE]
2020-03-08 11:18:59 +01:00
class(pca_model) <- c("pca", class(pca_model))
pca_model
2020-03-07 21:48:21 +01:00
}