Skip to content

Commit

Permalink
lintr, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed May 26, 2024
1 parent c86e88e commit 6fef450
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 155 deletions.
16 changes: 8 additions & 8 deletions R/cor_sort.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion R/cor_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion R/cor_test_bayes.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions R/cor_test_biserial.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 21 additions & 21 deletions R/cor_test_distance.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}


Expand Down
6 changes: 3 additions & 3 deletions R/cor_test_freq.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
34 changes: 17 additions & 17 deletions R/cor_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand All @@ -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
}
10 changes: 5 additions & 5 deletions R/cor_to_cov.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
55 changes: 26 additions & 29 deletions R/cor_to_pcor.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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
)
)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Loading

0 comments on commit 6fef450

Please sign in to comment.