From 8bfbf8df0b15e255e457086f2bc423398ef3dad7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 26 May 2024 18:31:41 +0200 Subject: [PATCH 1/5] test on devs --- DESCRIPTION | 4 ++-- R/methods.easycorrelation.R | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 788ff0eb..9f5d33e2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: correlation Title: Methods for Correlation Analysis -Version: 0.8.4.2 +Version: 0.8.4.3 Authors@R: c(person(given = "Dominique", family = "Makowski", @@ -95,4 +95,4 @@ Config/Needs/website: rstudio/bslib, r-lib/pkgdown, easystats/easystatstemplate -Remotes: easystats/insight +Remotes: easystats/insight, easystats/datawizard, easystats/parameters, easystats/bayestestR diff --git a/R/methods.easycorrelation.R b/R/methods.easycorrelation.R index 1b06136b..22c2f17c 100644 --- a/R/methods.easycorrelation.R +++ b/R/methods.easycorrelation.R @@ -11,7 +11,7 @@ summary.easycorrelation <- function(object, redundant <- FALSE } - frame <- .get_matrix(object, square = redundant) + cormatrix <- .get_matrix(object, square = redundant) # Add redundant if (redundant) { @@ -30,11 +30,11 @@ summary.easycorrelation <- function(object, } } - out <- .create_matrix(frame, object, column = target, redundant = redundant) + out <- .create_matrix(cormatrix, object, column = target, redundant = redundant) # Fill attributes for (i in names(object)[!names(object) %in% c("Group", "Parameter1", "Parameter2", target)]) { - attri <- .create_matrix(frame, object, column = i, redundant = redundant) + attri <- .create_matrix(cormatrix, object, column = i, redundant = redundant) attr(out, i) <- attri } @@ -120,7 +120,7 @@ as.list.easycorrelation <- function(x, cols = NULL, redundant = FALSE, ...) { #' @export standardize_names.easycorrelation <- function(data, ...) { ori <- data - names(data)[names(data) == datawizard::data_find(data, select = "(rho|tau)", regex = TRUE, verbose = FALSE)] <- "r" + names(data)[names(data) == datawizard::extract_column_names(data, select = "(rho|tau)", regex = TRUE, verbose = FALSE)] <- "r" data <- insight::standardize_names(as.data.frame(data), ...) class(data) <- class(ori) data @@ -134,8 +134,8 @@ standardize_names.easycorrelation <- function(data, ...) { if ("Group" %in% names(object)) { out <- data.frame() for (g in unique(object$Group)) { - data <- object[object$Group == g, ] - m <- .fill_matrix(frame, data, column = column, redundant = redundant) + my_data <- object[object$Group == g, ] + m <- .fill_matrix(frame, my_data, column = column, redundant = redundant) m$Group <- g out <- rbind(out, m) } From 8e9499555af9b8fc6219ae31512868fdc30f95c7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 26 May 2024 20:41:00 +0200 Subject: [PATCH 2/5] lintr --- R/cor_test.R | 64 +++++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/R/cor_test.R b/R/cor_test.R index cf1fa9d4..ccc83a6f 100644 --- a/R/cor_test.R +++ b/R/cor_test.R @@ -234,40 +234,38 @@ cor_test <- function(data, } # Bayesian + } else if (method %in% c("tetra", "tetrachoric")) { + insight::format_error("Tetrachoric Bayesian correlations are not supported yet. Get in touch if you want to contribute.") + } else if (method %in% c("poly", "polychoric")) { + insight::format_error("Polychoric Bayesian correlations are not supported yet. Get in touch if you want to contribute.") + } else if (method %in% c("biserial", "pointbiserial", "point-biserial")) { + insight::format_error("Biserial Bayesian correlations are not supported yet. Get in touch if you want to contribute.") + } else if (method == "biweight") { + insight::format_error("Biweight Bayesian correlations are not supported yet. Get in touch if you want to contribute.") + } else if (method == "distance") { + insight::format_error("Bayesian distance correlations are not supported yet. Get in touch if you want to contribute.") + } else if (method %in% c("percentage", "percentage_bend", "percentagebend", "pb")) { + insight::format_error("Bayesian Percentage Bend correlations are not supported yet. Get in touch if you want to contribute.") + } else if (method %in% c("blomqvist", "median", "medial")) { + insight::format_error("Bayesian Blomqvist correlations are not supported yet. Check-out the BBcor package (https://github.com/donaldRwilliams/BBcor).") + } else if (method == "hoeffding") { + insight::format_error("Bayesian Hoeffding's correlations are not supported yet. Check-out the BBcor package (https://github.com/donaldRwilliams/BBcor).") + } else if (method == "gamma") { + insight::format_error("Bayesian gamma correlations are not supported yet. Get in touch if you want to contribute.") + } else if (method %in% c("shepherd", "sheperd", "shepherdspi", "pi")) { + out <- .cor_test_shepherd(data, x, y, ci = ci, bayesian = TRUE, ...) } else { - if (method %in% c("tetra", "tetrachoric")) { - insight::format_error("Tetrachoric Bayesian correlations are not supported yet. Get in touch if you want to contribute.") - } else if (method %in% c("poly", "polychoric")) { - insight::format_error("Polychoric Bayesian correlations are not supported yet. Get in touch if you want to contribute.") - } else if (method %in% c("biserial", "pointbiserial", "point-biserial")) { - insight::format_error("Biserial Bayesian correlations are not supported yet. Get in touch if you want to contribute.") - } else if (method == "biweight") { - insight::format_error("Biweight Bayesian correlations are not supported yet. Get in touch if you want to contribute.") - } else if (method == "distance") { - insight::format_error("Bayesian distance correlations are not supported yet. Get in touch if you want to contribute.") - } else if (method %in% c("percentage", "percentage_bend", "percentagebend", "pb")) { - insight::format_error("Bayesian Percentage Bend correlations are not supported yet. Get in touch if you want to contribute.") - } else if (method %in% c("blomqvist", "median", "medial")) { - insight::format_error("Bayesian Blomqvist correlations are not supported yet. Check-out the BBcor package (https://github.com/donaldRwilliams/BBcor).") - } else if (method == "hoeffding") { - insight::format_error("Bayesian Hoeffding's correlations are not supported yet. Check-out the BBcor package (https://github.com/donaldRwilliams/BBcor).") - } else if (method == "gamma") { - insight::format_error("Bayesian gamma correlations are not supported yet. Get in touch if you want to contribute.") - } else if (method %in% c("shepherd", "sheperd", "shepherdspi", "pi")) { - out <- .cor_test_shepherd(data, x, y, ci = ci, bayesian = TRUE, ...) - } else { - out <- .cor_test_bayes( - data, - x, - y, - ci = ci, - method = method, - bayesian_prior = bayesian_prior, - bayesian_ci_method = bayesian_ci_method, - bayesian_test = bayesian_test, - ... - ) - } + out <- .cor_test_bayes( + data, + x, + y, + ci = ci, + method = method, + bayesian_prior = bayesian_prior, + bayesian_ci_method = bayesian_ci_method, + bayesian_test = bayesian_test, + ... + ) } # Replace by NANs if invalid From c86e88ea3f2ebd152c75d86dea4ea7b270feff7b Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 26 May 2024 20:41:55 +0200 Subject: [PATCH 3/5] lintr --- R/cor_test.R | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/R/cor_test.R b/R/cor_test.R index ccc83a6f..481dc782 100644 --- a/R/cor_test.R +++ b/R/cor_test.R @@ -282,8 +282,11 @@ cor_test <- function(data, # Reorder columns if ("CI_low" %in% names(out)) { - order <- c("Parameter1", "Parameter2", "r", "rho", "tau", "Dxy", "CI", "CI_low", "CI_high") - out <- out[c(order[order %in% names(out)], setdiff(colnames(out), order[order %in% names(out)]))] + col_order <- c("Parameter1", "Parameter2", "r", "rho", "tau", "Dxy", "CI", "CI_low", "CI_high") + out <- out[c( + col_order[col_order %in% names(out)], + setdiff(colnames(out), col_order[col_order %in% names(out)]) + )] } # Output From 6fef4508c8a4fa3765ad86e3f4fb5f132ba4d098 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 26 May 2024 21:00:20 +0200 Subject: [PATCH 4/5] lintr, docs --- R/cor_sort.R | 16 ++-- R/cor_test.R | 4 +- R/cor_test_bayes.R | 2 +- R/cor_test_biserial.R | 8 +- R/cor_test_distance.R | 42 +++++------ R/cor_test_freq.R | 6 +- R/cor_text.R | 34 ++++----- R/cor_to_cov.R | 10 +-- R/cor_to_pcor.R | 55 +++++++------- R/cormatrix_to_excel.R | 16 ++-- R/correlation.R | 91 +++++++++++------------ R/methods_format.R | 14 ++-- inst/WORDLIST | 1 + man/cor_test.Rd | 4 +- tests/testthat/test-cor_test_na_present.R | 8 +- 15 files changed, 156 insertions(+), 155 deletions(-) diff --git a/R/cor_sort.R b/R/cor_sort.R index c761ceaa..7c30d22f 100644 --- a/R/cor_sort.R +++ b/R/cor_sort.R @@ -26,9 +26,9 @@ cor_sort <- function(x, distance = "correlation", hclust_method = "complete", .. #' @export cor_sort.easycorrelation <- function(x, distance = "correlation", hclust_method = "complete", ...) { - order <- .cor_sort_order(as.matrix(x), distance = distance, hclust_method = hclust_method, ...) - x$Parameter1 <- factor(x$Parameter1, levels = order) - x$Parameter2 <- factor(x$Parameter2, levels = order) + col_order <- .cor_sort_order(as.matrix(x), distance = distance, hclust_method = hclust_method, ...) + x$Parameter1 <- factor(x$Parameter1, levels = col_order) + x$Parameter2 <- factor(x$Parameter2, levels = col_order) reordered <- x[order(x$Parameter1, x$Parameter2), ] # Restore class and attributes @@ -55,11 +55,11 @@ cor_sort.easycormatrix <- function(x, distance = "correlation", hclust_method = m <- x row.names(m) <- x$Parameter m <- as.matrix(m[names(m)[names(m) != "Parameter"]]) - order <- .cor_sort_order(m, distance = distance, hclust_method = hclust_method, ...) + col_order <- .cor_sort_order(m, distance = distance, hclust_method = hclust_method, ...) # Reorder - x$Parameter <- factor(x$Parameter, levels = order) - reordered <- x[order(x$Parameter), c("Parameter", order)] + x$Parameter <- factor(x$Parameter, levels = col_order) + reordered <- x[order(x$Parameter), c("Parameter", col_order)] # Restore class and attributes attributes(reordered) <- utils::modifyList( @@ -76,8 +76,8 @@ cor_sort.easycormatrix <- function(x, distance = "correlation", hclust_method = #' @export cor_sort.matrix <- function(x, distance = "correlation", hclust_method = "complete", ...) { - order <- .cor_sort_order(x, distance = distance, hclust_method = hclust_method, ...) - reordered <- x[order, order] + col_order <- .cor_sort_order(x, distance = distance, hclust_method = hclust_method, ...) + reordered <- x[col_order, col_order] # Restore class and attributes attributes(reordered) <- utils::modifyList( diff --git a/R/cor_test.R b/R/cor_test.R index 481dc782..60d31cdc 100644 --- a/R/cor_test.R +++ b/R/cor_test.R @@ -112,7 +112,9 @@ #' #' # Partial #' cor_test(iris, "Sepal.Length", "Sepal.Width", partial = TRUE) -#' cor_test(iris, "Sepal.Length", "Sepal.Width", multilevel = TRUE) +#' if (require("lme4", quietly = TRUE)) { +#' cor_test(iris, "Sepal.Length", "Sepal.Width", multilevel = TRUE) +#' } #' cor_test(iris, "Sepal.Length", "Sepal.Width", partial_bayesian = TRUE) #' } #' @export diff --git a/R/cor_test_bayes.R b/R/cor_test_bayes.R index 288b5a2c..d3519b25 100644 --- a/R/cor_test_bayes.R +++ b/R/cor_test_bayes.R @@ -17,7 +17,7 @@ var_x <- datawizard::ranktransform(var_x, sign = TRUE, method = "average") var_y <- datawizard::ranktransform(var_y, sign = TRUE, method = "average") method <- "Bayesian Spearman" - } else if (tolower(method) %in% "gaussian") { + } else if (tolower(method) == "gaussian") { var_x <- stats::qnorm(rank(var_x) / (length(var_x) + 1)) var_y <- stats::qnorm(rank(var_y) / (length(var_y) + 1)) method <- "Bayesian Gaussian rank" diff --git a/R/cor_test_biserial.R b/R/cor_test_biserial.R index f6aea4d4..99ecda53 100644 --- a/R/cor_test_biserial.R +++ b/R/cor_test_biserial.R @@ -56,11 +56,11 @@ m1 <- mean(var_x[var_y == 1]) m0 <- mean(var_x[var_y == 0]) - q <- mean(var_y) - p <- 1 - q - zp <- stats::dnorm(stats::qnorm(q)) + quan <- mean(var_y) + p <- 1 - quan + zp <- stats::dnorm(stats::qnorm(quan)) - r <- (((m1 - m0) * (p * q / zp)) / stats::sd(var_x)) + r <- (((m1 - m0) * (p * quan / zp)) / stats::sd(var_x)) p <- cor_to_p(r, n = length(var_x)) ci_vals <- cor_to_ci(r, n = length(var_x), ci = ci) diff --git a/R/cor_test_distance.R b/R/cor_test_distance.R index 2eb1c863..da35cb8a 100644 --- a/R/cor_test_distance.R +++ b/R/cor_test_distance.R @@ -3,21 +3,7 @@ var_x <- .complete_variable_x(data, x, y) var_y <- .complete_variable_y(data, x, y) - if (!corrected) { - rez <- .cor_test_distance_raw(var_x, var_y, index = 1) - rez <- data.frame( - Parameter1 = x, - Parameter2 = y, - r = rez$r, - CI_low = NA, - CI_high = NA, - t = NA, - df_error = NA, - p = NA, - Method = "Distance", - stringsAsFactors = FALSE - ) - } else { + if (corrected) { rez <- .cor_test_distance_corrected(var_x, var_y, ci = ci) rez <- data.frame( Parameter1 = x, @@ -31,6 +17,20 @@ Method = "Distance (Bias Corrected)", stringsAsFactors = FALSE ) + } else { + rez <- .cor_test_distance_raw(var_x, var_y, index = 1) + rez <- data.frame( + Parameter1 = x, + Parameter2 = y, + r = rez$r, + CI_low = NA, + CI_high = NA, + t = NA, + df_error = NA, + p = NA, + Method = "Distance", + stringsAsFactors = FALSE + ) } rez @@ -60,14 +60,14 @@ M <- n * (n - 3) / 2 dof <- M - 1 - t <- sqrt(M - 1) * r / sqrt(1 - r^2) - p <- 1 - stats::pt(t, df = dof) + tstat <- sqrt(M - 1) * r / sqrt(1 - r^2) + p <- 1 - stats::pt(tstat, df = dof) ci_vals <- cor_to_ci(r, n = n, ci = ci) list( r = r, - t = t, + t = tstat, df_error = dof, p = p, CI_low = ci_vals$CI_low, @@ -91,16 +91,16 @@ A <- .A_kl(x, index) B <- .A_kl(y, index) - cov <- sqrt(mean(A * B)) + cov_ab <- sqrt(mean(A * B)) dVarX <- sqrt(mean(A * A)) dVarY <- sqrt(mean(B * B)) V <- sqrt(dVarX * dVarY) if (V > 0) { - r <- cov / V + r <- cov_ab / V } else { r <- 0 } - list(r = r, cov = cov) + list(r = r, cov = cov_ab) } diff --git a/R/cor_test_freq.R b/R/cor_test_freq.R index 0d43056f..7ea4d175 100644 --- a/R/cor_test_freq.R +++ b/R/cor_test_freq.R @@ -42,10 +42,10 @@ .extract_corr_parameters <- function(model) { - names <- unlist(strsplit(model$data.name, " and ", fixed = TRUE)) + data_names <- unlist(strsplit(model$data.name, " and ", fixed = TRUE)) out <- data.frame( - "Parameter1" = names[1], - "Parameter2" = names[2], + Parameter1 = data_names[1], + Parameter2 = data_names[2], stringsAsFactors = FALSE ) diff --git a/R/cor_text.R b/R/cor_text.R index 481dc118..fa3d0ded 100644 --- a/R/cor_text.R +++ b/R/cor_text.R @@ -20,28 +20,28 @@ cor_text <- function(x, show_ci = TRUE, show_statistic = TRUE, show_sig = TRUE, # Estimate candidates <- c("rho", "r", "tau", "Difference", "r_rank_biserial") estimate <- candidates[candidates %in% names(x)][1] - text <- paste0(tolower(estimate), " = ", insight::format_value(x[[estimate]])) + out_text <- paste0(tolower(estimate), " = ", insight::format_value(x[[estimate]])) # CI if (show_ci && all(c("CI_high", "CI_low") %in% names(x))) { if (!is.null(attributes(x$conf.int)$conf.level)) { # htest - text <- paste0( - text, + out_text <- paste0( + out_text, ", ", insight::format_ci(x$CI_low, x$CI_high, ci = attributes(x$conf.int)$conf.level) ) } else if ("CI" %in% names(x)) { # param - text <- paste0( - text, + out_text <- paste0( + out_text, ", ", insight::format_ci(x$CI_low, x$CI_high, ci = x$CI) ) } else if ("ci" %in% names(attributes(x))) { # param - text <- paste0( - text, + out_text <- paste0( + out_text, ", ", insight::format_ci(x$CI_low, x$CI_high, ci = attributes(x)$ci) ) @@ -51,36 +51,36 @@ cor_text <- function(x, show_ci = TRUE, show_statistic = TRUE, show_sig = TRUE, # Statistic if (show_statistic) { if ("t" %in% names(x)) { - text <- paste0( - text, + out_text <- paste0( + out_text, ", t(", insight::format_value(x$df, protect_integers = TRUE), ") = ", insight::format_value(x$t) ) } else if ("S" %in% names(x)) { - text <- paste0(text, ", S = ", insight::format_value(x$S)) + out_text <- paste0(out_text, ", S = ", insight::format_value(x$S)) } else if ("z" %in% names(x)) { - text <- paste0(text, ", z = ", insight::format_value(table$z)) + out_text <- paste0(out_text, ", z = ", insight::format_value(table$z)) } else if ("W" %in% names(x)) { - text <- paste0("W = ", insight::format_value(x$W)) + out_text <- paste0("W = ", insight::format_value(x$W)) } else if ("Chi2" %in% names(x)) { - text <- paste0(text, ", Chi2 = ", insight::format_value(x$Chi2)) + out_text <- paste0(out_text, ", Chi2 = ", insight::format_value(x$Chi2)) } } # Significance if (show_sig) { if ("p" %in% names(x)) { - text <- paste0(text, ", ", insight::format_p(x$p, digits = "apa", ...)) + out_text <- paste0(out_text, ", ", insight::format_p(x$p, digits = "apa", ...)) } else if ("BF" %in% names(x)) { exact <- match.call()[["exact"]] if (is.null(exact)) exact <- TRUE - text <- paste0(text, ", ", insight::format_bf(x$BF, exact = exact, ...)) + out_text <- paste0(out_text, ", ", insight::format_bf(x$BF, exact = exact, ...)) } else if ("pd" %in% names(x)) { - text <- paste0(text, ", ", insight::format_pd(x$pd, ...)) + out_text <- paste0(out_text, ", ", insight::format_pd(x$pd, ...)) } } - text + out_text } diff --git a/R/cor_to_cov.R b/R/cor_to_cov.R index f876c9c6..9ba75ff7 100644 --- a/R/cor_to_cov.R +++ b/R/cor_to_cov.R @@ -43,8 +43,8 @@ cor_to_cov <- function(cor, sd = NULL, variance = NULL, tol = .Machine$double.ep is_symmetric <- FALSE } p <- dim(cor)[1] - q <- p * (p - 1) / 2 - if (isTRUE(all.equal(cor[lower.tri(cor)], rep(0, q))) || isTRUE(all.equal(cor[upper.tri(cor)], rep(0, q)))) { + quan <- p * (p - 1) / 2 + if (isTRUE(all.equal(cor[lower.tri(cor)], rep(0, quan))) || isTRUE(all.equal(cor[upper.tri(cor)], rep(0, quan)))) { is_triangular <- TRUE } else { is_triangular <- FALSE @@ -53,7 +53,7 @@ cor_to_cov <- function(cor, sd = NULL, variance = NULL, tol = .Machine$double.ep insight::format_error("'cor' should be either a symmetric or a triangular matrix") } - cov <- diag(sd) %*% cor %*% diag(sd) - colnames(cov) <- rownames(cov) <- colnames(cor) - cov + cov_matrix <- diag(sd) %*% cor %*% diag(sd) + colnames(cov_matrix) <- rownames(cov_matrix) <- colnames(cor) + cov_matrix } diff --git a/R/cor_to_pcor.R b/R/cor_to_pcor.R index b0bc1512..1837b980 100644 --- a/R/cor_to_pcor.R +++ b/R/cor_to_pcor.R @@ -52,10 +52,10 @@ cor_to_pcor.matrix <- function(cor, tol = .Machine$double.eps^(2 / 3)) { #' @export cor_to_pcor.easycormatrix <- function(cor, tol = .Machine$double.eps^(2 / 3)) { - if (!inherits(cor, "matrix")) { - .cor_to_pcor_easycormatrix(cor = cor, tol = tol) - } else { + if (inherits(cor, "matrix")) { NextMethod() + } else { + .cor_to_pcor_easycormatrix(cor = cor, tol = tol) } } @@ -86,10 +86,10 @@ pcor_to_cor.matrix <- function(pcor, tol = .Machine$double.eps^(2 / 3)) { #' @export pcor_to_cor.easycormatrix <- function(pcor, tol = .Machine$double.eps^(2 / 3)) { - if (!inherits(pcor, "matrix")) { - .cor_to_pcor_easycormatrix(pcor = pcor, tol = tol) - } else { + if (inherits(pcor, "matrix")) { NextMethod() + } else { + .cor_to_pcor_easycormatrix(pcor = pcor, tol = tol) } } @@ -114,28 +114,28 @@ pcor_to_cor.easycorrelation <- function(pcor, tol = .Machine$double.eps^(2 / 3)) # Extract info p_adjust <- attributes(cor)$p_adjust - nobs <- as.matrix(attributes(summary(cor, redundant = TRUE))$n_Obs[-1]) + number_obs <- as.matrix(attributes(summary(cor, redundant = TRUE))$n_Obs[-1]) # Get Statistics - p <- cor_to_p(r, n = nobs, method = "pearson") - ci_vals <- cor_to_ci(r, n = nobs, ci = attributes(cor)$ci) + p <- cor_to_p(r, n = number_obs, method = "pearson") + ci_vals <- cor_to_ci(r, n = number_obs, ci = attributes(cor)$ci) # Replace newdata <- data.frame() for (i in seq_len(nrow(cor))) { - row <- row.names(r) == cor[i, "Parameter1"] - col <- colnames(r) == cor[i, "Parameter2"] + row_index <- row.names(r) == cor[i, "Parameter1"] + col_index <- colnames(r) == cor[i, "Parameter2"] newdata <- rbind( newdata, data.frame( - r = r[row, col], - CI_low = ci_vals$CI_low[row, col], - CI_high = ci_vals$CI_high[row, col], - t = p$statistic[row, col], - df_error = nobs[row, col] - 2, - p = p$p[row, col], + r = r[row_index, col_index], + CI_low = ci_vals$CI_low[row_index, col_index], + CI_high = ci_vals$CI_high[row_index, col_index], + t = p$statistic[row_index, col_index], + df_error = number_obs[row_index, col_index] - 2, + p = p$p[row_index, col_index], Method = "Pearson", - n_Obs = nobs[row, col], + n_Obs = number_obs[row_index, col_index], stringsAsFactors = FALSE ) ) @@ -183,10 +183,10 @@ pcor_to_cor.easycorrelation <- function(pcor, tol = .Machine$double.eps^(2 / 3)) } p_adjust <- attributes(cor)$p_adjust - nobs <- as.matrix(attributes(cor)$n_Obs[-1]) + number_obs <- as.matrix(attributes(cor)$n_Obs[-1]) - p <- cor_to_p(r, n = nobs, method = "pearson") - ci_vals <- cor_to_ci(r, n = nobs, ci = attributes(cor)$ci) + p <- cor_to_p(r, n = number_obs, method = "pearson") + ci_vals <- cor_to_ci(r, n = number_obs, ci = attributes(cor)$ci) r <- cbind(data.frame(Parameter = row.names(r)), r) row.names(r) <- NULL @@ -247,15 +247,12 @@ pcor_to_cor.easycorrelation <- function(pcor, tol = .Machine$double.eps^(2 / 3)) # Get Cormatrix if (is.null(cor)) { if (is.null(cov)) { - stop("A correlation or covariance matrix is required.", call. = FALSE) - } else { - cor <- stats::cov2cor(cov) - } - } else { - if (inherits(cor, "easycormatrix") && colnames(cor)[1] == "Parameter") { - row.names(cor) <- cor$Parameter - cor <- as.matrix(cor[-1]) + insight::format_error("A correlation or covariance matrix is required.") } + cor <- stats::cov2cor(cov) + } else if (inherits(cor, "easycormatrix") && colnames(cor)[1] == "Parameter") { + row.names(cor) <- cor$Parameter + cor <- as.matrix(cor[-1]) } cor } diff --git a/R/cormatrix_to_excel.R b/R/cormatrix_to_excel.R index a028ad56..700f4673 100644 --- a/R/cormatrix_to_excel.R +++ b/R/cormatrix_to_excel.R @@ -47,19 +47,19 @@ cormatrix_to_excel <- function(data, print.mat = TRUE, ...) { if (missing(filename)) { - stop("Argument 'filename' required (as per CRAN policies)") + insight::format_error("Argument 'filename' required (as per CRAN policies).") } insight::check_if_installed("openxlsx2") # create correlation matrix with p values - cm <- correlation::correlation(data, ...) - cm <- summary(cm, redundant = TRUE) - all.columns <- 2:(ncol(cm)) + cormatrix <- correlation::correlation(data, ...) + cormatrix <- summary(cormatrix, redundant = TRUE) + all.columns <- 2:(ncol(cormatrix)) if (isTRUE(print.mat)) { - print(cm) + print(cormatrix) } - pf <- attr(cm, "p") + p_val <- attr(cormatrix, "p") # Define colours style_gray <- c(rgb = "C1CDCD") @@ -237,8 +237,8 @@ cormatrix_to_excel <- function(data, # wb$styles_mgr$dxf # create the worksheets and write the data to the worksheets. - wb$add_worksheet("r_values")$add_data(x = cm) - wb$add_worksheet("p_values")$add_data(x = pf) + wb$add_worksheet("r_values")$add_data(x = cormatrix) + wb$add_worksheet("p_values")$add_data(x = p_val) # create conditional formatting for the stars (as well as colours as we have no) # one star diff --git a/R/correlation.R b/R/correlation.R index 4297cefe..c450d6c9 100644 --- a/R/correlation.R +++ b/R/correlation.R @@ -369,19 +369,19 @@ correlation <- function(data, attributes(out) <- c( attributes(out), list( - "data" = data, - "data2" = data2, - "modelframe" = rez$data, - "ci" = ci, - "n" = nrow(data), - "method" = method, - "bayesian" = bayesian, - "p_adjust" = p_adjust, - "partial" = partial, - "multilevel" = multilevel, - "partial_bayesian" = partial_bayesian, - "bayesian_prior" = bayesian_prior, - "include_factors" = include_factors + data = data, + data2 = data2, + modelframe = rez$data, + ci = ci, + n = nrow(data), + method = method, + bayesian = bayesian, + p_adjust = p_adjust, + partial = partial, + multilevel = multilevel, + partial_bayesian = partial_bayesian, + bayesian_prior = bayesian_prior, + include_factors = include_factors ) ) @@ -429,39 +429,38 @@ correlation <- function(data, if (!is.null(data2)) { if (inherits(data2, "grouped_df")) { groups2 <- setdiff(colnames(attributes(data2)$groups), ".rows") - if (all.equal(groups, groups2)) { - ungrouped_y <- as.data.frame(data2) - ylist <- split(ungrouped_y, ungrouped_y[groups], sep = " - ") - modelframe <- data.frame() - out <- data.frame() - for (i in names(xlist)) { - xlist[[i]][groups] <- NULL - ylist[[i]][groups] <- NULL - rez <- .correlation( - xlist[[i]], - data2 = ylist[[i]], - method = method, - p_adjust = p_adjust, - ci = ci, - bayesian = bayesian, - bayesian_prior = bayesian_prior, - bayesian_ci_method = bayesian_ci_method, - bayesian_test = bayesian_test, - redundant = redundant, - include_factors = include_factors, - partial = partial, - partial_bayesian = partial_bayesian, - multilevel = multilevel, - ranktransform = ranktransform, - winsorize = winsorize - ) - modelframe_current <- rez$data - rez$params$Group <- modelframe_current$Group <- i - out <- rbind(out, rez$params) - modelframe <- rbind(modelframe, modelframe_current) - } - } else { - stop("'data2' should have the same grouping characteristics as data.", call. = FALSE) + if (!all.equal(groups, groups2)) { + insight::format_error("'data2' should have the same grouping characteristics as data.") + } + ungrouped_y <- as.data.frame(data2) + ylist <- split(ungrouped_y, ungrouped_y[groups], sep = " - ") + modelframe <- data.frame() + out <- data.frame() + for (i in names(xlist)) { + xlist[[i]][groups] <- NULL + ylist[[i]][groups] <- NULL + rez <- .correlation( + xlist[[i]], + data2 = ylist[[i]], + method = method, + p_adjust = p_adjust, + ci = ci, + bayesian = bayesian, + bayesian_prior = bayesian_prior, + bayesian_ci_method = bayesian_ci_method, + bayesian_test = bayesian_test, + redundant = redundant, + include_factors = include_factors, + partial = partial, + partial_bayesian = partial_bayesian, + multilevel = multilevel, + ranktransform = ranktransform, + winsorize = winsorize + ) + modelframe_current <- rez$data + rez$params$Group <- modelframe_current$Group <- i + out <- rbind(out, rez$params) + modelframe <- rbind(modelframe, modelframe_current) } } # else diff --git a/R/methods_format.R b/R/methods_format.R index c30d910c..24300db7 100644 --- a/R/methods_format.R +++ b/R/methods_format.R @@ -45,7 +45,7 @@ format.easycormatrix <- function(x, # If it's a real matrix if (!"Parameter" %in% colnames(x)) { m <- as.data.frame(x) - return(cbind(data.frame("Variables" = row.names(x)), m)) + return(cbind(data.frame(Variables = row.names(x)), m)) } # Find attributes @@ -105,7 +105,7 @@ format.easycormatrix <- function(x, } if (!stars_only) { - sig[, nums] <- sapply(sig[, nums], function(x) ifelse(x != "", paste0(" (", x, ")"), "")) + sig[, nums] <- sapply(sig[, nums], function(x) ifelse(x != "", paste0(" (", x, ")"), "")) # nolint } if (include_significance || stars) { @@ -146,11 +146,11 @@ format.easycormatrix <- function(x, # N-obs if (!is.null(x$n_Obs)) { if (length(unique(x$n_Obs)) == 1) { - nobs <- unique(x$n_Obs) + number_obs <- unique(x$n_Obs) } else { - nobs <- paste0(min(x$n_Obs), "-", max(x$n_Obs)) + number_obs <- paste0(min(x$n_Obs), "-", max(x$n_Obs)) } - footer <- paste0(footer, "\nObservations: ", nobs) + footer <- paste0(footer, "\nObservations: ", number_obs) } # final new line @@ -158,8 +158,8 @@ format.easycormatrix <- function(x, # for html/markdown, create list if (!is.null(format) && format != "text") { - footer <- unlist(strsplit(footer, "\n")) - footer <- as.list(footer[nchar(footer) > 0]) + footer <- unlist(strsplit(footer, "\n", fixed = TRUE)) + footer <- as.list(footer[nzchar(footer, keepNA = TRUE)]) } footer diff --git a/inst/WORDLIST b/inst/WORDLIST index ad94ff56..101ffc38 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -94,6 +94,7 @@ pinv polychoric pracma rOpenSci +rempsyc rescaled ressembles rmarkdown diff --git a/man/cor_test.Rd b/man/cor_test.Rd index faa3a768..a4e18f39 100644 --- a/man/cor_test.Rd +++ b/man/cor_test.Rd @@ -271,7 +271,9 @@ if (require("psych", quietly = TRUE) && require("rstanarm", quietly = TRUE)) { # Partial cor_test(iris, "Sepal.Length", "Sepal.Width", partial = TRUE) -cor_test(iris, "Sepal.Length", "Sepal.Width", multilevel = TRUE) +if (require("lme4", quietly = TRUE)) { + cor_test(iris, "Sepal.Length", "Sepal.Width", multilevel = TRUE) +} cor_test(iris, "Sepal.Length", "Sepal.Width", partial_bayesian = TRUE) } } diff --git a/tests/testthat/test-cor_test_na_present.R b/tests/testthat/test-cor_test_na_present.R index 8e1b0c4e..5f8d6502 100644 --- a/tests/testthat/test-cor_test_na_present.R +++ b/tests/testthat/test-cor_test_na_present.R @@ -11,7 +11,7 @@ test_that("cor_test kendall", { skip_if_not_or_load_if_installed("ggplot2") out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "kendall") - out2 <- stats::cor.test(ggplot2::msleep$brainwt, ggplot2::msleep$sleep_rem, method = "kendall") + out2 <- suppressWarnings(stats::cor.test(ggplot2::msleep$brainwt, ggplot2::msleep$sleep_rem, method = "kendall")) expect_equal(out$tau, out2$estimate[[1]], tolerance = 0.001) expect_equal(out$p, out2$p.value[[1]], tolerance = 0.001) @@ -31,8 +31,8 @@ test_that("cor_test tetrachoric", { skip_if_not_or_load_if_installed("ggplot2") data <- ggplot2::msleep - data$brainwt_binary <- ifelse(data$brainwt > 3, 1, 0) - data$sleep_rem_binary <- ifelse(data$sleep_rem > 1.2, 1, 0) + data$brainwt_binary <- as.numeric(data$brainwt > 3) + data$sleep_rem_binary <- as.numeric(data$sleep_rem > 1.2) # With Factors / Binary expect_error(cor_test(data, "brainwt_binary", "sleep_rem_binary", method = "tetrachoric")) @@ -40,7 +40,7 @@ test_that("cor_test tetrachoric", { data$sleep_rem_ordinal <- as.factor(round(data$sleep_rem)) data$brainwt_ordinal <- as.factor(round(data$brainwt)) - out <- cor_test(data, "brainwt", "brainwt_ordinal", method = "polychoric") + out <- suppressWarnings(cor_test(data, "brainwt", "brainwt_ordinal", method = "polychoric")) expect_equal(out$rho, 0.9999, tolerance = 0.01) # Biserial From 293880eee42ddd077a6251c2e236e4e99653a2bf Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 26 May 2024 21:06:31 +0200 Subject: [PATCH 5/5] fix warnings --- R/cor_test.R | 4 +++- man/cor_test.Rd | 4 +++- tests/testthat/test-cor_test.R | 8 ++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/R/cor_test.R b/R/cor_test.R index 60d31cdc..787caf23 100644 --- a/R/cor_test.R +++ b/R/cor_test.R @@ -115,7 +115,9 @@ #' if (require("lme4", quietly = TRUE)) { #' cor_test(iris, "Sepal.Length", "Sepal.Width", multilevel = TRUE) #' } -#' cor_test(iris, "Sepal.Length", "Sepal.Width", partial_bayesian = TRUE) +#' if (require("rstanarm", quietly = TRUE)) { +#' cor_test(iris, "Sepal.Length", "Sepal.Width", partial_bayesian = TRUE) +#' } #' } #' @export cor_test <- function(data, diff --git a/man/cor_test.Rd b/man/cor_test.Rd index a4e18f39..b28684c2 100644 --- a/man/cor_test.Rd +++ b/man/cor_test.Rd @@ -274,6 +274,8 @@ cor_test(iris, "Sepal.Length", "Sepal.Width", partial = TRUE) if (require("lme4", quietly = TRUE)) { cor_test(iris, "Sepal.Length", "Sepal.Width", multilevel = TRUE) } -cor_test(iris, "Sepal.Length", "Sepal.Width", partial_bayesian = TRUE) +if (require("rstanarm", quietly = TRUE)) { + cor_test(iris, "Sepal.Length", "Sepal.Width", partial_bayesian = TRUE) +} } } diff --git a/tests/testthat/test-cor_test.R b/tests/testthat/test-cor_test.R index ef5b36a4..60e1fafd 100644 --- a/tests/testthat/test-cor_test.R +++ b/tests/testthat/test-cor_test.R @@ -31,7 +31,7 @@ test_that("cor_test bayesian", { df <- iris df$Petal.Length2 <- df$Petal.Length - out3 <- cor_test(df, "Petal.Length", "Petal.Length2", bayesian = TRUE) + out3 <- suppressWarnings(cor_test(df, "Petal.Length", "Petal.Length2", bayesian = TRUE)) expect_equal(out3$rho, 1.000, tolerance = 0.01) if (getRversion() >= "3.6") { @@ -60,8 +60,8 @@ test_that("cor_test tetrachoric", { skip_if_not_or_load_if_installed("psych") skip_if_not_or_load_if_installed("polycor") data <- iris - data$Sepal.Width_binary <- ifelse(data$Sepal.Width > 3, 1, 0) - data$Petal.Width_binary <- ifelse(data$Petal.Width > 1.2, 1, 0) + data$Sepal.Width_binary <- as.numeric(data$Sepal.Width > 3) + data$Petal.Width_binary <- as.numeric(data$Petal.Width > 1.2) # With Factors / Binary out <- cor_test(data, "Sepal.Width_binary", "Petal.Width_binary", method = "tetrachoric") @@ -184,6 +184,6 @@ test_that("cor_test one-sided p value", { # Edge cases -------------------------------------------------------------- test_that("cor_test 2 valid observations", { - out <- correlation(data.frame(v2 = c(2, 1, 1, 2), v3 = c(1, 2, NA, NA))) + out <- suppressWarnings(correlation(data.frame(v2 = c(2, 1, 1, 2), v3 = c(1, 2, NA, NA)))) expect_true(is.na(out$r)) })