1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <gsl/gsl_vector.h>
21 #include <gsl/gsl_linalg.h>
22 #include <gsl/gsl_matrix.h>
23 #include <gsl/gsl_eigen.h>
24 #include <gsl/gsl_blas.h>
25 #include <gsl/gsl_sort_vector.h>
27 #include <math/covariance.h>
29 #include <math/correlation.h>
30 #include <math/moments.h>
31 #include <data/procedure.h>
32 #include <language/lexer/variable-parser.h>
33 #include <language/lexer/value-parser.h>
34 #include <language/command.h>
35 #include <language/lexer/lexer.h>
37 #include <data/casegrouper.h>
38 #include <data/casereader.h>
39 #include <data/casewriter.h>
40 #include <data/dictionary.h>
41 #include <data/format.h>
42 #include <data/subcase.h>
44 #include <libpspp/misc.h>
45 #include <libpspp/message.h>
47 #include <output/table.h>
51 #define _(msgid) gettext (msgid)
52 #define N_(msgid) msgid
67 enum extraction_method
75 PRINT_UNIVARIATE = 0x0001,
76 PRINT_DETERMINANT = 0x0002,
80 PRINT_COVARIANCE = 0x0020,
81 PRINT_CORRELATION = 0x0040,
82 PRINT_ROTATION = 0x0080,
83 PRINT_EXTRACTION = 0x0100,
84 PRINT_INITIAL = 0x0200,
94 const struct variable **vars;
96 const struct variable *wv;
99 enum missing_type missing_type;
100 enum mv_class exclude;
101 enum print_opts print;
102 enum extraction_method extraction;
104 /* Extraction Criteria */
117 /* Intermediate values used in calculation */
119 const gsl_matrix *corr ; /* The correlation matrix */
120 const gsl_matrix *cov ; /* The covariance matrix */
121 const gsl_matrix *n ; /* Matrix of number of samples */
123 gsl_vector *eval ; /* The eigenvalues */
124 gsl_matrix *evec ; /* The eigenvectors */
128 gsl_vector *msr ; /* Multiple Squared Regressions */
131 static struct idata *
132 idata_alloc (size_t n_vars)
134 struct idata *id = xzalloc (sizeof (*id));
136 id->n_extractions = 0;
137 id->msr = gsl_vector_alloc (n_vars);
139 id->eval = gsl_vector_alloc (n_vars);
140 id->evec = gsl_matrix_alloc (n_vars, n_vars);
146 idata_free (struct idata *id)
148 gsl_vector_free (id->msr);
149 gsl_vector_free (id->eval);
150 gsl_matrix_free (id->evec);
157 dump_matrix (const gsl_matrix *m)
161 for (i = 0 ; i < m->size1; ++i)
163 for (j = 0 ; j < m->size2; ++j)
164 printf ("%02f ", gsl_matrix_get (m, i, j));
171 dump_matrix_permute (const gsl_matrix *m, const gsl_permutation *p)
175 for (i = 0 ; i < m->size1; ++i)
177 for (j = 0 ; j < m->size2; ++j)
178 printf ("%02f ", gsl_matrix_get (m, gsl_permutation_get (p, i), j));
185 dump_vector (const gsl_vector *v)
188 for (i = 0 ; i < v->size; ++i)
190 printf ("%02f\n", gsl_vector_get (v, i));
197 n_extracted_factors (const struct cmd_factor *factor, struct idata *idata)
201 /* If there is a cached value, then return that. */
202 if ( idata->n_extractions != 0)
203 return idata->n_extractions;
205 /* Otherwise, if the number of factors has been explicitly requested,
207 if (factor->n_factors > 0)
209 idata->n_extractions = factor->n_factors;
213 /* Use the MIN_EIGEN setting. */
214 for (i = 0 ; i < idata->eval->size; ++i)
216 double evali = fabs (gsl_vector_get (idata->eval, i));
218 idata->n_extractions = i;
220 if (evali < factor->min_eigen)
225 return idata->n_extractions;
229 /* Returns a newly allocated matrix identical to M.
230 It it the callers responsibility to free the returned value.
233 matrix_dup (const gsl_matrix *m)
235 gsl_matrix *n = gsl_matrix_alloc (m->size1, m->size2);
237 gsl_matrix_memcpy (n, m);
245 /* Copy of the subject */
250 gsl_permutation *perm;
257 static struct smr_workspace *ws_create (const gsl_matrix *input)
259 struct smr_workspace *ws = xmalloc (sizeof (*ws));
261 ws->m = gsl_matrix_alloc (input->size1, input->size2);
262 ws->inverse = gsl_matrix_calloc (input->size1 - 1, input->size2 - 1);
263 ws->perm = gsl_permutation_alloc (input->size1 - 1);
264 ws->result1 = gsl_matrix_calloc (input->size1 - 1, 1);
265 ws->result2 = gsl_matrix_calloc (1, 1);
271 ws_destroy (struct smr_workspace *ws)
273 gsl_matrix_free (ws->result2);
274 gsl_matrix_free (ws->result1);
275 gsl_permutation_free (ws->perm);
276 gsl_matrix_free (ws->inverse);
277 gsl_matrix_free (ws->m);
284 Return the square of the regression coefficient for VAR regressed against all other variables.
287 squared_multiple_correlation (const gsl_matrix *corr, int var, struct smr_workspace *ws)
289 /* For an explanation of what this is doing, see
290 http://www.visualstatistics.net/Visual%20Statistics%20Multimedia/multiple_regression_analysis.htm
296 gsl_matrix_memcpy (ws->m, corr);
298 gsl_matrix_swap_rows (ws->m, 0, var);
299 gsl_matrix_swap_columns (ws->m, 0, var);
301 rxx = gsl_matrix_submatrix (ws->m, 1, 1, ws->m->size1 - 1, ws->m->size1 - 1);
303 gsl_linalg_LU_decomp (&rxx.matrix, ws->perm, &signum);
305 gsl_linalg_LU_invert (&rxx.matrix, ws->perm, ws->inverse);
308 gsl_matrix_const_view rxy = gsl_matrix_const_submatrix (ws->m, 1, 0, ws->m->size1 - 1, 1);
309 gsl_matrix_const_view ryx = gsl_matrix_const_submatrix (ws->m, 0, 1, 1, ws->m->size1 - 1);
311 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
312 1.0, ws->inverse, &rxy.matrix, 0.0, ws->result1);
314 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
315 1.0, &ryx.matrix, ws->result1, 0.0, ws->result2);
318 return gsl_matrix_get (ws->result2, 0, 0);
323 static double the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors);
326 struct factor_matrix_workspace
329 gsl_eigen_symmv_workspace *eigen_ws;
339 static struct factor_matrix_workspace *
340 factor_matrix_workspace_alloc (size_t n, size_t nf)
342 struct factor_matrix_workspace *ws = xmalloc (sizeof (*ws));
345 ws->gamma = gsl_matrix_calloc (nf, nf);
346 ws->eigen_ws = gsl_eigen_symmv_alloc (n);
347 ws->eval = gsl_vector_alloc (n);
348 ws->evec = gsl_matrix_alloc (n, n);
349 ws->r = gsl_matrix_alloc (n, n);
355 factor_matrix_workspace_free (struct factor_matrix_workspace *ws)
357 gsl_eigen_symmv_free (ws->eigen_ws);
358 gsl_vector_free (ws->eval);
359 gsl_matrix_free (ws->evec);
360 gsl_matrix_free (ws->gamma);
361 gsl_matrix_free (ws->r);
366 Shift P left by OFFSET places, and overwrite TARGET
367 with the shifted result.
368 Positions in TARGET less than OFFSET are unchanged.
371 perm_shift_apply (gsl_permutation *target, const gsl_permutation *p,
375 assert (target->size == p->size);
376 assert (offset <= target->size);
378 for (i = 0; i < target->size - offset; ++i)
380 target->data[i] = p->data [i + offset];
386 Indirectly sort the rows of matrix INPUT, storing the sort order in PERM.
387 The sort criteria are as follows:
389 Rows are sorted on the first column, until the absolute value of an
390 element in a subsequent column is greater than that of the first
391 column. Thereafter, rows will be sorted on the second column,
392 until the absolute value of an element in a subsequent column
393 exceeds that of the second column ...
396 sort_matrix_indirect (const gsl_matrix *input, gsl_permutation *perm)
398 const size_t n = perm->size;
399 const size_t m = input->size2;
406 assert (perm->size == input->size1);
408 p = gsl_permutation_alloc (n);
410 /* Copy INPUT into MAT, discarding the sign */
411 mat = gsl_matrix_alloc (n, m);
412 for (i = 0 ; i < mat->size1; ++i)
414 for (j = 0 ; j < mat->size2; ++j)
416 double x = gsl_matrix_get (input, i, j);
417 gsl_matrix_set (mat, i, j, fabs (x));
421 while (column_n < m && row_n < n)
423 gsl_vector_const_view columni = gsl_matrix_const_column (mat, column_n);
424 gsl_sort_vector_index (p, &columni.vector);
426 for (i = 0 ; i < n; ++i)
428 gsl_vector_view row = gsl_matrix_row (mat, p->data[n - 1 - i]);
429 size_t maxindex = gsl_vector_max_index (&row.vector);
431 if ( maxindex > column_n )
434 /* All subsequent elements of this row, are of no interest.
435 So set them all to a highly negative value */
436 for (j = column_n + 1; j < row.vector.size ; ++j)
437 gsl_vector_set (&row.vector, j, -DBL_MAX);
440 perm_shift_apply (perm, p, row_n);
446 gsl_permutation_free (p);
447 gsl_matrix_free (mat);
449 assert ( 0 == gsl_permutation_valid (perm));
451 /* We want the biggest value to be first */
452 gsl_permutation_reverse (perm);
457 Get an approximation for the factor matrix into FACTORS, and the communalities into COMMUNALITIES.
458 R is the matrix to be analysed.
459 WS is a pointer to a structure which must have been initialised with factor_matrix_workspace_init.
462 iterate_factor_matrix (const gsl_matrix *r, gsl_vector *communalities, gsl_matrix *factors, struct factor_matrix_workspace *ws)
467 assert (r->size1 == r->size2);
468 assert (r->size1 == communalities->size);
470 assert (factors->size1 == r->size1);
471 assert (factors->size2 == ws->n_factors);
473 gsl_matrix_memcpy (ws->r, r);
475 /* Apply Communalities to diagonal of correlation matrix */
476 for (i = 0 ; i < communalities->size ; ++i)
478 double *x = gsl_matrix_ptr (ws->r, i, i);
479 *x = gsl_vector_get (communalities, i);
482 gsl_eigen_symmv (ws->r, ws->eval, ws->evec, ws->eigen_ws);
484 mv = gsl_matrix_submatrix (ws->evec, 0, 0, ws->evec->size1, ws->n_factors);
486 /* Gamma is the diagonal matrix containing the absolute values of the eigenvalues */
487 for (i = 0 ; i < ws->n_factors ; ++i)
489 double *ptr = gsl_matrix_ptr (ws->gamma, i, i);
490 *ptr = fabs (gsl_vector_get (ws->eval, i));
493 /* Take the square root of gamma */
494 gsl_linalg_cholesky_decomp (ws->gamma);
496 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
497 1.0, &mv.matrix, ws->gamma, 0.0, factors);
499 for (i = 0 ; i < r->size1 ; ++i)
501 double h = the_communality (ws->evec, ws->eval, i, ws->n_factors);
502 gsl_vector_set (communalities, i, h);
508 static bool run_factor (struct dataset *ds, const struct cmd_factor *factor);
512 cmd_factor (struct lexer *lexer, struct dataset *ds)
514 bool extraction_seen = false;
515 const struct dictionary *dict = dataset_dict (ds);
517 struct cmd_factor factor;
518 factor.method = METHOD_CORR;
519 factor.missing_type = MISS_LISTWISE;
520 factor.exclude = MV_ANY;
521 factor.print = PRINT_INITIAL | PRINT_EXTRACTION | PRINT_ROTATION;
522 factor.extraction = EXTRACTION_PC;
523 factor.n_factors = 0;
524 factor.min_eigen = SYSMIS;
525 factor.iterations = 25;
526 factor.econverge = 0.001;
530 factor.wv = dict_get_weight (dict);
532 lex_match (lexer, '/');
534 if (!lex_force_match_id (lexer, "VARIABLES"))
539 lex_match (lexer, '=');
541 if (!parse_variables_const (lexer, dict, &factor.vars, &factor.n_vars,
542 PV_NO_DUPLICATE | PV_NUMERIC))
545 while (lex_token (lexer) != '.')
547 lex_match (lexer, '/');
549 #if FACTOR_FULLY_IMPLEMENTED
550 if (lex_match_id (lexer, "PLOT"))
552 lex_match (lexer, '=');
553 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
555 if (lex_match_id (lexer, "EIGEN"))
558 else if (lex_match_id (lexer, "ROTATION"))
563 lex_error (lexer, NULL);
570 if (lex_match_id (lexer, "METHOD"))
572 lex_match (lexer, '=');
573 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
575 if (lex_match_id (lexer, "COVARIANCE"))
577 factor.method = METHOD_COV;
579 else if (lex_match_id (lexer, "CORRELATION"))
581 factor.method = METHOD_CORR;
585 lex_error (lexer, NULL);
590 #if FACTOR_FULLY_IMPLEMENTED
591 else if (lex_match_id (lexer, "ROTATION"))
593 lex_match (lexer, '=');
594 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
596 if (lex_match_id (lexer, "VARIMAX"))
599 else if (lex_match_id (lexer, "DEFAULT"))
604 lex_error (lexer, NULL);
610 else if (lex_match_id (lexer, "CRITERIA"))
612 lex_match (lexer, '=');
613 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
615 if (lex_match_id (lexer, "FACTORS"))
617 if ( lex_force_match (lexer, '('))
619 lex_force_int (lexer);
620 factor.n_factors = lex_integer (lexer);
622 lex_force_match (lexer, ')');
625 else if (lex_match_id (lexer, "MINEIGEN"))
627 if ( lex_force_match (lexer, '('))
629 lex_force_num (lexer);
630 factor.min_eigen = lex_number (lexer);
632 lex_force_match (lexer, ')');
635 else if (lex_match_id (lexer, "ECONVERGE"))
637 if ( lex_force_match (lexer, '('))
639 lex_force_num (lexer);
640 factor.econverge = lex_number (lexer);
642 lex_force_match (lexer, ')');
645 else if (lex_match_id (lexer, "ITERATE"))
647 if ( lex_force_match (lexer, '('))
649 lex_force_int (lexer);
650 factor.iterations = lex_integer (lexer);
652 lex_force_match (lexer, ')');
655 else if (lex_match_id (lexer, "DEFAULT"))
657 factor.n_factors = 0;
658 factor.min_eigen = 1;
659 factor.iterations = 25;
663 lex_error (lexer, NULL);
668 else if (lex_match_id (lexer, "EXTRACTION"))
670 extraction_seen = true;
671 lex_match (lexer, '=');
672 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
674 if (lex_match_id (lexer, "PAF"))
676 factor.extraction = EXTRACTION_PAF;
678 else if (lex_match_id (lexer, "PC"))
680 factor.extraction = EXTRACTION_PC;
682 else if (lex_match_id (lexer, "PA1"))
684 factor.extraction = EXTRACTION_PC;
686 else if (lex_match_id (lexer, "DEFAULT"))
688 factor.extraction = EXTRACTION_PC;
692 lex_error (lexer, NULL);
697 else if (lex_match_id (lexer, "FORMAT"))
699 lex_match (lexer, '=');
700 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
702 if (lex_match_id (lexer, "SORT"))
706 else if (lex_match_id (lexer, "BLANK"))
708 if ( lex_force_match (lexer, '('))
710 lex_force_num (lexer);
711 factor.blank = lex_number (lexer);
713 lex_force_match (lexer, ')');
716 else if (lex_match_id (lexer, "DEFAULT"))
723 lex_error (lexer, NULL);
728 else if (lex_match_id (lexer, "PRINT"))
731 lex_match (lexer, '=');
732 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
734 if (lex_match_id (lexer, "UNIVARIATE"))
736 factor.print |= PRINT_UNIVARIATE;
738 else if (lex_match_id (lexer, "DET"))
740 factor.print |= PRINT_DETERMINANT;
742 #if FACTOR_FULLY_IMPLEMENTED
743 else if (lex_match_id (lexer, "INV"))
746 else if (lex_match_id (lexer, "AIC"))
750 else if (lex_match_id (lexer, "SIG"))
752 factor.print |= PRINT_SIG;
754 else if (lex_match_id (lexer, "CORRELATION"))
756 factor.print |= PRINT_CORRELATION;
758 #if FACTOR_FULLY_IMPLEMENTED
759 else if (lex_match_id (lexer, "COVARIANCE"))
763 else if (lex_match_id (lexer, "ROTATION"))
765 factor.print |= PRINT_ROTATION;
767 else if (lex_match_id (lexer, "EXTRACTION"))
769 factor.print |= PRINT_EXTRACTION;
771 else if (lex_match_id (lexer, "INITIAL"))
773 factor.print |= PRINT_INITIAL;
775 #if FACTOR_FULLY_IMPLEMENTED
776 else if (lex_match_id (lexer, "KMO"))
779 else if (lex_match_id (lexer, "REPR"))
782 else if (lex_match_id (lexer, "FSCORE"))
786 else if (lex_match (lexer, T_ALL))
788 factor.print = 0xFFFF;
790 else if (lex_match_id (lexer, "DEFAULT"))
792 factor.print |= PRINT_INITIAL ;
793 factor.print |= PRINT_EXTRACTION ;
794 factor.print |= PRINT_ROTATION ;
798 lex_error (lexer, NULL);
803 else if (lex_match_id (lexer, "MISSING"))
805 lex_match (lexer, '=');
806 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
808 if (lex_match_id (lexer, "INCLUDE"))
810 factor.exclude = MV_SYSTEM;
812 else if (lex_match_id (lexer, "EXCLUDE"))
814 factor.exclude = MV_ANY;
816 else if (lex_match_id (lexer, "LISTWISE"))
818 factor.missing_type = MISS_LISTWISE;
820 else if (lex_match_id (lexer, "PAIRWISE"))
822 factor.missing_type = MISS_PAIRWISE;
824 else if (lex_match_id (lexer, "MEANSUB"))
826 factor.missing_type = MISS_MEANSUB;
830 lex_error (lexer, NULL);
837 lex_error (lexer, NULL);
842 if ( ! run_factor (ds, &factor))
853 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
857 run_factor (struct dataset *ds, const struct cmd_factor *factor)
859 struct dictionary *dict = dataset_dict (ds);
861 struct casereader *group;
863 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
865 while (casegrouper_get_next_group (grouper, &group))
867 if ( factor->missing_type == MISS_LISTWISE )
868 group = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
871 do_factor (factor, group);
874 ok = casegrouper_destroy (grouper);
875 ok = proc_commit (ds) && ok;
881 /* Return the communality of variable N, calculated to N_FACTORS */
883 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
890 assert (n < eval->size);
891 assert (n < evec->size1);
892 assert (n_factors <= eval->size);
894 for (i = 0 ; i < n_factors; ++i)
896 double evali = fabs (gsl_vector_get (eval, i));
898 double eveci = gsl_matrix_get (evec, n, i);
900 comm += pow2 (eveci) * evali;
906 /* Return the communality of variable N, calculated to N_FACTORS */
908 communality (struct idata *idata, int n, int n_factors)
910 return the_communality (idata->evec, idata->eval, n, n_factors);
916 show_communalities (const struct cmd_factor * factor,
917 const gsl_vector *initial, const gsl_vector *extracted)
921 const int heading_columns = 1;
922 int nc = heading_columns;
923 const int heading_rows = 1;
924 const int nr = heading_rows + factor->n_vars;
927 if (factor->print & PRINT_EXTRACTION)
930 if (factor->print & PRINT_INITIAL)
933 /* No point having a table with only headings */
937 t = tab_create (nc, nr);
939 tab_title (t, _("Communalities"));
941 tab_dim (t, tab_natural_dimensions, NULL, NULL);
943 tab_headers (t, heading_columns, 0, heading_rows, 0);
946 if (factor->print & PRINT_INITIAL)
947 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
949 if (factor->print & PRINT_EXTRACTION)
950 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
952 /* Outline the box */
966 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
967 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
969 for (i = 0 ; i < factor->n_vars; ++i)
972 tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
974 if (factor->print & PRINT_INITIAL)
975 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL);
977 if (factor->print & PRINT_EXTRACTION)
978 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL);
986 show_factor_matrix (const struct cmd_factor *factor, struct idata *idata, const gsl_matrix *fm)
989 const int n_factors = n_extracted_factors (factor, idata);
991 const int heading_columns = 1;
992 const int heading_rows = 2;
993 const int nr = heading_rows + factor->n_vars;
994 const int nc = heading_columns + n_factors;
995 gsl_permutation *perm;
997 struct tab_table *t = tab_create (nc, nr);
999 if ( factor->extraction == EXTRACTION_PC )
1000 tab_title (t, _("Component Matrix"));
1002 tab_title (t, _("Factor Matrix"));
1004 tab_dim (t, tab_natural_dimensions, NULL, NULL);
1006 tab_headers (t, heading_columns, 0, heading_rows, 0);
1008 if ( factor->extraction == EXTRACTION_PC )
1012 TAB_CENTER | TAT_TITLE, _("Component"));
1017 TAB_CENTER | TAT_TITLE, _("Factor"));
1020 tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1023 /* Outline the box */
1030 /* Vertical lines */
1037 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1038 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1041 /* Initialise to the identity permutation */
1042 perm = gsl_permutation_calloc (factor->n_vars);
1045 sort_matrix_indirect (fm, perm);
1047 for (i = 0 ; i < n_factors; ++i)
1049 tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1052 for (i = 0 ; i < factor->n_vars; ++i)
1055 const int matrix_row = perm->data[i];
1056 tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1058 for (j = 0 ; j < n_factors; ++j)
1060 double x = gsl_matrix_get (fm, matrix_row, j);
1062 if ( fabs (x) < factor->blank)
1065 tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL);
1069 gsl_permutation_free (perm);
1076 show_explained_variance (const struct cmd_factor * factor, struct idata *idata,
1077 const gsl_vector *initial_eigenvalues,
1078 const gsl_vector *extracted_eigenvalues)
1082 const int heading_columns = 1;
1083 const int heading_rows = 2;
1084 const int nr = heading_rows + factor->n_vars;
1086 struct tab_table *t ;
1088 double i_total = 0.0;
1091 double e_total = 0.0;
1094 int nc = heading_columns;
1096 if (factor->print & PRINT_EXTRACTION)
1099 if (factor->print & PRINT_INITIAL)
1102 if (factor->print & PRINT_ROTATION)
1105 /* No point having a table with only headings */
1106 if ( nc <= heading_columns)
1109 t = tab_create (nc, nr);
1111 tab_title (t, _("Total Variance Explained"));
1113 tab_dim (t, tab_natural_dimensions, NULL, NULL);
1115 tab_headers (t, heading_columns, 0, heading_rows, 0);
1117 /* Outline the box */
1124 /* Vertical lines */
1131 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1132 tab_hline (t, TAL_1, 1, nc - 1, 1);
1134 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1137 if ( factor->extraction == EXTRACTION_PC)
1138 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1140 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1143 if (factor->print & PRINT_INITIAL)
1145 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1149 if (factor->print & PRINT_EXTRACTION)
1151 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1155 if (factor->print & PRINT_ROTATION)
1157 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1161 for (i = 0; i < (nc - heading_columns) / 3 ; ++i)
1163 tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1164 tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1165 tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1167 tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1170 for (i = 0 ; i < initial_eigenvalues->size; ++i)
1171 i_total += gsl_vector_get (initial_eigenvalues, i);
1173 if ( factor->extraction == EXTRACTION_PAF)
1175 e_total = factor->n_vars;
1183 for (i = 0 ; i < factor->n_vars; ++i)
1185 const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1186 double i_percent = 100.0 * i_lambda / i_total ;
1188 const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1189 double e_percent = 100.0 * e_lambda / e_total ;
1193 tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%d"), i + 1);
1198 /* Initial Eigenvalues */
1199 if (factor->print & PRINT_INITIAL)
1201 tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL);
1202 tab_double (t, c++, i + heading_rows, 0, i_percent, NULL);
1203 tab_double (t, c++, i + heading_rows, 0, i_cum, NULL);
1206 if (factor->print & PRINT_EXTRACTION)
1208 if ( i < n_extracted_factors (factor, idata))
1210 /* Sums of squared loadings */
1211 tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL);
1212 tab_double (t, c++, i + heading_rows, 0, e_percent, NULL);
1213 tab_double (t, c++, i + heading_rows, 0, e_cum, NULL);
1223 show_correlation_matrix (const struct cmd_factor *factor, const struct idata *idata)
1225 struct tab_table *t ;
1227 int y_pos_corr = -1;
1229 int suffix_rows = 0;
1231 const int heading_rows = 1;
1232 const int heading_columns = 2;
1234 int nc = heading_columns ;
1235 int nr = heading_rows ;
1236 int n_data_sets = 0;
1238 if (factor->print & PRINT_CORRELATION)
1240 y_pos_corr = n_data_sets;
1242 nc = heading_columns + factor->n_vars;
1245 if (factor->print & PRINT_SIG)
1247 y_pos_sig = n_data_sets;
1249 nc = heading_columns + factor->n_vars;
1252 nr += n_data_sets * factor->n_vars;
1254 if (factor->print & PRINT_DETERMINANT)
1257 /* If the table would contain only headings, don't bother rendering it */
1258 if (nr <= heading_rows && suffix_rows == 0)
1261 t = tab_create (nc, nr + suffix_rows);
1263 tab_title (t, _("Correlation Matrix"));
1265 tab_dim (t, tab_natural_dimensions, NULL, NULL);
1267 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1269 if (nr > heading_rows)
1271 tab_headers (t, heading_columns, 0, heading_rows, 0);
1273 tab_vline (t, TAL_2, 2, 0, nr - 1);
1275 /* Outline the box */
1282 /* Vertical lines */
1290 for (i = 0; i < factor->n_vars; ++i)
1291 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
1294 for (i = 0 ; i < n_data_sets; ++i)
1296 int y = heading_rows + i * factor->n_vars;
1298 for (v = 0; v < factor->n_vars; ++v)
1299 tab_text (t, 1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
1301 tab_hline (t, TAL_1, 0, nc - 1, y);
1304 if (factor->print & PRINT_CORRELATION)
1306 const double y = heading_rows + y_pos_corr;
1307 tab_text (t, 0, y, TAT_TITLE, _("Correlations"));
1309 for (i = 0; i < factor->n_vars; ++i)
1311 for (j = 0; j < factor->n_vars; ++j)
1312 tab_double (t, heading_columns + i, y + j, 0, gsl_matrix_get (idata->corr, i, j), NULL);
1316 if (factor->print & PRINT_SIG)
1318 const double y = heading_rows + y_pos_sig * factor->n_vars;
1319 tab_text (t, 0, y, TAT_TITLE, _("Sig. 1-tailed"));
1321 for (i = 0; i < factor->n_vars; ++i)
1323 for (j = 0; j < factor->n_vars; ++j)
1325 double rho = gsl_matrix_get (idata->corr, i, j);
1326 double w = gsl_matrix_get (idata->n, i, j);
1331 tab_double (t, heading_columns + i, y + j, 0, significance_of_correlation (rho, w), NULL);
1337 if (factor->print & PRINT_DETERMINANT)
1342 const int size = idata->corr->size1;
1343 gsl_permutation *p = gsl_permutation_calloc (size);
1344 gsl_matrix *tmp = gsl_matrix_calloc (size, size);
1345 gsl_matrix_memcpy (tmp, idata->corr);
1347 gsl_linalg_LU_decomp (tmp, p, &sign);
1348 det = gsl_linalg_LU_det (tmp, sign);
1349 gsl_permutation_free (p);
1350 gsl_matrix_free (tmp);
1353 tab_text (t, 0, nr, TAB_LEFT | TAT_TITLE, _("Determinant"));
1354 tab_double (t, 1, nr, 0, det, NULL);
1363 do_factor (const struct cmd_factor *factor, struct casereader *r)
1366 const gsl_matrix *var_matrix;
1367 const gsl_matrix *mean_matrix;
1369 const gsl_matrix *analysis_matrix;
1370 struct idata *idata = idata_alloc (factor->n_vars);
1372 struct covariance *cov = covariance_create (factor->n_vars, factor->vars,
1373 factor->wv, factor->exclude);
1375 for ( ; (c = casereader_read (r) ); case_unref (c))
1377 covariance_accumulate (cov, c);
1380 idata->cov = covariance_calculate (cov);
1382 var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
1383 mean_matrix = covariance_moments (cov, MOMENT_MEAN);
1384 idata->n = covariance_moments (cov, MOMENT_NONE);
1386 if ( factor->method == METHOD_CORR)
1388 idata->corr = correlation_from_covariance (idata->cov, var_matrix);
1389 analysis_matrix = idata->corr;
1392 analysis_matrix = idata->cov;
1394 if ( factor->print & PRINT_UNIVARIATE)
1398 const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
1401 const int heading_columns = 1;
1402 const int heading_rows = 1;
1404 const int nr = heading_rows + factor->n_vars;
1406 struct tab_table *t = tab_create (nc, nr);
1407 tab_title (t, _("Descriptive Statistics"));
1408 tab_dim (t, tab_natural_dimensions, NULL, NULL);
1410 tab_headers (t, heading_columns, 0, heading_rows, 0);
1412 /* Outline the box */
1419 /* Vertical lines */
1426 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1427 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1429 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
1430 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1431 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
1433 for (i = 0 ; i < factor->n_vars; ++i)
1435 const struct variable *v = factor->vars[i];
1436 tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
1438 tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (mean_matrix, i, i), NULL);
1439 tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (var_matrix, i, i)), NULL);
1440 tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (idata->n, i, i), wfmt);
1446 show_correlation_matrix (factor, idata);
1450 gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
1452 gsl_eigen_symmv (matrix_dup (analysis_matrix), idata->eval, idata->evec, workspace);
1454 gsl_eigen_symmv_free (workspace);
1457 gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
1461 const gsl_vector *extracted_eigenvalues = NULL;
1462 gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
1463 gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
1465 struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, n_extracted_factors (factor, idata));
1466 gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
1468 if ( factor->extraction == EXTRACTION_PAF)
1470 gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
1471 struct smr_workspace *ws = ws_create (analysis_matrix);
1473 for (i = 0 ; i < factor->n_vars ; ++i)
1475 double r2 = squared_multiple_correlation (analysis_matrix, i, ws);
1477 gsl_vector_set (idata->msr, i, r2);
1481 gsl_vector_memcpy (initial_communalities, idata->msr);
1483 for (i = 0; i < factor->iterations; ++i)
1486 gsl_vector_memcpy (diff, idata->msr);
1488 iterate_factor_matrix (analysis_matrix, idata->msr, factor_matrix, fmw);
1490 gsl_vector_sub (diff, idata->msr);
1492 gsl_vector_minmax (diff, &min, &max);
1494 if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
1497 gsl_vector_free (diff);
1499 gsl_vector_memcpy (extracted_communalities, idata->msr);
1500 extracted_eigenvalues = fmw->eval;
1502 else if (factor->extraction == EXTRACTION_PC)
1504 for (i = 0 ; i < factor->n_vars; ++i)
1506 gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
1508 gsl_vector_memcpy (extracted_communalities, initial_communalities);
1510 iterate_factor_matrix (analysis_matrix, extracted_communalities, factor_matrix, fmw);
1511 extracted_eigenvalues = idata->eval;
1514 show_communalities (factor, initial_communalities, extracted_communalities);
1516 show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues);
1518 factor_matrix_workspace_free (fmw);
1520 show_factor_matrix (factor, idata, factor_matrix);
1522 gsl_vector_free (initial_communalities);
1523 gsl_vector_free (extracted_communalities);
1528 casereader_destroy (r);