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 if (factor.n_vars < 2)
546 msg (MW, _("Factor analysis on a single variable is not useful."));
548 while (lex_token (lexer) != '.')
550 lex_match (lexer, '/');
553 if (lex_match_id (lexer, "PLOT"))
555 lex_match (lexer, '=');
556 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
558 if (lex_match_id (lexer, "EIGEN"))
561 #if FACTOR_FULLY_IMPLEMENTED
562 else if (lex_match_id (lexer, "ROTATION"))
568 lex_error (lexer, NULL);
573 else if (lex_match_id (lexer, "METHOD"))
575 lex_match (lexer, '=');
576 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
578 if (lex_match_id (lexer, "COVARIANCE"))
580 factor.method = METHOD_COV;
582 else if (lex_match_id (lexer, "CORRELATION"))
584 factor.method = METHOD_CORR;
588 lex_error (lexer, NULL);
593 #if FACTOR_FULLY_IMPLEMENTED
594 else if (lex_match_id (lexer, "ROTATION"))
596 lex_match (lexer, '=');
597 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
599 if (lex_match_id (lexer, "VARIMAX"))
602 else if (lex_match_id (lexer, "DEFAULT"))
607 lex_error (lexer, NULL);
613 else if (lex_match_id (lexer, "CRITERIA"))
615 lex_match (lexer, '=');
616 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
618 if (lex_match_id (lexer, "FACTORS"))
620 if ( lex_force_match (lexer, '('))
622 lex_force_int (lexer);
623 factor.n_factors = lex_integer (lexer);
625 lex_force_match (lexer, ')');
628 else if (lex_match_id (lexer, "MINEIGEN"))
630 if ( lex_force_match (lexer, '('))
632 lex_force_num (lexer);
633 factor.min_eigen = lex_number (lexer);
635 lex_force_match (lexer, ')');
638 else if (lex_match_id (lexer, "ECONVERGE"))
640 if ( lex_force_match (lexer, '('))
642 lex_force_num (lexer);
643 factor.econverge = lex_number (lexer);
645 lex_force_match (lexer, ')');
648 else if (lex_match_id (lexer, "ITERATE"))
650 if ( lex_force_match (lexer, '('))
652 lex_force_int (lexer);
653 factor.iterations = lex_integer (lexer);
655 lex_force_match (lexer, ')');
658 else if (lex_match_id (lexer, "DEFAULT"))
660 factor.n_factors = 0;
661 factor.min_eigen = 1;
662 factor.iterations = 25;
666 lex_error (lexer, NULL);
671 else if (lex_match_id (lexer, "EXTRACTION"))
673 extraction_seen = true;
674 lex_match (lexer, '=');
675 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
677 if (lex_match_id (lexer, "PAF"))
679 factor.extraction = EXTRACTION_PAF;
681 else if (lex_match_id (lexer, "PC"))
683 factor.extraction = EXTRACTION_PC;
685 else if (lex_match_id (lexer, "PA1"))
687 factor.extraction = EXTRACTION_PC;
689 else if (lex_match_id (lexer, "DEFAULT"))
691 factor.extraction = EXTRACTION_PC;
695 lex_error (lexer, NULL);
700 else if (lex_match_id (lexer, "FORMAT"))
702 lex_match (lexer, '=');
703 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
705 if (lex_match_id (lexer, "SORT"))
709 else if (lex_match_id (lexer, "BLANK"))
711 if ( lex_force_match (lexer, '('))
713 lex_force_num (lexer);
714 factor.blank = lex_number (lexer);
716 lex_force_match (lexer, ')');
719 else if (lex_match_id (lexer, "DEFAULT"))
726 lex_error (lexer, NULL);
731 else if (lex_match_id (lexer, "PRINT"))
734 lex_match (lexer, '=');
735 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
737 if (lex_match_id (lexer, "UNIVARIATE"))
739 factor.print |= PRINT_UNIVARIATE;
741 else if (lex_match_id (lexer, "DET"))
743 factor.print |= PRINT_DETERMINANT;
745 #if FACTOR_FULLY_IMPLEMENTED
746 else if (lex_match_id (lexer, "INV"))
749 else if (lex_match_id (lexer, "AIC"))
753 else if (lex_match_id (lexer, "SIG"))
755 factor.print |= PRINT_SIG;
757 else if (lex_match_id (lexer, "CORRELATION"))
759 factor.print |= PRINT_CORRELATION;
761 #if FACTOR_FULLY_IMPLEMENTED
762 else if (lex_match_id (lexer, "COVARIANCE"))
766 else if (lex_match_id (lexer, "ROTATION"))
768 factor.print |= PRINT_ROTATION;
770 else if (lex_match_id (lexer, "EXTRACTION"))
772 factor.print |= PRINT_EXTRACTION;
774 else if (lex_match_id (lexer, "INITIAL"))
776 factor.print |= PRINT_INITIAL;
778 #if FACTOR_FULLY_IMPLEMENTED
779 else if (lex_match_id (lexer, "KMO"))
782 else if (lex_match_id (lexer, "REPR"))
785 else if (lex_match_id (lexer, "FSCORE"))
789 else if (lex_match (lexer, T_ALL))
791 factor.print = 0xFFFF;
793 else if (lex_match_id (lexer, "DEFAULT"))
795 factor.print |= PRINT_INITIAL ;
796 factor.print |= PRINT_EXTRACTION ;
797 factor.print |= PRINT_ROTATION ;
801 lex_error (lexer, NULL);
806 else if (lex_match_id (lexer, "MISSING"))
808 lex_match (lexer, '=');
809 while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
811 if (lex_match_id (lexer, "INCLUDE"))
813 factor.exclude = MV_SYSTEM;
815 else if (lex_match_id (lexer, "EXCLUDE"))
817 factor.exclude = MV_ANY;
819 else if (lex_match_id (lexer, "LISTWISE"))
821 factor.missing_type = MISS_LISTWISE;
823 else if (lex_match_id (lexer, "PAIRWISE"))
825 factor.missing_type = MISS_PAIRWISE;
827 else if (lex_match_id (lexer, "MEANSUB"))
829 factor.missing_type = MISS_MEANSUB;
833 lex_error (lexer, NULL);
840 lex_error (lexer, NULL);
845 if ( ! run_factor (ds, &factor))
856 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
860 run_factor (struct dataset *ds, const struct cmd_factor *factor)
862 struct dictionary *dict = dataset_dict (ds);
864 struct casereader *group;
866 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
868 while (casegrouper_get_next_group (grouper, &group))
870 if ( factor->missing_type == MISS_LISTWISE )
871 group = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
874 do_factor (factor, group);
877 ok = casegrouper_destroy (grouper);
878 ok = proc_commit (ds) && ok;
884 /* Return the communality of variable N, calculated to N_FACTORS */
886 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
893 assert (n < eval->size);
894 assert (n < evec->size1);
895 assert (n_factors <= eval->size);
897 for (i = 0 ; i < n_factors; ++i)
899 double evali = fabs (gsl_vector_get (eval, i));
901 double eveci = gsl_matrix_get (evec, n, i);
903 comm += pow2 (eveci) * evali;
909 /* Return the communality of variable N, calculated to N_FACTORS */
911 communality (struct idata *idata, int n, int n_factors)
913 return the_communality (idata->evec, idata->eval, n, n_factors);
919 show_communalities (const struct cmd_factor * factor,
920 const gsl_vector *initial, const gsl_vector *extracted)
924 const int heading_columns = 1;
925 int nc = heading_columns;
926 const int heading_rows = 1;
927 const int nr = heading_rows + factor->n_vars;
930 if (factor->print & PRINT_EXTRACTION)
933 if (factor->print & PRINT_INITIAL)
936 /* No point having a table with only headings */
940 t = tab_create (nc, nr, 0);
942 tab_title (t, _("Communalities"));
944 tab_dim (t, tab_natural_dimensions, NULL);
946 tab_headers (t, heading_columns, 0, heading_rows, 0);
949 if (factor->print & PRINT_INITIAL)
950 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
952 if (factor->print & PRINT_EXTRACTION)
953 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
955 /* Outline the box */
969 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
970 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
972 for (i = 0 ; i < factor->n_vars; ++i)
975 tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
977 if (factor->print & PRINT_INITIAL)
978 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL);
980 if (factor->print & PRINT_EXTRACTION)
981 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL);
989 show_factor_matrix (const struct cmd_factor *factor, struct idata *idata, const gsl_matrix *fm)
992 const int n_factors = idata->n_extractions;
994 const int heading_columns = 1;
995 const int heading_rows = 2;
996 const int nr = heading_rows + factor->n_vars;
997 const int nc = heading_columns + n_factors;
998 gsl_permutation *perm;
1000 struct tab_table *t = tab_create (nc, nr, 0);
1002 if ( factor->extraction == EXTRACTION_PC )
1003 tab_title (t, _("Component Matrix"));
1005 tab_title (t, _("Factor Matrix"));
1007 tab_dim (t, tab_natural_dimensions, NULL);
1009 tab_headers (t, heading_columns, 0, heading_rows, 0);
1011 if ( factor->extraction == EXTRACTION_PC )
1015 TAB_CENTER | TAT_TITLE, _("Component"));
1020 TAB_CENTER | TAT_TITLE, _("Factor"));
1023 tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1026 /* Outline the box */
1033 /* Vertical lines */
1040 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1041 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1044 /* Initialise to the identity permutation */
1045 perm = gsl_permutation_calloc (factor->n_vars);
1048 sort_matrix_indirect (fm, perm);
1050 for (i = 0 ; i < n_factors; ++i)
1052 tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1055 for (i = 0 ; i < factor->n_vars; ++i)
1058 const int matrix_row = perm->data[i];
1059 tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1061 for (j = 0 ; j < n_factors; ++j)
1063 double x = gsl_matrix_get (fm, matrix_row, j);
1065 if ( fabs (x) < factor->blank)
1068 tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL);
1072 gsl_permutation_free (perm);
1079 show_explained_variance (const struct cmd_factor * factor, struct idata *idata,
1080 const gsl_vector *initial_eigenvalues,
1081 const gsl_vector *extracted_eigenvalues)
1085 const int heading_columns = 1;
1086 const int heading_rows = 2;
1087 const int nr = heading_rows + factor->n_vars;
1089 struct tab_table *t ;
1091 double i_total = 0.0;
1094 double e_total = 0.0;
1097 int nc = heading_columns;
1099 if (factor->print & PRINT_EXTRACTION)
1102 if (factor->print & PRINT_INITIAL)
1105 if (factor->print & PRINT_ROTATION)
1108 /* No point having a table with only headings */
1109 if ( nc <= heading_columns)
1112 t = tab_create (nc, nr, 0);
1114 tab_title (t, _("Total Variance Explained"));
1116 tab_dim (t, tab_natural_dimensions, NULL);
1118 tab_headers (t, heading_columns, 0, heading_rows, 0);
1120 /* Outline the box */
1127 /* Vertical lines */
1134 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1135 tab_hline (t, TAL_1, 1, nc - 1, 1);
1137 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1140 if ( factor->extraction == EXTRACTION_PC)
1141 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1143 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1146 if (factor->print & PRINT_INITIAL)
1148 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1152 if (factor->print & PRINT_EXTRACTION)
1154 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1158 if (factor->print & PRINT_ROTATION)
1160 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1164 for (i = 0; i < (nc - heading_columns) / 3 ; ++i)
1166 tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1167 tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1168 tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1170 tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1173 for (i = 0 ; i < initial_eigenvalues->size; ++i)
1174 i_total += gsl_vector_get (initial_eigenvalues, i);
1176 if ( factor->extraction == EXTRACTION_PAF)
1178 e_total = factor->n_vars;
1186 for (i = 0 ; i < factor->n_vars; ++i)
1188 const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1189 double i_percent = 100.0 * i_lambda / i_total ;
1191 const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1192 double e_percent = 100.0 * e_lambda / e_total ;
1196 tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%d"), i + 1);
1201 /* Initial Eigenvalues */
1202 if (factor->print & PRINT_INITIAL)
1204 tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL);
1205 tab_double (t, c++, i + heading_rows, 0, i_percent, NULL);
1206 tab_double (t, c++, i + heading_rows, 0, i_cum, NULL);
1209 if (factor->print & PRINT_EXTRACTION)
1211 if (i < idata->n_extractions)
1213 /* Sums of squared loadings */
1214 tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL);
1215 tab_double (t, c++, i + heading_rows, 0, e_percent, NULL);
1216 tab_double (t, c++, i + heading_rows, 0, e_cum, NULL);
1226 show_correlation_matrix (const struct cmd_factor *factor, const struct idata *idata)
1228 struct tab_table *t ;
1230 int y_pos_corr = -1;
1232 int suffix_rows = 0;
1234 const int heading_rows = 1;
1235 const int heading_columns = 2;
1237 int nc = heading_columns ;
1238 int nr = heading_rows ;
1239 int n_data_sets = 0;
1241 if (factor->print & PRINT_CORRELATION)
1243 y_pos_corr = n_data_sets;
1245 nc = heading_columns + factor->n_vars;
1248 if (factor->print & PRINT_SIG)
1250 y_pos_sig = n_data_sets;
1252 nc = heading_columns + factor->n_vars;
1255 nr += n_data_sets * factor->n_vars;
1257 if (factor->print & PRINT_DETERMINANT)
1260 /* If the table would contain only headings, don't bother rendering it */
1261 if (nr <= heading_rows && suffix_rows == 0)
1264 t = tab_create (nc, nr + suffix_rows, 0);
1266 tab_title (t, _("Correlation Matrix"));
1268 tab_dim (t, tab_natural_dimensions, NULL);
1270 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1272 if (nr > heading_rows)
1274 tab_headers (t, heading_columns, 0, heading_rows, 0);
1276 tab_vline (t, TAL_2, 2, 0, nr - 1);
1278 /* Outline the box */
1285 /* Vertical lines */
1293 for (i = 0; i < factor->n_vars; ++i)
1294 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
1297 for (i = 0 ; i < n_data_sets; ++i)
1299 int y = heading_rows + i * factor->n_vars;
1301 for (v = 0; v < factor->n_vars; ++v)
1302 tab_text (t, 1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
1304 tab_hline (t, TAL_1, 0, nc - 1, y);
1307 if (factor->print & PRINT_CORRELATION)
1309 const double y = heading_rows + y_pos_corr;
1310 tab_text (t, 0, y, TAT_TITLE, _("Correlations"));
1312 for (i = 0; i < factor->n_vars; ++i)
1314 for (j = 0; j < factor->n_vars; ++j)
1315 tab_double (t, heading_columns + i, y + j, 0, gsl_matrix_get (idata->corr, i, j), NULL);
1319 if (factor->print & PRINT_SIG)
1321 const double y = heading_rows + y_pos_sig * factor->n_vars;
1322 tab_text (t, 0, y, TAT_TITLE, _("Sig. 1-tailed"));
1324 for (i = 0; i < factor->n_vars; ++i)
1326 for (j = 0; j < factor->n_vars; ++j)
1328 double rho = gsl_matrix_get (idata->corr, i, j);
1329 double w = gsl_matrix_get (idata->n, i, j);
1334 tab_double (t, heading_columns + i, y + j, 0, significance_of_correlation (rho, w), NULL);
1340 if (factor->print & PRINT_DETERMINANT)
1345 const int size = idata->corr->size1;
1346 gsl_permutation *p = gsl_permutation_calloc (size);
1347 gsl_matrix *tmp = gsl_matrix_calloc (size, size);
1348 gsl_matrix_memcpy (tmp, idata->corr);
1350 gsl_linalg_LU_decomp (tmp, p, &sign);
1351 det = gsl_linalg_LU_det (tmp, sign);
1352 gsl_permutation_free (p);
1353 gsl_matrix_free (tmp);
1356 tab_text (t, 0, nr, TAB_LEFT | TAT_TITLE, _("Determinant"));
1357 tab_double (t, 1, nr, 0, det, NULL);
1366 do_factor (const struct cmd_factor *factor, struct casereader *r)
1369 const gsl_matrix *var_matrix;
1370 const gsl_matrix *mean_matrix;
1372 const gsl_matrix *analysis_matrix;
1373 struct idata *idata = idata_alloc (factor->n_vars);
1375 struct covariance *cov = covariance_create (factor->n_vars, factor->vars,
1376 factor->wv, factor->exclude);
1378 for ( ; (c = casereader_read (r) ); case_unref (c))
1380 covariance_accumulate (cov, c);
1383 idata->cov = covariance_calculate (cov);
1385 var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
1386 mean_matrix = covariance_moments (cov, MOMENT_MEAN);
1387 idata->n = covariance_moments (cov, MOMENT_NONE);
1389 if ( factor->method == METHOD_CORR)
1391 idata->corr = correlation_from_covariance (idata->cov, var_matrix);
1392 analysis_matrix = idata->corr;
1395 analysis_matrix = idata->cov;
1397 if ( factor->print & PRINT_UNIVARIATE)
1401 const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
1404 const int heading_columns = 1;
1405 const int heading_rows = 1;
1407 const int nr = heading_rows + factor->n_vars;
1409 struct tab_table *t = tab_create (nc, nr, 0);
1410 tab_title (t, _("Descriptive Statistics"));
1411 tab_dim (t, tab_natural_dimensions, NULL);
1413 tab_headers (t, heading_columns, 0, heading_rows, 0);
1415 /* Outline the box */
1422 /* Vertical lines */
1429 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1430 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1432 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
1433 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1434 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
1436 for (i = 0 ; i < factor->n_vars; ++i)
1438 const struct variable *v = factor->vars[i];
1439 tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
1441 tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (mean_matrix, i, i), NULL);
1442 tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (var_matrix, i, i)), NULL);
1443 tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (idata->n, i, i), wfmt);
1449 show_correlation_matrix (factor, idata);
1453 gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
1455 gsl_eigen_symmv (matrix_dup (analysis_matrix), idata->eval, idata->evec, workspace);
1457 gsl_eigen_symmv_free (workspace);
1460 gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
1463 idata->n_extractions = n_extracted_factors (factor, idata);
1465 if (idata->n_extractions == 0)
1467 msg (MW, _("The FACTOR criteria result in zero factors extracted. Therefore no analysis will be performed."));
1471 if (idata->n_extractions > factor->n_vars)
1473 msg (MW, _("The FACTOR criteria result in more factors than variables, which is not meaningful. No analysis will be performed."));
1478 const gsl_vector *extracted_eigenvalues = NULL;
1479 gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
1480 gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
1482 struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, idata->n_extractions);
1483 gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
1485 if ( factor->extraction == EXTRACTION_PAF)
1487 gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
1488 struct smr_workspace *ws = ws_create (analysis_matrix);
1490 for (i = 0 ; i < factor->n_vars ; ++i)
1492 double r2 = squared_multiple_correlation (analysis_matrix, i, ws);
1494 gsl_vector_set (idata->msr, i, r2);
1498 gsl_vector_memcpy (initial_communalities, idata->msr);
1500 for (i = 0; i < factor->iterations; ++i)
1503 gsl_vector_memcpy (diff, idata->msr);
1505 iterate_factor_matrix (analysis_matrix, idata->msr, factor_matrix, fmw);
1507 gsl_vector_sub (diff, idata->msr);
1509 gsl_vector_minmax (diff, &min, &max);
1511 if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
1514 gsl_vector_free (diff);
1516 gsl_vector_memcpy (extracted_communalities, idata->msr);
1517 extracted_eigenvalues = fmw->eval;
1519 else if (factor->extraction == EXTRACTION_PC)
1521 for (i = 0; i < factor->n_vars; ++i)
1522 gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
1524 gsl_vector_memcpy (extracted_communalities, initial_communalities);
1526 iterate_factor_matrix (analysis_matrix, extracted_communalities, factor_matrix, fmw);
1527 extracted_eigenvalues = idata->eval;
1530 show_communalities (factor, initial_communalities, extracted_communalities);
1532 show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues);
1534 factor_matrix_workspace_free (fmw);
1536 show_factor_matrix (factor, idata, factor_matrix);
1538 gsl_vector_free (initial_communalities);
1539 gsl_vector_free (extracted_communalities);
1546 casereader_destroy (r);