From 1884436473cda80c4e6e7b70aa48a003877edd94 Mon Sep 17 00:00:00 2001 From: Ernesto Arbitrio Date: Fri, 3 Nov 2023 13:16:38 -0400 Subject: [PATCH] fix bases calculation for pw sig means --- src/cr/cube/matrix/measure.py | 6 +- tests/fixtures/means-cat-x-mr-2.json | 2479 +++++++++++++++++ .../integration/test_pairwise_significance.py | 90 +- tests/unit/matrix/test_measure.py | 4 +- 4 files changed, 2562 insertions(+), 17 deletions(-) create mode 100644 tests/fixtures/means-cat-x-mr-2.json diff --git a/src/cr/cube/matrix/measure.py b/src/cr/cube/matrix/measure.py index 7ff860e91..53e868394 100644 --- a/src/cr/cube/matrix/measure.py +++ b/src/cr/cube/matrix/measure.py @@ -1389,13 +1389,11 @@ def t_stats(self): means = self._cube_measures.cube_means.means variance = np.power(self._cube_measures.cube_stddev.stddev, 2) - col_bases = self._cube_measures.unweighted_cube_counts.column_bases + col_bases = self._cube_measures.unweighted_cube_counts.counts idx = self._selected_column_idx - ref_means = np.broadcast_to(means[:, [idx]], means.shape) ref_variance = np.broadcast_to(variance[:, [idx]], variance.shape) ref_col_bases = np.broadcast_to(col_bases[:, [idx]], col_bases.shape) - with np.errstate(divide="ignore", invalid="ignore"): return (means - ref_means) / np.sqrt( (variance / col_bases) + (ref_variance / ref_col_bases) @@ -1431,7 +1429,7 @@ def _df(self): df = ( (s1/N1) + (s2/N2) )^2 / ( (s1/N1)^2/(N1-1) + (s2/N2)^2/(N2-1) ) """ variance = np.power(self._cube_measures.cube_stddev.stddev, 2) - col_bases = self._cube_measures.unweighted_cube_counts.column_bases + col_bases = self._cube_measures.unweighted_cube_counts.counts idx = self._selected_column_idx ref_variance = np.broadcast_to(variance[:, [idx]], variance.shape) diff --git a/tests/fixtures/means-cat-x-mr-2.json b/tests/fixtures/means-cat-x-mr-2.json new file mode 100644 index 000000000..f4e24f779 --- /dev/null +++ b/tests/fixtures/means-cat-x-mr-2.json @@ -0,0 +1,2479 @@ +{ + "result": { + "n": 39248, + "counts": [ + 24358, + 8315, + 2557, + 12634, + 17750, + 4846, + 15840, + 15252, + 4138, + 966, + 470, + 179, + 642, + 714, + 259, + 560, + 760, + 295, + 60, + 24, + 21, + 48, + 37, + 20, + 35, + 42, + 28, + 30, + 13, + 13, + 27, + 12, + 17, + 23, + 17, + 16, + 90, + 55, + 24, + 69, + 74, + 26, + 35, + 101, + 33, + 96, + 36, + 22, + 76, + 48, + 30, + 42, + 78, + 34, + 179, + 70, + 33, + 111, + 107, + 64, + 129, + 108, + 45, + 90, + 29, + 26, + 45, + 61, + 39, + 68, + 48, + 29, + 36, + 22, + 9, + 24, + 22, + 21, + 22, + 28, + 17, + 58, + 19, + 12, + 39, + 29, + 21, + 27, + 42, + 20, + 69, + 52, + 16, + 46, + 68, + 23, + 65, + 57, + 15, + 119, + 32, + 33, + 80, + 55, + 49, + 88, + 50, + 46, + 9, + 6, + 2, + 5, + 11, + 1, + 9, + 6, + 2, + 95, + 33, + 8, + 46, + 61, + 29, + 46, + 62, + 28, + 127, + 71, + 31, + 78, + 103, + 48, + 66, + 119, + 44, + 294, + 181, + 100, + 170, + 278, + 127, + 167, + 276, + 132, + 40, + 10, + 8, + 20, + 27, + 11, + 23, + 27, + 8 + ], + "dimensions": [ + { + "derived": false, + "references": { + "alias": "profile_ethnicity", + "name": "Ethnicity", + "header_order": 134, + "description": "Ethnicity" + }, + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "id": 1, + "name": "White British", + "missing": false + }, + { + "numeric_value": 2, + "id": 2, + "name": "Any other white background", + "missing": false + }, + { + "numeric_value": 3, + "id": 3, + "name": "White and Black Caribbean", + "missing": false + }, + { + "numeric_value": 4, + "id": 4, + "name": "White and Black African", + "missing": false + }, + { + "numeric_value": 5, + "id": 5, + "name": "White and Asian", + "missing": false + }, + { + "numeric_value": 6, + "id": 6, + "name": "Any other mixed background", + "missing": false + }, + { + "numeric_value": 7, + "id": 7, + "name": "Indian", + "missing": false + }, + { + "numeric_value": 8, + "id": 8, + "name": "Pakistani", + "missing": false + }, + { + "numeric_value": 9, + "id": 9, + "name": "Bangladeshi", + "missing": false + }, + { + "numeric_value": 10, + "id": 10, + "name": "Any other Asian background", + "missing": false + }, + { + "numeric_value": 11, + "id": 11, + "name": "Black Caribbean", + "missing": false + }, + { + "numeric_value": 12, + "id": 12, + "name": "Black African", + "missing": false + }, + { + "numeric_value": 13, + "id": 13, + "name": "Any other black background", + "missing": false + }, + { + "numeric_value": 14, + "id": 14, + "name": "Chinese", + "missing": false + }, + { + "numeric_value": 15, + "id": 15, + "name": "Other ethnic group", + "missing": false + }, + { + "numeric_value": 16, + "id": 16, + "name": "Prefer not to say", + "missing": false + }, + { + "numeric_value": null, + "id": -1, + "missing": true, + "name": "No Data" + } + ] + } + }, + { + "type": { + "class": "enum", + "elements": [ + { + "id": 1, + "value": { + "id": "00000", + "references": { + "alias": "segments_bbc__3", + "name": "Segments, BBC" + }, + "derived": false + }, + "missing": false + }, + { + "id": 2, + "value": { + "id": "00001", + "references": { + "alias": "segments_channel4__3", + "name": "Segments, Channel4" + }, + "derived": false + }, + "missing": false + }, + { + "id": 3, + "value": { + "id": "00002", + "references": { + "alias": "segments_itv__2", + "name": "Segments, ITV" + }, + "derived": false + }, + "missing": false + } + ], + "subtype": { + "class": "variable" + } + }, + "references": { + "subreferences": [ + { + "alias": "segments_bbc__3", + "name": "Segments, BBC" + }, + { + "alias": "segments_channel4__3", + "name": "Segments, Channel4" + }, + { + "alias": "segments_itv__2", + "name": "Segments, ITV" + } + ], + "view": { + "show_counts": false, + "include_missing": false, + "include_noneoftheabove": false, + "summary_statistic": "mean" + }, + "name": "Segments MR", + "alias": "Segments_MR", + "uniform_basis": false + }, + "derived": true + }, + { + "type": { + "class": "categorical", + "ordinal": false, + "categories": [ + { + "id": 1, + "name": "Selected", + "numeric_value": 1, + "selected": true, + "missing": false + }, + { + "id": 0, + "name": "Other", + "numeric_value": 0, + "missing": false + }, + { + "id": -1, + "name": "No Data", + "numeric_value": null, + "missing": true + } + ], + "subvariables": [ + "00000", + "00001", + "00002" + ] + }, + "references": { + "subreferences": [ + { + "alias": "segments_bbc__3", + "name": "Segments, BBC" + }, + { + "alias": "segments_channel4__3", + "name": "Segments, Channel4" + }, + { + "alias": "segments_itv__2", + "name": "Segments, ITV" + } + ], + "view": { + "show_counts": false, + "include_missing": false, + "include_noneoftheabove": false, + "summary_statistic": "mean" + }, + "name": "Segments MR", + "alias": "Segments_MR", + "uniform_basis": false + }, + "derived": true + } + ], + "measures": { + "count": { + "metadata": { + "type": { + "class": "numeric", + "integer": false, + "missing_reasons": { + "No Data": -1 + }, + "missing_rules": {} + }, + "references": {}, + "derived": true + }, + "data": [ + 22420.867510858196, + 8776.956276591389, + 2944.7911127863645, + 13907.718088721213, + 15493.138824409154, + 4741.757987106615, + 13908.144138381494, + 15332.054118865624, + 4902.416642989878, + 1036.0065828136703, + 550.8902956762648, + 235.90027552636593, + 792.4490815044409, + 727.3149671396694, + 303.0331053721899, + 584.3600023164498, + 848.6295091901715, + 389.8076425096756, + 85.88583462122992, + 38.03511716222851, + 35.50798007438883, + 71.42964341865927, + 52.82665021157563, + 35.17263822761239, + 44.0155986783896, + 69.80957380833662, + 45.60375937112106, + 46.33639379066931, + 19.736527123385528, + 24.19021111396291, + 39.59329469697815, + 19.401185276609084, + 31.268652054430525, + 28.335709061050743, + 31.34676409181007, + 30.58065887515694, + 135.64776820030627, + 93.72524562718195, + 39.20468619227784, + 126.0228724125042, + 100.57468538293818, + 41.98014222432372, + 51.49697189647077, + 161.33512750024084, + 55.74560062305449, + 136.5608657538606, + 54.3039760843794, + 35.518009504449005, + 115.07400172849162, + 68.93956018816384, + 42.36928942603359, + 54.55973627826076, + 115.1925334276242, + 56.630581636804095, + 237.291923858588, + 103.65043771694143, + 54.82127972163268, + 150.42691997761608, + 141.4752767338789, + 103.8614445856672, + 170.62679208883918, + 148.68239610438397, + 76.45445310393889, + 145.2093705274819, + 52.40967191760263, + 50.36380594187521, + 75.17246755356543, + 97.52261338174485, + 75.28776745164943, + 117.8813931692285, + 73.08690088968642, + 57.01455432804482, + 62.86116361257732, + 42.253345907286025, + 16.777232403091496, + 43.19074755467999, + 38.94078321594097, + 39.76021115233387, + 37.550243698754926, + 52.057578573237, + 32.2839196509629, + 92.54081869983021, + 33.93500588136563, + 23.028177977648557, + 65.64664907468337, + 44.59376092911765, + 39.26359255504343, + 39.26657826785087, + 71.97961118803835, + 38.25781310295523, + 72.44072159599254, + 58.30879980675127, + 24.06740177449277, + 51.895790107823096, + 71.33701754319033, + 31.584115526223208, + 67.99296275779841, + 62.005752012881416, + 24.818208406556746, + 178.0352226776287, + 53.18579681448258, + 62.852292106687024, + 129.5807953173577, + 79.01693072542783, + 85.47558555601285, + 133.74837256698606, + 73.20432377378285, + 87.12061525802949, + 11.895297870084576, + 10.122018938765578, + 2.910493140526511, + 9.023888779155795, + 15.480693694079175, + 0.423227476141694, + 13.50540386199631, + 7.837010263385753, + 3.585395823994599, + 150.7399120401076, + 50.295352784243086, + 12.587510039273361, + 76.17378192749732, + 90.74459057794881, + 46.70440235817798, + 69.56566129186439, + 97.38060095626727, + 46.67651261549249, + 141.8231756903773, + 77.48832717735239, + 43.97352891598442, + 103.44999897528534, + 102.24015651170036, + 57.59487629672832, + 66.20762420416574, + 130.5946544238207, + 66.4827531557276, + 303.5409356784622, + 182.24833356000516, + 129.8567167064069, + 201.66912910991533, + 259.9851137798866, + 153.9917430550734, + 162.36978883392908, + 270.3226780344577, + 182.95351907648808, + 34.744365766703474, + 11.823858999645013, + 9.853034245718483, + 23.398860358074277, + 21.312664925746294, + 11.709733728246398, + 24.124212133355577, + 22.88901467301164, + 9.408032205699758 + ], + "n_missing": 1754 + }, + "valid_count_unweighted": { + "metadata": { + "type": { + "class": "numeric", + "integer": false, + "missing_reasons": { + "No Data": -1 + }, + "missing_rules": {} + }, + "references": { + "alias": "profile_gender", + "name": "Gender", + "header_order": 0, + "description": "Gender", + "view": { + "column_width": null, + "show_numeric_values": false, + "show_counts": false, + "include_missing": false, + "summary_statistic": "mean", + "transform": { + "insertions": [] + } + } + }, + "derived": true + }, + "data": [ + 24358, + 8315, + 2557, + 12634, + 17750, + 4846, + 15840, + 15252, + 4138, + 966, + 470, + 179, + 642, + 714, + 259, + 560, + 760, + 295, + 60, + 24, + 21, + 48, + 37, + 20, + 35, + 42, + 28, + 30, + 13, + 13, + 27, + 12, + 17, + 23, + 17, + 16, + 90, + 55, + 24, + 69, + 74, + 26, + 35, + 101, + 33, + 96, + 36, + 22, + 76, + 48, + 30, + 42, + 78, + 34, + 179, + 70, + 33, + 111, + 107, + 64, + 129, + 108, + 45, + 90, + 29, + 26, + 45, + 61, + 39, + 68, + 48, + 29, + 36, + 22, + 9, + 24, + 22, + 21, + 22, + 28, + 17, + 58, + 19, + 12, + 39, + 29, + 21, + 27, + 42, + 20, + 69, + 52, + 16, + 46, + 68, + 23, + 65, + 57, + 15, + 119, + 32, + 33, + 80, + 55, + 49, + 88, + 50, + 46, + 9, + 6, + 2, + 5, + 11, + 1, + 9, + 6, + 2, + 95, + 33, + 8, + 46, + 61, + 29, + 46, + 62, + 28, + 127, + 71, + 31, + 78, + 103, + 48, + 66, + 119, + 44, + 294, + 181, + 100, + 170, + 278, + 127, + 167, + 276, + 132, + 40, + 10, + 8, + 20, + 27, + 11, + 23, + 27, + 8 + ], + "n_missing": 1754 + }, + "stddev": { + "metadata": { + "type": { + "class": "numeric", + "integer": false, + "missing_reasons": { + "No Data": -1, + "NaN": -8 + }, + "missing_rules": {} + }, + "references": { + "alias": "profile_gender", + "name": "Gender", + "header_order": 0, + "description": "Gender", + "view": { + "column_width": null, + "show_numeric_values": false, + "show_counts": false, + "include_missing": false, + "summary_statistic": "mean", + "transform": { + "insertions": [] + } + } + }, + "derived": true + }, + "data": [ + 0.498513467050486, + 0.495545157522054, + 0.496965275678542, + 0.495598409577837, + 0.499270163447247, + 0.496734134413563, + 0.483452860360986, + 0.499919882883813, + 0.500058586954618, + 0.498468706611769, + 0.492860660707421, + 0.487771562905782, + 0.496053535690651, + 0.497805142874461, + 0.490741927150103, + 0.484894161820097, + 0.50032657531442, + 0.487026947455889, + 0.492210971438244, + 0.4928735898193, + 0.511387378285776, + 0.494159960504586, + 0.48281754390877, + 0.51297935749995, + 0.440148362097915, + 0.484743350798848, + 0.49371571605582, + 0.508416723780096, + 0.511672686182658, + 0.506115818999192, + 0.509046144086547, + 0.518859669033129, + 0.489422313604061, + 0.489032661923063, + 0.452152459216098, + 0.513001085038617, + 0.50192228189989, + 0.433681129580503, + 0.441913836269665, + 0.487587108577777, + 0.480113923495274, + 0.481730135128734, + 0.424525630843473, + 0.491225255517034, + 0.492815737847471, + 0.470109847558454, + 0.472411937481712, + 0.488533531646406, + 0.445645254722705, + 0.500828415052526, + 0.470861094847314, + 0.487915278804562, + 0.472722285812349, + 0.456892212505282, + 0.499971143809471, + 0.493717314056183, + 0.476760183621257, + 0.50187485196049, + 0.502252702820625, + 0.489006199812121, + 0.501290944307111, + 0.500344640495668, + 0.499528476355939, + 0.498559962907411, + 0.508836070656932, + 0.357293832675336, + 0.505518661441074, + 0.496988621126116, + 0.460422301943536, + 0.499053091287128, + 0.493080842942549, + 0.486573759773255, + 0.505379372224677, + 0.510476165596272, + 0.528331292109569, + 0.507713947886514, + 0.507338760707833, + 0.510777254288315, + 0.509973458132352, + 0.505300024533291, + 0.490165571799485, + 0.501094769753986, + 0.512882532220579, + 0.515701737471263, + 0.496895807211229, + 0.508770252830756, + 0.508618247874178, + 0.508948712340428, + 0.505902254850381, + 0.499000869070525, + 0.487000216347697, + 0.500864331810068, + 0.275573281277674, + 0.461090089333784, + 0.503715142705332, + 0.319101345512273, + 0.472385595988302, + 0.497754917799302, + 0.447822006276227, + 0.501721292133532, + 0.507933765314141, + 0.497041823367695, + 0.471432617487572, + 0.472361041144038, + 0.5048146517285, + 0.502258948819146, + 0.504063425410092, + 0.488097998792441, + 0.454813654965387, + 0.545357267933817, + 0, + 0, + 0.522354813306002, + { + "?": -8 + }, + 0.510568657268466, + 0.247597960850538, + 0.694466336912908, + 0.488541398046357, + 0.383286524243681, + 0.5031089404377, + 0.433641981048689, + 0.485095947783469, + 0.499646417172708, + 0.499504911921109, + 0.438647258963965, + 0.485660175145657, + 0.499098301569708, + 0.469433179450739, + 0.507514015168006, + 0.502613789997736, + 0.467060503010476, + 0.504705544096774, + 0.498449528854086, + 0.4839194056069, + 0.481334090303766, + 0.499223030754097, + 0.499630788285626, + 0.500117293033917, + 0.501477107066086, + 0.500717467470915, + 0.501842388004001, + 0.494477140928321, + 0.500641265625434, + 0.500258867078942, + 0.505549317238422, + 0.525379271839324, + 0.462770114730869, + 0.509295698269478, + 0.508083708273982, + 0.478979203482833, + 0.510052343800291, + 0.507837451185492, + 0.515841426551144 + ], + "n_missing": 1754 + }, + "mean": { + "metadata": { + "type": { + "class": "numeric", + "integer": false, + "missing_reasons": { + "No Data": -1 + }, + "missing_rules": {} + }, + "references": { + "alias": "profile_gender", + "name": "Gender", + "header_order": 0, + "description": "Gender", + "view": { + "column_width": null, + "show_numeric_values": false, + "show_counts": false, + "include_missing": false, + "summary_statistic": "mean", + "transform": { + "insertions": [] + } + } + }, + "derived": true + }, + "data": [ + 1.53865909724669, + 1.566817135743159, + 1.555875776206192, + 1.566344988046905, + 1.527264396396688, + 1.557498842758316, + 1.627624791009776, + 1.490176724684016, + 1.498643743460079, + 1.542263044591918, + 1.587207811486254, + 1.615793223304963, + 1.565682379275522, + 1.550369757803928, + 1.600509687997036, + 1.623683115940688, + 1.50164172082131, + 1.616656779221801, + 1.608472209663226, + 1.63113913771078, + 1.530596441280782, + 1.604371020024217, + 1.652274635834046, + 1.496906484202176, + 1.748605267019584, + 1.64359161984008, + 1.377728438006362, + 1.511344035790821, + 1.591269991098042, + 1.616408211812279, + 1.478335382185689, + 1.556738495426494, + 1.656703688748048, + 1.354243320297185, + 1.739966965618352, + 1.557253985253229, + 1.529549965404054, + 1.75561749135172, + 1.750697336627326, + 1.625316942266684, + 1.650351628129857, + 1.663895126261756, + 1.773728312525726, + 1.605294252201898, + 1.620383824665399, + 1.676914806083615, + 1.681731154178602, + 1.648940921417441, + 1.732407961996027, + 1.566306145669604, + 1.688891948956403, + 1.63269064533286, + 1.671460181451314, + 1.717690667138766, + 1.462246295553606, + 1.598617714453283, + 1.327989493447251, + 1.519768850431613, + 1.509988585070903, + 1.379131075491725, + 1.525600661923324, + 1.455578746555689, + 1.422434650115946, + 1.435194415421307, + 1.49626808011145, + 1.856722685240997, + 1.511390882447826, + 1.416027070299682, + 1.708439227272638, + 1.567886454633392, + 1.390745874488262, + 1.646321249830088, + 1.541065379627662, + 1.535480962578335, + 1.456630314961987, + 1.554471163546364, + 1.565628091114126, + 1.460883610770278, + 1.541815890852394, + 1.43843096192667, + 1.654501741006704, + 1.556861872617102, + 1.528216406737292, + 1.421170596824363, + 1.597084837006195, + 1.508865343929789, + 1.439782316080968, + 1.476233065777929, + 1.512515942242668, + 1.615966486682652, + 1.62754616503567, + 1.562922567974025, + 1.922854178447476, + 1.704982333720088, + 1.501537213034956, + 1.890642463487458, + 1.674025564743438, + 1.581158534608677, + 1.750649331367721, + 1.519775613609744, + 1.508104766475207, + 1.602155796991527, + 1.674726632426789, + 1.324125430661336, + 1.519050970949163, + 1.475452387450716, + 1.53164904264852, + 1.630151705868878, + 1.757154582575498, + 1.546417043491255, + 2, + 2, + 1.544163426003997, + 1, + 1.635218822539453, + 1.945996309572415, + 1.405881705033594, + 1.617642027270137, + 1.827937821650013, + 1.668882266076691, + 1.75698749979106, + 1.636160180901054, + 1.594667625202205, + 1.576934180437454, + 1.746357447965068, + 1.650193119194233, + 1.446499572696587, + 1.319068361592517, + 1.472829254203393, + 1.475138302031963, + 1.315683955841796, + 1.475934660719453, + 1.572886832895982, + 1.366621249235839, + 1.346432408944629, + 1.459700310155678, + 1.541812100833434, + 1.548824853060128, + 1.500031990662239, + 1.513560273409687, + 1.488284401034714, + 1.583405483761634, + 1.483676851696885, + 1.459540552095519, + 1.471550208370338, + 1.539736112661196, + 1.249773416084579, + 1.440108455204471, + 1.537576874315909, + 1.296443167125358, + 1.465976130600991, + 1.459343716029884, + 1.36896882118377 + ], + "n_missing": 1754 + }, + "valid_count_weighted": { + "metadata": { + "type": { + "class": "numeric", + "integer": false, + "missing_reasons": { + "No Data": -1 + }, + "missing_rules": {} + }, + "references": { + "alias": "profile_gender", + "name": "Gender", + "header_order": 0, + "description": "Gender", + "view": { + "column_width": null, + "show_numeric_values": false, + "show_counts": false, + "include_missing": false, + "summary_statistic": "mean", + "transform": { + "insertions": [] + } + } + }, + "derived": true + }, + "data": [ + 22420.867510858196, + 8776.956276591389, + 2944.7911127863645, + 13907.718088721213, + 15493.138824409154, + 4741.757987106615, + 13908.144138381494, + 15332.054118865624, + 4902.416642989878, + 1036.0065828136703, + 550.8902956762648, + 235.90027552636593, + 792.4490815044409, + 727.3149671396694, + 303.0331053721899, + 584.3600023164498, + 848.6295091901715, + 389.8076425096756, + 85.88583462122992, + 38.03511716222851, + 35.50798007438883, + 71.42964341865927, + 52.82665021157563, + 35.17263822761239, + 44.0155986783896, + 69.80957380833662, + 45.60375937112106, + 46.33639379066931, + 19.736527123385528, + 24.19021111396291, + 39.59329469697815, + 19.401185276609084, + 31.268652054430525, + 28.335709061050743, + 31.34676409181007, + 30.58065887515694, + 135.64776820030627, + 93.72524562718195, + 39.20468619227784, + 126.0228724125042, + 100.57468538293818, + 41.98014222432372, + 51.49697189647077, + 161.33512750024084, + 55.74560062305449, + 136.5608657538606, + 54.3039760843794, + 35.518009504449005, + 115.07400172849162, + 68.93956018816384, + 42.36928942603359, + 54.55973627826076, + 115.1925334276242, + 56.630581636804095, + 237.291923858588, + 103.65043771694143, + 54.82127972163268, + 150.42691997761608, + 141.4752767338789, + 103.8614445856672, + 170.62679208883918, + 148.68239610438397, + 76.45445310393889, + 145.2093705274819, + 52.40967191760263, + 50.36380594187521, + 75.17246755356543, + 97.52261338174485, + 75.28776745164943, + 117.8813931692285, + 73.08690088968642, + 57.01455432804482, + 62.86116361257732, + 42.253345907286025, + 16.777232403091496, + 43.19074755467999, + 38.94078321594097, + 39.76021115233387, + 37.550243698754926, + 52.057578573237, + 32.2839196509629, + 92.54081869983021, + 33.93500588136563, + 23.028177977648557, + 65.64664907468337, + 44.59376092911765, + 39.26359255504343, + 39.26657826785087, + 71.97961118803835, + 38.25781310295523, + 72.44072159599254, + 58.30879980675127, + 24.06740177449277, + 51.895790107823096, + 71.33701754319033, + 31.584115526223208, + 67.99296275779841, + 62.005752012881416, + 24.818208406556746, + 178.0352226776287, + 53.18579681448258, + 62.852292106687024, + 129.5807953173577, + 79.01693072542783, + 85.47558555601285, + 133.74837256698606, + 73.20432377378285, + 87.12061525802949, + 11.895297870084576, + 10.122018938765578, + 2.910493140526511, + 9.023888779155795, + 15.480693694079175, + 0.423227476141694, + 13.50540386199631, + 7.837010263385753, + 3.585395823994599, + 150.7399120401076, + 50.295352784243086, + 12.587510039273361, + 76.17378192749732, + 90.74459057794881, + 46.70440235817798, + 69.56566129186439, + 97.38060095626727, + 46.67651261549249, + 141.8231756903773, + 77.48832717735239, + 43.97352891598442, + 103.44999897528534, + 102.24015651170036, + 57.59487629672832, + 66.20762420416574, + 130.5946544238207, + 66.4827531557276, + 303.5409356784622, + 182.24833356000516, + 129.8567167064069, + 201.66912910991533, + 259.9851137798866, + 153.9917430550734, + 162.36978883392908, + 270.3226780344577, + 182.95351907648808, + 34.744365766703474, + 11.823858999645013, + 9.853034245718483, + 23.398860358074277, + 21.312664925746294, + 11.709733728246398, + 24.124212133355577, + 22.88901467301164, + 9.408032205699758 + ], + "n_missing": 1754 + }, + "overlap": { + "metadata": { + "type": { + "class": "numeric", + "integer": true, + "missing_reasons": { + "No Data": -1 + }, + "missing_rules": {}, + "subvariables": [ + "00000", + "00001", + "00002" + ] + }, + "references": { + "subreferences": [ + { + "alias": "segments_bbc__3", + "name": "Segments, BBC" + }, + { + "alias": "segments_channel4__3", + "name": "Segments, Channel4" + }, + { + "alias": "segments_itv__2", + "name": "Segments, ITV" + } + ], + "view": { + "show_counts": false, + "include_missing": false, + "include_noneoftheabove": false, + "summary_statistic": "mean" + }, + "name": "Segments MR", + "alias": "Segments_MR", + "uniform_basis": false + }, + "derived": true + }, + "data": [ + 24358, + 9895, + 13069, + 0, + 2342, + 2249, + 0, + 397, + 522, + 9895, + 12634, + 6678, + 12167, + 0, + 7606, + 2296, + 0, + 1556, + 13069, + 6678, + 15840, + 9571, + 5101, + 0, + 1718, + 855, + 0, + 966, + 446, + 429, + 0, + 164, + 105, + 0, + 32, + 26, + 446, + 642, + 278, + 424, + 0, + 234, + 96, + 0, + 48, + 429, + 278, + 560, + 419, + 296, + 0, + 118, + 68, + 0, + 60, + 28, + 25, + 0, + 14, + 7, + 0, + 6, + 3, + 28, + 48, + 18, + 28, + 0, + 16, + 4, + 0, + 1, + 25, + 18, + 35, + 27, + 23, + 0, + 8, + 7, + 0, + 30, + 20, + 16, + 0, + 7, + 6, + 0, + 0, + 1, + 20, + 27, + 16, + 9, + 0, + 5, + 1, + 0, + 2, + 16, + 16, + 23, + 11, + 10, + 0, + 3, + 1, + 0, + 90, + 42, + 25, + 0, + 22, + 6, + 0, + 5, + 4, + 42, + 69, + 16, + 43, + 0, + 16, + 5, + 0, + 3, + 25, + 16, + 35, + 56, + 43, + 0, + 9, + 10, + 0, + 96, + 59, + 29, + 0, + 13, + 10, + 0, + 4, + 3, + 59, + 76, + 28, + 30, + 0, + 10, + 7, + 0, + 4, + 29, + 28, + 42, + 57, + 40, + 0, + 10, + 8, + 0, + 179, + 88, + 101, + 0, + 20, + 23, + 0, + 3, + 5, + 88, + 111, + 71, + 63, + 0, + 42, + 28, + 0, + 16, + 101, + 71, + 129, + 62, + 33, + 0, + 16, + 7, + 0, + 90, + 37, + 45, + 0, + 7, + 15, + 0, + 1, + 8, + 37, + 45, + 23, + 39, + 0, + 33, + 14, + 0, + 12, + 45, + 23, + 68, + 34, + 18, + 0, + 11, + 4, + 0, + 36, + 17, + 17, + 0, + 7, + 5, + 0, + 0, + 0, + 17, + 24, + 12, + 8, + 0, + 7, + 11, + 0, + 3, + 17, + 12, + 22, + 13, + 7, + 0, + 6, + 5, + 0, + 58, + 26, + 23, + 0, + 12, + 2, + 0, + 1, + 2, + 26, + 39, + 14, + 23, + 0, + 9, + 9, + 0, + 4, + 23, + 14, + 27, + 24, + 20, + 0, + 11, + 5, + 0, + 69, + 29, + 36, + 0, + 13, + 23, + 0, + 4, + 6, + 29, + 46, + 31, + 32, + 0, + 26, + 8, + 0, + 8, + 36, + 31, + 65, + 31, + 15, + 0, + 2, + 0, + 0, + 119, + 60, + 73, + 0, + 17, + 12, + 0, + 3, + 3, + 60, + 80, + 44, + 43, + 0, + 32, + 16, + 0, + 12, + 73, + 44, + 88, + 34, + 25, + 0, + 12, + 11, + 0, + 9, + 3, + 5, + 0, + 1, + 3, + 0, + 1, + 1, + 3, + 5, + 3, + 6, + 0, + 6, + 0, + 0, + 0, + 5, + 3, + 9, + 4, + 2, + 0, + 0, + 0, + 0, + 95, + 38, + 40, + 0, + 6, + 5, + 0, + 2, + 1, + 38, + 46, + 22, + 38, + 0, + 16, + 19, + 0, + 8, + 40, + 22, + 46, + 37, + 21, + 0, + 18, + 3, + 0, + 127, + 55, + 50, + 0, + 18, + 13, + 0, + 5, + 3, + 55, + 78, + 33, + 52, + 0, + 25, + 20, + 0, + 8, + 50, + 33, + 66, + 61, + 35, + 0, + 16, + 10, + 0, + 294, + 116, + 124, + 0, + 43, + 31, + 0, + 11, + 12, + 116, + 170, + 66, + 132, + 0, + 74, + 46, + 0, + 27, + 124, + 66, + 167, + 128, + 75, + 0, + 42, + 29, + 0, + 40, + 14, + 17, + 0, + 4, + 3, + 0, + 2, + 3, + 14, + 20, + 10, + 21, + 0, + 10, + 5, + 0, + 3, + 17, + 10, + 23, + 20, + 8, + 0, + 3, + 2, + 0 + ], + "n_missing": 1754 + }, + "valid_overlap": { + "metadata": { + "type": { + "class": "numeric", + "integer": true, + "missing_reasons": { + "No Data": -1 + }, + "missing_rules": {}, + "subvariables": [ + "00000", + "00001", + "00002" + ] + }, + "references": { + "subreferences": [ + { + "alias": "segments_bbc__3", + "name": "Segments, BBC" + }, + { + "alias": "segments_channel4__3", + "name": "Segments, Channel4" + }, + { + "alias": "segments_itv__2", + "name": "Segments, ITV" + } + ], + "view": { + "show_counts": false, + "include_missing": false, + "include_noneoftheabove": false, + "summary_statistic": "mean" + }, + "name": "Segments MR", + "alias": "Segments_MR", + "uniform_basis": false + }, + "derived": true + }, + "data": [ + 24358, + 22062, + 22640, + 8315, + 7375, + 7477, + 0, + 947, + 975, + 12237, + 12634, + 11779, + 17200, + 17750, + 16622, + 3236, + 0, + 2691, + 15318, + 14284, + 15840, + 14799, + 14117, + 15252, + 2556, + 1983, + 0, + 966, + 870, + 848, + 470, + 418, + 408, + 0, + 68, + 64, + 610, + 642, + 574, + 678, + 714, + 629, + 148, + 0, + 117, + 534, + 512, + 560, + 722, + 691, + 760, + 180, + 153, + 0, + 60, + 56, + 52, + 24, + 23, + 20, + 0, + 6, + 5, + 42, + 48, + 41, + 37, + 37, + 34, + 5, + 0, + 2, + 32, + 34, + 35, + 40, + 41, + 42, + 12, + 10, + 0, + 30, + 29, + 27, + 13, + 9, + 12, + 0, + 1, + 1, + 27, + 27, + 26, + 11, + 12, + 11, + 5, + 0, + 3, + 22, + 21, + 23, + 17, + 16, + 17, + 4, + 2, + 0, + 90, + 85, + 81, + 55, + 50, + 47, + 0, + 8, + 8, + 64, + 69, + 59, + 71, + 74, + 68, + 10, + 0, + 9, + 31, + 32, + 35, + 97, + 95, + 101, + 17, + 16, + 0, + 96, + 89, + 86, + 36, + 29, + 29, + 0, + 6, + 5, + 72, + 76, + 68, + 46, + 48, + 44, + 14, + 0, + 8, + 39, + 38, + 42, + 76, + 74, + 78, + 17, + 12, + 0, + 179, + 151, + 163, + 70, + 61, + 66, + 0, + 6, + 8, + 108, + 111, + 104, + 104, + 107, + 101, + 37, + 0, + 32, + 124, + 113, + 129, + 105, + 92, + 108, + 20, + 13, + 0, + 90, + 76, + 79, + 29, + 25, + 27, + 0, + 5, + 10, + 44, + 45, + 41, + 57, + 61, + 60, + 18, + 0, + 15, + 60, + 56, + 68, + 46, + 45, + 48, + 13, + 5, + 0, + 36, + 25, + 30, + 22, + 21, + 20, + 0, + 0, + 0, + 24, + 24, + 19, + 22, + 22, + 20, + 12, + 0, + 11, + 22, + 19, + 22, + 28, + 20, + 28, + 8, + 7, + 0, + 58, + 49, + 47, + 19, + 18, + 18, + 0, + 1, + 4, + 38, + 39, + 34, + 29, + 29, + 26, + 10, + 0, + 9, + 25, + 23, + 27, + 40, + 37, + 42, + 12, + 8, + 0, + 69, + 61, + 67, + 52, + 47, + 47, + 0, + 6, + 8, + 42, + 46, + 46, + 66, + 68, + 62, + 13, + 0, + 14, + 59, + 57, + 65, + 55, + 51, + 57, + 7, + 6, + 0, + 119, + 103, + 107, + 32, + 27, + 27, + 0, + 5, + 4, + 77, + 80, + 69, + 53, + 55, + 52, + 21, + 0, + 17, + 85, + 76, + 88, + 49, + 45, + 50, + 17, + 14, + 0, + 9, + 9, + 9, + 6, + 5, + 4, + 0, + 2, + 2, + 4, + 5, + 5, + 10, + 11, + 9, + 1, + 0, + 1, + 8, + 9, + 9, + 5, + 5, + 6, + 2, + 2, + 0, + 95, + 76, + 77, + 33, + 29, + 28, + 0, + 2, + 3, + 44, + 46, + 43, + 61, + 61, + 51, + 23, + 0, + 14, + 45, + 38, + 46, + 60, + 56, + 62, + 23, + 13, + 0, + 127, + 107, + 111, + 71, + 63, + 63, + 0, + 11, + 11, + 73, + 78, + 68, + 97, + 103, + 92, + 28, + 0, + 25, + 63, + 58, + 66, + 111, + 102, + 119, + 24, + 21, + 0, + 294, + 248, + 252, + 181, + 168, + 161, + 0, + 32, + 30, + 159, + 170, + 141, + 257, + 278, + 253, + 59, + 0, + 49, + 155, + 140, + 167, + 258, + 254, + 276, + 62, + 54, + 0, + 40, + 35, + 37, + 10, + 9, + 10, + 0, + 3, + 3, + 18, + 20, + 18, + 26, + 27, + 26, + 6, + 0, + 6, + 20, + 20, + 23, + 27, + 24, + 27, + 3, + 3, + 0 + ], + "n_missing": 1754 + } + }, + "missing": 1754, + "filter_stats": { + "is_cat_date": false, + "filtered": { + "unweighted": { + "selected": 39248, + "other": 0, + "missing": 0 + }, + "weighted": { + "selected": 39248, + "other": 0, + "missing": 0 + } + }, + "filtered_complete": { + "unweighted": { + "selected": 39248, + "other": 0, + "missing": 0 + }, + "weighted": { + "selected": 39248, + "other": 0, + "missing": 0 + } + } + }, + "unfiltered": { + "unweighted_n": 39248, + "weighted_n": 39248 + }, + "filtered": { + "unweighted_n": 39248, + "weighted_n": 39248 + }, + "element": "crunch:cube" + }, + "query": { + "dimensions": [ + { + "variable": "67379d371fc44f18b8aac456c425ca5d" + }, + { + "function": "dimension", + "args": [ + { + "function": "as_selected", + "args": [ + { + "variable": "F-70ed496d70a94f7994190f2f330e9ae2" + } + ] + }, + { + "value": "subvariables" + } + ] + }, + { + "function": "as_selected", + "args": [ + { + "variable": "F-70ed496d70a94f7994190f2f330e9ae2" + } + ] + } + ], + "measures": { + "count": { + "function": "cube_count", + "args": [] + }, + "valid_count_unweighted": { + "function": "cube_valid_count", + "args": [ + { + "function": "cast", + "args": [ + { + "variable": "98be0ee30a4b4109912919ea387656b2" + }, + { + "class": "numeric" + } + ] + } + ] + }, + "stddev": { + "function": "cube_stddev", + "args": [ + { + "function": "cast", + "args": [ + { + "variable": "98be0ee30a4b4109912919ea387656b2" + }, + { + "class": "numeric" + } + ] + } + ] + }, + "mean": { + "function": "cube_mean", + "args": [ + { + "function": "cast", + "args": [ + { + "variable": "98be0ee30a4b4109912919ea387656b2" + }, + { + "class": "numeric" + } + ] + } + ] + }, + "valid_count_weighted": { + "function": "cube_valid_count", + "args": [ + { + "function": "cast", + "args": [ + { + "variable": "98be0ee30a4b4109912919ea387656b2" + }, + { + "class": "numeric" + } + ] + }, + { + "variable": "0eccac23e37842b8aba05a74f3d8de6b" + } + ] + } + }, + "weight": "0eccac23e37842b8aba05a74f3d8de6b" + }, + "query_environment": { + "filter": [] + } +} diff --git a/tests/integration/test_pairwise_significance.py b/tests/integration/test_pairwise_significance.py index 4963dda7f..b71f06e2e 100644 --- a/tests/integration/test_pairwise_significance.py +++ b/tests/integration/test_pairwise_significance.py @@ -1234,6 +1234,74 @@ def test_mean_diff_significance_is_not_available(self): " without a mean measure" ) + def test_mean_diff_significance_for_cat_x_mr(self): + slice_ = Cube(CR.MEANS_CAT_X_MR_2).partitions[0] + + assert slice_.pairwise_significance_means_t_stats(1) == pytest.approx( + np.array( + [ + [-5.08502035, 0.0, 10.47916351], + [-0.92536791, 0.0, 2.04661479], + [0.04293266, 0.0, 1.39944002], + [0.24458516, 0.0, -0.8775668], + [-1.21188084, 0.0, 1.60085202], + [-0.7915269, 0.0, -1.09577613], + [-0.95008399, 0.0, 0.08980333], + [-0.8293663, 0.0, 0.58453131], + [-0.10038129, 0.0, -0.08425238], + [-0.38957621, 0.0, -0.9576773], + [-0.86258439, 0.0, -0.34492621], + [-2.2150695, 0.0, -2.65233866], + [-1.60183461, np.nan, -2.14338173], + [-1.71518691, 0.0, -1.84614557], + [-0.39714252, 0.0, 1.16805569], + [-0.83608247, 0.0, 1.5367485], + ] + ), + nan_ok=True, + ) + assert slice_.pairwise_significance_means_p_vals(1) == pytest.approx( + np.array( + [ + [3.70166878e-07, 1.0, 0.00000000e00], + [3.54936608e-01, 1.0, 4.09162522e-02], + [9.65840240e-01, 1.0, 1.65659571e-01], + [8.07698559e-01, 1.0, 3.84616965e-01], + [2.27482880e-01, 1.0, 1.13483809e-01], + [4.29776148e-01, 1.0, 2.76531120e-01], + [3.43055663e-01, 1.0, 9.28520777e-01], + [4.09166258e-01, 1.0, 5.60270523e-01], + [9.20448828e-01, 1.0, 9.33241280e-01], + [6.97858264e-01, 1.0, 3.42401879e-01], + [3.90426642e-01, 1.0, 7.30885456e-01], + [2.80344874e-02, 1.0, 8.76989883e-03], + [1.47859829e-01, np.nan, 6.44361344e-02], + [8.94285264e-02, 1.0, 6.82231709e-02], + [6.91784592e-01, 1.0, 2.44791804e-01], + [4.03676354e-01, 1.0, 1.25298850e-01], + ] + ), + nan_ok=True, + ) + assert slice_.pairwise_means_indices.tolist() == [ + [(), (0,), (0, 1)], + [(), (), (0, 1)], + [(), (), ()], + [(), (), ()], + [(), (), (0,)], + [(), (), ()], + [(), (), ()], + [(), (), ()], + [(), (), ()], + [(), (), ()], + [(), (), ()], + [(), (0, 2), ()], + [(), (), ()], + [(), (), ()], + [(), (), ()], + [(), (), (0,)], + ] + def test_mean_diff_significance_for_cat_x_cat(self): slice_ = Cube(CR.MEAN_CAT_X_CAT).partitions[0] @@ -1241,8 +1309,8 @@ def test_mean_diff_significance_for_cat_x_cat(self): np.array( [ [np.nan, np.nan, np.nan, np.nan], - [9.35414347, 0.0, 2.1529364, np.nan], - [1.26202085, 0, np.nan, np.nan], + [5.0, 0.0, 1.4, np.nan], + [0.94553674, 0, np.nan, np.nan], ] ), nan_ok=True, @@ -1251,15 +1319,15 @@ def test_mean_diff_significance_for_cat_x_cat(self): np.array( [ [np.nan, np.nan, np.nan, np.nan], - [8.46461576e-05, 1.00000000e00, 6.33217224e-02, np.nan], - [2.27836998e-01, 1.00000000e00, np.nan, np.nan], + [0.12566592, 1.0, 0.25628034, np.nan], + [0.37638264, 1.0, np.nan, np.nan], ] ), nan_ok=True, ) assert slice_.pairwise_means_indices.tolist() == [ [(), (), (), ()], - [(1, 2), (), (), ()], + [(), (), (), ()], [(), (), (), ()], ] @@ -1281,13 +1349,13 @@ def test_mean_diff_significance_for_cat_x_cat(self): ).partitions[0] assert slice_.pairwise_means_indices.tolist() == [ - [(), (), (1,)], + [(), (), ()], [(), (), ()], ] assert slice_.pairwise_significance_means_t_stats(1) == pytest.approx( np.array( [ - [np.nan, 0, 2.1529364], + [np.nan, 0, 1.40000000], [np.nan, 0, np.nan], ] ), @@ -1296,7 +1364,7 @@ def test_mean_diff_significance_for_cat_x_cat(self): assert slice_.pairwise_significance_means_p_vals(1) == pytest.approx( np.array( [ - [np.nan, 1, 0.06332172], + [np.nan, 1, 0.2562803], [np.nan, 1.0, np.nan], ] ), @@ -1309,12 +1377,12 @@ def test_mean_diff_significance_indices_for_cat_x_cat(self): assert slice_.pairwise_means_indices.tolist() == [ [(), (), (), ()], - [(1, 2), (), (), ()], + [(), (), (), ()], [(), (), (), ()], ] assert slice_.pairwise_means_indices_alt.tolist() == [ - [(), (), (), (0,)], - [(1, 2), (), (1,), ()], + [(), (), (), ()], + [(1,), (), (), ()], [(), (), (), ()], ] diff --git a/tests/unit/matrix/test_measure.py b/tests/unit/matrix/test_measure.py index bf06a3eb8..fc5963bd2 100644 --- a/tests/unit/matrix/test_measure.py +++ b/tests/unit/matrix/test_measure.py @@ -841,7 +841,7 @@ def it_provides_the_df(self, request): request, _BaseCubeStdDev, stddev=np.array([[17.14873, 11.00736]]) ) unweighted_cube_counts_ = instance_mock( - request, _BaseCubeCounts, column_bases=np.array([[23, 21]]) + request, _BaseCubeCounts, counts=np.array([[23, 21]]) ) cube_measures_ = instance_mock(request, CubeMeasures) cube_measures_.cube_stddev = cube_std_dev_ @@ -864,7 +864,7 @@ def it_provides_the_tstats(self, request): request, _BaseCubeStdDev, stddev=np.array([[17.14873, 11.00736]]) ) unweighted_cube_counts_ = instance_mock( - request, _BaseCubeCounts, column_bases=np.array([[23, 21]]) + request, _BaseCubeCounts, counts=np.array([[23, 21]]) ) cube_measures_ = instance_mock(request, CubeMeasures) cube_measures_.cube_means = cube_means_