| Type: | Package | 
| Title: | Create and Format Correlation Matrices | 
| Version: | 1.0.7.2 | 
| Date: | 2023-06-15 | 
| 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'. | 
| License: | MIT + file LICENSE | 
| Encoding: | UTF-8 | 
| URL: | https://github.com/conig/corx | 
| Depends: | R (≥ 3.6) | 
| Imports: | crayon, ggcorrplot, glue, clipr, tidyselect, moments, ggpubr, ggplot2, stats, methods, ppcor | 
| RoxygenNote: | 7.2.3 | 
| Suggests: | covr, papaja, psych, testthat | 
| NeedsCompilation: | no | 
| Packaged: | 2023-06-15 23:35:20 UTC; james | 
| Author: | James Conigrave | 
| Maintainer: | James Conigrave <james.conigrave@gmail.com> | 
| Repository: | CRAN | 
| Date/Publication: | 2023-06-16 04:10:02 UTC | 
adjust_p
Description
adjust_p
Usage
adjust_pmat(pmat, p_adjust)
Arguments
| pmat | matrix of p-values to adjust | 
| p_adjust | character describing adjustment to make. See stats::p.adjust | 
apa matrix
Description
Creates an apa matrix
Usage
apa_matrix(r_matrix, p_matrix, stars, round, remove_lead, triangle)
Arguments
| 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 | 
apa_table.corx
Description
method for papaja::apa_table for corx objects
Usage
apa_table.corx(corx, ...)
Arguments
| corx | corx object | 
| ... | Other arguments to papaja::apa_table | 
check_classes
Description
check all classes are as expected
Usage
check_classes(data, ok_classes, stop_message, stop = TRUE)
Arguments
| 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
Description
cormat_list
Usage
cormat_list(data, x, y, z, method, p_adjust)
Arguments
| 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 | 
corx
Description
Calculates correlations matrices. Relevant values are stored in a list with methods for easy retrieval and formatting in publication ready tables.
Usage
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
)
Arguments
| 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 | 
Details
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.
Value
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
Examples
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
digits
Description
Consistent rounding for strings
Usage
digits(x, n = 2)
Arguments
| x | number to round | 
| n | number of digits | 
partial_n_matrix
Description
Calculate complete observations for a crosstab + a third variable
Usage
partial_n_matrix(data, x, y, z)
Arguments
| data | data.frame or matrix | 
| x | rownames | 
| y | colnames | 
| z | partial variable vector | 
plot.corx
Description
plot.corx
Usage
## S3 method for class 'corx'
plot(x, ...)
Arguments
| x | a corx object | 
| ... | other arguments to ggcorrplot::ggcorrplot | 
plot_mds
Description
Perform multidimensional scaling of a corx object and plot results
Usage
plot_mds(corx, k = NULL, abs = TRUE, ...)
Arguments
| 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 | 
Details
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:
d = \sqrt(2*(1-r))
These distances are then passed to stats::cmdscale where k = 2. To compute latex, distances are predict from the cmdscale output and correlated with input distances. This correlation is squared.
If the value of R^2 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.
References
Carlson, D.L., 2017. Quantitative methods in archaeology using R. Cambridge University Press.
print.corx
Description
print.corx
Usage
## S3 method for class 'corx'
print(x, ...)
Arguments
| x | object | 
| ... | extra arguments | 
rename if needed
Description
Renames columns
Usage
rename_if_needed(data, x)
Arguments
| data | data object | 
| x | a character vector. If named, columns will be renamed | 
star_matrix
Description
Replaces p-values with stars
Usage
star_matrix(m, stars)
Arguments
| m | matrix of p-values | 
| stars | a vector of p-value thresholds to replace with stars | 
to_clipboard
Description
Sends a formatted corx table to the clipboard so that it can be pasted into excel.
Usage
to_clipboard(x, ...)
Arguments
| x | a corx object, matrix, or data.frame | 
| ... | additional arguments passed to 'clipr::write_clip' | 
to_table
Description
Tabulate correlation matrices
Usage
to_table(corx, include_p = FALSE)
Arguments
| corx | a corx object | 
| include_p | logical. should p-values be included? |