@@ -29,11 +29,22 @@ tol <- 1E-15 # error tolerance for tests
2929source("testFunctions") # utility test functions
3030options(digits=16) # override number of digits displayed
3131
32- # function to verify correlation computations
33- verifyCorrelation <- function(matrix, expectedCorrelation, name) {
32+ # Verify Pearson's correlation
33+ verifyPearsonsCorrelation <- function(matrix, expectedCorrelation, name) {
3434 correlation <- cor(matrix)
35- output <- c("Correlation matrix test dataset = ", name)
36- if (assertEquals(expectedCorrelation, correlation,tol,"Correlations")) {
35+ output <- c("Pearson's Correlation matrix test dataset = ", name)
36+ if (assertEquals(expectedCorrelation, correlation,tol,"Pearson's Correlations")) {
37+ displayPadded(output, SUCCEEDED, WIDTH)
38+ } else {
39+ displayPadded(output, FAILED, WIDTH)
40+ }
41+ }
42+
43+ # Verify Spearman's correlation
44+ verifySpearmansCorrelation <- function(matrix, expectedCorrelation, name) {
45+ correlation <- cor(matrix, method="spearman")
46+ output <- c("Spearman's Correlation matrix test dataset = ", name)
47+ if (assertEquals(expectedCorrelation, correlation,tol,"Spearman's Correlations")) {
3748 displayPadded(output, SUCCEEDED, WIDTH)
3849 } else {
3950 displayPadded(output, FAILED, WIDTH)
@@ -94,6 +105,7 @@ longley <- matrix(c(60323,83.0,234289,2356,1590,107608,1947,
94105 70551,116.9,554894,4007,2827,130081,1962),
95106 nrow = 16, ncol = 7, byrow = TRUE)
96107
108+ # Pearson's
97109expectedCorrelation <- matrix(c(
98110 1.000000000000000, 0.9708985250610560, 0.9835516111796693, 0.5024980838759942,
99111 0.4573073999764817, 0.960390571594376, 0.9713294591921188,
@@ -110,7 +122,7 @@ expectedCorrelation <- matrix(c(
110122 0.971329459192119, 0.9911491900672053, 0.9952734837647849, 0.6682566045621746,
111123 0.4172451498349454, 0.993952846232926, 1.0000000000000000),
112124 nrow = 7, ncol = 7, byrow = TRUE)
113- verifyCorrelation (longley, expectedCorrelation, "longley")
125+ verifyPearsonsCorrelation (longley, expectedCorrelation, "longley")
114126
115127 expectedPValues <- c(
116128 4.38904690369668e-10,
@@ -121,6 +133,19 @@ expectedCorrelation <- matrix(c(
121133 3.95834476307755e-10, 1.114663916723657e-13, 1.332267629550188e-15, 0.00466039138541463, 0.1078477071581498, 7.771561172376096e-15)
122134 verifyPValues(longley, expectedPValues, "longley")
123135
136+ # Spearman's
137+ expectedCorrelation <- matrix(c(
138+ 1, 0.982352941176471, 0.985294117647059, 0.564705882352941, 0.2264705882352941, 0.976470588235294,
139+ 0.976470588235294, 0.982352941176471, 1, 0.997058823529412, 0.664705882352941, 0.2205882352941176,
140+ 0.997058823529412, 0.997058823529412, 0.985294117647059, 0.997058823529412, 1, 0.638235294117647,
141+ 0.2235294117647059, 0.9941176470588236, 0.9941176470588236, 0.564705882352941, 0.664705882352941,
142+ 0.638235294117647, 1, -0.3411764705882353, 0.685294117647059, 0.685294117647059, 0.2264705882352941,
143+ 0.2205882352941176, 0.2235294117647059, -0.3411764705882353, 1, 0.2264705882352941, 0.2264705882352941,
144+ 0.976470588235294, 0.997058823529412, 0.9941176470588236, 0.685294117647059, 0.2264705882352941, 1, 1,
145+ 0.976470588235294, 0.997058823529412, 0.9941176470588236, 0.685294117647059, 0.2264705882352941, 1, 1),
146+ nrow = 7, ncol = 7, byrow = TRUE)
147+ verifySpearmansCorrelation(longley, expectedCorrelation, "longley")
148+
124149 # Swiss Fertility
125150
126151 fertility <- matrix(c(80.2,17.0,15,12,9.96,
@@ -171,15 +196,14 @@ expectedCorrelation <- matrix(c(
171196 44.7,46.6,16,29,50.43,
172197 42.8,27.7,22,29,58.33),
173198 nrow = 47, ncol = 5, byrow = TRUE)
174-
175- expectedCorrelation <- matrix(c(
176- 1.0000000000000000, 0.3530791836199747, -0.6458827064572875, -0.6637888570350691, 0.4636847006517939,
177- 0.3530791836199747, 1.0000000000000000,-0.6865422086171366, -0.6395225189483201, 0.4010950530487398,
178- -0.6458827064572875, -0.6865422086171366, 1.0000000000000000, 0.6984152962884830, -0.5727418060641666,
179- -0.6637888570350691, -0.6395225189483201, 0.6984152962884830, 1.0000000000000000, -0.1538589170909148,
180- 0.4636847006517939, 0.4010950530487398, -0.5727418060641666, -0.1538589170909148, 1.0000000000000000),
199+ expectedCorrelation <- matrix(c(
200+ 1, 0.3530791836199747, -0.6458827064572875, -0.663788857035069, 0.463684700651794,
201+ 0.3530791836199747, 1, -0.6865422086171366, -0.63952251894832, 0.4010950530487398,
202+ -0.6458827064572875, -0.6865422086171366, 1, 0.698415296288483, -0.572741806064167,
203+ -0.663788857035069, -0.63952251894832, 0.698415296288483, 1, -0.1538589170909148,
204+ 0.463684700651794, 0.4010950530487398, -0.572741806064167, -0.1538589170909148, 1),
181205 nrow = 5, ncol = 5, byrow = TRUE)
182- verifyCorrelation (fertility, expectedCorrelation, "swiss fertility")
206+ verifyPearsonsCorrelation (fertility, expectedCorrelation, "swiss fertility")
183207
184208expectedPValues <- c(
185209 0.01491720061472623,
@@ -188,4 +212,14 @@ expectedPValues <- c(
188212 0.001028523190118147, 0.005204433539191644, 2.588307925380906e-05, 0.301807756132683)
189213verifyPValues(fertility, expectedPValues, "swiss fertility")
190214
215+ # Spearman's
216+ expectedCorrelation <- matrix(c(
217+ 1, 0.2426642769364176, -0.660902996352354, -0.443257690360988, 0.4136455623012432,
218+ 0.2426642769364176, 1, -0.598859938748963, -0.650463814145816, 0.2886878090882852,
219+ -0.660902996352354, -0.598859938748963, 1, 0.674603831406147, -0.4750575257171745,
220+ -0.443257690360988, -0.650463814145816, 0.674603831406147, 1, -0.1444163088302244,
221+ 0.4136455623012432, 0.2886878090882852, -0.4750575257171745, -0.1444163088302244, 1),
222+ nrow = 5, ncol = 5, byrow = TRUE)
223+ verifySpearmansCorrelation(fertility, expectedCorrelation, "swiss fertility")
224+
191225displayDashes(WIDTH)
0 commit comments