0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 classdef sosCorrelTest < handle
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068 properties
0069 sosObj
0070 name
0071 s1
0072 s2
0073 s1ColName
0074 s2ColName
0075 s1Col
0076 s2Col
0077 type
0078 runSpecificTest
0079 desiredpvalCondition
0080 desiredpvalConditionHandle
0081 desiredpval
0082 targVal
0083 tail
0084 lastp
0085 label
0086 end
0087
0088 methods
0089
0090
0091 function obj = sosCorrelTest(varargin)
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 p = inputParser;
0108 p.addParamValue('sosObj', 'null',...
0109 @(sosObj)strcmp(class(sosObj),'sos'));
0110 p.addParamValue('type','null', ...
0111 @(type)sosCorrelTest.validTestType(type));
0112 p.addParamValue('sample1','null', ...
0113 @(sample1)strcmp(class(sample1),'sample'));
0114 p.addParamValue('name','noname',@(name)ischar(name));
0115
0116
0117 p.KeepUnmatched = true;
0118 p.parse(varargin{:});
0119
0120
0121
0122
0123
0124
0125 sosObj = p.Results.sosObj;
0126
0127 if(strcmp(p.Results.type,'matchCorrel'))
0128 obj.constructMatchCorrelztest(sosObj,varargin{:})
0129 else
0130 error(['Specified <type>: ',p.Results.type, ...
0131 ' is not supported']);
0132 end
0133
0134 obj.lastp = NaN;
0135 obj.sosObj = p.Results.sosObj;
0136
0137
0138 if any(strcmp(p.UsingDefaults,'name'))
0139
0140 numTests = length(obj.sosObj.sosstattests);
0141
0142 obj.name = ['ztest_',num2str(numTests+1)];
0143 else
0144 obj.name = p.Results.name;
0145 end
0146
0147 end
0148
0149
0150
0151 function [userHypothesis, prob, label] = runTest(obj,varargin)
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163 [userHypothesis, prob, label] = obj.runSpecificTest(varargin);
0164 obj.lastp = prob;
0165 end
0166
0167
0168
0169 function [userHypothesis, prob, label] = ...
0170 runMatchCorrelztest(obj,varargin)
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182 varargin = varargin{1};
0183
0184 p = inputParser;
0185
0186 p.addParamValue('reportStyle','short', ...
0187 @(reportStyle)any([strcmp(reportStyle,'short') ...
0188 strcmp(reportStyle,'full') ...
0189 strcmp(reportStyle,'none')]));
0190 p.parse(varargin{:});
0191
0192
0193 reportStyle = p.Results.reportStyle;
0194
0195
0196
0197
0198 sumx1sq = sum((obj.s1.zdata{obj.s1Col}).^2);
0199 sumx1 = sum((obj.s1.zdata{obj.s1Col}));
0200 n1 = length(obj.s1.zdata{obj.s1Col});
0201
0202 sumx2sq = sum((obj.s2.zdata{obj.s2Col}).^2);
0203 sumx2 = sum((obj.s2.zdata{obj.s2Col}));
0204 n2 = length(obj.s2.zdata{obj.s2Col});
0205
0206 sumx1x2 = (obj.s1.zdata{obj.s1Col})' * (obj.s2.zdata{obj.s2Col});
0207
0208 ssx1 = sumx1sq - ((sumx1)^2)/n1;
0209 ssx2 = sumx2sq - ((sumx2)^2)/n2;
0210
0211 spx1x2 = sumx1x2 - sumx1*sumx2/n1;
0212
0213
0214 if(ssx1 == 0 && ssx2 == 0)
0215 cor = 1;
0216 elseif(ssx1 == 0 || ssx2 == 0)
0217 cor = 0;
0218 else
0219 cor = spx1x2/(sqrt(ssx1)*sqrt(ssx2));
0220 end
0221
0222
0223
0224 r= cor;
0225
0226 if n1 > 4
0227
0228
0229 z = (0.5*log((1+r)/(1-r)) - 0.5*log((1+obj.targVal)/(1-obj.targVal))) ...
0230 /(1/sqrt(n1-3));
0231 [h,prob,ci,stats] = ztest(z,0,1);
0232 else
0233
0234
0235
0236 end
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255 userHypothesis = obj.desiredpvalConditionHandle(...
0256 prob,obj.desiredpval);
0257
0258 label = [obj.s1.name, '{',obj.s1ColName, '}-', ...
0259 obj.s2.name, '{',obj.s2ColName, '}'];
0260
0261 if (isnan(userHypothesis))
0262 printHyp = 'N/A';
0263 elseif userHypothesis == 1
0264 printHyp = 'PASS';
0265 elseif userHypothesis == 0
0266 printHyp = 'FAIL';
0267 end
0268
0269
0270 if (strcmp(reportStyle,'short'))
0271 verbosePrint([' UserHyp: ', printHyp, '; ', label, ': ', ...
0272 'z[',obj.type,'](',num2str(n1),') = ', ...
0273 num2str(stats), ', p = ', num2str(prob), ...
0274 ' p-des: ',num2str(obj.desiredpval)], ...
0275 'sosCorrelTest_runMatchCorrelztest');
0276 elseif (strcmp(reportStyle,'full'))
0277 verbosePrint([' UserHyp: ', printHyp , ...
0278 '; ', label, ': ', ...
0279 'z[',obj.type,'](',num2str(n1),') = ', ...
0280 num2str(stats), ', p = ', num2str(prob), ...
0281 ' p-des: ',num2str(obj.desiredpval), ...
0282 ' cor = ', num2str(r),' targCor = ', num2str(obj.targVal)], ...
0283 'sosCorrelTest_runMatchCorrelztest');
0284 end
0285 end
0286
0287
0288
0289
0290
0291 function constructMatchCorrelztest(obj,sosObj,varargin)
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321 p = inputParser;
0322
0323 p.addRequired('sosObj', ...
0324 @(sosObj)strcmp(class(sosObj),'sos'));
0325 p.addParamValue('type','null', ...
0326 @(type)sosCorrelTest.validTestType(type));
0327 p.addParamValue('sample1','null', ...
0328 @(sample1)strcmp(class(sample1),'sample'));
0329 p.addParamValue('sample2','null', ...
0330 @(sample2)strcmp(class(sample2),'sample'));
0331
0332 p.addParamValue('s1ColName',NaN, ...
0333 @(s1ColName)ischar(s1ColName));
0334 p.addParamValue('s2ColName',NaN, ...
0335 @(s2ColName)ischar(s2ColName));
0336 p.addParamValue('desiredpvalCondition','N/A', ...
0337 @(desiredpvalCondition)any([strcmp(desiredpvalCondition,'<='), ...
0338 strcmp(desiredpvalCondition,'=>'), ...
0339 strcmp(desiredpvalCondition,'N/A')]));
0340 p.addParamValue('desiredpval', 0.05, ...
0341 @(desiredpval)validateattributes(desiredpval, ...
0342 {'numeric'}, ...
0343 {'scalar', 'positive', '>=', 0, '<=', 1}));
0344 p.addParamValue('tail','both', ...
0345 @(tail)any([strcmp(tail,'both'), strcmp(tail,'left'),strcmp(tail,'right')]));
0346 p.addParamValue('name','noname',@(name)ischar(name));
0347 p.addParamValue('targVal',0.0, ...
0348 @(targVal)validateattributes(targVal, ...
0349 {'numeric'}, ...
0350 {'scalar', '>=', -1, '<=', 1}));
0351 p.parse(sosObj,varargin{:});
0352
0353
0354 sample1 = p.Results.sample1;
0355 sample2 = p.Results.sample2;
0356 s1ColName = p.Results.s1ColName;
0357 s2ColName = p.Results.s2ColName;
0358 obj.desiredpvalCondition = p.Results.desiredpvalCondition;
0359
0360
0361 if strcmp(obj.desiredpvalCondition,'N/A')
0362 obj.desiredpvalConditionHandle = @sosCorrelTest.returnNaN;
0363 elseif strcmp(obj.desiredpvalCondition,'<=')
0364 obj.desiredpvalConditionHandle = @le;
0365 elseif strcmp(obj.desiredpvalCondition,'=>')
0366 obj.desiredpvalConditionHandle = @ge;
0367 end
0368
0369
0370 if strcmp(obj.desiredpvalCondition,'N/A')
0371 obj.desiredpval = NaN;
0372 else
0373 obj.desiredpval = p.Results.desiredpval;
0374 end
0375
0376
0377
0378
0379 present1 = sosObj.containsSample(sample1);
0380 if (present1 == 0 )
0381 error('sos sosObject does not contain sample1');
0382 end
0383
0384 present2 = sosObj.containsSample(sample2);
0385 if (present2 == 0 )
0386 error('sos sosObject does not contain sample2');
0387 end
0388
0389 col1 = sample1.colName2colNum(s1ColName);
0390 if(col1 == -1)
0391 error('<s1ColName> not a column of data in <sample1>');
0392 end
0393
0394 col2 = sample1.colName2colNum(s2ColName);
0395 if(col2 == -1)
0396 error('<s2ColName> not a column of data in <sample2>');
0397 end
0398
0399 if isempty(sample1.data)
0400 error('sample 1 does not contain items - did you fill it yet?');
0401 end
0402
0403 if isempty(sample2.data)
0404 error('sample 2 does not contain items - did you fill it yet?');
0405 end
0406
0407
0408
0409 if (length(sample1.data{col1}) ~= length(sample2.data{col2}))
0410 error('Sample 1 and Sample 2 must have the same number of observations for a paired comparison');
0411 end
0412
0413 if sample1.n < 4
0414
0415
0416 error('The sosCorrelTest requires at least 4 items per sample');
0417 end
0418
0419
0420 obj.s1 = sample1;
0421 obj.s2 = sample2;
0422 obj.s1ColName = s1ColName;
0423 obj.s2ColName = s2ColName;
0424 obj.s1Col = col1;
0425 obj.s2Col = col2;
0426 obj.type = p.Results.type;
0427 obj.tail = p.Results.tail;
0428 obj.targVal = p.Results.targVal;
0429
0430 obj.runSpecificTest = @obj.runMatchCorrelztest;
0431
0432 obj.label = [obj.s1.name, '{',obj.s1ColName, '}-', ...
0433 obj.s2.name, '{',obj.s2ColName, '}', ...
0434 ':z[',obj.type,']'];
0435
0436
0437 end
0438
0439
0440 end
0441
0442
0443 methods (Static)
0444
0445
0446 function userHypothesis = returnNaN(~,~)
0447
0448 userHypothesis = NaN;
0449 end
0450
0451
0452 function flag = validTestType(str)
0453
0454
0455 flag = 0;
0456
0457 if(ischar(str) == false)
0458 error('<Type> must be "matchCorrel"');
0459 end
0460
0461 if (strcmp(str,'matchCorrel'))
0462 flag = 1;
0463 else
0464 error('<Type> must be "matchCorrel"');
0465 end
0466
0467 end
0468 end
0469
0470 end
0471