diff --git a/DESCRIPTION b/DESCRIPTION index 2ad6a56f..7608ca48 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: correlation Title: Methods for Correlation Analysis -Version: 0.8.6 +Version: 0.8.6.1 Authors@R: c(person(given = "Dominique", family = "Makowski", @@ -57,8 +57,8 @@ Imports: bayestestR (>= 0.15.0), datasets, datawizard (>= 0.13.0), - insight (>= 0.20.5), - parameters (>= 0.22.2), + insight (>= 1.0.0), + parameters (>= 0.24.0), stats Suggests: BayesFactor, diff --git a/R/cor_sort.R b/R/cor_sort.R index 7c30d22f..554464e6 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", ...) { - 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) + m <- cor_sort(as.matrix(x), distance = distance, hclust_method = hclust_method, ...) + x$Parameter1 <- factor(x$Parameter1, levels = rownames(m)) + x$Parameter2 <- factor(x$Parameter2, levels = colnames(m)) reordered <- x[order(x$Parameter1, x$Parameter2), ] # Restore class and attributes @@ -38,6 +38,8 @@ cor_sort.easycorrelation <- function(x, distance = "correlation", hclust_method ) # Make sure Parameter columns are character + # Was added to fix a test, but makes the function not work + # (See https://github.com/easystats/correlation/issues/259) # reordered$Parameter1 <- as.character(reordered$Parameter1) # reordered$Parameter2 <- as.character(reordered$Parameter2) @@ -55,11 +57,18 @@ 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"]]) - col_order <- .cor_sort_order(m, distance = distance, hclust_method = hclust_method, ...) + + # If non-redundant matrix, fail (## TODO: fix that) + if (anyNA(m)) { + insight::format_error("Non-redundant matrices are not supported yet. Try again by setting summary(..., redundant = TRUE)") + } + + # Get sorted matrix + m <- cor_sort(m, distance = distance, hclust_method = hclust_method, ...) # Reorder - x$Parameter <- factor(x$Parameter, levels = col_order) - reordered <- x[order(x$Parameter), c("Parameter", col_order)] + x$Parameter <- factor(x$Parameter, levels = row.names(m)) + reordered <- x[order(x$Parameter), c("Parameter", colnames(m))] # Restore class and attributes attributes(reordered) <- utils::modifyList( @@ -67,6 +76,13 @@ cor_sort.easycormatrix <- function(x, distance = "correlation", hclust_method = attributes(reordered) ) + # Reorder attributes (p-values) etc. + for (id in c("p", "CI", "CI_low", "CI_high", "BF", "Method", "n_Obs", "df_error", "t")) { + if (id %in% names(attributes(reordered))) { + attributes(reordered)[[id]] <- attributes(reordered)[[id]][order(x$Parameter), names(reordered)] + } + } + # make sure Parameter columns are character reordered$Parameter <- as.character(reordered$Parameter) @@ -76,8 +92,13 @@ cor_sort.easycormatrix <- function(x, distance = "correlation", hclust_method = #' @export cor_sort.matrix <- function(x, distance = "correlation", hclust_method = "complete", ...) { - col_order <- .cor_sort_order(x, distance = distance, hclust_method = hclust_method, ...) - reordered <- x[col_order, col_order] + if (isSquare(x) && all(colnames(x) %in% rownames(x))) { + i <- .cor_sort_square(x, distance = distance, hclust_method = hclust_method, ...) + } else { + i <- .cor_sort_nonsquare(x, distance = "euclidean", ...) + } + + reordered <- x[i$row_order, i$col_order] # Restore class and attributes attributes(reordered) <- utils::modifyList( @@ -91,7 +112,7 @@ cor_sort.matrix <- function(x, distance = "correlation", hclust_method = "comple # Utils ------------------------------------------------------------------- -.cor_sort_order <- function(m, distance = "correlation", hclust_method = "complete", ...) { +.cor_sort_square <- function(m, distance = "correlation", hclust_method = "complete", ...) { if (distance == "correlation") { d <- stats::as.dist((1 - m) / 2) # r = -1 -> d = 1; r = 1 -> d = 0 } else if (distance == "raw") { @@ -101,5 +122,54 @@ cor_sort.matrix <- function(x, distance = "correlation", hclust_method = "comple } hc <- stats::hclust(d, method = hclust_method) - row.names(m)[hc$order] + row_order <- row.names(m)[hc$order] + list(row_order = row_order, col_order = row_order) +} + + +.cor_sort_nonsquare <- function(m, distance = "euclidean", ...) { + # Step 1: Perform clustering on rows and columns independently + row_dist <- stats::dist(m, method = distance) # Distance between rows + col_dist <- stats::dist(t(m), method = distance) # Distance between columns + + row_hclust <- stats::hclust(row_dist, method = "average") + col_hclust <- stats::hclust(col_dist, method = "average") + + # Obtain clustering orders + row_order <- row_hclust$order + col_order <- col_hclust$order + + # Reorder matrix based on clustering + clustered_matrix <- m[row_order, col_order] + + # Step 2: Refine alignment to emphasize strong correlations along the diagonal + n_rows <- nrow(clustered_matrix) + n_cols <- ncol(clustered_matrix) + + used_rows <- logical(n_rows) + refined_row_order <- integer(0) + + for (col in seq_len(n_cols)) { + max_value <- -Inf + best_row <- NA + + for (row in seq_len(n_rows)[!used_rows]) { + if (abs(clustered_matrix[row, col]) > max_value) { + max_value <- abs(clustered_matrix[row, col]) + best_row <- row + } + } + + if (!is.na(best_row)) { + refined_row_order <- c(refined_row_order, best_row) + used_rows[best_row] <- TRUE + } + } + + # Append any unused rows at the end + refined_row_order <- c(refined_row_order, which(!used_rows)) + + # Apply + m <- clustered_matrix[refined_row_order, ] + list(row_order = rownames(m), col_order = colnames(m)) } diff --git a/R/correlation.R b/R/correlation.R index c450d6c9..cd03d6db 100644 --- a/R/correlation.R +++ b/R/correlation.R @@ -180,10 +180,9 @@ #' `stats` package are supported. #' } #' -#' @examplesIf requireNamespace("poorman", quietly = TRUE) && requireNamespace("psych", quietly = TRUE) -#' +#' @examplesIf all(insight::check_if_installed(c("psych", "datawizard"), quietly = TRUE)) && getRversion() >= "4.1.0" #' library(correlation) -#' library(poorman) +#' data(iris) #' #' results <- correlation(iris) #' @@ -192,22 +191,20 @@ #' summary(results, redundant = TRUE) #' #' # pipe-friendly usage with grouped dataframes from {dplyr} package -#' iris %>% +#' iris |> #' correlation(select = "Petal.Width", select2 = "Sepal.Length") #' #' # Grouped dataframe #' # grouped correlations -#' iris %>% -#' group_by(Species) %>% +#' iris |> +#' datawizard::data_group(Species) |> #' correlation() #' #' # selecting specific variables for correlation -#' mtcars %>% -#' group_by(am) %>% -#' correlation( -#' select = c("cyl", "wt"), -#' select2 = c("hp") -#' ) +#' data(mtcars) +#' mtcars |> +#' datawizard::data_group(am) |> +#' correlation(select = c("cyl", "wt"), select2 = "hp") #' #' # supplying custom variable names #' correlation(anscombe, select = c("x1", "x2"), rename = c("var1", "var2")) @@ -425,8 +422,36 @@ correlation <- function(data, ungrouped_x <- as.data.frame(data) xlist <- split(ungrouped_x, ungrouped_x[groups], sep = " - ") - # If data 2 is provided - if (!is.null(data2)) { + # If data 2 is not provided + if (is.null(data2)) { + modelframe <- data.frame() + out <- data.frame() + for (i in names(xlist)) { + xlist[[i]][groups] <- NULL + rez <- .correlation( + xlist[[i]], + data2, + 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 { if (inherits(data2, "grouped_df")) { groups2 <- setdiff(colnames(attributes(data2)$groups), ".rows") if (!all.equal(groups, groups2)) { @@ -463,35 +488,6 @@ correlation <- function(data, modelframe <- rbind(modelframe, modelframe_current) } } - # else - } else { - modelframe <- data.frame() - out <- data.frame() - for (i in names(xlist)) { - xlist[[i]][groups] <- NULL - rez <- .correlation( - xlist[[i]], - data2, - 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) - } } # Group as first column diff --git a/R/display.R b/R/display.R index f8b93f3c..986fc3ea 100644 --- a/R/display.R +++ b/R/display.R @@ -2,7 +2,8 @@ #' @name display.easycormatrix #' #' @description Export tables (i.e. data frame) into different output formats. -#' `print_md()` is a alias for `display(format = "markdown")`. +#' `print_md()` is a alias for `display(format = "markdown")`. Note that +#' you can use `format()` to get the formatted table as a dataframe. #' #' @param object,x An object returned by #' [`correlation()`][correlation] or its summary. diff --git a/R/methods_format.R b/R/methods_format.R index 24300db7..e4ab7fe4 100644 --- a/R/methods_format.R +++ b/R/methods_format.R @@ -156,10 +156,17 @@ format.easycormatrix <- function(x, # final new line footer <- paste0(footer, "\n") - # for html/markdown, create list + # for html/markdown, modify footer format if (!is.null(format) && format != "text") { + # no line break if not text format footer <- unlist(strsplit(footer, "\n", fixed = TRUE)) - footer <- as.list(footer[nzchar(footer, keepNA = TRUE)]) + # remove empty elements + footer <- footer[nzchar(footer, keepNA = TRUE)] + # create list or separate by ";" + footer <- switch(format, + html = paste(footer, collapse = "; "), + as.list(footer) + ) } footer @@ -168,7 +175,9 @@ format.easycormatrix <- function(x, #' @keywords internal .format_easycorrelation_caption <- function(x, format = NULL) { - if (!is.null(attributes(x)$method)) { + if (is.null(attributes(x)$method)) { + caption <- NULL + } else { if (isTRUE(attributes(x)$smoothed)) { prefix <- "Smoothed Correlation Matrix (" } else { @@ -179,8 +188,6 @@ format.easycormatrix <- function(x, } else { caption <- paste0(prefix, unique(attributes(x)$method), "-method)") } - } else { - caption <- NULL } caption diff --git a/R/methods_print.R b/R/methods_print.R index 6d90e881..0ef1ae39 100644 --- a/R/methods_print.R +++ b/R/methods_print.R @@ -3,7 +3,7 @@ #' @export print.easycorrelation <- function(x, ...) { - cat(insight::export_table(format(x, ...), format = "text")) + cat(insight::export_table(format(x, ...), ...)) invisible(x) } @@ -13,9 +13,9 @@ print.easycormatrix <- function(x, ...) { # If real matrix, print as matrix if (colnames(formatted)[1] == "Variables") { formatted$Variables <- NULL - print(as.matrix(formatted)) + print(as.matrix(formatted), ...) } else { - cat(insight::export_table(format(x, ...), format = "text")) + cat(insight::export_table(format(x, ...), ...)) } invisible(x) } @@ -31,7 +31,7 @@ print.easymatrixlist <- function(x, cols = "auto", ...) { for (i in cols) { cat(" ", i, " ", "\n", rep("-", nchar(i) + 2), "\n", sep = "") - print(x[[i]]) + print(x[[i]], ...) cat("\n") } } @@ -40,7 +40,7 @@ print.easymatrixlist <- function(x, cols = "auto", ...) { print.grouped_easymatrixlist <- function(x, cols = "auto", ...) { for (i in names(x)) { cat(rep("=", nchar(i) + 2), "\n ", i, " ", "\n", rep("=", nchar(i) + 2), "\n\n", sep = "") - print(x[[i]]) + print(x[[i]], ...) cat("\n") } } diff --git a/correlation.Rproj b/correlation.Rproj index c1ab2141..ba2d565c 100644 --- a/correlation.Rproj +++ b/correlation.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: a2737226-16da-4377-8659-b462bf604f1e RestoreWorkspace: No SaveWorkspace: No diff --git a/man/correlation-package.Rd b/man/correlation-package.Rd index 8e313dd7..5ca9e30d 100644 --- a/man/correlation-package.Rd +++ b/man/correlation-package.Rd @@ -24,15 +24,15 @@ Useful links: } \author{ -\strong{Maintainer}: Brenton M. Wiernik \email{brenton@wiernik.org} (\href{https://orcid.org/0000-0001-9560-6336}{ORCID}) (@bmwiernik) +\strong{Maintainer}: Brenton M. Wiernik \email{brenton@wiernik.org} (\href{https://orcid.org/0000-0001-9560-6336}{ORCID}) Authors: \itemize{ - \item Dominique Makowski \email{dom.makowski@gmail.com} (\href{https://orcid.org/0000-0001-5375-9967}{ORCID}) (@Dom_Makowski) [inventor] - \item Indrajeet Patil \email{patilindrajeet.science@gmail.com} (\href{https://orcid.org/0000-0003-1995-6531}{ORCID}) (@patilindrajeets) - \item Daniel Lüdecke \email{d.luedecke@uke.de} (\href{https://orcid.org/0000-0002-8895-3206}{ORCID}) (@strengejacke) - \item Mattan S. Ben-Shachar \email{matanshm@post.bgu.ac.il} (\href{https://orcid.org/0000-0002-4287-4801}{ORCID}) (@mattansb) - \item Rémi Thériault \email{remi.theriault@mail.mcgill.ca} (\href{https://orcid.org/0000-0003-4315-6788}{ORCID}) (@rempsyc) + \item Dominique Makowski \email{dom.makowski@gmail.com} (\href{https://orcid.org/0000-0001-5375-9967}{ORCID}) [inventor] + \item Indrajeet Patil \email{patilindrajeet.science@gmail.com} (\href{https://orcid.org/0000-0003-1995-6531}{ORCID}) + \item Daniel Lüdecke \email{d.luedecke@uke.de} (\href{https://orcid.org/0000-0002-8895-3206}{ORCID}) + \item Mattan S. Ben-Shachar \email{matanshm@post.bgu.ac.il} (\href{https://orcid.org/0000-0002-4287-4801}{ORCID}) + \item Rémi Thériault \email{remi.theriault@mail.mcgill.ca} (\href{https://orcid.org/0000-0003-4315-6788}{ORCID}) } Other contributors: diff --git a/man/correlation.Rd b/man/correlation.Rd index 413c8f6c..da2a6b2a 100644 --- a/man/correlation.Rd +++ b/man/correlation.Rd @@ -264,10 +264,9 @@ Bayesian rank correlations (which have different priors). } } \examples{ -\dontshow{if (requireNamespace("poorman", quietly = TRUE) && requireNamespace("psych", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} - +\dontshow{if (all(insight::check_if_installed(c("psych", "datawizard"), quietly = TRUE)) && getRversion() >= "4.1.0") (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} library(correlation) -library(poorman) +data(iris) results <- correlation(iris) @@ -276,22 +275,20 @@ summary(results) summary(results, redundant = TRUE) # pipe-friendly usage with grouped dataframes from {dplyr} package -iris \%>\% +iris |> correlation(select = "Petal.Width", select2 = "Sepal.Length") # Grouped dataframe # grouped correlations -iris \%>\% - group_by(Species) \%>\% +iris |> + datawizard::data_group(Species) |> correlation() # selecting specific variables for correlation -mtcars \%>\% - group_by(am) \%>\% - correlation( - select = c("cyl", "wt"), - select2 = c("hp") - ) +data(mtcars) +mtcars |> + datawizard::data_group(am) |> + correlation(select = c("cyl", "wt"), select2 = "hp") # supplying custom variable names correlation(anscombe, select = c("x1", "x2"), rename = c("var1", "var2")) diff --git a/man/display.easycormatrix.Rd b/man/display.easycormatrix.Rd index 4dc2f96c..79d13083 100644 --- a/man/display.easycormatrix.Rd +++ b/man/display.easycormatrix.Rd @@ -61,7 +61,8 @@ will be a character vector in markdown-table format. } \description{ Export tables (i.e. data frame) into different output formats. -\code{print_md()} is a alias for \code{display(format = "markdown")}. +\code{print_md()} is a alias for \code{display(format = "markdown")}. Note that +you can use \code{format()} to get the formatted table as a dataframe. } \details{ \code{display()} is useful when the table-output from functions, diff --git a/tests/testthat/_snaps/as.list.md b/tests/testthat/_snaps/as_list.md similarity index 97% rename from tests/testthat/_snaps/as.list.md rename to tests/testthat/_snaps/as_list.md index 558523e2..ab7574f6 100644 --- a/tests/testthat/_snaps/as.list.md +++ b/tests/testthat/_snaps/as_list.md @@ -1,7 +1,7 @@ # as.list Code - as.list(correlation(mtcars)) + print(out, table_width = Inf) Output r --- @@ -55,7 +55,7 @@ --- Code - as.list(correlation(datawizard::data_group(msleep, "vore"), method = "spearman")) + print(out, table_width = Inf) Output ======= carni @@ -213,8 +213,7 @@ --- Code - as.list(correlation(datawizard::data_group(mtcars, "am"), select = c("cyl", - "wt"), select2 = "hp", method = "percentage")) + print(out, table_width = Inf) Output === 0 diff --git a/tests/testthat/_snaps/display_print_dataframe.md b/tests/testthat/_snaps/display_print_dataframe.md index 6aa3849c..dc3c009e 100644 --- a/tests/testthat/_snaps/display_print_dataframe.md +++ b/tests/testthat/_snaps/display_print_dataframe.md @@ -1,72 +1,29 @@ # display and print method works - markdown Code - print(correlation(iris)) + print(correlation(iris), format = "markdown") Output - # Correlation Matrix (pearson-method) - - Parameter1 | Parameter2 | r | 95% CI | t(148) | p - ------------------------------------------------------------------------- - Sepal.Length | Sepal.Width | -0.12 | [-0.27, 0.04] | -1.44 | 0.152 - Sepal.Length | Petal.Length | 0.87 | [ 0.83, 0.91] | 21.65 | < .001*** - Sepal.Length | Petal.Width | 0.82 | [ 0.76, 0.86] | 17.30 | < .001*** - Sepal.Width | Petal.Length | -0.43 | [-0.55, -0.29] | -5.77 | < .001*** - Sepal.Width | Petal.Width | -0.37 | [-0.50, -0.22] | -4.79 | < .001*** - Petal.Length | Petal.Width | 0.96 | [ 0.95, 0.97] | 43.39 | < .001*** - - p-value adjustment method: Holm (1979) - Observations: 150 + Table: Correlation Matrix (pearson-method) |Parameter1 | Parameter2| r | 95% CI | t(148)| p | |:------------|------------:|:-----|:--------------|------:|:---------| |Sepal.Length | Sepal.Width|-0.12 |[-0.27, 0.04] | -1.44|0.152 | |Sepal.Length | Petal.Length| 0.87 |[ 0.83, 0.91] | 21.65|< .001*** | |Sepal.Length | Petal.Width| 0.82 |[ 0.76, 0.86] | 17.30|< .001*** | |Sepal.Width | Petal.Length|-0.43 |[-0.55, -0.29] | -5.77|< .001*** | |Sepal.Width | Petal.Width|-0.37 |[-0.50, -0.22] | -4.79|< .001*** | |Petal.Length | Petal.Width| 0.96 |[ 0.95, 0.97] | 43.39|< .001*** | p-value adjustment method: Holm (1979) Observations: 150 --- Code display(correlation(iris)) Output - - - Table: Correlation Matrix (pearson-method) - - |Parameter1 | Parameter2 | r | 95% CI | t(148) | p | - |:------------|:------------:|:-----:|:--------------:|:------:|:---------:| - |Sepal.Length | Sepal.Width | -0.12 | (-0.27, 0.04) | -1.44 | 0.152 | - |Sepal.Length | Petal.Length | 0.87 | (0.83, 0.91) | 21.65 | < .001*** | - |Sepal.Length | Petal.Width | 0.82 | (0.76, 0.86) | 17.30 | < .001*** | - |Sepal.Width | Petal.Length | -0.43 | (-0.55, -0.29) | -5.77 | < .001*** | - |Sepal.Width | Petal.Width | -0.37 | (-0.50, -0.22) | -4.79 | < .001*** | - |Petal.Length | Petal.Width | 0.96 | (0.95, 0.97) | 43.39 | < .001*** | - p-value adjustment method: Holm (1979) - Observations: 150 - -# display and print method works - HTML - - Code - display(print(correlation(subset(mtcars, select = c("wt", "mpg"))), format = "html")) - Output - Correlation Matrix (pearson-method) - - Parameter1 | Parameter2 | r | 95% CI | t(30) | p - -------------------------------------------------------------------- - wt | mpg | -0.87 | [-0.93, -0.74] | -9.56 | < .001*** - p-value adjustment method: Holm (1979)Observations: 32 - - - Table: Correlation Matrix (pearson-method) - - |Parameter1 | Parameter2 | r | 95% CI | t(30) | p | - |:----------|:----------:|:-----:|:--------------:|:-----:|:---------:| - |wt | mpg | -0.87 | (-0.93, -0.74) | -9.56 | < .001*** | - p-value adjustment method: Holm (1979) - Observations: 32 - ---- - - Code - print(correlation(subset(mtcars, select = c("wt", "mpg"))), format = "html") - Output - Correlation Matrix (pearson-method) - - Parameter1 | Parameter2 | r | 95% CI | t(30) | p - -------------------------------------------------------------------- - wt | mpg | -0.87 | [-0.93, -0.74] | -9.56 | < .001*** - p-value adjustment method: Holm (1979)Observations: 32 + [1] "Table: Correlation Matrix (pearson-method)" + [2] "" + [3] "|Parameter1 | Parameter2 | r | 95% CI | t(148) | p |" + [4] "|:------------|:------------:|:-----:|:--------------:|:------:|:---------:|" + [5] "|Sepal.Length | Sepal.Width | -0.12 | (-0.27, 0.04) | -1.44 | 0.152 |" + [6] "|Sepal.Length | Petal.Length | 0.87 | (0.83, 0.91) | 21.65 | < .001*** |" + [7] "|Sepal.Length | Petal.Width | 0.82 | (0.76, 0.86) | 17.30 | < .001*** |" + [8] "|Sepal.Width | Petal.Length | -0.43 | (-0.55, -0.29) | -5.77 | < .001*** |" + [9] "|Sepal.Width | Petal.Width | -0.37 | (-0.50, -0.22) | -4.79 | < .001*** |" + [10] "|Petal.Length | Petal.Width | 0.96 | (0.95, 0.97) | 43.39 | < .001*** |" + [11] "p-value adjustment method: Holm (1979)" + [12] "Observations: 150" + attr(,"format") + [1] "pipe" + attr(,"class") + [1] "knitr_kable" "character" diff --git a/tests/testthat/_snaps/display_print_matrix.md b/tests/testthat/_snaps/display_print_matrix.md index 1f5bfff8..31636ba4 100644 --- a/tests/testthat/_snaps/display_print_matrix.md +++ b/tests/testthat/_snaps/display_print_matrix.md @@ -3,16 +3,18 @@ Code display(summary(correlation(iris))) Output - - - Table: Correlation Matrix (pearson-method) - - |Parameter | Petal.Width | Petal.Length | Sepal.Width | - |:------------|:-----------:|:------------:|:-----------:| - |Sepal.Length | 0.82*** | 0.87*** | -0.12 | - |Sepal.Width | -0.37*** | -0.43*** | | - |Petal.Length | 0.96*** | | | - p-value adjustment method: Holm (1979) + [1] "Table: Correlation Matrix (pearson-method)" + [2] "" + [3] "|Parameter | Petal.Width | Petal.Length | Sepal.Width |" + [4] "|:------------|:-----------:|:------------:|:-----------:|" + [5] "|Sepal.Length | 0.82*** | 0.87*** | -0.12 |" + [6] "|Sepal.Width | -0.37*** | -0.43*** | |" + [7] "|Petal.Length | 0.96*** | | |" + [8] "p-value adjustment method: Holm (1979)" + attr(,"format") + [1] "pipe" + attr(,"class") + [1] "knitr_kable" "character" --- @@ -29,20 +31,6 @@ p-value adjustment method: Holm (1979) -# display and print method works - html - - Code - print(summary(correlation(iris)), format = "html") - Output - Correlation Matrix (pearson-method) - - Parameter | Petal.Width | Petal.Length | Sepal.Width - ------------------------------------------------------- - Sepal.Length | 0.82*** | 0.87*** | -0.12 - Sepal.Width | -0.37*** | -0.43*** | - Petal.Length | 0.96*** | | - p-value adjustment method: Holm (1979) - # as.matrix works Code diff --git a/tests/testthat/test-as.list.R b/tests/testthat/test-as_list.R similarity index 66% rename from tests/testthat/test-as.list.R rename to tests/testthat/test-as_list.R index c0a63277..3342a49f 100644 --- a/tests/testthat/test-as.list.R +++ b/tests/testthat/test-as_list.R @@ -4,21 +4,24 @@ test_that("as.list", { # no groups set.seed(123) - expect_snapshot(as.list(correlation(mtcars))) + out <- as.list(correlation(mtcars)) + expect_snapshot(print(out, table_width = Inf)) # with groups set.seed(123) data(msleep, package = "ggplot2") - expect_snapshot(as.list( + out <- as.list( correlation(datawizard::data_group(msleep, "vore"), method = "spearman") - )) + ) + expect_snapshot(print(out, table_width = Inf)) - expect_snapshot(as.list( + out <- as.list( correlation( datawizard::data_group(mtcars, "am"), select = c("cyl", "wt"), select2 = "hp", method = "percentage" ) - )) + ) + expect_snapshot(print(out, table_width = Inf)) }) diff --git a/tests/testthat/test-cor_sort.R b/tests/testthat/test-cor_sort.R new file mode 100644 index 00000000..61886466 --- /dev/null +++ b/tests/testthat/test-cor_sort.R @@ -0,0 +1,56 @@ +test_that("cor_sort", { + # Basic ------------------------------------------------------------------- + + # Square + r1 <- cor(mtcars) + expect_equal(as.numeric(diag(r1)), rep(1, ncol(mtcars))) + # heatmap(r1, Rowv = NA, Colv = NA) # visualize + + r1sort <- cor_sort(r1) + expect_equal(as.numeric(diag(r1sort)), rep(1, ncol(mtcars))) + # heatmap(r1sort, Rowv = NA, Colv = NA) # visualize + + # Non-square + r2 <- cor(mtcars[names(mtcars)[1:5]], mtcars[names(mtcars)[6:11]]) + expect_equal(rownames(r2), names(mtcars)[1:5]) + expect_identical(colnames(r2), c("wt", "qsec", "vs", "am", "gear", "carb")) + expect_identical(rownames(r2), c("mpg", "cyl", "disp", "hp", "drat")) + # heatmap(r2, Rowv = NA, Colv = NA) # visualize + + r2sort <- cor_sort(r2) + expect_false(all(rownames(r2sort) == names(mtcars)[1:5])) + expect_identical(colnames(r2sort), c("am", "gear", "qsec", "vs", "wt", "carb")) + expect_identical(rownames(r2sort), c("drat", "disp", "hp", "cyl", "mpg")) + # heatmap(r2sort, Rowv = NA, Colv = NA) # visualize + + # correlation() ----------------------------------------------------------- + # Square + rez1 <- correlation::correlation(mtcars) + rez1sort <- cor_sort(rez1) + expect_false(all(rez1$Parameter1 == rez1sort$Parameter1)) + + # Non-square + rez2 <- correlation::correlation(mtcars[names(mtcars)[1:5]], mtcars[names(mtcars)[6:11]]) + rez2sort <- cor_sort(rez2) + expect_false(all(rez2$Parameter1 == rez2sort$Parameter1)) + + # summary(correlation()) -------------------------------------------------- + # Square + rez1sum <- summary(rez1) # TODO: doesn't work with non-redundant + # TODO: fix + expect_error(cor_sort(rez1sum)) + + rez1sum <- summary(rez1, redundant = TRUE) + rez1sumsort <- cor_sort(rez1sum) + expect_false(all(rownames(rez1sumsort) == rownames(rez1sum))) + + # Non-square + rez2sum <- summary(rez2) + rez2sumsort <- cor_sort(rez2sum) + expect_false(all(rownames(rez2sumsort) == rownames(rez2sum))) + + # as.matrix(correlation()) ------------------------------------------------ + # TODO. + m1 <- as.matrix(rez1) + # m1sort <- as.matrix(rez1sort) +}) diff --git a/tests/testthat/test-display_print_dataframe.R b/tests/testthat/test-display_print_dataframe.R index 37560603..891eb551 100644 --- a/tests/testthat/test-display_print_dataframe.R +++ b/tests/testthat/test-display_print_dataframe.R @@ -3,8 +3,7 @@ test_that("display and print method works - markdown", { skip_if(getRversion() < "4.0.0") skip_if_not_or_load_if_installed("gt") - expect_snapshot(print(correlation(iris))) - + expect_snapshot(print(correlation(iris), format = "markdown")) expect_snapshot(display(correlation(iris))) }) @@ -15,7 +14,6 @@ test_that("display and print method works - HTML", { skip_if(getRversion() < "4.0.0") skip_if_not_or_load_if_installed("gt") - expect_snapshot(display(print(correlation(subset(mtcars, select = c("wt", "mpg"))), format = "html"))) - - expect_snapshot(print(correlation(subset(mtcars, select = c("wt", "mpg"))), format = "html")) + expect_s3_class(display(correlation(subset(mtcars, select = c("wt", "mpg"))), format = "html"), "gt_tbl") + expect_s3_class(print_html(correlation(subset(mtcars, select = c("wt", "mpg")))), "gt_tbl") }) diff --git a/tests/testthat/test-display_print_matrix.R b/tests/testthat/test-display_print_matrix.R index 393eaa10..a70d5e9e 100644 --- a/tests/testthat/test-display_print_matrix.R +++ b/tests/testthat/test-display_print_matrix.R @@ -3,7 +3,6 @@ test_that("display and print method works - markdown", { skip_on_cran() skip_if_not_or_load_if_installed("gt") - skip_if_not_or_load_if_installed("poorman") expect_snapshot(display(summary(correlation(iris)))) expect_snapshot(print(summary(correlation(iris)))) }) @@ -12,21 +11,21 @@ test_that("display and print method works - markdown", { test_that("display and print method works - html", { skip_on_cran() skip_if_not_or_load_if_installed("gt") - skip_if_not_or_load_if_installed("poorman") - expect_snapshot(print(summary(correlation(iris)), format = "html")) + expect_s3_class(print_html(summary(correlation(iris))), "gt_tbl") }) test_that("as.matrix works", { skip_if_not_or_load_if_installed("gt") - skip_if_not_or_load_if_installed("poorman") + skip_if_not_installed("datawizard") + skip_if(getRversion() < "4.1.0") set.seed(123) - mat1 <- select(mtcars, am, wt, hp) %>% - correlation() %>% + mat1 <- select(mtcars, am, wt, hp) |> + correlation() |> as.matrix() set.seed(123) - mat2 <- select(mtcars, am, wt, hp) %>% - group_by(am) %>% - correlation() %>% + mat2 <- select(mtcars, am, wt, hp) |> + datawizard::data_group(am) |> + correlation() |> as.matrix() expect_snapshot(list(mat1, mat2)) })