Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed May 10, 2024
1 parent 9849004 commit 5122786
Show file tree
Hide file tree
Showing 7 changed files with 427 additions and 96 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ S3method(print,sj_chi2gof)
S3method(print,sj_htest_chi)
S3method(print,sj_htest_kw)
S3method(print,sj_htest_mwu)
S3method(print,sj_htest_t)
S3method(print,sj_resample)
S3method(print,sj_ttest)
S3method(print,sj_wcor)
Expand Down Expand Up @@ -89,6 +90,7 @@ export(se)
export(se_ybar)
export(smpsize_lmm)
export(survey_median)
export(t_test)
export(table_values)
export(var_pop)
export(weight)
Expand Down
82 changes: 52 additions & 30 deletions R/helpfunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,6 @@ is_merMod <- function(fit) {
}


is_stan_model <- function(fit) {
inherits(fit, c("stanreg", "stanfit", "brmsfit"))
}


get_glm_family <- function(fit) {
c.f <- class(fit)

# do we have glm? if so, get link family. make exceptions
# for specific models that don't have family function
if (any(c.f %in% c("lme", "plm"))) {
fitfam <- ""
logit_link <- FALSE
} else {
fitfam <- stats::family(fit)$family
logit_link <- stats::family(fit)$link == "logit"
}

# create logical for family
binom_fam <- fitfam %in% c("binomial", "quasibinomial")
poisson_fam <- fitfam %in% c("poisson", "quasipoisson") ||
grepl("negative binomial", fitfam, ignore.case = TRUE, fixed = TRUE)

list(is_bin = binom_fam, is_pois = poisson_fam, is_logit = logit_link)
}

# return names of objects passed as ellipses argument
dot_names <- function(dots) unname(unlist(lapply(dots, as.character)))


.compact_character <- function(x) {
x[!sapply(x, function(i) is.null(i) || !nzchar(i, keepNA = TRUE) || is.na(i) || any(i == "NULL", na.rm = TRUE))]
}
Expand Down Expand Up @@ -89,3 +59,55 @@ dot_names <- function(dots) unname(unlist(lapply(dots, as.character)))
.is_pseudo_numeric <- function(x) {
(is.character(x) && !anyNA(suppressWarnings(as.numeric(stats::na.omit(x[nzchar(x, keepNA = TRUE)]))))) || (is.factor(x) && !anyNA(suppressWarnings(as.numeric(levels(x))))) # nolint
}


.misspelled_string <- function(source, searchterm, default_message = NULL) {
if (is.null(searchterm) || length(searchterm) < 1) {
return(default_message)
}
# used for many matches
more_found <- ""
# init default
msg <- ""
# remove matching strings
same <- intersect(source, searchterm)
searchterm <- setdiff(searchterm, same)
source <- setdiff(source, same)
# guess the misspelled string
possible_strings <- unlist(lapply(searchterm, function(s) {
source[.fuzzy_grep(source, s)] # nolint
}), use.names = FALSE)
if (length(possible_strings)) {
msg <- "Did you mean "
if (length(possible_strings) > 1) {
# make sure we don't print dozens of alternatives for larger data frames
if (length(possible_strings) > 5) {
more_found <- sprintf(
" We even found %i more possible matches, not shown here.",
length(possible_strings) - 5
)
possible_strings <- possible_strings[1:5]
}
msg <- paste0(msg, "one of ", toString(paste0("\"", possible_strings, "\"")))
} else {
msg <- paste0(msg, "\"", possible_strings, "\"")
}
msg <- paste0(msg, "?", more_found)
} else {
msg <- default_message
}
# no double white space
insight::trim_ws(msg)
}


.fuzzy_grep <- function(x, pattern, precision = NULL) {
if (is.null(precision)) {
precision <- round(nchar(pattern) / 3)
}
if (precision > nchar(pattern)) {
return(NULL)
}
p <- sprintf("(%s){~%i}", pattern, precision)
grep(pattern = p, x = x, ignore.case = FALSE)
}
64 changes: 4 additions & 60 deletions R/mann_whitney_test.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' @title Mann-Whitney-Test
#' @title Mann-Whitney test
#' @name mann_whitney_test
#' @description This function performs a Mann-Whitney-Test (or Wilcoxon rank
#' @description This function performs a Mann-Whitney test (or Wilcoxon rank
#' sum test for _unpaired_ samples.
#'
#' A Mann-Whitney-Test is a non-parametric test for the null hypothesis that two
#' A Mann-Whitney test is a non-parametric test for the null hypothesis that two
#' independent samples have identical continuous distributions. It can be used
#' when the two continuous variables are not normally distributed.
#'
Expand Down Expand Up @@ -42,7 +42,7 @@
#'
#' @examples
#' data(efc)
#' # Mann-Whitney-U-Tests for elder's age by elder's sex.
#' # Mann-Whitney-U tests for elder's age by elder's sex.
#' mann_whitney_test(efc, "e17age", by = "e16sex")
#'
#' # when data is in wide-format, specify all relevant continuous
Expand Down Expand Up @@ -238,67 +238,11 @@ mann_whitney_test <- function(data,

# helper ----------------------------------------------------------------------

.misspelled_string <- function(source, searchterm, default_message = NULL) {
if (is.null(searchterm) || length(searchterm) < 1) {
return(default_message)
}
# used for many matches
more_found <- ""
# init default
msg <- ""
# remove matching strings
same <- intersect(source, searchterm)
searchterm <- setdiff(searchterm, same)
source <- setdiff(source, same)
# guess the misspelled string
possible_strings <- unlist(lapply(searchterm, function(s) {
source[.fuzzy_grep(source, s)] # nolint
}), use.names = FALSE)
if (length(possible_strings)) {
msg <- "Did you mean "
if (length(possible_strings) > 1) {
# make sure we don't print dozens of alternatives for larger data frames
if (length(possible_strings) > 5) {
more_found <- sprintf(
" We even found %i more possible matches, not shown here.",
length(possible_strings) - 5
)
possible_strings <- possible_strings[1:5]
}
msg <- paste0(msg, "one of ", toString(paste0("\"", possible_strings, "\"")))
} else {
msg <- paste0(msg, "\"", possible_strings, "\"")
}
msg <- paste0(msg, "?", more_found)
} else {
msg <- default_message
}
# no double white space
insight::trim_ws(msg)
}


.fuzzy_grep <- function(x, pattern, precision = NULL) {
if (is.null(precision)) {
precision <- round(nchar(pattern) / 3)
}
if (precision > nchar(pattern)) {
return(NULL)
}
p <- sprintf("(%s){~%i}", pattern, precision)
grep(pattern = p, x = x, ignore.case = FALSE)
}


.sanitize_htest_input <- function(data, select, by, weights) {
# check if arguments are NULL
if (is.null(select)) {
insight::format_error("Argument `select` is missing.")
}
# `by` is only allowed to be NULL if `select` specifies more than one variable
if (is.null(by) && length(select) == 1) {
insight::format_error("Arguments `by` is missing.")
}

# check if arguments have correct length or are of correct type
if (!is.character(select)) {
Expand Down
Loading

0 comments on commit 5122786

Please sign in to comment.