#' Permutated p-values for average frequency
#'
#' The 'average_fre_p()' function uses 'average_fre()' to calculate the average
#' frequency for a data set and 'shuffle()' to calculate average frequency for
#' shuffles (permutations) of the same data set.
#'
#' @param data A data frame
#' @param variable A numeric variable that includes the first decimal place.
#' @param group A second variable used to group the primary variable such that
#' average frequency Z-scores are calculated separately for each group.
#' @param decimal_place The number of decimal places used for the calculation.
#' The default is set to "1" meaning decimals in the second (hundreds place)
#' and below are discarded.
#' @param reps The number of shuffles (simulations) to perform. The default is set to 1,000.
#'
#' @return A tibble
#' @export
#'
#' @examples
#' average_fre_p(bodyweight, obs, group, reps = 100)
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#' @useDynLib fabricated
#' @importFrom Rcpp sourceCpp
#'
#'
average_fre_p <- function(data, variable,
group = NULL,
decimal_place = 1,
reps = 1000) {
if(data %>%
dplyr::summarise(class = class({{ variable }})) %>%
dplyr::pull() != "numeric") {stop("average_fre() will only function on a numeric variable")}
data_1 <- data %>%
dplyr::filter(!is.na({{ variable }})) %>%
dplyr::mutate(var = trunc({{ variable }} * 10^{{ decimal_place }}),
decimal = str_sub(.data$var, start = - {{ decimal_place }}),
integer = str_sub(.data$var, end = -(2 + {{ decimal_place }})))
var <- deparse(substitute(group))
if(is.null(data[[var]])) {
actual <- average_fre(data, {{ variable }}, {{ group }}, {{ decimal_place }}) %>%
dplyr::pull()
int <- as.numeric(data_1$integer)
dec <- as.numeric(data_1$decimal)
out <- data.frame(p_value = NA)
out$p_value <- permutate(int, dec, actual = actual, reps = reps)
return(out)
}
else {
actual <- average_fre(data, {{ variable }}, {{ group }}, {{ decimal_place }})
out <- data %>%
dplyr::select({{ group }}) %>%
dplyr::distinct()
groups_ <- out[[var]]
for(i in groups_) {
data_2 <- data_1 %>%
dplyr::filter({{ group }} == i)
int <- as.numeric(data_2$integer)
dec <- as.numeric(data_2$decimal)
out$p_value[i] <- permutate(int, dec, actual = actual$af[i], reps = reps)
}
return(out)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.