Skip to contents

Computes pairwise correlations and prints a formatted lower-triangle correlation matrix showing r, p values, and pairwise N for each pair. Supports Pearson (default), Spearman, and Kendall methods. Handles haven-labelled and factor variables with numeric levels. Warns when variables may be categorical rather than continuous.

Usage

jcorr(
  data,
  ...,
  method = "pearson",
  subset = NULL,
  variable.id = NULL,
  numeric = NULL,
  categorical = NULL,
  count = NULL,
  value.id = NULL,
  layout = NULL,
  case.processing.detail = NULL,
  digits = NULL,
  print = NULL
)

Arguments

data

A data frame.

...

Unquoted variable names within data. A c() of names, bare or quoted, is read as the names.

method

Character. Correlation method: "pearson" (default), "spearman", or "kendall".

subset

An optional unquoted logical expression (e.g. Group == 1) to subset cases for this call only. Applied after jcomplete and jsubset. Does not affect other function calls. Written in R syntax and checked the way jsubset() checks a filter: subset = NOT(Age < 40), for example, is refused with the corrected subset = !(Age < 40) shown (see jsubset for a translation table).

variable.id

Character or NULL. Variable label display mode: one of "both", "names", "labels", "legend", or "legend.bottom". "names" shows variable names only; "both" shows "name: label"; "labels" shows variable labels as the matrix row/column headers (honored even if the matrix grows wide – best for short labels; rerun with a legend mode otherwise); "legend"/"legend.bottom" keep names and print a label legend after the table. NULL (default) defers to joutput()'s variable.id setting. Not a logical.

numeric

Optional. Variables to treat as continuous for this call (the per-call counterpart of jnumeric()), named bare or in quotes, several with c(): numeric = Age. Its only effect in jcorr() is to suppress the structural "seems categorical" caution for those variables; correlations are computed the same way regardless (labelled variables are coerced to numeric either way).

categorical

Not supported by jcorr() yet. Correlation requires numeric variables; supplying categorical raises an error pointing to jcrosstab() for association between categorical variables. (How jcorr() should handle an asserted-categorical variable is a parked design decision.)

count

Optional. Variables to treat as counts for this call (the per-call counterpart of jcount()), named bare or in quotes, several with c(). A count is numeric-like here, so it behaves like numeric: it suppresses the "seems categorical" caution for those variables.

value.id

Not supported by jcorr(). The function does not display value labels, so passing this argument is an error. It exists only to return a clear message rather than report value.id as a variable that was not found. Leave at NULL (default).

layout

Character or NULL. How each correlation cell is laid out when three or more variables are given: "wide" (default) puts r and its p-value on one line with N on a second line beneath; "stacked" places r, p, and N on three separate lines, giving a narrower table that fits more variables before wrapping. Ignored for a single pair (two variables), which always prints a one-line summary. NULL (default) defers to the corr.layout setting in joptions() (itself defaulting to "wide").

case.processing.detail

Per-call override of the Case Processing Summary detail tier: one of "none", "totals", or "per_code". NULL (default) uses the active joutput() level default. The Case Processing table itself prints only when a filter or listwise deletion excluded cases; otherwise a one-line N statement takes its place. See ?joutput (case.processing).

digits

Integer or NULL. Number of decimal places for continuous statistics in the output tables (range 0-7; digits = 0 prints whole numbers with no trailing decimal point). Does not affect p-values, percentages, or integer quantities (counts, N, degrees of freedom), which keep their own fixed conventions. NULL (default) defers to joutput()'s digits setting (default 3).

print

Logical or NULL. FALSE prints nothing – no table, no title, no note – and returns the result as every call does; errors and warnings still show. TRUE prints even under joutput("silent"). NULL (the default) prints unless the level is silent. For a script that keeps many results and wants no output: see joutput.

Value

Invisibly returns a list of class jst_corr containing: r (correlation matrix), p (p-value matrix), n (pairwise N matrix), method, model_frame (the analysis data frame used for plotting), sample_info (pipeline and missing data counts), data_name (the data frame's name, for the lines jplot() offers) and labels.

Details

A red title identifying the correlation method is printed first, followed by variable labels (if present), then the matrix.

See also

jstats for the package overview, workflow conventions, and complete function listing.

Examples

# With explicit data frame
jcorr(community, Income, Age, WellbeingScore)
#> Pearson Bivariate Correlations
#> 
#> 103 Cases in the 3 Variable Pool; 97 Complete on All
#> 
#> Missing data   From 103   %
#>     Income
#>       Missing      6     5.8
#> ----------------------------
#> 
#> Bivariate Correlations (Pearson)
#>                 Income          Age             WellbeingScore
#> --------------  --------------  --------------  --------------
#> Income           1
#> 
#> Age              .289 (p=.004)   1
#>                 N=97
#> 
#> WellbeingScore   .616 (p<.001)   .343 (p<.001)   1
#>                 N=97            N=103
#> 
jcorr(community, Income, Age, WellbeingScore, method = "spearman")
#> Spearman Bivariate Correlations
#> 
#> 103 Cases in the 3 Variable Pool; 97 Complete on All
#> 
#> Missing data   From 103   %
#>     Income
#>       Missing      6     5.8
#> ----------------------------
#> 
#> Bivariate Correlations (Spearman)
#>                 Income          Age             WellbeingScore
#> --------------  --------------  --------------  --------------
#> Income           1
#> 
#> Age              .305 (p=.002)   1
#>                 N=97
#> 
#> WellbeingScore   .606 (p<.001)   .378 (p<.001)   1
#>                 N=97            N=103
#> 
#> Note: Spearman p-values are approximate due to tied values in the data.
#> 

# The data frame may also be given by name
jcorr(data = community, Income, Age, WellbeingScore)
#> Pearson Bivariate Correlations
#> 
#> 103 Cases in the 3 Variable Pool; 97 Complete on All
#> 
#> Missing data   From 103   %
#>     Income
#>       Missing      6     5.8
#> ----------------------------
#> 
#> Bivariate Correlations (Pearson)
#>                 Income          Age             WellbeingScore
#> --------------  --------------  --------------  --------------
#> Income           1
#> 
#> Age              .289 (p=.004)   1
#>                 N=97
#> 
#> WellbeingScore   .616 (p<.001)   .343 (p<.001)   1
#>                 N=97            N=103
#> 

# Using juse() default
juse(community)
#> Default data frame set to: community
jcorr(Income, Age, WellbeingScore)
#> Pearson Bivariate Correlations
#> Using default data frame: community
#> 
#> 103 Cases in the 3 Variable Pool; 97 Complete on All
#> 
#> Missing data   From 103   %
#>     Income
#>       Missing      6     5.8
#> ----------------------------
#> 
#> Bivariate Correlations (Pearson)
#>                 Income          Age             WellbeingScore
#> --------------  --------------  --------------  --------------
#> Income           1
#> 
#> Age              .289 (p=.004)   1
#>                 N=97
#> 
#> WellbeingScore   .616 (p<.001)   .343 (p<.001)   1
#>                 N=97            N=103
#>