Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ references.bib
^revdep$
^CRAN-SUBMISSION$
^LICENSE\.md$
^[.]?air[.]toml$
5 changes: 4 additions & 1 deletion R/cor_lower.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ cor_lower.easycorrelation <- function(x, diag = FALSE, ...) {
for (param1 in rownames(m)) {
for (param2 in colnames(m)) {
if (tri[param1, param2]) {
tokeep <- c(tokeep, which(x$Parameter1 == param1 & x$Parameter2 == param2))
tokeep <- c(
tokeep,
which(x$Parameter1 == param1 & x$Parameter2 == param2)
)
}
}
}
Expand Down
101 changes: 65 additions & 36 deletions R/cor_smooth.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,66 @@ cor_smooth <- function(x, method = "psych", verbose = TRUE, ...) {


#' @export
cor_smooth.easycorrelation <- function(x,
method = "psych",
verbose = TRUE,
tol = 10^-12,
...) {
m <- cor_smooth(as.matrix(x), method = method, verbose = verbose, tol = tol, ...)
cor_smooth.easycorrelation <- function(
x,
method = "psych",
verbose = TRUE,
tol = 10^-12,
...
) {
m <- cor_smooth(
as.matrix(x),
method = method,
verbose = verbose,
tol = tol,
...
)

if (isTRUE(attributes(m)$smoothed)) {
estim <- names(x)[names(x) %in% c("r", "rho", "tau", "D")][1]

for (param1 in row.names(m)) {
for (param2 in colnames(m)) {
if (nrow(x[x$Parameter1 == param1 & x$Parameter2 == param2, ]) == 0) next
if (nrow(x[x$Parameter1 == param1 & x$Parameter2 == param2, ]) == 0) {
next
}
# Print changes
if (verbose) {
val1 <- x[x$Parameter1 == param1 & x$Parameter2 == param2, estim]
val2 <- m[param1, param2]
if (round(val1 - val2, digits = 2) == 0) {
insight::print_color(paste0(
param1,
" - ",
param2,
": no change (",
insight::format_value(val1),
")\n"
), "green")
insight::print_color(
paste0(
param1,
" - ",
param2,
": no change (",
insight::format_value(val1),
")\n"
),
"green"
)
} else {
insight::print_color(paste0(
param1,
" - ",
param2,
": ",
insight::format_value(val1),
" -> ",
insight::format_value(val2),
"\n"
), "red")
insight::print_color(
paste0(
param1,
" - ",
param2,
": ",
insight::format_value(val1),
" -> ",
insight::format_value(val2),
"\n"
),
"red"
)
}
cat("\n")
}
x[x$Parameter1 == param1 & x$Parameter2 == param2, estim] <- m[param1, param2]
x[x$Parameter1 == param1 & x$Parameter2 == param2, estim] <- m[
param1,
param2
]
}
}

Expand All @@ -87,24 +106,31 @@ cor_smooth.easycorrelation <- function(x,


#' @export
cor_smooth.matrix <- function(x,
method = "psych",
verbose = TRUE,
tol = 10^-12,
...) {
cor_smooth.matrix <- function(
x,
method = "psych",
verbose = TRUE,
tol = 10^-12,
...
) {
method <- match.arg(method, choices = c("psych", "hj", "lrs"))

# Already positive definite
if (is.positive_definite(x, tol = tol, ...)) {
if (verbose) message("Matrix is positive definite, smoothing was not needed.")
if (verbose) {
message("Matrix is positive definite, smoothing was not needed.")
}
return(x)
}

if (method == "psych") {
insight::check_if_installed("psych")
x <- suppressWarnings(psych::cor.smooth(x, eig.tol = tol, ...))
} else {
out <- try(suppressMessages(mbend::bend(x, method = method, ...)), silent = TRUE)
out <- try(
suppressMessages(mbend::bend(x, method = method, ...)),
silent = TRUE
)
if (inherits(out, "try-error")) {
return(x)
}
Expand Down Expand Up @@ -134,9 +160,12 @@ is.positive_definite.matrix <- function(x, tol = 10^-12, ...) {

# validation checks
if (inherits(eigens, "try-error")) {
stop(insight::format_message(
"There is something seriously wrong with the correlation matrix, as some of the eigen values are NA."
), call. = FALSE)
stop(
insight::format_message(
"There is something seriously wrong with the correlation matrix, as some of the eigen values are NA."
),
call. = FALSE
)
}

# Find out
Expand Down
71 changes: 60 additions & 11 deletions R/cor_sort.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,28 @@
#' cor_sort(x, hclust_method = "ward.D2") # It can also reorder the long form output
#' cor_sort(summary(x, redundant = TRUE)) # As well as from the summary
#' @export
cor_sort <- function(x, distance = "correlation", hclust_method = "complete", ...) {
cor_sort <- function(
x,
distance = "correlation",
hclust_method = "complete",
...
) {
UseMethod("cor_sort")
}

#' @export
cor_sort.easycorrelation <- function(x, distance = "correlation", hclust_method = "complete", ...) {
m <- cor_sort(as.matrix(x), distance = distance, hclust_method = hclust_method, ...)
cor_sort.easycorrelation <- function(
x,
distance = "correlation",
hclust_method = "complete",
...
) {
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), ]
Expand All @@ -48,7 +63,12 @@ cor_sort.easycorrelation <- function(x, distance = "correlation", hclust_method


#' @export
cor_sort.easycormatrix <- function(x, distance = "correlation", hclust_method = "complete", ...) {
cor_sort.easycormatrix <- function(
x,
distance = "correlation",
hclust_method = "complete",
...
) {
if (!"Parameter" %in% colnames(x)) {
return(NextMethod())
}
Expand All @@ -60,7 +80,9 @@ cor_sort.easycormatrix <- function(x, distance = "correlation", 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)")
insight::format_error(
"Non-redundant matrices are not supported yet. Try again by setting summary(..., redundant = TRUE)"
)
}

# Get sorted matrix
Expand All @@ -77,9 +99,22 @@ cor_sort.easycormatrix <- function(x, distance = "correlation", hclust_method =
)

# Reorder attributes (p-values) etc.
for (id in c("p", "CI", "CI_low", "CI_high", "BF", "Method", "n_Obs", "df_error", "t")) {
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)]
attributes(reordered)[[id]] <- attributes(reordered)[[id]][
order(x$Parameter),
names(reordered)
]
}
}

Expand All @@ -91,9 +126,19 @@ cor_sort.easycormatrix <- function(x, distance = "correlation", hclust_method =


#' @export
cor_sort.matrix <- function(x, distance = "correlation", hclust_method = "complete", ...) {
cor_sort.matrix <- function(
x,
distance = "correlation",
hclust_method = "complete",
...
) {
if (isSquare(x) && all(colnames(x) %in% rownames(x))) {
i <- .cor_sort_square(x, distance = distance, hclust_method = hclust_method, ...)
i <- .cor_sort_square(
x,
distance = distance,
hclust_method = hclust_method,
...
)
} else {
i <- .cor_sort_nonsquare(x, distance = "euclidean", ...)
}
Expand All @@ -111,8 +156,12 @@ cor_sort.matrix <- function(x, distance = "correlation", hclust_method = "comple

# Utils -------------------------------------------------------------------


.cor_sort_square <- 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") {
Expand Down
Loading
Loading