diff --git a/R/p_test.R b/R/p_test.R index c7ee4fd3..a5f5c6cf 100644 --- a/R/p_test.R +++ b/R/p_test.R @@ -16,9 +16,13 @@ #' @param paired Specify whether the dataset is paired or not. Defaults to TRUE. #' #' @import dplyr -#' @import stats +#' +#' @details +#' This function is a wrapper around `wilcox.test()` from {stats}. #' #' @examples +#' # Simulate a binary variable X +#' # Returns a single p-value #' sq_data %>% #' mutate(X = ifelse(Email_hours > 6, 1, 0)) %>% #' p_test(outcome = "X", behavior = "External_network_size") @@ -26,19 +30,20 @@ -p_test <- function(data, - outcome, +p_test <- function(data, + outcome, behavior, paired = FALSE){ - train <- data %>% + train <- data %>% filter(!!sym(outcome) == 1 | !!sym(outcome) == 0) %>% select(!!sym(outcome), !!sym(behavior)) %>% mutate(outcome = as.character(!!sym(outcome))) %>% mutate(outcome = as.factor(!!sym(outcome))) - - pos <- train %>% filter(outcome == 1, na.rm=TRUE) %>% select(behavior) - neg <- train %>% filter(outcome == 0, na.rm=TRUE) %>% select(behavior) - + + pos <- train %>% filter(outcome == 1, na.rm=TRUE) %>% select(behavior) + neg <- train %>% filter(outcome == 0, na.rm=TRUE) %>% select(behavior) + s <- stats::wilcox.test(unlist(pos), unlist(neg), paired = paired) + return(s$p.value) -} \ No newline at end of file +} diff --git a/man/p_test.Rd b/man/p_test.Rd index ef2ac00f..c82a7f35 100644 --- a/man/p_test.Rd +++ b/man/p_test.Rd @@ -4,7 +4,7 @@ \alias{p_test} \title{Calculate the p-value of the null hypothesis that two outcomes are from the same dataset} \usage{ -p_test(data, outcome, behavior) +p_test(data, outcome, behavior, paired = FALSE) } \arguments{ \item{data}{A Person Query dataset in the form of a data frame.} @@ -13,8 +13,20 @@ p_test(data, outcome, behavior) the values 1 or 0. Used to group the two distributions.} \item{behavior}{A character vector specifying the column to be used as the behavior to test.} + +\item{paired}{Specify whether the dataset is paired or not. Defaults to TRUE.} } \description{ Specify an outcome variable and return p-test outputs. All numeric variables in the dataset are used as predictor variables. } +\details{ +This function is a wrapper around \code{wilcox.test()} from {stats}. +} +\examples{ +# Simulate a binary variable X +# Returns a single p-value +sq_data \%>\% + mutate(X = ifelse(Email_hours > 6, 1, 0)) \%>\% + p_test(outcome = "X", behavior = "External_network_size") +}