Skip to content

Commit 2ffb100

Browse files
Merge pull request #59 from easystats/dev
dev
2 parents e20bff7 + 673d985 commit 2ffb100

39 files changed

+637
-419
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: correlation
22
Type: Package
33
Title: Methods for Correlation Analysis
4-
Version: 0.1.1
4+
Version: 0.2.0
55
Authors@R: c(
66
person("Dominique",
77
"Makowski",

R/cor_test.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @param method A character string indicating which correlation coefficient is to be used for the test. One of "pearson" (default), "kendall", or "spearman", "biserial", "polychoric", "tetrachoric", "biweight", "distance", "percentage" (for percentage bend correlation) or "shepherd" (for Shepherd's Pi correlation). Setting "auto" will attempt at selecting the most relevant method (polychoric when ordinal factors involved, tetrachoric when dichotomous factors involved, point-biserial if one dichotomous and one continuous and pearson otherwise).
99
#' @param bayesian,partial_bayesian If TRUE, will run the correlations under a Bayesian framework. Note that for partial correlations, you will also need to set \code{partial_bayesian} to \code{TRUE} to obtain "full" Bayesian partial correlations. Otherwise, you will obtain pseudo-Bayesian partial correlations (i.e., Bayesian correlation based on frequentist partialization).
1010
#' @param include_factors If \code{TRUE}, the factors are kept and eventually converted to numeric or used as random effects (depending of \code{multilevel}). If \code{FALSE}, factors are removed upfront.
11-
#' @param partial Can be TRUE or "semi" for partial and semi-partial correlations, respectively. This only works for Frequentist correlations.
11+
#' @param partial Can be TRUE or "semi" for partial and semi-partial correlations, respectively.
1212
#' @inheritParams effectsize::adjust
1313
#' @param bayesian_prior For the prior argument, several named values are recognized: "medium.narrow", "medium", "wide", and "ultrawide". These correspond to scale values of 1/sqrt(27), 1/3, 1/sqrt(3) and 1, respectively. See the \code{BayesFactor::correlationBF} function.
1414
#' @param bayesian_ci_method,bayesian_test See arguments in \code{\link[=parameters]{model_parameters}} for \code{BayesFactor} tests.
@@ -71,7 +71,7 @@ cor_test <- function(data, x, y, method = "pearson", ci = 0.95, bayesian = FALSE
7171

7272
# Make sure factor is no factor
7373
if (!method %in% c("tetra", "tetrachoric", "poly", "polychoric")) {
74-
data[c(x, y)] <- parameters::data_to_numeric(data[c(x, y)], dummy_factors=FALSE)
74+
data[c(x, y)] <- parameters::data_to_numeric(data[c(x, y)], dummy_factors = FALSE)
7575
}
7676

7777
# Partial

R/cor_to_pcor.R

+29-11
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,13 @@ pcor_to_cor.easycorrelation <- function(pcor, tol = .Machine$double.eps^(2 / 3))
106106
r <- .cor_to_pcor(.get_cor(as.table(cor), cov = NULL))
107107
}
108108

109-
p <- cor_to_p(r, n = attributes(cor)$n, method = "pearson")
110-
ci_vals <- cor_to_ci(r, n = attributes(cor)$n, ci = attributes(cor)$ci)
109+
# Extract info
110+
p_adjust <- attributes(cor)$p_adjust
111+
nobs <- as.matrix(attributes(as.table(cor))$n_Obs[-1])
112+
113+
# Get Statistics
114+
p <- cor_to_p(r, n = nobs, method = "pearson")
115+
ci_vals <- cor_to_ci(r, n = nobs, ci = attributes(cor)$ci)
111116

112117
# Replace
113118
newdata <- data.frame()
@@ -118,20 +123,27 @@ pcor_to_cor.easycorrelation <- function(pcor, tol = .Machine$double.eps^(2 / 3))
118123
newdata,
119124
data.frame(
120125
r = r[row, col],
121-
t = p$statistic[row, col],
122-
df = attributes(cor)$n - 2,
123-
p = p$p[row, col],
124126
CI_low = ci_vals$CI_low[row, col],
125127
CI_high = ci_vals$CI_high[row, col],
126-
Method = "Pearson"
128+
t = p$statistic[row, col],
129+
df = nobs[row, col] - 2,
130+
p = p$p[row, col],
131+
Method = "Pearson",
132+
n_Obs = nobs[row, col]
127133
)
128134
)
129135
}
136+
137+
# Format
130138
newdata <- cbind(cor[1:2], newdata)
131139
cor <- cor[, 1:ncol(newdata)]
132140
cor[, ] <- newdata
133141
names(cor) <- names(newdata)
134-
attributes(cor)$p_adjust <- "none"
142+
143+
# P-values adjustments
144+
cor$p <- stats::p.adjust(cor$p, method = p_adjust, n = nrow(cor))
145+
attributes(cor)$p_adjust <- p_adjust
146+
135147
cor
136148
}
137149

@@ -146,19 +158,25 @@ pcor_to_cor.easycorrelation <- function(pcor, tol = .Machine$double.eps^(2 / 3))
146158
r <- .cor_to_pcor(.get_cor(cor, cov = NULL))
147159
}
148160

149-
p <- cor_to_p(r, n = attributes(cor)$n, method = "pearson")
150-
ci_vals <- cor_to_ci(r, n = attributes(cor)$n, ci = attributes(cor)$ci)
161+
# Extract info
162+
p_adjust <- attributes(cor)$p_adjust
163+
nobs <- as.matrix(attributes(cor)$n_Obs[-1])
164+
165+
p <- cor_to_p(r, n = nobs, method = "pearson")
166+
ci_vals <- cor_to_ci(r, n = nobs, ci = attributes(cor)$ci)
151167
r <- cbind(data.frame(Parameter = row.names(r)), r)
152168
row.names(r) <- NULL
153169

170+
# P-values adjustments
171+
p$p <- stats::p.adjust(p$p, method = p_adjust, n = nrow(cor) * (ncol(cor) - 1))
172+
attributes(cor)$p_adjust <- p_adjust
173+
154174
# Statistic and p-value
155175
attributes(cor)$pd <- attributes(cor)$BF <- NULL
156176
attributes(cor)$p[-1] <- p$p
157177
attributes(cor)$t[-1] <- p$statistic
158178
attributes(cor)$CI_low[-1] <- ci_vals$CI_low
159179
attributes(cor)$CI_high[-1] <- ci_vals$CI_high
160-
attributes(cor)$p_adjust <- "none"
161-
162180

163181
attributes(r) <- attributes(cor)
164182
r

R/correlation.R

+13-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#' \item \strong{Polychoric correlation}: Correlation between two theorised normally distributed continuous latent variables, from two observed ordinal variables.
2323
#' \item \strong{Tetrachoric correlation}: Special case of the polychoric correlation applicable when both observed variables are dichotomous.
2424
#'
25-
#' \item \strong{Partial correlation}: orrelation between two variables after adjusting for the (linear) the effect of one or more variable.
25+
#' \item \strong{Partial correlation}: Correlation between two variables after adjusting for the (linear) the effect of one or more variable. The correlation test is here run after having partialized the dataset, independently from it. In other words, it considers partialization as an independent step generating a different dataset, rather than belonging to the same model. This is why small discrepancies are to be expected for the t- and the p-values (but not the correlation coefficient) compared to other implementations such as \code{ppcor}.
2626
#'
2727
#' \item \strong{Multilevel correlation}: Multilevel correlations are a special case of partial correlations where the variable to be adjusted for is a factor and is included as a random effect in a mixed model.
2828
#'
@@ -36,18 +36,24 @@
3636
#' }
3737
#'
3838
#' @examples
39-
#' library(dplyr)
39+
#' library(correlation)
40+
#'
4041
#' cor <- correlation(iris)
4142
#'
4243
#' cor
4344
#' summary(cor)
4445
#' as.table(cor)
4546
#'
46-
#' iris %>%
47-
#' group_by(Species) %>%
48-
#' correlation()
47+
#' # Grouped dataframe
48+
#' if (require("dplyr")) {
49+
#' library(dplyr)
50+
#'
51+
#' iris %>%
52+
#' group_by(Species) %>%
53+
#' correlation()
4954
#'
50-
#' correlation(mtcars[-2], method = "auto")
55+
#' correlation(mtcars[-2], method = "auto")
56+
#' }
5157
#' @importFrom stats p.adjust
5258
#' @export
5359
correlation <- function(data, data2 = NULL, method = "pearson", p_adjust = "holm", ci = 0.95, bayesian = FALSE, bayesian_prior = "medium", bayesian_ci_method = "hdi", bayesian_test = c("pd", "rope", "bf"), redundant = FALSE, include_factors = FALSE, partial = FALSE, partial_bayesian = FALSE, multilevel = FALSE, robust = FALSE, ...) {
@@ -95,7 +101,7 @@ correlation <- function(data, data2 = NULL, method = "pearson", p_adjust = "holm
95101

96102
class(out) <- unique(c("easycorrelation", "see_easycorrelation", "parameters_model", class(out)))
97103

98-
if (convert_back_to_r) out <- pcor_to_cor(out) # Revert back to r if needed.
104+
if (convert_back_to_r) out <- pcor_to_cor(pcor = out) # Revert back to r if needed.
99105
out
100106
}
101107

R/mahalanobis.R

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
#' @return Description of the Mahalanobis distance.
1414
#'
1515
#' @examples
16-
#' distance_mahalanobis(data = iris[, 1:4])
16+
#' distance_mahalanobis(iris[, 1:4])
17+
#' distance_mahalanobis(iris[, 1:4], robust = FALSE)
1718
#' @export
1819
distance_mahalanobis <- function(data, ci = 0.95, iterations = 1000, robust = TRUE, ...) {
1920
if (robust) {
@@ -30,6 +31,7 @@ distance_mahalanobis <- function(data, ci = 0.95, iterations = 1000, robust = TR
3031
# Get summary
3132
d <- bayestestR::describe_posterior(as.data.frame(Ms), centrality = "median", ci = ci, test = "pd")
3233
d <- d[c("Median", "CI_low", "CI_high")]
34+
rownames(d) <- NULL
3335
names(d) <- c("Distance", "CI_low", "CI_high")
3436
} else {
3537
d <- stats::mahalanobis(data, center = colMeans(data), cov = stats::cov(data))

R/methods.easycorrelation.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#' @export
2-
summary.easycorrelation <- function(object, redundant = FALSE, ...) {
2+
summary.easycorrelation <- function(object, redundant = FALSE, stars = TRUE, ...) {
33

44
# If data2 is present
55
if (!is.null(attributes(object)$data2)) {
@@ -28,6 +28,7 @@ summary.easycorrelation <- function(object, redundant = FALSE, ...) {
2828

2929
# Transfer attributes
3030
attributes(out) <- c(attributes(out), attributes(object)[!names(attributes(object)) %in% c("names", "row.names", "class", names(attributes(out)))])
31+
attr(out, "stars") <- stars
3132
class(out) <- c("easycormatrix", "see_easycormatrix", class(out))
3233
out
3334
}

R/print.easycormatrix.R

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
print.easycormatrix <- function(x, digits = 2, stars = TRUE, ...) {
33
nums <- sapply(as.data.frame(x), is.numeric)
44

5-
# Significance
5+
# Find attributes
66
p <- attributes(x)
7+
8+
if ("stars" %in% names(p)) {
9+
stars <- p$stars
10+
}
11+
12+
# Significance
713
type <- names(p)[names(p) %in% c("BF", "pd", "p")][1]
814
p <- p[[type]]
915

docs/404.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/LICENSE-text.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/index.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/multilevel.html

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Loading
Loading

docs/articles/types.html

+9-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/authors.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/news/index.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)