Skip to content

Commit

Permalink
Clean a few lints
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Dec 29, 2022
1 parent 18a0b98 commit ef861ec
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 45 deletions.
2 changes: 1 addition & 1 deletion R/cor_lower.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ cor_lower.easycorrelation <- function(x, diag = FALSE, ...) {
rownames(tri) <- rownames(m)
colnames(tri) <- colnames(m)

tokeep <- c()
tokeep <- NULL

for (param1 in rownames(m)) {
for (param2 in colnames(m)) {
Expand Down
4 changes: 2 additions & 2 deletions R/cor_smooth.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ cor_smooth.matrix <- function(x,
x <- suppressWarnings(psych::cor.smooth(x, eig.tol = tol, ...))
} else {
out <- try(suppressMessages(mbend::bend(x, method = method, ...)), silent = TRUE)
if (inherits(out, as.character("try-error"))) {
if (inherits(out, "try-error")) {
return(x)
}
x <- out$bent
Expand Down Expand Up @@ -133,7 +133,7 @@ is.positive_definite.matrix <- function(x, tol = 10^-12, ...) {
eigens <- try(eigen(x), silent = TRUE)

# validation checks
if (inherits(eigens, as.character("try-error"))) {
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)
Expand Down
5 changes: 4 additions & 1 deletion R/cor_sort.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ cor_sort.easycorrelation <- function(x, distance = "correlation", ...) {
reordered <- x[order(x$Parameter1, x$Parameter2), ]

# Restore class and attributes
attributes(reordered) <- utils::modifyList(attributes(x)[!names(attributes(x)) %in% c("names", "row.names")], attributes(reordered))
attributes(reordered) <- utils::modifyList(
attributes(x)[!names(attributes(x)) %in% c("names", "row.names")],
attributes(reordered)
)

# make sure Parameter columns are character
reordered$Parameter1 <- as.character(reordered$Parameter1)
Expand Down
2 changes: 1 addition & 1 deletion R/cor_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ cor_test <- function(data,
if (!isFALSE(winsorize) && !is.null(winsorize)) {
# set default (if not specified)
if (isTRUE(winsorize)) {
winsorize <- .2
winsorize <- 0.2
}

# winsorization would otherwise fail in case of NAs present
Expand Down
6 changes: 5 additions & 1 deletion R/cor_test_biserial.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
if (.vartype(data[[binary]])$is_factor || .vartype(data[[binary]])$is_character) {
data[[binary]] <- as.numeric(as.factor(data[[binary]]))
}
data[[binary]] <- as.vector((data[[binary]] - min(data[[binary]], na.rm = TRUE)) / diff(range(data[[binary]], na.rm = TRUE), na.rm = TRUE))

data[[binary]] <- as.vector(
(data[[binary]] - min(data[[binary]], na.rm = TRUE)) /
(diff(range(data[[binary]], na.rm = TRUE), na.rm = TRUE))
)

# Get biserial or point-biserial correlation
if (method == "biserial") {
Expand Down
6 changes: 3 additions & 3 deletions R/cor_test_biweight.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
u <- (var_x - stats::median(var_x)) / (9 * stats::mad(var_x, constant = 1))
v <- (var_y - stats::median(var_y)) / (9 * stats::mad(var_y, constant = 1))

I_x <- ifelse((1 - abs(u)) > 0, 1, 0)
I_y <- ifelse((1 - abs(v)) > 0, 1, 0)
I_x <- as.numeric((1 - abs(u)) > 0)
I_y <- as.numeric((1 - abs(v)) > 0)

w_x <- I_x * (1 - u^2)^2
w_y <- I_y * (1 - v^2)^2
Expand All @@ -31,7 +31,7 @@
Parameter2 = y,
r = r,
t = p$statistic,
df_error = length(var_x) - 2,
df_error = length(var_x) - 2L,
p = p$p,
CI_low = ci_vals$CI_low,
CI_high = ci_vals$CI_high,
Expand Down
6 changes: 4 additions & 2 deletions R/cor_test_distance.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
t = NA,
df_error = NA,
p = NA,
Method = "Distance"
Method = "Distance",
stringsAsFactors = FALSE
)
} else {
rez <- .cor_test_distance_corrected(var_x, var_y, ci = ci)
Expand All @@ -27,7 +28,8 @@
t = rez$t,
df_error = rez$df,
p = rez$p,
Method = "Distance (Bias Corrected)"
Method = "Distance (Bias Corrected)",
stringsAsFactors = FALSE
)
}

Expand Down
7 changes: 5 additions & 2 deletions R/correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,12 @@ correlation <- function(data,
data <- cbind(data, data2)
}

if (ncol(data) <= 2 && any(sapply(data, is.factor)) && !include_factors) {
if (ncol(data) <= 2L && any(sapply(data, is.factor)) && !include_factors) {
if (isTRUE(verbose)) {
warning(insight::format_message("It seems like there is not enough continuous variables in your data. Maybe you want to include the factors? We're setting `include_factors=TRUE` for you."), call. = FALSE)
warning(
insight::format_message("It seems like there is not enough continuous variables in your data. Maybe you want to include the factors? We're setting `include_factors=TRUE` for you."),
call. = FALSE
)
}
include_factors <- TRUE
}
Expand Down
4 changes: 2 additions & 2 deletions R/utils_remove_redundant.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@

#' @keywords internal
.get_rows_non_NA <- function(m) {
rows <- c()
cols <- c()
rows <- NULL
cols <- NULL

for (col in colnames(m)) {
for (row in seq_len(nrow(m))) {
Expand Down
21 changes: 10 additions & 11 deletions tests/testthat/test-cor_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ test_that("cor_test percentage", {
test_that("cor_test shepherd", {
set.seed(333)
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "shepherd")
expect_equal(out$r, as.numeric(0.94762), tolerance = 0.01)
expect_equal(out$r, 0.94762, tolerance = 0.01)

if (requiet("BayesFactor")) {
set.seed(333)
out2 <- cor_test(iris, "Petal.Length", "Petal.Width", method = "shepherd", bayesian = TRUE)
expect_equal(out2$rho, as.numeric(0.9429992), tolerance = 0.01)
expect_equal(out2$rho, 0.9429992, tolerance = 0.01)
}
})

Expand All @@ -137,38 +137,37 @@ test_that("cor_test blomqvist", {
if (requiet("wdm")) {
set.seed(333)
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "blomqvist")
expect_equal(out$r, as.numeric(0.9066667), tolerance = 0.01)
expect_equal(out$r, 0.9066667, tolerance = 0.01)
}
})

test_that("cor_test hoeffding and somers", {
if (requiet("Hmisc")) {
set.seed(333)
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "hoeffding")
expect_equal(out$r, as.numeric(0.5629277), tolerance = 0.01)
expect_equal(out$r, 0.5629277, tolerance = 0.01)

set.seed(333)
df <- data.frame(x = 1:6, y = c(0, 0, 1, 0, 1, 1))
out2 <- cor_test(df, "y", "x", method = "somers")
expect_equal(out2$Dxy, as.numeric(0.7777778), tolerance = 0.01)
expect_equal(out2$Dxy, 0.7777778, tolerance = 0.01)
}
})

test_that("cor_test gamma", {
set.seed(333)
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "gamma")
expect_equal(out$r, as.numeric(0.8453925), tolerance = 0.01)
expect_equal(out$r, 0.8453925, tolerance = 0.01)
})

test_that("cor_test gaussian", {
requiet("BayesFactor")
set.seed(333)
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "gaussian")
expect_equal(out$r, as.numeric(0.87137), tolerance = 0.01)
expect_equal(out$r, 0.87137, tolerance = 0.01)

if (requiet("BayesFactor")) {
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "gaussian", bayesian = TRUE)
expect_equal(out$r, as.numeric(0.8620878), tolerance = 0.01)
}
out <- cor_test(iris, "Petal.Length", "Petal.Width", method = "gaussian", bayesian = TRUE)
expect_equal(out$r, 0.8620878, tolerance = 0.01)
})


Expand Down
38 changes: 19 additions & 19 deletions tests/testthat/test-correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,20 @@ test_that("format checks", {
skip_if_not_installed("psych")

out <- correlation(iris, include_factors = TRUE)
expect_equal(c(nrow(summary(out, redundant = TRUE)), ncol(summary(out, redundant = TRUE))), c(7, 8))
expect_equal(c(nrow(summary(out)), ncol(summary(out))), c(6, 7))
expect_identical(c(nrow(summary(out, redundant = TRUE)), ncol(summary(out, redundant = TRUE))), c(7L, 8L))
expect_identical(c(nrow(summary(out)), ncol(summary(out))), c(6L, 7L))

out <- correlation(iris, method = "auto", include_factors = TRUE)
expect_equal(c(nrow(summary(out, redundant = TRUE)), ncol(summary(out, redundant = TRUE))), c(7, 8))
expect_equal(c(nrow(summary(out)), ncol(summary(out))), c(6, 7))
expect_identical(c(nrow(summary(out, redundant = TRUE)), ncol(summary(out, redundant = TRUE))), c(7L, 8L))
expect_identical(c(nrow(summary(out)), ncol(summary(out))), c(6L, 7L))

expect_true(all(c("Pearson correlation", "Point-biserial correlation", "Tetrachoric correlation") %in% out$Method))

# X and Y
out <- correlation(iris[1:2], iris[3:4])
expect_equal(c(nrow(out), ncol(out)), c(4, 11))
expect_equal(c(nrow(summary(out, redundant = TRUE)), ncol(summary(out, redundant = TRUE))), c(2, 3))
expect_equal(c(nrow(summary(out)), ncol(summary(out))), c(2, 3))
expect_identical(c(nrow(out), ncol(out)), c(4L, 11L))
expect_identical(c(nrow(summary(out, redundant = TRUE)), ncol(summary(out, redundant = TRUE))), c(2L, 3L))
expect_identical(c(nrow(summary(out)), ncol(summary(out))), c(2L, 3L))

# Grouped
skip_if_not_installed("poorman")
Expand All @@ -129,22 +129,22 @@ test_that("format checks", {
out <- iris %>%
group_by(Species) %>%
correlation(include_factors = TRUE)
expect_equal(c(nrow(out), ncol(out)), c(18, 12))
expect_equal(c(nrow(summary(out, redundant = TRUE)), ncol(summary(out, redundant = TRUE))), c(12, 6))
expect_equal(c(nrow(summary(out)), ncol(summary(out))), c(9, 5))
expect_identical(c(nrow(out), ncol(out)), c(18L, 12L))
expect_identical(c(nrow(summary(out, redundant = TRUE)), ncol(summary(out, redundant = TRUE))), c(12L, 6L))
expect_identical(c(nrow(summary(out)), ncol(summary(out))), c(9L, 5L))

# pipe and select
out <- iris %>%
correlation(
select = "Petal.Width",
select2 = c("Sepal.Length", "Sepal.Width")
)
expect_equal(c(nrow(out), ncol(out)), c(2, 11))
expect_equal(c(nrow(summary(out, redundant = TRUE)), ncol(summary(out, redundant = TRUE))), c(1, 3))
expect_equal(c(nrow(summary(out)), ncol(summary(out))), c(1, 3))
expect_identical(c(nrow(out), ncol(out)), c(2L, 11L))
expect_identical(c(nrow(summary(out, redundant = TRUE)), ncol(summary(out, redundant = TRUE))), c(1L, 3L))
expect_identical(c(nrow(summary(out)), ncol(summary(out))), c(1L, 3L))
expect_equal(out[["r"]], c(0.8179, -0.3661), tolerance = 1e-2)
expect_equal(out$Parameter1, c("Petal.Width", "Petal.Width"))
expect_equal(out$Parameter2, c("Sepal.Length", "Sepal.Width"))
expect_identical(out$Parameter1, c("Petal.Width", "Petal.Width"))
expect_identical(out$Parameter2, c("Sepal.Length", "Sepal.Width"))

# Bayesian full partial
skip_if_not_installed("BayesFactor")
Expand All @@ -159,9 +159,9 @@ test_that("format checks", {
partial = TRUE,
partial_bayesian = TRUE
)
expect_equal(c(nrow(out), ncol(out)), c(6, 14))
expect_equal(c(nrow(summary(out, redundant = TRUE)), ncol(summary(out, redundant = TRUE))), c(4, 5))
expect_equal(c(nrow(summary(out)), ncol(summary(out))), c(3, 4))
expect_identical(c(nrow(out), ncol(out)), c(6L, 14L))
expect_identical(c(nrow(summary(out, redundant = TRUE)), ncol(summary(out, redundant = TRUE))), c(4L, 5L))
expect_identical(c(nrow(summary(out)), ncol(summary(out))), c(3L, 4L))
})


Expand All @@ -185,7 +185,7 @@ test_that("correlation doesn't fail when BFs are NA", {

set.seed(123)
df_corr <- correlation(subset(df, vore == "carni"), bayesian = TRUE)
expect_equal(nrow(df_corr), 15L)
expect_identical(nrow(df_corr), 15L)
})

test_that("as.data.frame for correlation output", {
Expand Down

0 comments on commit ef861ec

Please sign in to comment.