Title: | Create and Format Correlation Matrices |
---|---|
Description: | Create correlation (or partial correlation) matrices. Correlation matrices are formatted with significance stars based on user preferences. Matrices of coefficients, p-values, and number of pairwise observations are returned. Send resultant formatted matrices to the clipboard to be pasted into excel and other programs. A plot method allows users to visualize correlation matrices created with 'corx'. |
Authors: | James Conigrave [aut, cre] |
Maintainer: | James Conigrave <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.7.2 |
Built: | 2025-01-23 03:10:14 UTC |
Source: | https://github.com/conig/corx |
adjust_p
adjust_pmat(pmat, p_adjust)
adjust_pmat(pmat, p_adjust)
pmat |
matrix of p-values to adjust |
p_adjust |
character describing adjustment to make. See stats::p.adjust |
Creates an apa matrix
apa_matrix(r_matrix, p_matrix, stars, round, remove_lead, triangle)
apa_matrix(r_matrix, p_matrix, stars, round, remove_lead, triangle)
r_matrix |
correlation coefficient matrix |
p_matrix |
p-value matrix |
stars |
a vector of pvalue stars |
round |
How many digits to round to? |
remove_lead |
a logical. Should leading zeros be removed? |
triangle |
can select lower upper or NULL |
method for papaja::apa_table for corx objects
apa_table.corx(corx, ...)
apa_table.corx(corx, ...)
corx |
corx object |
... |
Other arguments to papaja::apa_table |
check all classes are as expected
check_classes(data, ok_classes, stop_message, stop = TRUE)
check_classes(data, ok_classes, stop_message, stop = TRUE)
data |
the data object |
ok_classes |
a vector of allowed classes |
stop_message |
a character string provided to users if error triggers. |
stop |
should the variable stop, or create a warning? |
cormat_list
cormat_list(data, x, y, z, method, p_adjust)
cormat_list(data, x, y, z, method, p_adjust)
data |
data.frame |
x |
character vector, row names |
y |
character vector, column names |
z |
character vector, partial variable names |
method |
string, passed to cor.test |
p_adjust |
string, passed to p.adjust |
Calculates correlations matrices. Relevant values are stored in a list with methods for easy retrieval and formatting in publication ready tables.
corx( data, x = NULL, y = NULL, z = NULL, method = c("pearson", "spearman", "kendall"), stars = c(0.05, 0.01, 0.001), p_adjust = c("none", "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr"), round = 2, remove_lead = TRUE, triangle = NULL, caption = NULL, note = NULL, describe = FALSE, grey_nonsig = TRUE, call_only = FALSE )
corx( data, x = NULL, y = NULL, z = NULL, method = c("pearson", "spearman", "kendall"), stars = c(0.05, 0.01, 0.001), p_adjust = c("none", "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr"), round = 2, remove_lead = TRUE, triangle = NULL, caption = NULL, note = NULL, describe = FALSE, grey_nonsig = TRUE, call_only = FALSE )
data |
data.frame or matrix |
x |
a vector of rownames. Defaults to all |
y |
a vector of colnames. If not supplied, y is set to x. |
z |
a vector of variable names. Control variables to be used in partial correlations - defaults to NULL |
method |
character. One of "pearson", "spearman", or "kendall" |
stars |
a numeric vector. This argument defines cut-offs for p-value stars. |
p_adjust |
character. What adjustment for multiple tests should be used? One of "none" (default), "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", or "fdr" |
round |
numeric. Number of digits in printing |
remove_lead |
logical. if TRUE (the default), leading zeros are removed in summaries |
triangle |
character. one of "lower", "upper" or NULL (the default) |
caption |
character. table caption. Passed to plots |
note |
character. Text for a table note |
describe |
list of named functions. If functions are supplied to describe, new columns will be bound to the 'APA matrix' for each function in the list. Describe also accepts a variety of shortcuts. If describe is set to TRUE, mean and standard deviation are returned for all row variables. Describe can accept a character vector to call the following descriptive functions: c('mean','sd','var','median','iqr','skewness','kurtosis'). These shortcuts are powered by 'tidyselect'. Skewness and kurtosis are calculated using the 'moments' package. All functions retrieved with shortcuts remove missing values. |
grey_nonsig |
logical. Should non-significant values be grey in output? This argument does nothing if describe is not set to FALSE |
call_only |
logical. For debugging, if TRUE only the call is returned |
Constructs correlation matrices using 'stats::cor.test' unless z is specified. When z is specified ppcor::ppcor.test is used instead. Character and factor variables are not accepted. To prevent errors, users must first convert all variables to numeric.
## Partial correlations:
Supplying the argument z will call ppcor::pcor.test the correlation pair are supplied to arguments x and y. The vector of z given to corx is passed to argument z in pcor.test.
## Missing data:
Observations containing missing data required to complete a correlation or partial correlation are automatically removed.
## P-adjust:
P-values attained can be adjusted for multiple comparisons by using the 'p_adjust' argument. This calls the function stats::p.adjust. When a matrix is symmetrical, p-values are only adjusted for unique comparisons. When a correlation matrix is not symmetrical, all comparisons are assumed to be unique.
A list of class 'corx' which includes: * "call" The call which if evaluated reproduces the object * "apa" An 'APA' formatted correlation matrix with significance stars * "r" Raw correlation coefficients * "p" p-values * "n" Pairwise observations * "caption" Object caption * "note" Object note
cor_mat <- corx(mtcars, x = c(mpg,cyl,disp), y = c(wt,drat,disp,qsec), z = wt, round = 2, stars = c(0.05), caption = "Controlling for weight" , describe = list("mean" = function(x) mean(x,na.rm=TRUE))) cor_mat coef(cor_mat) cor_mat$p plot(cor_mat) cor_2 <- corx(iris[,-5], describe = c(median, IQR = iqr, kurt = kurtosis), note = "Using shortcuts to select describe functions", triangle = "lower") cor_2
cor_mat <- corx(mtcars, x = c(mpg,cyl,disp), y = c(wt,drat,disp,qsec), z = wt, round = 2, stars = c(0.05), caption = "Controlling for weight" , describe = list("mean" = function(x) mean(x,na.rm=TRUE))) cor_mat coef(cor_mat) cor_mat$p plot(cor_mat) cor_2 <- corx(iris[,-5], describe = c(median, IQR = iqr, kurt = kurtosis), note = "Using shortcuts to select describe functions", triangle = "lower") cor_2
Consistent rounding for strings
digits(x, n = 2)
digits(x, n = 2)
x |
number to round |
n |
number of digits |
Calculate complete observations for a crosstab + a third variable
partial_n_matrix(data, x, y, z)
partial_n_matrix(data, x, y, z)
data |
data.frame or matrix |
x |
rownames |
y |
colnames |
z |
partial variable vector |
Perform multidimensional scaling of a corx object and plot results
plot_mds(corx, k = NULL, abs = TRUE, ...)
plot_mds(corx, k = NULL, abs = TRUE, ...)
corx |
corx object |
k |
numeric. The number of clusters. If set to "auto" will be equal to the number of principal components that explain more than 5% of total variance. |
abs |
logical. If TRUE (the default) negative correlations will be turned positive. This means items with high negative correlations will be treated as highly similar. |
... |
additional arguments passed to ggpubr::ggscatter |
plot_mds performs classic multidimensional scaling on a correlation matrix. The correlation matrix is first converted to a distance matrix using psych::cor2dist. This function employs the following formula:
These distances are then passed to stats::cmdscale where k = 2. To compute , distances are predict from the cmdscale output and correlated with input distances. This correlation is squared.
If the value of
is less than 70%, a warning will inform users that two-dimensions may not be sufficient to represent item relationships.
The position of variables is then plotted with ggplot2. Clusters of items are identified using stats::kmeans. The number of clusters is determined using principal component analysis unless specified.
Carlson, D.L., 2017. Quantitative methods in archaeology using R. Cambridge University Press.
plot.corx
## S3 method for class 'corx' plot(x, ...)
## S3 method for class 'corx' plot(x, ...)
x |
a corx object |
... |
other arguments to ggcorrplot::ggcorrplot |
print.corx
## S3 method for class 'corx' print(x, ...)
## S3 method for class 'corx' print(x, ...)
x |
object |
... |
extra arguments |
Renames columns
rename_if_needed(data, x)
rename_if_needed(data, x)
data |
data object |
x |
a character vector. If named, columns will be renamed |
Replaces p-values with stars
star_matrix(m, stars)
star_matrix(m, stars)
m |
matrix of p-values |
stars |
a vector of p-value thresholds to replace with stars |
Sends a formatted corx table to the clipboard so that it can be pasted into excel.
to_clipboard(x, ...)
to_clipboard(x, ...)
x |
a corx object, matrix, or data.frame |
... |
additional arguments passed to 'clipr::write_clip' |
Tabulate correlation matrices
to_table(corx, include_p = FALSE)
to_table(corx, include_p = FALSE)
corx |
a corx object |
include_p |
logical. should p-values be included? |