From: John Darrington Date: Thu, 4 May 2017 16:17:11 +0000 (+0200) Subject: FACTOR: Add printout of the covariance matrix and add a test X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1f24fde0bb9ada58658fd60125948a45a221f3d9;p=pspp FACTOR: Add printout of the covariance matrix and add a test --- diff --git a/src/language/stats/factor.c b/src/language/stats/factor.c index 3043fced29..6db35f34e3 100644 --- a/src/language/stats/factor.c +++ b/src/language/stats/factor.c @@ -1380,11 +1380,10 @@ cmd_factor (struct lexer *lexer, struct dataset *ds) { factor.print |= PRINT_CORRELATION; } -#if FACTOR_FULLY_IMPLEMENTED else if (lex_match_id (lexer, "COVARIANCE")) { + factor.print |= PRINT_COVARIANCE; } -#endif else if (lex_match_id (lexer, "ROTATION")) { factor.print |= PRINT_ROTATION; @@ -2100,6 +2099,91 @@ show_correlation_matrix (const struct cmd_factor *factor, const struct idata *id tab_submit (t); } +static void +show_covariance_matrix (const struct cmd_factor *factor, const struct idata *idata) +{ + struct tab_table *t ; + size_t i, j; + int y_pos_corr = -1; + int y_pos_sig = -1; + int suffix_rows = 0; + + const int heading_rows = 1; + const int heading_columns = 1; + + int nc = heading_columns ; + int nr = heading_rows ; + int n_data_sets = 0; + + if (factor->print & PRINT_COVARIANCE) + { + y_pos_corr = n_data_sets; + n_data_sets++; + nc = heading_columns + factor->n_vars; + } + + nr += n_data_sets * factor->n_vars; + + /* If the table would contain only headings, don't bother rendering it */ + if (nr <= heading_rows && suffix_rows == 0) + return; + + t = tab_create (nc, nr + suffix_rows); + + tab_title (t, _("Covariance Matrix")); + + tab_hline (t, TAL_1, 0, nc - 1, heading_rows); + + if (nr > heading_rows) + { + tab_headers (t, heading_columns, 0, heading_rows, 0); + + tab_vline (t, TAL_2, heading_columns, 0, nr - 1); + + /* Outline the box */ + tab_box (t, + TAL_2, TAL_2, + -1, -1, + 0, 0, + nc - 1, nr - 1); + + /* Vertical lines */ + tab_box (t, + -1, -1, + -1, TAL_1, + heading_columns, 0, + nc - 1, nr - 1); + + + for (i = 0; i < factor->n_vars; ++i) + tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i])); + + + for (i = 0 ; i < n_data_sets; ++i) + { + int y = heading_rows + i * factor->n_vars; + size_t v; + for (v = 0; v < factor->n_vars; ++v) + tab_text (t, heading_columns -1, y + v, TAT_TITLE, var_to_string (factor->vars[v])); + + tab_hline (t, TAL_1, 0, nc - 1, y); + } + + if (factor->print & PRINT_COVARIANCE) + { + const double y = heading_rows + y_pos_corr; + + for (i = 0; i < factor->n_vars; ++i) + { + for (j = 0; j < factor->n_vars; ++j) + tab_double (t, heading_columns + i, y + j, 0, gsl_matrix_get (idata->mm.cov, i, j), NULL, RC_OTHER); + } + } + } + + tab_submit (t); +} + static void do_factor (const struct cmd_factor *factor, struct casereader *r) @@ -2295,6 +2379,7 @@ do_factor_by_matrix (const struct cmd_factor *factor, struct idata *idata) } show_correlation_matrix (factor, idata); + show_covariance_matrix (factor, idata); if (idata->cvm) covariance_destroy (idata->cvm); diff --git a/tests/language/stats/factor.at b/tests/language/stats/factor.at index 9ac08b52c7..32378e1f6d 100644 --- a/tests/language/stats/factor.at +++ b/tests/language/stats/factor.at @@ -2065,3 +2065,93 @@ Factor,1,2,3,4 AT_CLEANUP + + + + + + +AT_SETUP([FACTOR covariance matrix]) + +AT_DATA([covariance-matrix.sps], [dnl +set format = F10.3. +matrix data + variables = rowtype_ var01 var02 var03 var04 var05 var06 var07 var08 + / format = lower diagonal . +begin data +mean 24.3 5.4 69.7 20.1 13.4 2.7 27.9 3.7 +sd 5.7 1.5 23.5 5.8 2.8 4.5 5.4 1.5 +n 92 92 92 92 92 92 92 92 +cov 32.490000 +cov 1.539000 2.250000 +cov -29.469000 -5.992500 552.250000 +cov 11.901600 2.697000 -19.082000 33.640000 +cov 4.309200 0.672000 -7.896000 3.572800 7.840000 +cov 8.464500 1.012500 -17.977500 6.264000 2.646000 20.250000 +cov 15.390000 2.349000 -25.380000 10.022400 1.814400 9.234000 29.160000 +cov 1.453500 0.652500 -1.762500 1.740000 1.134000 1.350000 0.324000 2.250000 +end data. + +factor matrix in (cov = *) + / method = covariance + / print = initial covariance kmo + / extraction = pc + / rotation = norotate. +]) + +AT_CHECK([pspp -O format=csv covariance-matrix.sps], [0], [dnl +Table: KMO and Bartlett's Test +Kaiser-Meyer-Olkin Measure of Sampling Adequacy,,.741 +Bartlett's Test of Sphericity,Approx. Chi-Square,104.810 +,df,28 +,Sig.,.000 + +Table: Covariance Matrix +,var01,var02,var03,var04,var05,var06,var07,var08 +var01,32.490,1.539,-29.469,11.902,4.309,8.464,15.390,1.454 +var02,1.539,2.250,-5.992,2.697,.672,1.013,2.349,.653 +var03,-29.469,-5.992,552.250,-19.082,-7.896,-17.977,-25.380,-1.763 +var04,11.902,2.697,-19.082,33.640,3.573,6.264,10.022,1.740 +var05,4.309,.672,-7.896,3.573,7.840,2.646,1.814,1.134 +var06,8.464,1.013,-17.977,6.264,2.646,20.250,9.234,1.350 +var07,15.390,2.349,-25.380,10.022,1.814,9.234,29.160,.324 +var08,1.454,.653,-1.763,1.740,1.134,1.350,.324,2.250 + +Table: Communalities +,Initial +var01,32.490 +var02,2.250 +var03,552.250 +var04,33.640 +var05,7.840 +var06,20.250 +var07,29.160 +var08,2.250 + +Table: Total Variance Explained +,Initial Eigenvalues,, +Component,Total,% of Variance,Cumulative % +1,556.895,81.881,81.881 +2,57.963,8.522,90.403 +3,23.576,3.466,93.869 +4,16.446,2.418,96.288 +5,14.603,2.147,98.435 +6,6.831,1.004,99.439 +7,2.375,.349,99.788 +8,1.440,.212,100.000 + +Table: Component Matrix +,Component,,,,,, +,1,2,3,4,5,6,7 +var01,1.394,4.388,1.513,-2.851,.849,.396,.033 +var02,.269,.460,-.173,.147,-.146,-.213,.872 +var03,-23.489,.725,.058,.003,.022,-.012,.006 +var04,.926,4.007,-4.068,.241,-.253,.218,-.026 +var05,.363,.829,-.172,-.255,.805,-2.492,.058 +var06,.843,2.354,.971,2.425,2.649,.392,.046 +var07,1.205,3.948,1.926,1.515,-2.450,-.317,-.087 +var08,.085,.319,-.157,-.011,.353,-.341,-.816 +]) + + +AT_CLEANUP