1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010 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/tab.h>
49 #include <output/charts/scree.h>
50 #include <output/chart-item.h>
53 #define _(msgid) gettext (msgid)
54 #define N_(msgid) msgid
69 enum extraction_method
78 PLOT_ROTATION = 0x0002
83 PRINT_UNIVARIATE = 0x0001,
84 PRINT_DETERMINANT = 0x0002,
88 PRINT_COVARIANCE = 0x0020,
89 PRINT_CORRELATION = 0x0040,
90 PRINT_ROTATION = 0x0080,
91 PRINT_EXTRACTION = 0x0100,
92 PRINT_INITIAL = 0x0200,
102 const struct variable **vars;
104 const struct variable *wv;
107 enum missing_type missing_type;
108 enum mv_class exclude;
109 enum print_opts print;
110 enum extraction_method extraction;
113 /* Extraction Criteria */
126 /* Intermediate values used in calculation */
128 const gsl_matrix *corr ; /* The correlation matrix */
129 const gsl_matrix *cov ; /* The covariance matrix */
130 const gsl_matrix *n ; /* Matrix of number of samples */
132 gsl_vector *eval ; /* The eigenvalues */
133 gsl_matrix *evec ; /* The eigenvectors */
137 gsl_vector *msr ; /* Multiple Squared Regressions */
140 static struct idata *
141 idata_alloc (size_t n_vars)
143 struct idata *id = xzalloc (sizeof (*id));
145 id->n_extractions = 0;
146 id->msr = gsl_vector_alloc (n_vars);
148 id->eval = gsl_vector_alloc (n_vars);
149 id->evec = gsl_matrix_alloc (n_vars, n_vars);
155 idata_free (struct idata *id)
157 gsl_vector_free (id->msr);
158 gsl_vector_free (id->eval);
159 gsl_matrix_free (id->evec);
166 dump_matrix (const gsl_matrix *m)
170 for (i = 0 ; i < m->size1; ++i)
172 for (j = 0 ; j < m->size2; ++j)
173 printf ("%02f ", gsl_matrix_get (m, i, j));
180 dump_matrix_permute (const gsl_matrix *m, const gsl_permutation *p)
184 for (i = 0 ; i < m->size1; ++i)
186 for (j = 0 ; j < m->size2; ++j)
187 printf ("%02f ", gsl_matrix_get (m, gsl_permutation_get (p, i), j));
194 dump_vector (const gsl_vector *v)
197 for (i = 0 ; i < v->size; ++i)
199 printf ("%02f\n", gsl_vector_get (v, i));
206 n_extracted_factors (const struct cmd_factor *factor, struct idata *idata)
210 /* If there is a cached value, then return that. */
211 if ( idata->n_extractions != 0)
212 return idata->n_extractions;
214 /* Otherwise, if the number of factors has been explicitly requested,
216 if (factor->n_factors > 0)
218 idata->n_extractions = factor->n_factors;
222 /* Use the MIN_EIGEN setting. */
223 for (i = 0 ; i < idata->eval->size; ++i)
225 double evali = fabs (gsl_vector_get (idata->eval, i));
227 idata->n_extractions = i;
229 if (evali < factor->min_eigen)
234 return idata->n_extractions;
238 /* Returns a newly allocated matrix identical to M.
239 It it the callers responsibility to free the returned value.
242 matrix_dup (const gsl_matrix *m)
244 gsl_matrix *n = gsl_matrix_alloc (m->size1, m->size2);
246 gsl_matrix_memcpy (n, m);
254 /* Copy of the subject */
259 gsl_permutation *perm;
266 static struct smr_workspace *ws_create (const gsl_matrix *input)
268 struct smr_workspace *ws = xmalloc (sizeof (*ws));
270 ws->m = gsl_matrix_alloc (input->size1, input->size2);
271 ws->inverse = gsl_matrix_calloc (input->size1 - 1, input->size2 - 1);
272 ws->perm = gsl_permutation_alloc (input->size1 - 1);
273 ws->result1 = gsl_matrix_calloc (input->size1 - 1, 1);
274 ws->result2 = gsl_matrix_calloc (1, 1);
280 ws_destroy (struct smr_workspace *ws)
282 gsl_matrix_free (ws->result2);
283 gsl_matrix_free (ws->result1);
284 gsl_permutation_free (ws->perm);
285 gsl_matrix_free (ws->inverse);
286 gsl_matrix_free (ws->m);
293 Return the square of the regression coefficient for VAR regressed against all other variables.
296 squared_multiple_correlation (const gsl_matrix *corr, int var, struct smr_workspace *ws)
298 /* For an explanation of what this is doing, see
299 http://www.visualstatistics.net/Visual%20Statistics%20Multimedia/multiple_regression_analysis.htm
305 gsl_matrix_memcpy (ws->m, corr);
307 gsl_matrix_swap_rows (ws->m, 0, var);
308 gsl_matrix_swap_columns (ws->m, 0, var);
310 rxx = gsl_matrix_submatrix (ws->m, 1, 1, ws->m->size1 - 1, ws->m->size1 - 1);
312 gsl_linalg_LU_decomp (&rxx.matrix, ws->perm, &signum);
314 gsl_linalg_LU_invert (&rxx.matrix, ws->perm, ws->inverse);
317 gsl_matrix_const_view rxy = gsl_matrix_const_submatrix (ws->m, 1, 0, ws->m->size1 - 1, 1);
318 gsl_matrix_const_view ryx = gsl_matrix_const_submatrix (ws->m, 0, 1, 1, ws->m->size1 - 1);
320 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
321 1.0, ws->inverse, &rxy.matrix, 0.0, ws->result1);
323 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
324 1.0, &ryx.matrix, ws->result1, 0.0, ws->result2);
327 return gsl_matrix_get (ws->result2, 0, 0);
332 static double the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors);
335 struct factor_matrix_workspace
338 gsl_eigen_symmv_workspace *eigen_ws;
348 static struct factor_matrix_workspace *
349 factor_matrix_workspace_alloc (size_t n, size_t nf)
351 struct factor_matrix_workspace *ws = xmalloc (sizeof (*ws));
354 ws->gamma = gsl_matrix_calloc (nf, nf);
355 ws->eigen_ws = gsl_eigen_symmv_alloc (n);
356 ws->eval = gsl_vector_alloc (n);
357 ws->evec = gsl_matrix_alloc (n, n);
358 ws->r = gsl_matrix_alloc (n, n);
364 factor_matrix_workspace_free (struct factor_matrix_workspace *ws)
366 gsl_eigen_symmv_free (ws->eigen_ws);
367 gsl_vector_free (ws->eval);
368 gsl_matrix_free (ws->evec);
369 gsl_matrix_free (ws->gamma);
370 gsl_matrix_free (ws->r);
375 Shift P left by OFFSET places, and overwrite TARGET
376 with the shifted result.
377 Positions in TARGET less than OFFSET are unchanged.
380 perm_shift_apply (gsl_permutation *target, const gsl_permutation *p,
384 assert (target->size == p->size);
385 assert (offset <= target->size);
387 for (i = 0; i < target->size - offset; ++i)
389 target->data[i] = p->data [i + offset];
395 Indirectly sort the rows of matrix INPUT, storing the sort order in PERM.
396 The sort criteria are as follows:
398 Rows are sorted on the first column, until the absolute value of an
399 element in a subsequent column is greater than that of the first
400 column. Thereafter, rows will be sorted on the second column,
401 until the absolute value of an element in a subsequent column
402 exceeds that of the second column ...
405 sort_matrix_indirect (const gsl_matrix *input, gsl_permutation *perm)
407 const size_t n = perm->size;
408 const size_t m = input->size2;
415 assert (perm->size == input->size1);
417 p = gsl_permutation_alloc (n);
419 /* Copy INPUT into MAT, discarding the sign */
420 mat = gsl_matrix_alloc (n, m);
421 for (i = 0 ; i < mat->size1; ++i)
423 for (j = 0 ; j < mat->size2; ++j)
425 double x = gsl_matrix_get (input, i, j);
426 gsl_matrix_set (mat, i, j, fabs (x));
430 while (column_n < m && row_n < n)
432 gsl_vector_const_view columni = gsl_matrix_const_column (mat, column_n);
433 gsl_sort_vector_index (p, &columni.vector);
435 for (i = 0 ; i < n; ++i)
437 gsl_vector_view row = gsl_matrix_row (mat, p->data[n - 1 - i]);
438 size_t maxindex = gsl_vector_max_index (&row.vector);
440 if ( maxindex > column_n )
443 /* All subsequent elements of this row, are of no interest.
444 So set them all to a highly negative value */
445 for (j = column_n + 1; j < row.vector.size ; ++j)
446 gsl_vector_set (&row.vector, j, -DBL_MAX);
449 perm_shift_apply (perm, p, row_n);
455 gsl_permutation_free (p);
456 gsl_matrix_free (mat);
458 assert ( 0 == gsl_permutation_valid (perm));
460 /* We want the biggest value to be first */
461 gsl_permutation_reverse (perm);
466 Get an approximation for the factor matrix into FACTORS, and the communalities into COMMUNALITIES.
467 R is the matrix to be analysed.
468 WS is a pointer to a structure which must have been initialised with factor_matrix_workspace_init.
471 iterate_factor_matrix (const gsl_matrix *r, gsl_vector *communalities, gsl_matrix *factors, struct factor_matrix_workspace *ws)
476 assert (r->size1 == r->size2);
477 assert (r->size1 == communalities->size);
479 assert (factors->size1 == r->size1);
480 assert (factors->size2 == ws->n_factors);
482 gsl_matrix_memcpy (ws->r, r);
484 /* Apply Communalities to diagonal of correlation matrix */
485 for (i = 0 ; i < communalities->size ; ++i)
487 double *x = gsl_matrix_ptr (ws->r, i, i);
488 *x = gsl_vector_get (communalities, i);
491 gsl_eigen_symmv (ws->r, ws->eval, ws->evec, ws->eigen_ws);
493 mv = gsl_matrix_submatrix (ws->evec, 0, 0, ws->evec->size1, ws->n_factors);
495 /* Gamma is the diagonal matrix containing the absolute values of the eigenvalues */
496 for (i = 0 ; i < ws->n_factors ; ++i)
498 double *ptr = gsl_matrix_ptr (ws->gamma, i, i);
499 *ptr = fabs (gsl_vector_get (ws->eval, i));
502 /* Take the square root of gamma */
503 gsl_linalg_cholesky_decomp (ws->gamma);
505 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
506 1.0, &mv.matrix, ws->gamma, 0.0, factors);
508 for (i = 0 ; i < r->size1 ; ++i)
510 double h = the_communality (ws->evec, ws->eval, i, ws->n_factors);
511 gsl_vector_set (communalities, i, h);
517 static bool run_factor (struct dataset *ds, const struct cmd_factor *factor);
521 cmd_factor (struct lexer *lexer, struct dataset *ds)
523 bool extraction_seen = false;
524 const struct dictionary *dict = dataset_dict (ds);
526 struct cmd_factor factor;
529 factor.method = METHOD_CORR;
530 factor.missing_type = MISS_LISTWISE;
531 factor.exclude = MV_ANY;
532 factor.print = PRINT_INITIAL | PRINT_EXTRACTION | PRINT_ROTATION;
533 factor.extraction = EXTRACTION_PC;
534 factor.n_factors = 0;
535 factor.min_eigen = SYSMIS;
536 factor.iterations = 25;
537 factor.econverge = 0.001;
542 factor.wv = dict_get_weight (dict);
544 lex_match (lexer, '/');
546 if (!lex_force_match_id (lexer, "VARIABLES"))
551 lex_match (lexer, '=');
553 if (!parse_variables_const (lexer, dict, &factor.vars, &factor.n_vars,
554 PV_NO_DUPLICATE | PV_NUMERIC))
557 if (factor.n_vars < 2)
558 msg (MW, _("Factor analysis on a single variable is not useful."));
560 while (lex_token (lexer) != '.')
562 lex_match (lexer, '/');
564 if (lex_match_id (lexer, "PLOT"))
566 lex_match (lexer, '=');
567 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
569 if (lex_match_id (lexer, "EIGEN"))
571 factor.plot |= PLOT_SCREE;
573 #if FACTOR_FULLY_IMPLEMENTED
574 else if (lex_match_id (lexer, "ROTATION"))
580 lex_error (lexer, NULL);
585 else if (lex_match_id (lexer, "METHOD"))
587 lex_match (lexer, '=');
588 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
590 if (lex_match_id (lexer, "COVARIANCE"))
592 factor.method = METHOD_COV;
594 else if (lex_match_id (lexer, "CORRELATION"))
596 factor.method = METHOD_CORR;
600 lex_error (lexer, NULL);
605 #if FACTOR_FULLY_IMPLEMENTED
606 else if (lex_match_id (lexer, "ROTATION"))
608 lex_match (lexer, '=');
609 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
611 if (lex_match_id (lexer, "VARIMAX"))
614 else if (lex_match_id (lexer, "DEFAULT"))
619 lex_error (lexer, NULL);
625 else if (lex_match_id (lexer, "CRITERIA"))
627 lex_match (lexer, '=');
628 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
630 if (lex_match_id (lexer, "FACTORS"))
632 if ( lex_force_match (lexer, '('))
634 lex_force_int (lexer);
635 factor.n_factors = lex_integer (lexer);
637 lex_force_match (lexer, ')');
640 else if (lex_match_id (lexer, "MINEIGEN"))
642 if ( lex_force_match (lexer, '('))
644 lex_force_num (lexer);
645 factor.min_eigen = lex_number (lexer);
647 lex_force_match (lexer, ')');
650 else if (lex_match_id (lexer, "ECONVERGE"))
652 if ( lex_force_match (lexer, '('))
654 lex_force_num (lexer);
655 factor.econverge = lex_number (lexer);
657 lex_force_match (lexer, ')');
660 else if (lex_match_id (lexer, "ITERATE"))
662 if ( lex_force_match (lexer, '('))
664 lex_force_int (lexer);
665 factor.iterations = lex_integer (lexer);
667 lex_force_match (lexer, ')');
670 else if (lex_match_id (lexer, "DEFAULT"))
672 factor.n_factors = 0;
673 factor.min_eigen = 1;
674 factor.iterations = 25;
678 lex_error (lexer, NULL);
683 else if (lex_match_id (lexer, "EXTRACTION"))
685 extraction_seen = true;
686 lex_match (lexer, '=');
687 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
689 if (lex_match_id (lexer, "PAF"))
691 factor.extraction = EXTRACTION_PAF;
693 else if (lex_match_id (lexer, "PC"))
695 factor.extraction = EXTRACTION_PC;
697 else if (lex_match_id (lexer, "PA1"))
699 factor.extraction = EXTRACTION_PC;
701 else if (lex_match_id (lexer, "DEFAULT"))
703 factor.extraction = EXTRACTION_PC;
707 lex_error (lexer, NULL);
712 else if (lex_match_id (lexer, "FORMAT"))
714 lex_match (lexer, '=');
715 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
717 if (lex_match_id (lexer, "SORT"))
721 else if (lex_match_id (lexer, "BLANK"))
723 if ( lex_force_match (lexer, '('))
725 lex_force_num (lexer);
726 factor.blank = lex_number (lexer);
728 lex_force_match (lexer, ')');
731 else if (lex_match_id (lexer, "DEFAULT"))
738 lex_error (lexer, NULL);
743 else if (lex_match_id (lexer, "PRINT"))
746 lex_match (lexer, '=');
747 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
749 if (lex_match_id (lexer, "UNIVARIATE"))
751 factor.print |= PRINT_UNIVARIATE;
753 else if (lex_match_id (lexer, "DET"))
755 factor.print |= PRINT_DETERMINANT;
757 #if FACTOR_FULLY_IMPLEMENTED
758 else if (lex_match_id (lexer, "INV"))
761 else if (lex_match_id (lexer, "AIC"))
765 else if (lex_match_id (lexer, "SIG"))
767 factor.print |= PRINT_SIG;
769 else if (lex_match_id (lexer, "CORRELATION"))
771 factor.print |= PRINT_CORRELATION;
773 #if FACTOR_FULLY_IMPLEMENTED
774 else if (lex_match_id (lexer, "COVARIANCE"))
778 else if (lex_match_id (lexer, "ROTATION"))
780 factor.print |= PRINT_ROTATION;
782 else if (lex_match_id (lexer, "EXTRACTION"))
784 factor.print |= PRINT_EXTRACTION;
786 else if (lex_match_id (lexer, "INITIAL"))
788 factor.print |= PRINT_INITIAL;
790 #if FACTOR_FULLY_IMPLEMENTED
791 else if (lex_match_id (lexer, "KMO"))
794 else if (lex_match_id (lexer, "REPR"))
797 else if (lex_match_id (lexer, "FSCORE"))
801 else if (lex_match (lexer, T_ALL))
803 factor.print = 0xFFFF;
805 else if (lex_match_id (lexer, "DEFAULT"))
807 factor.print |= PRINT_INITIAL ;
808 factor.print |= PRINT_EXTRACTION ;
809 factor.print |= PRINT_ROTATION ;
813 lex_error (lexer, NULL);
818 else if (lex_match_id (lexer, "MISSING"))
820 lex_match (lexer, '=');
821 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
823 if (lex_match_id (lexer, "INCLUDE"))
825 factor.exclude = MV_SYSTEM;
827 else if (lex_match_id (lexer, "EXCLUDE"))
829 factor.exclude = MV_ANY;
831 else if (lex_match_id (lexer, "LISTWISE"))
833 factor.missing_type = MISS_LISTWISE;
835 else if (lex_match_id (lexer, "PAIRWISE"))
837 factor.missing_type = MISS_PAIRWISE;
839 else if (lex_match_id (lexer, "MEANSUB"))
841 factor.missing_type = MISS_MEANSUB;
845 lex_error (lexer, NULL);
852 lex_error (lexer, NULL);
857 if ( ! run_factor (ds, &factor))
868 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
872 run_factor (struct dataset *ds, const struct cmd_factor *factor)
874 struct dictionary *dict = dataset_dict (ds);
876 struct casereader *group;
878 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
880 while (casegrouper_get_next_group (grouper, &group))
882 if ( factor->missing_type == MISS_LISTWISE )
883 group = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
886 do_factor (factor, group);
889 ok = casegrouper_destroy (grouper);
890 ok = proc_commit (ds) && ok;
896 /* Return the communality of variable N, calculated to N_FACTORS */
898 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
905 assert (n < eval->size);
906 assert (n < evec->size1);
907 assert (n_factors <= eval->size);
909 for (i = 0 ; i < n_factors; ++i)
911 double evali = fabs (gsl_vector_get (eval, i));
913 double eveci = gsl_matrix_get (evec, n, i);
915 comm += pow2 (eveci) * evali;
921 /* Return the communality of variable N, calculated to N_FACTORS */
923 communality (struct idata *idata, int n, int n_factors)
925 return the_communality (idata->evec, idata->eval, n, n_factors);
930 show_scree (const struct cmd_factor *f, struct idata *idata)
935 if ( !(f->plot & PLOT_SCREE) )
939 label = f->extraction == EXTRACTION_PC ? _("Component Number") : _("Factor Number");
941 s = scree_create (idata->eval, label);
947 show_communalities (const struct cmd_factor * factor,
948 const gsl_vector *initial, const gsl_vector *extracted)
952 const int heading_columns = 1;
953 int nc = heading_columns;
954 const int heading_rows = 1;
955 const int nr = heading_rows + factor->n_vars;
958 if (factor->print & PRINT_EXTRACTION)
961 if (factor->print & PRINT_INITIAL)
964 /* No point having a table with only headings */
968 t = tab_create (nc, nr);
970 tab_title (t, _("Communalities"));
972 tab_headers (t, heading_columns, 0, heading_rows, 0);
975 if (factor->print & PRINT_INITIAL)
976 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
978 if (factor->print & PRINT_EXTRACTION)
979 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
981 /* Outline the box */
995 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
996 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
998 for (i = 0 ; i < factor->n_vars; ++i)
1001 tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
1003 if (factor->print & PRINT_INITIAL)
1004 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL);
1006 if (factor->print & PRINT_EXTRACTION)
1007 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL);
1015 show_factor_matrix (const struct cmd_factor *factor, struct idata *idata, const gsl_matrix *fm)
1018 const int n_factors = idata->n_extractions;
1020 const int heading_columns = 1;
1021 const int heading_rows = 2;
1022 const int nr = heading_rows + factor->n_vars;
1023 const int nc = heading_columns + n_factors;
1024 gsl_permutation *perm;
1026 struct tab_table *t = tab_create (nc, nr);
1028 if ( factor->extraction == EXTRACTION_PC )
1029 tab_title (t, _("Component Matrix"));
1031 tab_title (t, _("Factor Matrix"));
1033 tab_headers (t, heading_columns, 0, heading_rows, 0);
1035 if ( factor->extraction == EXTRACTION_PC )
1039 TAB_CENTER | TAT_TITLE, _("Component"));
1044 TAB_CENTER | TAT_TITLE, _("Factor"));
1047 tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1050 /* Outline the box */
1057 /* Vertical lines */
1064 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1065 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1068 /* Initialise to the identity permutation */
1069 perm = gsl_permutation_calloc (factor->n_vars);
1072 sort_matrix_indirect (fm, perm);
1074 for (i = 0 ; i < n_factors; ++i)
1076 tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1079 for (i = 0 ; i < factor->n_vars; ++i)
1082 const int matrix_row = perm->data[i];
1083 tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1085 for (j = 0 ; j < n_factors; ++j)
1087 double x = gsl_matrix_get (fm, matrix_row, j);
1089 if ( fabs (x) < factor->blank)
1092 tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL);
1096 gsl_permutation_free (perm);
1103 show_explained_variance (const struct cmd_factor * factor, struct idata *idata,
1104 const gsl_vector *initial_eigenvalues,
1105 const gsl_vector *extracted_eigenvalues)
1109 const int heading_columns = 1;
1110 const int heading_rows = 2;
1111 const int nr = heading_rows + factor->n_vars;
1113 struct tab_table *t ;
1115 double i_total = 0.0;
1118 double e_total = 0.0;
1121 int nc = heading_columns;
1123 if (factor->print & PRINT_EXTRACTION)
1126 if (factor->print & PRINT_INITIAL)
1129 if (factor->print & PRINT_ROTATION)
1132 /* No point having a table with only headings */
1133 if ( nc <= heading_columns)
1136 t = tab_create (nc, nr);
1138 tab_title (t, _("Total Variance Explained"));
1140 tab_headers (t, heading_columns, 0, heading_rows, 0);
1142 /* Outline the box */
1149 /* Vertical lines */
1156 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1157 tab_hline (t, TAL_1, 1, nc - 1, 1);
1159 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1162 if ( factor->extraction == EXTRACTION_PC)
1163 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1165 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1168 if (factor->print & PRINT_INITIAL)
1170 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1174 if (factor->print & PRINT_EXTRACTION)
1176 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1180 if (factor->print & PRINT_ROTATION)
1182 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1186 for (i = 0; i < (nc - heading_columns) / 3 ; ++i)
1188 tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1189 /* xgettext:no-c-format */
1190 tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1191 tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1193 tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1196 for (i = 0 ; i < initial_eigenvalues->size; ++i)
1197 i_total += gsl_vector_get (initial_eigenvalues, i);
1199 if ( factor->extraction == EXTRACTION_PAF)
1201 e_total = factor->n_vars;
1209 for (i = 0 ; i < factor->n_vars; ++i)
1211 const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1212 double i_percent = 100.0 * i_lambda / i_total ;
1214 const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1215 double e_percent = 100.0 * e_lambda / e_total ;
1219 tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%d"), i + 1);
1224 /* Initial Eigenvalues */
1225 if (factor->print & PRINT_INITIAL)
1227 tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL);
1228 tab_double (t, c++, i + heading_rows, 0, i_percent, NULL);
1229 tab_double (t, c++, i + heading_rows, 0, i_cum, NULL);
1232 if (factor->print & PRINT_EXTRACTION)
1234 if (i < idata->n_extractions)
1236 /* Sums of squared loadings */
1237 tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL);
1238 tab_double (t, c++, i + heading_rows, 0, e_percent, NULL);
1239 tab_double (t, c++, i + heading_rows, 0, e_cum, NULL);
1249 show_correlation_matrix (const struct cmd_factor *factor, const struct idata *idata)
1251 struct tab_table *t ;
1253 int y_pos_corr = -1;
1255 int suffix_rows = 0;
1257 const int heading_rows = 1;
1258 const int heading_columns = 2;
1260 int nc = heading_columns ;
1261 int nr = heading_rows ;
1262 int n_data_sets = 0;
1264 if (factor->print & PRINT_CORRELATION)
1266 y_pos_corr = n_data_sets;
1268 nc = heading_columns + factor->n_vars;
1271 if (factor->print & PRINT_SIG)
1273 y_pos_sig = n_data_sets;
1275 nc = heading_columns + factor->n_vars;
1278 nr += n_data_sets * factor->n_vars;
1280 if (factor->print & PRINT_DETERMINANT)
1283 /* If the table would contain only headings, don't bother rendering it */
1284 if (nr <= heading_rows && suffix_rows == 0)
1287 t = tab_create (nc, nr + suffix_rows);
1289 tab_title (t, _("Correlation Matrix"));
1291 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1293 if (nr > heading_rows)
1295 tab_headers (t, heading_columns, 0, heading_rows, 0);
1297 tab_vline (t, TAL_2, 2, 0, nr - 1);
1299 /* Outline the box */
1306 /* Vertical lines */
1314 for (i = 0; i < factor->n_vars; ++i)
1315 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
1318 for (i = 0 ; i < n_data_sets; ++i)
1320 int y = heading_rows + i * factor->n_vars;
1322 for (v = 0; v < factor->n_vars; ++v)
1323 tab_text (t, 1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
1325 tab_hline (t, TAL_1, 0, nc - 1, y);
1328 if (factor->print & PRINT_CORRELATION)
1330 const double y = heading_rows + y_pos_corr;
1331 tab_text (t, 0, y, TAT_TITLE, _("Correlations"));
1333 for (i = 0; i < factor->n_vars; ++i)
1335 for (j = 0; j < factor->n_vars; ++j)
1336 tab_double (t, heading_columns + i, y + j, 0, gsl_matrix_get (idata->corr, i, j), NULL);
1340 if (factor->print & PRINT_SIG)
1342 const double y = heading_rows + y_pos_sig * factor->n_vars;
1343 tab_text (t, 0, y, TAT_TITLE, _("Sig. 1-tailed"));
1345 for (i = 0; i < factor->n_vars; ++i)
1347 for (j = 0; j < factor->n_vars; ++j)
1349 double rho = gsl_matrix_get (idata->corr, i, j);
1350 double w = gsl_matrix_get (idata->n, i, j);
1355 tab_double (t, heading_columns + i, y + j, 0, significance_of_correlation (rho, w), NULL);
1361 if (factor->print & PRINT_DETERMINANT)
1366 const int size = idata->corr->size1;
1367 gsl_permutation *p = gsl_permutation_calloc (size);
1368 gsl_matrix *tmp = gsl_matrix_calloc (size, size);
1369 gsl_matrix_memcpy (tmp, idata->corr);
1371 gsl_linalg_LU_decomp (tmp, p, &sign);
1372 det = gsl_linalg_LU_det (tmp, sign);
1373 gsl_permutation_free (p);
1374 gsl_matrix_free (tmp);
1377 tab_text (t, 0, nr, TAB_LEFT | TAT_TITLE, _("Determinant"));
1378 tab_double (t, 1, nr, 0, det, NULL);
1387 do_factor (const struct cmd_factor *factor, struct casereader *r)
1390 const gsl_matrix *var_matrix;
1391 const gsl_matrix *mean_matrix;
1393 const gsl_matrix *analysis_matrix;
1394 struct idata *idata = idata_alloc (factor->n_vars);
1396 struct covariance *cov = covariance_create (factor->n_vars, factor->vars,
1397 factor->wv, factor->exclude);
1399 for ( ; (c = casereader_read (r) ); case_unref (c))
1401 covariance_accumulate (cov, c);
1404 idata->cov = covariance_calculate (cov);
1406 var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
1407 mean_matrix = covariance_moments (cov, MOMENT_MEAN);
1408 idata->n = covariance_moments (cov, MOMENT_NONE);
1410 if ( factor->method == METHOD_CORR)
1412 idata->corr = correlation_from_covariance (idata->cov, var_matrix);
1413 analysis_matrix = idata->corr;
1416 analysis_matrix = idata->cov;
1418 if ( factor->print & PRINT_UNIVARIATE)
1422 const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
1425 const int heading_columns = 1;
1426 const int heading_rows = 1;
1428 const int nr = heading_rows + factor->n_vars;
1430 struct tab_table *t = tab_create (nc, nr);
1431 tab_title (t, _("Descriptive Statistics"));
1433 tab_headers (t, heading_columns, 0, heading_rows, 0);
1435 /* Outline the box */
1442 /* Vertical lines */
1449 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1450 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1452 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
1453 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1454 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
1456 for (i = 0 ; i < factor->n_vars; ++i)
1458 const struct variable *v = factor->vars[i];
1459 tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
1461 tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (mean_matrix, i, i), NULL);
1462 tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (var_matrix, i, i)), NULL);
1463 tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (idata->n, i, i), wfmt);
1469 show_correlation_matrix (factor, idata);
1473 gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
1475 gsl_eigen_symmv (matrix_dup (analysis_matrix), idata->eval, idata->evec, workspace);
1477 gsl_eigen_symmv_free (workspace);
1480 gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
1483 idata->n_extractions = n_extracted_factors (factor, idata);
1485 if (idata->n_extractions == 0)
1487 msg (MW, _("The FACTOR criteria result in zero factors extracted. Therefore no analysis will be performed."));
1491 if (idata->n_extractions > factor->n_vars)
1493 msg (MW, _("The FACTOR criteria result in more factors than variables, which is not meaningful. No analysis will be performed."));
1498 const gsl_vector *extracted_eigenvalues = NULL;
1499 gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
1500 gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
1502 struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, idata->n_extractions);
1503 gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
1505 if ( factor->extraction == EXTRACTION_PAF)
1507 gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
1508 struct smr_workspace *ws = ws_create (analysis_matrix);
1510 for (i = 0 ; i < factor->n_vars ; ++i)
1512 double r2 = squared_multiple_correlation (analysis_matrix, i, ws);
1514 gsl_vector_set (idata->msr, i, r2);
1518 gsl_vector_memcpy (initial_communalities, idata->msr);
1520 for (i = 0; i < factor->iterations; ++i)
1523 gsl_vector_memcpy (diff, idata->msr);
1525 iterate_factor_matrix (analysis_matrix, idata->msr, factor_matrix, fmw);
1527 gsl_vector_sub (diff, idata->msr);
1529 gsl_vector_minmax (diff, &min, &max);
1531 if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
1534 gsl_vector_free (diff);
1536 gsl_vector_memcpy (extracted_communalities, idata->msr);
1537 extracted_eigenvalues = fmw->eval;
1539 else if (factor->extraction == EXTRACTION_PC)
1541 for (i = 0; i < factor->n_vars; ++i)
1542 gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
1544 gsl_vector_memcpy (extracted_communalities, initial_communalities);
1546 iterate_factor_matrix (analysis_matrix, extracted_communalities, factor_matrix, fmw);
1547 extracted_eigenvalues = idata->eval;
1550 show_communalities (factor, initial_communalities, extracted_communalities);
1552 show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues);
1554 factor_matrix_workspace_free (fmw);
1556 show_scree (factor, idata);
1558 show_factor_matrix (factor, idata, factor_matrix);
1560 gsl_vector_free (initial_communalities);
1561 gsl_vector_free (extracted_communalities);
1568 casereader_destroy (r);