Correlation of All Pairs

 

Menu location: Analysis_Regression and Correlation_Principal Components_Correlation.

 

Please select the correlation matrix from the principal components function in order to produce a matrix of correlations (Pearson's product moment) of all pairs.

 

Example

An illustration with the four questionnaire items of the principal components example (test workbook, Agreement worksheet: Question 1 to Question 4). Run the principal components (correlation) function on the four columns and select the correlation matrix from its results; the matrix is of the items as that function reversed them (questions 3 and 4).

 

For this example:

 

Correlation matrix:

 

  Question 1 Question 2 Question 3 Question 4
Question 1 1 0.337395 0.083735 0.16767
Question 2 0.337395 1 -0.00133 0.037286
Question 3 0.083735 -0.00133 1 0.881114
Question 4 0.16767 0.037286 0.881114 1

 

R code

This R code reproduces the illustration above. It needs no packages and was checked with R 4.6.1. Paste it into R, or save it as a script and run it.

# Correlation of all pairs: an illustration with the four questionnaire items of
# the principal components topic in R. The data are the test workbook's Agreement
# worksheet columns Question 1 to 4.
q <- data.frame(
  q1 = c(2, 5, 4, 4, 3, 3, 3, 4, 4, 4, 4, 5, 4, 4, 4, 3, 4, 5, 5, 4, 4, 4, 3, 5,
    5, 3, 4, 3, 4, 4, 4, 4, 4, 4, 4, 3, 4, 3, 4, 3, 4, 5, 4, 4, 4, 3, 5, 4, 3,
    5, 5, 4, 2, 3, 4, 4, 4, 3, 4, 4),
  q2 = c(3, 5, 5, 3, 3, 3, 4, 4, 5, 4, 4, 5, 4, 3, 4, 3, 5, 5, 5, 4, 4, 4, 4, 3,
    5, 3, 4, 3, 4, 4, 4, 5, 4, 5, 4, 3, 3, 4, 4, 4, 5, 5, 4, 4, 4, 4, 5, 5, 4,
    3, 3, 5, 5, 4, 3, 4, 4, 4, 4, 4),
  q3 = c(5, 4, 5, 4, 5, 4, 4, 5, 5, 3, 5, 4, 4, 5, 5, 4, 4, 5, 4, 4, 4, 4, 5, 5,
    3, 4, 4, 5, 3, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 4, 5, 5, 5, 4, 5, 4, 5, 4, 4,
    4, 4, 4, 5, 5, 5, 4, 5, 4, 5, 5),
  q4 = c(5, 4, 5, 4, 5, 5, 4, 5, 5, 3, 5, 4, 4, 5, 5, 5, 4, 5, 4, 4, 4, 4, 5, 5,
    3, 4, 4, 5, 3, 5, 5, 5, 5, 5, 5, 4, 4, 5, 4, 4, 5, 5, 5, 4, 5, 4, 5, 4, 4,
    4, 4, 4, 5, 5, 5, 4, 5, 4, 4, 5))
names(q) <- paste("Question", 1:4)

# The principal components function had reversed questions 3 and 4 so that all
# the items point the same way, and its correlation matrix is of the items as
# reversed
for (item in c("Question 3", "Question 4")) {
  q[[item]] <- max(q[[item]]) + min(q[[item]]) - q[[item]]
}

# R's correlation matrix (Pearson's product moment) of every pair of columns, to
# 6 places as the report prints it
print(round(cor(q), 6))