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 gsl_vector *eval ; /* The eigenvalues */
120 gsl_matrix *evec ; /* The eigenvectors */
124 gsl_vector *msr ; /* Multiple Squared Regressions */
127 static struct idata *
128 idata_alloc (size_t n_vars)
130 struct idata *id = xmalloc (sizeof (*id));
132 id->n_extractions = 0;
133 id->msr = gsl_vector_alloc (n_vars);
135 id->eval = gsl_vector_alloc (n_vars);
136 id->evec = gsl_matrix_alloc (n_vars, n_vars);
142 idata_free (struct idata *id)
144 gsl_vector_free (id->msr);
145 gsl_vector_free (id->eval);
146 gsl_matrix_free (id->evec);
153 dump_matrix (const gsl_matrix *m)
157 for (i = 0 ; i < m->size1; ++i)
159 for (j = 0 ; j < m->size2; ++j)
160 printf ("%02f ", gsl_matrix_get (m, i, j));
167 dump_matrix_permute (const gsl_matrix *m, const gsl_permutation *p)
171 for (i = 0 ; i < m->size1; ++i)
173 for (j = 0 ; j < m->size2; ++j)
174 printf ("%02f ", gsl_matrix_get (m, gsl_permutation_get (p, i), j));
181 dump_vector (const gsl_vector *v)
184 for (i = 0 ; i < v->size; ++i)
186 printf ("%02f\n", gsl_vector_get (v, i));
193 n_extracted_factors (const struct cmd_factor *factor, struct idata *idata)
197 /* If there is a cached value, then return that. */
198 if ( idata->n_extractions != 0)
199 return idata->n_extractions;
201 /* Otherwise, if the number of factors has been explicitly requested,
203 if (factor->n_factors > 0)
205 idata->n_extractions = factor->n_factors;
209 /* Use the MIN_EIGEN setting. */
210 for (i = 0 ; i < idata->eval->size; ++i)
212 double evali = fabs (gsl_vector_get (idata->eval, i));
214 idata->n_extractions = i;
216 if (evali < factor->min_eigen)
221 return idata->n_extractions;
225 /* Returns a newly allocated matrix identical to M.
226 It it the callers responsibility to free the returned value.
229 matrix_dup (const gsl_matrix *m)
231 gsl_matrix *n = gsl_matrix_alloc (m->size1, m->size2);
233 gsl_matrix_memcpy (n, m);
241 /* Copy of the subject */
246 gsl_permutation *perm;
253 static struct smr_workspace *ws_create (const gsl_matrix *input)
255 struct smr_workspace *ws = xmalloc (sizeof (*ws));
257 ws->m = gsl_matrix_alloc (input->size1, input->size2);
258 ws->inverse = gsl_matrix_calloc (input->size1 - 1, input->size2 - 1);
259 ws->perm = gsl_permutation_alloc (input->size1 - 1);
260 ws->result1 = gsl_matrix_calloc (input->size1 - 1, 1);
261 ws->result2 = gsl_matrix_calloc (1, 1);
267 ws_destroy (struct smr_workspace *ws)
269 gsl_matrix_free (ws->result2);
270 gsl_matrix_free (ws->result1);
271 gsl_permutation_free (ws->perm);
272 gsl_matrix_free (ws->inverse);
273 gsl_matrix_free (ws->m);
280 Return the square of the regression coefficient for VAR regressed against all other variables.
283 squared_multiple_correlation (const gsl_matrix *analysis_matrix, int var, struct smr_workspace *ws)
285 /* For an explanation of what this is doing, see
286 http://www.visualstatistics.net/Visual%20Statistics%20Multimedia/multiple_regression_analysis.htm
292 gsl_matrix_memcpy (ws->m, analysis_matrix);
294 gsl_matrix_swap_rows (ws->m, 0, var);
295 gsl_matrix_swap_columns (ws->m, 0, var);
297 rxx = gsl_matrix_submatrix (ws->m, 1, 1, ws->m->size1 - 1, ws->m->size1 - 1);
299 gsl_linalg_LU_decomp (&rxx.matrix, ws->perm, &signum);
301 gsl_linalg_LU_invert (&rxx.matrix, ws->perm, ws->inverse);
304 gsl_matrix_const_view rxy = gsl_matrix_const_submatrix (ws->m, 1, 0, ws->m->size1 - 1, 1);
305 gsl_matrix_const_view ryx = gsl_matrix_const_submatrix (ws->m, 0, 1, 1, ws->m->size1 - 1);
307 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
308 1.0, ws->inverse, &rxy.matrix, 0.0, ws->result1);
310 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
311 1.0, &ryx.matrix, ws->result1, 0.0, ws->result2);
314 return gsl_matrix_get (ws->result2, 0, 0);
319 static double the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors);
322 struct factor_matrix_workspace
325 gsl_eigen_symmv_workspace *eigen_ws;
335 static struct factor_matrix_workspace *
336 factor_matrix_workspace_alloc (size_t n, size_t nf)
338 struct factor_matrix_workspace *ws = xmalloc (sizeof (*ws));
341 ws->gamma = gsl_matrix_calloc (nf, nf);
342 ws->eigen_ws = gsl_eigen_symmv_alloc (n);
343 ws->eval = gsl_vector_alloc (n);
344 ws->evec = gsl_matrix_alloc (n, n);
345 ws->r = gsl_matrix_alloc (n, n);
351 factor_matrix_workspace_free (struct factor_matrix_workspace *ws)
353 gsl_eigen_symmv_free (ws->eigen_ws);
354 gsl_vector_free (ws->eval);
355 gsl_matrix_free (ws->evec);
356 gsl_matrix_free (ws->gamma);
357 gsl_matrix_free (ws->r);
362 Shift P left by OFFSET places, and overwrite TARGET
363 with the shifted result.
364 Positions in TARGET less than OFFSET are unchanged.
367 perm_shift_apply (gsl_permutation *target, const gsl_permutation *p,
371 assert (target->size == p->size);
372 assert (offset <= target->size);
374 for (i = 0; i < target->size - offset; ++i)
376 target->data[i] = p->data [i + offset];
382 Indirectly sort the rows of matrix INPUT, storing the sort order in PERM.
383 The sort criteria are as follows:
385 Rows are sorted on the first column, until the absolute value of an
386 element in a subsequent column is greater than that of the first
387 column. Thereafter, rows will be sorted on the second column,
388 until the absolute value of an element in a subsequent column
389 exceeds that of the second column ...
392 sort_matrix_indirect (const gsl_matrix *input, gsl_permutation *perm)
394 const size_t n = perm->size;
395 const size_t m = input->size2;
402 assert (perm->size == input->size1);
404 p = gsl_permutation_alloc (n);
406 /* Copy INPUT into MAT, discarding the sign */
407 mat = gsl_matrix_alloc (n, m);
408 for (i = 0 ; i < mat->size1; ++i)
410 for (j = 0 ; j < mat->size2; ++j)
412 double x = gsl_matrix_get (input, i, j);
413 gsl_matrix_set (mat, i, j, fabs (x));
417 while (column_n < m && row_n < n)
419 gsl_vector_const_view columni = gsl_matrix_const_column (mat, column_n);
420 gsl_sort_vector_index (p, &columni.vector);
422 for (i = 0 ; i < n; ++i)
424 gsl_vector_view row = gsl_matrix_row (mat, p->data[n - 1 - i]);
425 size_t maxindex = gsl_vector_max_index (&row.vector);
427 if ( maxindex > column_n )
430 /* All subsequent elements of this row, are of no interest.
431 So set them all to a highly negative value */
432 for (j = column_n + 1; j < row.vector.size ; ++j)
433 gsl_vector_set (&row.vector, j, -DBL_MAX);
436 perm_shift_apply (perm, p, row_n);
442 gsl_permutation_free (p);
443 gsl_matrix_free (mat);
445 assert ( 0 == gsl_permutation_valid (perm));
447 /* We want the biggest value to be first */
448 gsl_permutation_reverse (perm);
453 Get an approximation for the factor matrix into FACTORS, and the communalities into COMMUNALITIES.
454 R is the matrix to be analysed.
455 WS is a pointer to a structure which must have been initialised with factor_matrix_workspace_init.
458 iterate_factor_matrix (const gsl_matrix *r, gsl_vector *communalities, gsl_matrix *factors, struct factor_matrix_workspace *ws)
463 assert (r->size1 == r->size2);
464 assert (r->size1 == communalities->size);
466 assert (factors->size1 == r->size1);
467 assert (factors->size2 == ws->n_factors);
469 gsl_matrix_memcpy (ws->r, r);
471 /* Apply Communalities to diagonal of correlation matrix */
472 for (i = 0 ; i < communalities->size ; ++i)
474 double *x = gsl_matrix_ptr (ws->r, i, i);
475 *x = gsl_vector_get (communalities, i);
478 gsl_eigen_symmv (ws->r, ws->eval, ws->evec, ws->eigen_ws);
480 mv = gsl_matrix_submatrix (ws->evec, 0, 0, ws->evec->size1, ws->n_factors);
482 /* Gamma is the diagonal matrix containing the absolute values of the eigenvalues */
483 for (i = 0 ; i < ws->n_factors ; ++i)
485 double *ptr = gsl_matrix_ptr (ws->gamma, i, i);
486 *ptr = fabs (gsl_vector_get (ws->eval, i));
489 /* Take the square root of gamma */
490 gsl_linalg_cholesky_decomp (ws->gamma);
492 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
493 1.0, &mv.matrix, ws->gamma, 0.0, factors);
495 for (i = 0 ; i < r->size1 ; ++i)
497 double h = the_communality (ws->evec, ws->eval, i, ws->n_factors);
498 gsl_vector_set (communalities, i, h);
504 static bool run_factor (struct dataset *ds, const struct cmd_factor *factor);
508 cmd_factor (struct lexer *lexer, struct dataset *ds)
510 bool extraction_seen = false;
511 const struct dictionary *dict = dataset_dict (ds);
513 struct cmd_factor factor;
514 factor.method = METHOD_CORR;
515 factor.missing_type = MISS_LISTWISE;
516 factor.exclude = MV_ANY;
517 factor.print = PRINT_INITIAL | PRINT_EXTRACTION | PRINT_ROTATION;
518 factor.extraction = EXTRACTION_PC;
519 factor.n_factors = 0;
520 factor.min_eigen = SYSMIS;
521 factor.iterations = 25;
522 factor.econverge = 0.001;
526 factor.wv = dict_get_weight (dict);
528 lex_match (lexer, '/');
530 if (!lex_force_match_id (lexer, "VARIABLES"))
535 lex_match (lexer, '=');
537 if (!parse_variables_const (lexer, dict, &factor.vars, &factor.n_vars,
538 PV_NO_DUPLICATE | PV_NUMERIC))
541 while (lex_token (lexer) != '.')
543 lex_match (lexer, '/');
545 #if FACTOR_FULLY_IMPLEMENTED
546 if (lex_match_id (lexer, "PLOT"))
548 lex_match (lexer, '=');
549 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
551 if (lex_match_id (lexer, "EIGEN"))
554 else if (lex_match_id (lexer, "ROTATION"))
559 lex_error (lexer, NULL);
566 if (lex_match_id (lexer, "METHOD"))
568 lex_match (lexer, '=');
569 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
571 if (lex_match_id (lexer, "COVARIANCE"))
573 factor.method = METHOD_COV;
575 else if (lex_match_id (lexer, "CORRELATION"))
577 factor.method = METHOD_CORR;
581 lex_error (lexer, NULL);
586 #if FACTOR_FULLY_IMPLEMENTED
587 else if (lex_match_id (lexer, "ROTATION"))
589 lex_match (lexer, '=');
590 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
592 if (lex_match_id (lexer, "VARIMAX"))
595 else if (lex_match_id (lexer, "DEFAULT"))
600 lex_error (lexer, NULL);
606 else if (lex_match_id (lexer, "CRITERIA"))
608 lex_match (lexer, '=');
609 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
611 if (lex_match_id (lexer, "FACTORS"))
613 if ( lex_force_match (lexer, '('))
615 lex_force_int (lexer);
616 factor.n_factors = lex_integer (lexer);
618 lex_force_match (lexer, ')');
621 else if (lex_match_id (lexer, "MINEIGEN"))
623 if ( lex_force_match (lexer, '('))
625 lex_force_num (lexer);
626 factor.min_eigen = lex_number (lexer);
628 lex_force_match (lexer, ')');
631 else if (lex_match_id (lexer, "ECONVERGE"))
633 if ( lex_force_match (lexer, '('))
635 lex_force_num (lexer);
636 factor.econverge = lex_number (lexer);
638 lex_force_match (lexer, ')');
641 else if (lex_match_id (lexer, "ITERATE"))
643 if ( lex_force_match (lexer, '('))
645 lex_force_int (lexer);
646 factor.iterations = lex_integer (lexer);
648 lex_force_match (lexer, ')');
651 else if (lex_match_id (lexer, "DEFAULT"))
653 factor.n_factors = 0;
654 factor.min_eigen = 1;
655 factor.iterations = 25;
659 lex_error (lexer, NULL);
664 else if (lex_match_id (lexer, "EXTRACTION"))
666 extraction_seen = true;
667 lex_match (lexer, '=');
668 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
670 if (lex_match_id (lexer, "PAF"))
672 factor.extraction = EXTRACTION_PAF;
674 else if (lex_match_id (lexer, "PC"))
676 factor.extraction = EXTRACTION_PC;
678 else if (lex_match_id (lexer, "PA1"))
680 factor.extraction = EXTRACTION_PC;
682 else if (lex_match_id (lexer, "DEFAULT"))
684 factor.extraction = EXTRACTION_PC;
688 lex_error (lexer, NULL);
693 else if (lex_match_id (lexer, "FORMAT"))
695 lex_match (lexer, '=');
696 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
698 if (lex_match_id (lexer, "SORT"))
702 else if (lex_match_id (lexer, "BLANK"))
704 if ( lex_force_match (lexer, '('))
706 lex_force_num (lexer);
707 factor.blank = lex_number (lexer);
709 lex_force_match (lexer, ')');
712 else if (lex_match_id (lexer, "DEFAULT"))
719 lex_error (lexer, NULL);
724 else if (lex_match_id (lexer, "PRINT"))
727 lex_match (lexer, '=');
728 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
730 if (lex_match_id (lexer, "UNIVARIATE"))
732 factor.print |= PRINT_UNIVARIATE;
734 else if (lex_match_id (lexer, "DET"))
736 factor.print |= PRINT_DETERMINANT;
738 #if FACTOR_FULLY_IMPLEMENTED
739 else if (lex_match_id (lexer, "INV"))
742 else if (lex_match_id (lexer, "AIC"))
745 else if (lex_match_id (lexer, "SIG"))
748 else if (lex_match_id (lexer, "COVARIANCE"))
751 else if (lex_match_id (lexer, "CORRELATION"))
755 else if (lex_match_id (lexer, "ROTATION"))
757 factor.print |= PRINT_ROTATION;
759 else if (lex_match_id (lexer, "EXTRACTION"))
761 factor.print |= PRINT_EXTRACTION;
763 else if (lex_match_id (lexer, "INITIAL"))
765 factor.print |= PRINT_INITIAL;
767 #if FACTOR_FULLY_IMPLEMENTED
768 else if (lex_match_id (lexer, "KMO"))
771 else if (lex_match_id (lexer, "REPR"))
774 else if (lex_match_id (lexer, "FSCORE"))
778 else if (lex_match (lexer, T_ALL))
780 factor.print = 0xFFFF;
782 else if (lex_match_id (lexer, "DEFAULT"))
784 factor.print |= PRINT_INITIAL ;
785 factor.print |= PRINT_EXTRACTION ;
786 factor.print |= PRINT_ROTATION ;
790 lex_error (lexer, NULL);
795 else if (lex_match_id (lexer, "MISSING"))
797 lex_match (lexer, '=');
798 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
800 if (lex_match_id (lexer, "INCLUDE"))
802 factor.exclude = MV_SYSTEM;
804 else if (lex_match_id (lexer, "EXCLUDE"))
806 factor.exclude = MV_ANY;
808 else if (lex_match_id (lexer, "LISTWISE"))
810 factor.missing_type = MISS_LISTWISE;
812 else if (lex_match_id (lexer, "PAIRWISE"))
814 factor.missing_type = MISS_PAIRWISE;
816 else if (lex_match_id (lexer, "MEANSUB"))
818 factor.missing_type = MISS_MEANSUB;
822 lex_error (lexer, NULL);
829 lex_error (lexer, NULL);
834 if ( ! run_factor (ds, &factor))
845 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
849 run_factor (struct dataset *ds, const struct cmd_factor *factor)
851 struct dictionary *dict = dataset_dict (ds);
853 struct casereader *group;
855 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
857 while (casegrouper_get_next_group (grouper, &group))
859 if ( factor->missing_type == MISS_LISTWISE )
860 group = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
863 do_factor (factor, group);
866 ok = casegrouper_destroy (grouper);
867 ok = proc_commit (ds) && ok;
873 /* Return the communality of variable N, calculated to N_FACTORS */
875 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
882 assert (n < eval->size);
883 assert (n < evec->size1);
884 assert (n_factors <= eval->size);
886 for (i = 0 ; i < n_factors; ++i)
888 double evali = fabs (gsl_vector_get (eval, i));
890 double eveci = gsl_matrix_get (evec, n, i);
892 comm += pow2 (eveci) * evali;
898 /* Return the communality of variable N, calculated to N_FACTORS */
900 communality (struct idata *idata, int n, int n_factors)
902 return the_communality (idata->evec, idata->eval, n, n_factors);
908 show_communalities (const struct cmd_factor * factor,
909 const gsl_vector *initial, const gsl_vector *extracted)
913 const int heading_columns = 1;
914 int nc = heading_columns;
915 const int heading_rows = 1;
916 const int nr = heading_rows + factor->n_vars;
919 if (factor->print & PRINT_EXTRACTION)
922 if (factor->print & PRINT_INITIAL)
925 /* No point having a table with only headings */
929 t = tab_create (nc, nr, 0);
931 tab_title (t, _("Communalities"));
933 tab_dim (t, tab_natural_dimensions, NULL);
935 tab_headers (t, heading_columns, 0, heading_rows, 0);
938 if (factor->print & PRINT_INITIAL)
939 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
941 if (factor->print & PRINT_EXTRACTION)
942 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
944 /* Outline the box */
958 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
959 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
961 for (i = 0 ; i < factor->n_vars; ++i)
964 tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
966 if (factor->print & PRINT_INITIAL)
967 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL);
969 if (factor->print & PRINT_EXTRACTION)
970 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL);
978 show_factor_matrix (const struct cmd_factor *factor, struct idata *idata, const gsl_matrix *fm)
981 const int n_factors = n_extracted_factors (factor, idata);
983 const int heading_columns = 1;
984 const int heading_rows = 2;
985 const int nr = heading_rows + factor->n_vars;
986 const int nc = heading_columns + n_factors;
987 gsl_permutation *perm;
989 struct tab_table *t = tab_create (nc, nr, 0);
991 if ( factor->extraction == EXTRACTION_PC )
992 tab_title (t, _("Component Matrix"));
994 tab_title (t, _("Factor Matrix"));
996 tab_dim (t, tab_natural_dimensions, NULL);
998 tab_headers (t, heading_columns, 0, heading_rows, 0);
1000 if ( factor->extraction == EXTRACTION_PC )
1004 TAB_CENTER | TAT_TITLE, _("Component"));
1009 TAB_CENTER | TAT_TITLE, _("Factor"));
1012 tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1015 /* Outline the box */
1022 /* Vertical lines */
1029 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1030 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1034 gsl_vector_const_view r1 = gsl_matrix_const_row (fm, 0);
1035 gsl_vector_const_view r2 = gsl_matrix_const_row (fm, 1);
1038 /* Initialise to the identity permutation */
1039 perm = gsl_permutation_calloc (factor->n_vars);
1042 sort_matrix_indirect (fm, perm);
1044 for (i = 0 ; i < n_factors; ++i)
1046 tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1049 for (i = 0 ; i < factor->n_vars; ++i)
1052 const int matrix_row = perm->data[i];
1053 tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1055 for (j = 0 ; j < n_factors; ++j)
1057 double x = gsl_matrix_get (fm, matrix_row, j);
1059 if ( fabs (x) < factor->blank)
1062 tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL);
1066 gsl_permutation_free (perm);
1073 show_explained_variance (const struct cmd_factor * factor, struct idata *idata,
1074 const gsl_vector *initial_eigenvalues,
1075 const gsl_vector *extracted_eigenvalues)
1079 const int heading_columns = 1;
1080 const int heading_rows = 2;
1081 const int nr = heading_rows + factor->n_vars;
1083 struct tab_table *t ;
1085 double i_total = 0.0;
1088 double e_total = 0.0;
1091 int nc = heading_columns;
1093 if (factor->print & PRINT_EXTRACTION)
1096 if (factor->print & PRINT_INITIAL)
1099 if (factor->print & PRINT_ROTATION)
1102 /* No point having a table with only headings */
1103 if ( nc <= heading_columns)
1106 t = tab_create (nc, nr, 0);
1108 tab_title (t, _("Total Variance Explained"));
1110 tab_dim (t, tab_natural_dimensions, NULL);
1112 tab_headers (t, heading_columns, 0, heading_rows, 0);
1114 /* Outline the box */
1121 /* Vertical lines */
1128 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1129 tab_hline (t, TAL_1, 1, nc - 1, 1);
1131 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1134 if ( factor->extraction == EXTRACTION_PC)
1135 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1137 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1140 if (factor->print & PRINT_INITIAL)
1142 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1146 if (factor->print & PRINT_EXTRACTION)
1148 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1152 if (factor->print & PRINT_ROTATION)
1154 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1158 for (i = 0; i < (nc - heading_columns) / 3 ; ++i)
1160 tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1161 tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1162 tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1164 tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1167 for (i = 0 ; i < initial_eigenvalues->size; ++i)
1168 i_total += gsl_vector_get (initial_eigenvalues, i);
1170 if ( factor->extraction == EXTRACTION_PAF)
1172 e_total = factor->n_vars;
1180 for (i = 0 ; i < factor->n_vars; ++i)
1182 const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1183 double i_percent = 100.0 * i_lambda / i_total ;
1185 const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1186 double e_percent = 100.0 * e_lambda / e_total ;
1190 tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%d"), i + 1);
1195 /* Initial Eigenvalues */
1196 if (factor->print & PRINT_INITIAL)
1198 tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL);
1199 tab_double (t, c++, i + heading_rows, 0, i_percent, NULL);
1200 tab_double (t, c++, i + heading_rows, 0, i_cum, NULL);
1203 if (factor->print & PRINT_EXTRACTION)
1205 if ( i < n_extracted_factors (factor, idata))
1207 /* Sums of squared loadings */
1208 tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL);
1209 tab_double (t, c++, i + heading_rows, 0, e_percent, NULL);
1210 tab_double (t, c++, i + heading_rows, 0, e_cum, NULL);
1221 do_factor (const struct cmd_factor *factor, struct casereader *r)
1224 const gsl_matrix *cov_matrix;
1225 const gsl_matrix *var_matrix;
1226 const gsl_matrix *mean_matrix;
1227 const gsl_matrix *n_matrix;
1229 const gsl_matrix *analysis_matrix;
1230 struct idata *idata;
1232 struct covariance *cov = covariance_create (factor->n_vars, factor->vars,
1233 factor->wv, factor->exclude);
1235 for ( ; (c = casereader_read (r) ); case_unref (c))
1237 covariance_accumulate (cov, c);
1240 cov_matrix = covariance_calculate (cov);
1242 var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
1243 mean_matrix = covariance_moments (cov, MOMENT_MEAN);
1244 n_matrix = covariance_moments (cov, MOMENT_NONE);
1246 if ( factor->method == METHOD_CORR)
1248 analysis_matrix = correlation_from_covariance (cov_matrix, var_matrix);
1251 analysis_matrix = cov_matrix;
1253 if ( factor->print & PRINT_UNIVARIATE)
1257 const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
1260 const int heading_columns = 1;
1261 const int heading_rows = 1;
1263 const int nr = heading_rows + factor->n_vars;
1265 struct tab_table *t = tab_create (nc, nr, 0);
1266 tab_title (t, _("Descriptive Statistics"));
1267 tab_dim (t, tab_natural_dimensions, NULL);
1269 tab_headers (t, heading_columns, 0, heading_rows, 0);
1271 /* Outline the box */
1278 /* Vertical lines */
1285 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1286 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1288 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
1289 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1290 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
1292 for (i = 0 ; i < factor->n_vars; ++i)
1294 const struct variable *v = factor->vars[i];
1295 tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
1297 tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (mean_matrix, i, i), NULL);
1298 tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (var_matrix, i, i)), NULL);
1299 tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (n_matrix, i, i), wfmt);
1305 if ( factor->print & PRINT_DETERMINANT)
1308 const int heading_columns = 0;
1309 const int heading_rows = 0;
1311 struct tab_table *t ;
1315 const int size = analysis_matrix->size1;
1316 gsl_permutation *p = gsl_permutation_calloc (size);
1317 gsl_matrix *tmp = gsl_matrix_calloc (size, size);
1319 gsl_matrix_memcpy (tmp, analysis_matrix);
1320 gsl_linalg_LU_decomp (tmp, p, &sign);
1321 det = gsl_linalg_LU_det (tmp, sign);
1322 gsl_permutation_free (p);
1323 gsl_matrix_free (tmp);
1325 t = tab_create (nc, nr, 0);
1327 if ( factor->method == METHOD_CORR)
1328 tab_title (t, _("Correlation Matrix"));
1330 tab_title (t, _("Covariance Matrix"));
1332 tab_dim (t, tab_natural_dimensions, NULL);
1334 tab_headers (t, heading_columns, 0, heading_rows, 0);
1336 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1338 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Determinant"));
1339 tab_double (t, 1, 0, 0, det, NULL);
1345 idata = idata_alloc (factor->n_vars);
1350 gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
1352 gsl_eigen_symmv (matrix_dup (analysis_matrix), idata->eval, idata->evec, workspace);
1354 gsl_eigen_symmv_free (workspace);
1357 gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
1361 const gsl_vector *extracted_eigenvalues = NULL;
1362 gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
1363 gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
1365 struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, n_extracted_factors (factor, idata));
1366 gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
1368 if ( factor->extraction == EXTRACTION_PAF)
1370 gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
1371 struct smr_workspace *ws = ws_create (analysis_matrix);
1373 for (i = 0 ; i < factor->n_vars ; ++i)
1375 double r2 = squared_multiple_correlation (analysis_matrix, i, ws);
1377 gsl_vector_set (idata->msr, i, r2);
1381 gsl_vector_memcpy (initial_communalities, idata->msr);
1383 for (i = 0; i < factor->iterations; ++i)
1386 gsl_vector_memcpy (diff, idata->msr);
1388 iterate_factor_matrix (analysis_matrix, idata->msr, factor_matrix, fmw);
1390 gsl_vector_sub (diff, idata->msr);
1392 gsl_vector_minmax (diff, &min, &max);
1394 if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
1397 gsl_vector_free (diff);
1399 gsl_vector_memcpy (extracted_communalities, idata->msr);
1400 extracted_eigenvalues = fmw->eval;
1402 else if (factor->extraction == EXTRACTION_PC)
1404 for (i = 0 ; i < factor->n_vars; ++i)
1406 gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
1408 gsl_vector_memcpy (extracted_communalities, initial_communalities);
1410 iterate_factor_matrix (analysis_matrix, extracted_communalities, factor_matrix, fmw);
1411 extracted_eigenvalues = idata->eval;
1414 show_communalities (factor, initial_communalities, extracted_communalities);
1416 show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues);
1418 factor_matrix_workspace_free (fmw);
1420 show_factor_matrix (factor, idata, factor_matrix);
1422 gsl_vector_free (initial_communalities);
1423 gsl_vector_free (extracted_communalities);
1428 casereader_destroy (r);