1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011, 2012 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/>. */
19 #include <gsl/gsl_vector.h>
20 #include <gsl/gsl_linalg.h>
21 #include <gsl/gsl_matrix.h>
22 #include <gsl/gsl_eigen.h>
23 #include <gsl/gsl_blas.h>
24 #include <gsl/gsl_sort_vector.h>
25 #include <gsl/gsl_cdf.h>
27 #include "data/casegrouper.h"
28 #include "data/casereader.h"
29 #include "data/casewriter.h"
30 #include "data/dataset.h"
31 #include "data/dictionary.h"
32 #include "data/format.h"
33 #include "data/subcase.h"
34 #include "language/command.h"
35 #include "language/lexer/lexer.h"
36 #include "language/lexer/value-parser.h"
37 #include "language/lexer/variable-parser.h"
38 #include "libpspp/cast.h"
39 #include "libpspp/message.h"
40 #include "libpspp/misc.h"
41 #include "math/correlation.h"
42 #include "math/covariance.h"
43 #include "math/moments.h"
44 #include "output/chart-item.h"
45 #include "output/charts/scree.h"
46 #include "output/tab.h"
49 #define _(msgid) gettext (msgid)
50 #define N_(msgid) msgid
65 enum extraction_method
74 PLOT_ROTATION = 0x0002
79 PRINT_UNIVARIATE = 0x0001,
80 PRINT_DETERMINANT = 0x0002,
84 PRINT_COVARIANCE = 0x0020,
85 PRINT_CORRELATION = 0x0040,
86 PRINT_ROTATION = 0x0080,
87 PRINT_EXTRACTION = 0x0100,
88 PRINT_INITIAL = 0x0200,
102 typedef void (*rotation_coefficients) (double *x, double *y,
103 double a, double b, double c, double d,
104 const gsl_matrix *loadings );
108 varimax_coefficients (double *x, double *y,
109 double a, double b, double c, double d,
110 const gsl_matrix *loadings )
112 *x = d - 2 * a * b / loadings->size1;
113 *y = c - (a * a - b * b) / loadings->size1;
117 equamax_coefficients (double *x, double *y,
118 double a, double b, double c, double d,
119 const gsl_matrix *loadings )
121 *x = d - loadings->size2 * a * b / loadings->size1;
122 *y = c - loadings->size2 * (a * a - b * b) / (2 * loadings->size1);
126 quartimax_coefficients (double *x, double *y,
127 double a UNUSED, double b UNUSED, double c, double d,
128 const gsl_matrix *loadings UNUSED)
134 static const rotation_coefficients rotation_coeff[3] = {
135 varimax_coefficients,
136 equamax_coefficients,
137 quartimax_coefficients
144 const struct variable **vars;
146 const struct variable *wv;
149 enum missing_type missing_type;
150 enum mv_class exclude;
151 enum print_opts print;
152 enum extraction_method extraction;
154 enum rotation_type rotation;
155 int rotation_iterations;
157 /* Extraction Criteria */
161 int extraction_iterations;
172 /* Intermediate values used in calculation */
174 const gsl_matrix *corr ; /* The correlation matrix */
175 gsl_matrix *cov ; /* The covariance matrix */
176 const gsl_matrix *n ; /* Matrix of number of samples */
178 gsl_vector *eval ; /* The eigenvalues */
179 gsl_matrix *evec ; /* The eigenvectors */
183 gsl_vector *msr ; /* Multiple Squared Regressions */
185 double detR; /* The determinant of the correlation matrix */
188 static struct idata *
189 idata_alloc (size_t n_vars)
191 struct idata *id = xzalloc (sizeof (*id));
193 id->n_extractions = 0;
194 id->msr = gsl_vector_alloc (n_vars);
196 id->eval = gsl_vector_alloc (n_vars);
197 id->evec = gsl_matrix_alloc (n_vars, n_vars);
203 idata_free (struct idata *id)
205 gsl_vector_free (id->msr);
206 gsl_vector_free (id->eval);
207 gsl_matrix_free (id->evec);
209 gsl_matrix_free (id->cov);
210 if (id->corr != NULL)
211 gsl_matrix_free (CONST_CAST (gsl_matrix *, id->corr));
218 anti_image (const gsl_matrix *m)
222 assert (m->size1 == m->size2);
224 a = gsl_matrix_alloc (m->size1, m->size2);
226 for (i = 0; i < m->size1; ++i)
228 for (j = 0; j < m->size2; ++j)
230 double *p = gsl_matrix_ptr (a, i, j);
231 *p = gsl_matrix_get (m, i, j);
232 *p /= gsl_matrix_get (m, i, i);
233 *p /= gsl_matrix_get (m, j, j);
241 /* Return the sum of all the elements excluding row N */
243 ssq_od_n (const gsl_matrix *m, int n)
247 assert (m->size1 == m->size2);
249 assert (n < m->size1);
251 for (i = 0; i < m->size1; ++i)
253 if (i == n ) continue;
254 for (j = 0; j < m->size2; ++j)
256 ss += pow2 (gsl_matrix_get (m, i, j));
267 dump_matrix (const gsl_matrix *m)
271 for (i = 0 ; i < m->size1; ++i)
273 for (j = 0 ; j < m->size2; ++j)
274 printf ("%02f ", gsl_matrix_get (m, i, j));
280 dump_matrix_permute (const gsl_matrix *m, const gsl_permutation *p)
284 for (i = 0 ; i < m->size1; ++i)
286 for (j = 0 ; j < m->size2; ++j)
287 printf ("%02f ", gsl_matrix_get (m, gsl_permutation_get (p, i), j));
294 dump_vector (const gsl_vector *v)
297 for (i = 0 ; i < v->size; ++i)
299 printf ("%02f\n", gsl_vector_get (v, i));
307 n_extracted_factors (const struct cmd_factor *factor, struct idata *idata)
311 /* If there is a cached value, then return that. */
312 if ( idata->n_extractions != 0)
313 return idata->n_extractions;
315 /* Otherwise, if the number of factors has been explicitly requested,
317 if (factor->n_factors > 0)
319 idata->n_extractions = factor->n_factors;
323 /* Use the MIN_EIGEN setting. */
324 for (i = 0 ; i < idata->eval->size; ++i)
326 double evali = fabs (gsl_vector_get (idata->eval, i));
328 idata->n_extractions = i;
330 if (evali < factor->min_eigen)
335 return idata->n_extractions;
339 /* Returns a newly allocated matrix identical to M.
340 It it the callers responsibility to free the returned value.
343 matrix_dup (const gsl_matrix *m)
345 gsl_matrix *n = gsl_matrix_alloc (m->size1, m->size2);
347 gsl_matrix_memcpy (n, m);
355 /* Copy of the subject */
360 gsl_permutation *perm;
367 static struct smr_workspace *ws_create (const gsl_matrix *input)
369 struct smr_workspace *ws = xmalloc (sizeof (*ws));
371 ws->m = gsl_matrix_alloc (input->size1, input->size2);
372 ws->inverse = gsl_matrix_calloc (input->size1 - 1, input->size2 - 1);
373 ws->perm = gsl_permutation_alloc (input->size1 - 1);
374 ws->result1 = gsl_matrix_calloc (input->size1 - 1, 1);
375 ws->result2 = gsl_matrix_calloc (1, 1);
381 ws_destroy (struct smr_workspace *ws)
383 gsl_matrix_free (ws->result2);
384 gsl_matrix_free (ws->result1);
385 gsl_permutation_free (ws->perm);
386 gsl_matrix_free (ws->inverse);
387 gsl_matrix_free (ws->m);
394 Return the square of the regression coefficient for VAR regressed against all other variables.
397 squared_multiple_correlation (const gsl_matrix *corr, int var, struct smr_workspace *ws)
399 /* For an explanation of what this is doing, see
400 http://www.visualstatistics.net/Visual%20Statistics%20Multimedia/multiple_regression_analysis.htm
406 gsl_matrix_memcpy (ws->m, corr);
408 gsl_matrix_swap_rows (ws->m, 0, var);
409 gsl_matrix_swap_columns (ws->m, 0, var);
411 rxx = gsl_matrix_submatrix (ws->m, 1, 1, ws->m->size1 - 1, ws->m->size1 - 1);
413 gsl_linalg_LU_decomp (&rxx.matrix, ws->perm, &signum);
415 gsl_linalg_LU_invert (&rxx.matrix, ws->perm, ws->inverse);
418 gsl_matrix_const_view rxy = gsl_matrix_const_submatrix (ws->m, 1, 0, ws->m->size1 - 1, 1);
419 gsl_matrix_const_view ryx = gsl_matrix_const_submatrix (ws->m, 0, 1, 1, ws->m->size1 - 1);
421 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
422 1.0, ws->inverse, &rxy.matrix, 0.0, ws->result1);
424 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
425 1.0, &ryx.matrix, ws->result1, 0.0, ws->result2);
428 return gsl_matrix_get (ws->result2, 0, 0);
433 static double the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors);
436 struct factor_matrix_workspace
439 gsl_eigen_symmv_workspace *eigen_ws;
449 static struct factor_matrix_workspace *
450 factor_matrix_workspace_alloc (size_t n, size_t nf)
452 struct factor_matrix_workspace *ws = xmalloc (sizeof (*ws));
455 ws->gamma = gsl_matrix_calloc (nf, nf);
456 ws->eigen_ws = gsl_eigen_symmv_alloc (n);
457 ws->eval = gsl_vector_alloc (n);
458 ws->evec = gsl_matrix_alloc (n, n);
459 ws->r = gsl_matrix_alloc (n, n);
465 factor_matrix_workspace_free (struct factor_matrix_workspace *ws)
467 gsl_eigen_symmv_free (ws->eigen_ws);
468 gsl_vector_free (ws->eval);
469 gsl_matrix_free (ws->evec);
470 gsl_matrix_free (ws->gamma);
471 gsl_matrix_free (ws->r);
476 Shift P left by OFFSET places, and overwrite TARGET
477 with the shifted result.
478 Positions in TARGET less than OFFSET are unchanged.
481 perm_shift_apply (gsl_permutation *target, const gsl_permutation *p,
485 assert (target->size == p->size);
486 assert (offset <= target->size);
488 for (i = 0; i < target->size - offset; ++i)
490 target->data[i] = p->data [i + offset];
496 Indirectly sort the rows of matrix INPUT, storing the sort order in PERM.
497 The sort criteria are as follows:
499 Rows are sorted on the first column, until the absolute value of an
500 element in a subsequent column is greater than that of the first
501 column. Thereafter, rows will be sorted on the second column,
502 until the absolute value of an element in a subsequent column
503 exceeds that of the second column ...
506 sort_matrix_indirect (const gsl_matrix *input, gsl_permutation *perm)
508 const size_t n = perm->size;
509 const size_t m = input->size2;
516 assert (perm->size == input->size1);
518 p = gsl_permutation_alloc (n);
520 /* Copy INPUT into MAT, discarding the sign */
521 mat = gsl_matrix_alloc (n, m);
522 for (i = 0 ; i < mat->size1; ++i)
524 for (j = 0 ; j < mat->size2; ++j)
526 double x = gsl_matrix_get (input, i, j);
527 gsl_matrix_set (mat, i, j, fabs (x));
531 while (column_n < m && row_n < n)
533 gsl_vector_const_view columni = gsl_matrix_const_column (mat, column_n);
534 gsl_sort_vector_index (p, &columni.vector);
536 for (i = 0 ; i < n; ++i)
538 gsl_vector_view row = gsl_matrix_row (mat, p->data[n - 1 - i]);
539 size_t maxindex = gsl_vector_max_index (&row.vector);
541 if ( maxindex > column_n )
544 /* All subsequent elements of this row, are of no interest.
545 So set them all to a highly negative value */
546 for (j = column_n + 1; j < row.vector.size ; ++j)
547 gsl_vector_set (&row.vector, j, -DBL_MAX);
550 perm_shift_apply (perm, p, row_n);
556 gsl_permutation_free (p);
557 gsl_matrix_free (mat);
559 assert ( 0 == gsl_permutation_valid (perm));
561 /* We want the biggest value to be first */
562 gsl_permutation_reverse (perm);
567 drot_go (double phi, double *l0, double *l1)
569 double r0 = cos (phi) * *l0 + sin (phi) * *l1;
570 double r1 = - sin (phi) * *l0 + cos (phi) * *l1;
578 clone_matrix (const gsl_matrix *m)
581 gsl_matrix *c = gsl_matrix_calloc (m->size1, m->size2);
583 for (j = 0 ; j < c->size1; ++j)
585 for (k = 0 ; k < c->size2; ++k)
587 const double *v = gsl_matrix_const_ptr (m, j, k);
588 gsl_matrix_set (c, j, k, *v);
597 initial_sv (const gsl_matrix *fm)
602 for (j = 0 ; j < fm->size2; ++j)
607 for (k = j + 1 ; k < fm->size2; ++k)
609 double lambda = gsl_matrix_get (fm, k, j);
610 double lambda_sq = lambda * lambda;
611 double lambda_4 = lambda_sq * lambda_sq;
616 sv += ( fm->size1 * l4s - (l2s * l2s) ) / (fm->size1 * fm->size1 );
622 rotate (const struct cmd_factor *cf, const gsl_matrix *unrot,
623 const gsl_vector *communalities,
625 gsl_vector *rotated_loadings
632 /* First get a normalised version of UNROT */
633 gsl_matrix *normalised = gsl_matrix_calloc (unrot->size1, unrot->size2);
634 gsl_matrix *h_sqrt = gsl_matrix_calloc (communalities->size, communalities->size);
635 gsl_matrix *h_sqrt_inv ;
637 /* H is the diagonal matrix containing the absolute values of the communalities */
638 for (i = 0 ; i < communalities->size ; ++i)
640 double *ptr = gsl_matrix_ptr (h_sqrt, i, i);
641 *ptr = fabs (gsl_vector_get (communalities, i));
644 /* Take the square root of the communalities */
645 gsl_linalg_cholesky_decomp (h_sqrt);
648 /* Save a copy of h_sqrt and invert it */
649 h_sqrt_inv = clone_matrix (h_sqrt);
650 gsl_linalg_cholesky_decomp (h_sqrt_inv);
651 gsl_linalg_cholesky_invert (h_sqrt_inv);
653 /* normalised vertion is H^{1/2} x UNROT */
654 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, h_sqrt_inv, unrot, 0.0, normalised);
656 gsl_matrix_free (h_sqrt_inv);
659 /* Now perform the rotation iterations */
661 prev_sv = initial_sv (normalised);
662 for (i = 0 ; i < cf->rotation_iterations ; ++i)
665 for (j = 0 ; j < normalised->size2; ++j)
667 /* These variables relate to the convergence criterium */
671 for (k = j + 1 ; k < normalised->size2; ++k)
681 for (p = 0; p < normalised->size1; ++p)
683 double jv = gsl_matrix_get (normalised, p, j);
684 double kv = gsl_matrix_get (normalised, p, k);
686 double u = jv * jv - kv * kv;
687 double v = 2 * jv * kv;
694 rotation_coeff [cf->rotation] (&x, &y, a, b, c, d, normalised);
696 phi = atan2 (x, y) / 4.0 ;
698 /* Don't bother rotating if the angle is small */
699 if ( fabs (sin (phi) ) <= pow (10.0, -15.0))
702 for (p = 0; p < normalised->size1; ++p)
704 double *lambda0 = gsl_matrix_ptr (normalised, p, j);
705 double *lambda1 = gsl_matrix_ptr (normalised, p, k);
706 drot_go (phi, lambda0, lambda1);
709 /* Calculate the convergence criterium */
711 double lambda = gsl_matrix_get (normalised, k, j);
712 double lambda_sq = lambda * lambda;
713 double lambda_4 = lambda_sq * lambda_sq;
719 sv += ( normalised->size1 * l4s - (l2s * l2s) ) / (normalised->size1 * normalised->size1 );
722 if ( fabs (sv - prev_sv) <= cf->rconverge)
728 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0,
729 h_sqrt, normalised, 0.0, result);
731 gsl_matrix_free (h_sqrt);
732 gsl_matrix_free (normalised);
735 /* reflect negative sums and populate the rotated loadings vector*/
736 for (i = 0 ; i < result->size2; ++i)
740 for (j = 0 ; j < result->size1; ++j)
742 double s = gsl_matrix_get (result, j, i);
744 sum += gsl_matrix_get (result, j, i);
747 gsl_vector_set (rotated_loadings, i, ssq);
750 for (j = 0 ; j < result->size1; ++j)
752 double *lambda = gsl_matrix_ptr (result, j, i);
760 Get an approximation for the factor matrix into FACTORS, and the communalities into COMMUNALITIES.
761 R is the matrix to be analysed.
762 WS is a pointer to a structure which must have been initialised with factor_matrix_workspace_init.
765 iterate_factor_matrix (const gsl_matrix *r, gsl_vector *communalities, gsl_matrix *factors,
766 struct factor_matrix_workspace *ws)
771 assert (r->size1 == r->size2);
772 assert (r->size1 == communalities->size);
774 assert (factors->size1 == r->size1);
775 assert (factors->size2 == ws->n_factors);
777 gsl_matrix_memcpy (ws->r, r);
779 /* Apply Communalities to diagonal of correlation matrix */
780 for (i = 0 ; i < communalities->size ; ++i)
782 double *x = gsl_matrix_ptr (ws->r, i, i);
783 *x = gsl_vector_get (communalities, i);
786 gsl_eigen_symmv (ws->r, ws->eval, ws->evec, ws->eigen_ws);
788 mv = gsl_matrix_submatrix (ws->evec, 0, 0, ws->evec->size1, ws->n_factors);
790 /* Gamma is the diagonal matrix containing the absolute values of the eigenvalues */
791 for (i = 0 ; i < ws->n_factors ; ++i)
793 double *ptr = gsl_matrix_ptr (ws->gamma, i, i);
794 *ptr = fabs (gsl_vector_get (ws->eval, i));
797 /* Take the square root of gamma */
798 gsl_linalg_cholesky_decomp (ws->gamma);
800 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, &mv.matrix, ws->gamma, 0.0, factors);
802 for (i = 0 ; i < r->size1 ; ++i)
804 double h = the_communality (ws->evec, ws->eval, i, ws->n_factors);
805 gsl_vector_set (communalities, i, h);
811 static bool run_factor (struct dataset *ds, const struct cmd_factor *factor);
815 cmd_factor (struct lexer *lexer, struct dataset *ds)
817 const struct dictionary *dict = dataset_dict (ds);
818 int n_iterations = 25;
819 struct cmd_factor factor;
822 factor.method = METHOD_CORR;
823 factor.missing_type = MISS_LISTWISE;
824 factor.exclude = MV_ANY;
825 factor.print = PRINT_INITIAL | PRINT_EXTRACTION | PRINT_ROTATION;
826 factor.extraction = EXTRACTION_PC;
827 factor.n_factors = 0;
828 factor.min_eigen = SYSMIS;
829 factor.extraction_iterations = 25;
830 factor.rotation_iterations = 25;
831 factor.econverge = 0.001;
836 factor.rotation = ROT_VARIMAX;
838 factor.rconverge = 0.0001;
840 factor.wv = dict_get_weight (dict);
842 lex_match (lexer, T_SLASH);
844 if (!lex_force_match_id (lexer, "VARIABLES"))
849 lex_match (lexer, T_EQUALS);
851 if (!parse_variables_const (lexer, dict, &factor.vars, &factor.n_vars,
852 PV_NO_DUPLICATE | PV_NUMERIC))
855 if (factor.n_vars < 2)
856 msg (MW, _("Factor analysis on a single variable is not useful."));
858 while (lex_token (lexer) != T_ENDCMD)
860 lex_match (lexer, T_SLASH);
862 if (lex_match_id (lexer, "PLOT"))
864 lex_match (lexer, T_EQUALS);
865 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
867 if (lex_match_id (lexer, "EIGEN"))
869 factor.plot |= PLOT_SCREE;
871 #if FACTOR_FULLY_IMPLEMENTED
872 else if (lex_match_id (lexer, "ROTATION"))
878 lex_error (lexer, NULL);
883 else if (lex_match_id (lexer, "METHOD"))
885 lex_match (lexer, T_EQUALS);
886 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
888 if (lex_match_id (lexer, "COVARIANCE"))
890 factor.method = METHOD_COV;
892 else if (lex_match_id (lexer, "CORRELATION"))
894 factor.method = METHOD_CORR;
898 lex_error (lexer, NULL);
903 else if (lex_match_id (lexer, "ROTATION"))
905 lex_match (lexer, T_EQUALS);
906 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
908 /* VARIMAX and DEFAULT are defaults */
909 if (lex_match_id (lexer, "VARIMAX") || lex_match_id (lexer, "DEFAULT"))
911 factor.rotation = ROT_VARIMAX;
913 else if (lex_match_id (lexer, "EQUAMAX"))
915 factor.rotation = ROT_EQUAMAX;
917 else if (lex_match_id (lexer, "QUARTIMAX"))
919 factor.rotation = ROT_QUARTIMAX;
921 else if (lex_match_id (lexer, "NOROTATE"))
923 factor.rotation = ROT_NONE;
927 lex_error (lexer, NULL);
931 factor.rotation_iterations = n_iterations;
933 else if (lex_match_id (lexer, "CRITERIA"))
935 lex_match (lexer, T_EQUALS);
936 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
938 if (lex_match_id (lexer, "FACTORS"))
940 if ( lex_force_match (lexer, T_LPAREN))
942 lex_force_int (lexer);
943 factor.n_factors = lex_integer (lexer);
945 lex_force_match (lexer, T_RPAREN);
948 else if (lex_match_id (lexer, "MINEIGEN"))
950 if ( lex_force_match (lexer, T_LPAREN))
952 lex_force_num (lexer);
953 factor.min_eigen = lex_number (lexer);
955 lex_force_match (lexer, T_RPAREN);
958 else if (lex_match_id (lexer, "ECONVERGE"))
960 if ( lex_force_match (lexer, T_LPAREN))
962 lex_force_num (lexer);
963 factor.econverge = lex_number (lexer);
965 lex_force_match (lexer, T_RPAREN);
968 else if (lex_match_id (lexer, "RCONVERGE"))
970 if ( lex_force_match (lexer, T_LPAREN))
972 lex_force_num (lexer);
973 factor.rconverge = lex_number (lexer);
975 lex_force_match (lexer, T_RPAREN);
978 else if (lex_match_id (lexer, "ITERATE"))
980 if ( lex_force_match (lexer, T_LPAREN))
982 lex_force_int (lexer);
983 n_iterations = lex_integer (lexer);
985 lex_force_match (lexer, T_RPAREN);
988 else if (lex_match_id (lexer, "DEFAULT"))
990 factor.n_factors = 0;
991 factor.min_eigen = 1;
996 lex_error (lexer, NULL);
1001 else if (lex_match_id (lexer, "EXTRACTION"))
1003 lex_match (lexer, T_EQUALS);
1004 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1006 if (lex_match_id (lexer, "PAF"))
1008 factor.extraction = EXTRACTION_PAF;
1010 else if (lex_match_id (lexer, "PC"))
1012 factor.extraction = EXTRACTION_PC;
1014 else if (lex_match_id (lexer, "PA1"))
1016 factor.extraction = EXTRACTION_PC;
1018 else if (lex_match_id (lexer, "DEFAULT"))
1020 factor.extraction = EXTRACTION_PC;
1024 lex_error (lexer, NULL);
1028 factor.extraction_iterations = n_iterations;
1030 else if (lex_match_id (lexer, "FORMAT"))
1032 lex_match (lexer, T_EQUALS);
1033 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1035 if (lex_match_id (lexer, "SORT"))
1039 else if (lex_match_id (lexer, "BLANK"))
1041 if ( lex_force_match (lexer, T_LPAREN))
1043 lex_force_num (lexer);
1044 factor.blank = lex_number (lexer);
1046 lex_force_match (lexer, T_RPAREN);
1049 else if (lex_match_id (lexer, "DEFAULT"))
1052 factor.sort = false;
1056 lex_error (lexer, NULL);
1061 else if (lex_match_id (lexer, "PRINT"))
1064 lex_match (lexer, T_EQUALS);
1065 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1067 if (lex_match_id (lexer, "UNIVARIATE"))
1069 factor.print |= PRINT_UNIVARIATE;
1071 else if (lex_match_id (lexer, "DET"))
1073 factor.print |= PRINT_DETERMINANT;
1075 #if FACTOR_FULLY_IMPLEMENTED
1076 else if (lex_match_id (lexer, "INV"))
1079 else if (lex_match_id (lexer, "AIC"))
1083 else if (lex_match_id (lexer, "SIG"))
1085 factor.print |= PRINT_SIG;
1087 else if (lex_match_id (lexer, "CORRELATION"))
1089 factor.print |= PRINT_CORRELATION;
1091 #if FACTOR_FULLY_IMPLEMENTED
1092 else if (lex_match_id (lexer, "COVARIANCE"))
1096 else if (lex_match_id (lexer, "ROTATION"))
1098 factor.print |= PRINT_ROTATION;
1100 else if (lex_match_id (lexer, "EXTRACTION"))
1102 factor.print |= PRINT_EXTRACTION;
1104 else if (lex_match_id (lexer, "INITIAL"))
1106 factor.print |= PRINT_INITIAL;
1108 else if (lex_match_id (lexer, "KMO"))
1110 factor.print |= PRINT_KMO;
1112 #if FACTOR_FULLY_IMPLEMENTED
1113 else if (lex_match_id (lexer, "REPR"))
1116 else if (lex_match_id (lexer, "FSCORE"))
1120 else if (lex_match (lexer, T_ALL))
1122 factor.print = 0xFFFF;
1124 else if (lex_match_id (lexer, "DEFAULT"))
1126 factor.print |= PRINT_INITIAL ;
1127 factor.print |= PRINT_EXTRACTION ;
1128 factor.print |= PRINT_ROTATION ;
1132 lex_error (lexer, NULL);
1137 else if (lex_match_id (lexer, "MISSING"))
1139 lex_match (lexer, T_EQUALS);
1140 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1142 if (lex_match_id (lexer, "INCLUDE"))
1144 factor.exclude = MV_SYSTEM;
1146 else if (lex_match_id (lexer, "EXCLUDE"))
1148 factor.exclude = MV_ANY;
1150 else if (lex_match_id (lexer, "LISTWISE"))
1152 factor.missing_type = MISS_LISTWISE;
1154 else if (lex_match_id (lexer, "PAIRWISE"))
1156 factor.missing_type = MISS_PAIRWISE;
1158 else if (lex_match_id (lexer, "MEANSUB"))
1160 factor.missing_type = MISS_MEANSUB;
1164 lex_error (lexer, NULL);
1171 lex_error (lexer, NULL);
1176 if ( factor.rotation == ROT_NONE )
1177 factor.print &= ~PRINT_ROTATION;
1179 if ( ! run_factor (ds, &factor))
1190 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
1194 run_factor (struct dataset *ds, const struct cmd_factor *factor)
1196 struct dictionary *dict = dataset_dict (ds);
1198 struct casereader *group;
1200 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
1202 while (casegrouper_get_next_group (grouper, &group))
1204 if ( factor->missing_type == MISS_LISTWISE )
1205 group = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
1208 do_factor (factor, group);
1211 ok = casegrouper_destroy (grouper);
1212 ok = proc_commit (ds) && ok;
1218 /* Return the communality of variable N, calculated to N_FACTORS */
1220 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
1227 assert (n < eval->size);
1228 assert (n < evec->size1);
1229 assert (n_factors <= eval->size);
1231 for (i = 0 ; i < n_factors; ++i)
1233 double evali = fabs (gsl_vector_get (eval, i));
1235 double eveci = gsl_matrix_get (evec, n, i);
1237 comm += pow2 (eveci) * evali;
1243 /* Return the communality of variable N, calculated to N_FACTORS */
1245 communality (struct idata *idata, int n, int n_factors)
1247 return the_communality (idata->evec, idata->eval, n, n_factors);
1252 show_scree (const struct cmd_factor *f, struct idata *idata)
1257 if ( !(f->plot & PLOT_SCREE) )
1261 label = f->extraction == EXTRACTION_PC ? _("Component Number") : _("Factor Number");
1263 s = scree_create (idata->eval, label);
1269 show_communalities (const struct cmd_factor * factor,
1270 const gsl_vector *initial, const gsl_vector *extracted)
1274 const int heading_columns = 1;
1275 int nc = heading_columns;
1276 const int heading_rows = 1;
1277 const int nr = heading_rows + factor->n_vars;
1278 struct tab_table *t;
1280 if (factor->print & PRINT_EXTRACTION)
1283 if (factor->print & PRINT_INITIAL)
1286 /* No point having a table with only headings */
1290 t = tab_create (nc, nr);
1292 tab_title (t, _("Communalities"));
1294 tab_headers (t, heading_columns, 0, heading_rows, 0);
1297 if (factor->print & PRINT_INITIAL)
1298 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
1300 if (factor->print & PRINT_EXTRACTION)
1301 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
1303 /* Outline the box */
1310 /* Vertical lines */
1317 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1318 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1320 for (i = 0 ; i < factor->n_vars; ++i)
1323 tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
1325 if (factor->print & PRINT_INITIAL)
1326 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL);
1328 if (factor->print & PRINT_EXTRACTION)
1329 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL);
1337 show_factor_matrix (const struct cmd_factor *factor, struct idata *idata, const char *title, const gsl_matrix *fm)
1340 const int n_factors = idata->n_extractions;
1342 const int heading_columns = 1;
1343 const int heading_rows = 2;
1344 const int nr = heading_rows + factor->n_vars;
1345 const int nc = heading_columns + n_factors;
1346 gsl_permutation *perm;
1348 struct tab_table *t = tab_create (nc, nr);
1351 if ( factor->extraction == EXTRACTION_PC )
1352 tab_title (t, _("Component Matrix"));
1354 tab_title (t, _("Factor Matrix"));
1357 tab_title (t, "%s", title);
1359 tab_headers (t, heading_columns, 0, heading_rows, 0);
1361 if ( factor->extraction == EXTRACTION_PC )
1365 TAB_CENTER | TAT_TITLE, _("Component"));
1370 TAB_CENTER | TAT_TITLE, _("Factor"));
1373 tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1376 /* Outline the box */
1383 /* Vertical lines */
1390 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1391 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1394 /* Initialise to the identity permutation */
1395 perm = gsl_permutation_calloc (factor->n_vars);
1398 sort_matrix_indirect (fm, perm);
1400 for (i = 0 ; i < n_factors; ++i)
1402 tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1405 for (i = 0 ; i < factor->n_vars; ++i)
1408 const int matrix_row = perm->data[i];
1409 tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1411 for (j = 0 ; j < n_factors; ++j)
1413 double x = gsl_matrix_get (fm, matrix_row, j);
1415 if ( fabs (x) < factor->blank)
1418 tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL);
1422 gsl_permutation_free (perm);
1429 show_explained_variance (const struct cmd_factor * factor, struct idata *idata,
1430 const gsl_vector *initial_eigenvalues,
1431 const gsl_vector *extracted_eigenvalues,
1432 const gsl_vector *rotated_loadings)
1436 const int heading_columns = 1;
1437 const int heading_rows = 2;
1438 const int nr = heading_rows + factor->n_vars;
1440 struct tab_table *t ;
1442 double i_total = 0.0;
1445 double e_total = 0.0;
1450 int nc = heading_columns;
1452 if (factor->print & PRINT_EXTRACTION)
1455 if (factor->print & PRINT_INITIAL)
1458 if (factor->print & PRINT_ROTATION)
1461 /* No point having a table with only headings */
1462 if ( nc <= heading_columns)
1465 t = tab_create (nc, nr);
1467 tab_title (t, _("Total Variance Explained"));
1469 tab_headers (t, heading_columns, 0, heading_rows, 0);
1471 /* Outline the box */
1478 /* Vertical lines */
1485 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1486 tab_hline (t, TAL_1, 1, nc - 1, 1);
1488 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1491 if ( factor->extraction == EXTRACTION_PC)
1492 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1494 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1497 if (factor->print & PRINT_INITIAL)
1499 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1503 if (factor->print & PRINT_EXTRACTION)
1505 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1509 if (factor->print & PRINT_ROTATION)
1511 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1515 for (i = 0; i < (nc - heading_columns) / 3 ; ++i)
1517 tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1518 /* xgettext:no-c-format */
1519 tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1520 tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1522 tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1525 for (i = 0 ; i < initial_eigenvalues->size; ++i)
1526 i_total += gsl_vector_get (initial_eigenvalues, i);
1528 if ( factor->extraction == EXTRACTION_PAF)
1530 e_total = factor->n_vars;
1537 for (i = 0 ; i < factor->n_vars; ++i)
1539 const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1540 double i_percent = 100.0 * i_lambda / i_total ;
1542 const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1543 double e_percent = 100.0 * e_lambda / e_total ;
1547 tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%zu"), i + 1);
1552 /* Initial Eigenvalues */
1553 if (factor->print & PRINT_INITIAL)
1555 tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL);
1556 tab_double (t, c++, i + heading_rows, 0, i_percent, NULL);
1557 tab_double (t, c++, i + heading_rows, 0, i_cum, NULL);
1561 if (factor->print & PRINT_EXTRACTION)
1563 if (i < idata->n_extractions)
1565 /* Sums of squared loadings */
1566 tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL);
1567 tab_double (t, c++, i + heading_rows, 0, e_percent, NULL);
1568 tab_double (t, c++, i + heading_rows, 0, e_cum, NULL);
1572 if (rotated_loadings != NULL)
1574 const double r_lambda = gsl_vector_get (rotated_loadings, i);
1575 double r_percent = 100.0 * r_lambda / e_total ;
1577 if (factor->print & PRINT_ROTATION)
1579 if (i < idata->n_extractions)
1582 tab_double (t, c++, i + heading_rows, 0, r_lambda, NULL);
1583 tab_double (t, c++, i + heading_rows, 0, r_percent, NULL);
1584 tab_double (t, c++, i + heading_rows, 0, r_cum, NULL);
1595 show_correlation_matrix (const struct cmd_factor *factor, const struct idata *idata)
1597 struct tab_table *t ;
1599 int y_pos_corr = -1;
1601 int suffix_rows = 0;
1603 const int heading_rows = 1;
1604 const int heading_columns = 2;
1606 int nc = heading_columns ;
1607 int nr = heading_rows ;
1608 int n_data_sets = 0;
1610 if (factor->print & PRINT_CORRELATION)
1612 y_pos_corr = n_data_sets;
1614 nc = heading_columns + factor->n_vars;
1617 if (factor->print & PRINT_SIG)
1619 y_pos_sig = n_data_sets;
1621 nc = heading_columns + factor->n_vars;
1624 nr += n_data_sets * factor->n_vars;
1626 if (factor->print & PRINT_DETERMINANT)
1629 /* If the table would contain only headings, don't bother rendering it */
1630 if (nr <= heading_rows && suffix_rows == 0)
1633 t = tab_create (nc, nr + suffix_rows);
1635 tab_title (t, _("Correlation Matrix"));
1637 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1639 if (nr > heading_rows)
1641 tab_headers (t, heading_columns, 0, heading_rows, 0);
1643 tab_vline (t, TAL_2, 2, 0, nr - 1);
1645 /* Outline the box */
1652 /* Vertical lines */
1660 for (i = 0; i < factor->n_vars; ++i)
1661 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
1664 for (i = 0 ; i < n_data_sets; ++i)
1666 int y = heading_rows + i * factor->n_vars;
1668 for (v = 0; v < factor->n_vars; ++v)
1669 tab_text (t, 1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
1671 tab_hline (t, TAL_1, 0, nc - 1, y);
1674 if (factor->print & PRINT_CORRELATION)
1676 const double y = heading_rows + y_pos_corr;
1677 tab_text (t, 0, y, TAT_TITLE, _("Correlations"));
1679 for (i = 0; i < factor->n_vars; ++i)
1681 for (j = 0; j < factor->n_vars; ++j)
1682 tab_double (t, heading_columns + i, y + j, 0, gsl_matrix_get (idata->corr, i, j), NULL);
1686 if (factor->print & PRINT_SIG)
1688 const double y = heading_rows + y_pos_sig * factor->n_vars;
1689 tab_text (t, 0, y, TAT_TITLE, _("Sig. (1-tailed)"));
1691 for (i = 0; i < factor->n_vars; ++i)
1693 for (j = 0; j < factor->n_vars; ++j)
1695 double rho = gsl_matrix_get (idata->corr, i, j);
1696 double w = gsl_matrix_get (idata->n, i, j);
1701 tab_double (t, heading_columns + i, y + j, 0, significance_of_correlation (rho, w), NULL);
1707 if (factor->print & PRINT_DETERMINANT)
1709 tab_text (t, 0, nr, TAB_LEFT | TAT_TITLE, _("Determinant"));
1711 tab_double (t, 1, nr, 0, idata->detR, NULL);
1720 do_factor (const struct cmd_factor *factor, struct casereader *r)
1723 const gsl_matrix *var_matrix;
1724 const gsl_matrix *mean_matrix;
1726 const gsl_matrix *analysis_matrix;
1727 struct idata *idata = idata_alloc (factor->n_vars);
1729 struct covariance *cov = covariance_1pass_create (factor->n_vars, factor->vars,
1730 factor->wv, factor->exclude);
1732 for ( ; (c = casereader_read (r) ); case_unref (c))
1734 covariance_accumulate (cov, c);
1737 idata->cov = covariance_calculate (cov);
1739 if (idata->cov == NULL)
1741 msg (MW, _("The dataset contains no complete observations. No analysis will be performed."));
1742 covariance_destroy (cov);
1746 var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
1747 mean_matrix = covariance_moments (cov, MOMENT_MEAN);
1748 idata->n = covariance_moments (cov, MOMENT_NONE);
1751 if ( factor->method == METHOD_CORR)
1753 idata->corr = correlation_from_covariance (idata->cov, var_matrix);
1755 analysis_matrix = idata->corr;
1758 analysis_matrix = idata->cov;
1761 if (factor->print & PRINT_DETERMINANT
1762 || factor->print & PRINT_KMO)
1766 const int size = idata->corr->size1;
1767 gsl_permutation *p = gsl_permutation_calloc (size);
1768 gsl_matrix *tmp = gsl_matrix_calloc (size, size);
1769 gsl_matrix_memcpy (tmp, idata->corr);
1771 gsl_linalg_LU_decomp (tmp, p, &sign);
1772 idata->detR = gsl_linalg_LU_det (tmp, sign);
1773 gsl_permutation_free (p);
1774 gsl_matrix_free (tmp);
1777 if ( factor->print & PRINT_UNIVARIATE)
1779 const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
1783 const int heading_columns = 1;
1784 const int heading_rows = 1;
1786 const int nr = heading_rows + factor->n_vars;
1788 struct tab_table *t = tab_create (nc, nr);
1789 tab_title (t, _("Descriptive Statistics"));
1791 tab_headers (t, heading_columns, 0, heading_rows, 0);
1793 /* Outline the box */
1800 /* Vertical lines */
1807 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1808 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1810 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
1811 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1812 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
1814 for (i = 0 ; i < factor->n_vars; ++i)
1816 const struct variable *v = factor->vars[i];
1817 tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
1819 tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (mean_matrix, i, i), NULL);
1820 tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (var_matrix, i, i)), NULL);
1821 tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (idata->n, i, i), wfmt);
1827 if (factor->print & PRINT_KMO)
1830 double sum_ssq_r = 0;
1831 double sum_ssq_a = 0;
1833 double df = factor->n_vars * ( factor->n_vars - 1) / 2;
1840 const int heading_columns = 2;
1841 const int heading_rows = 0;
1843 const int nr = heading_rows + 4;
1844 const int nc = heading_columns + 1;
1848 struct tab_table *t = tab_create (nc, nr);
1849 tab_title (t, _("KMO and Bartlett's Test"));
1851 x = clone_matrix (idata->corr);
1852 gsl_linalg_cholesky_decomp (x);
1853 gsl_linalg_cholesky_invert (x);
1857 for (i = 0; i < x->size1; ++i)
1859 sum_ssq_r += ssq_od_n (x, i);
1860 sum_ssq_a += ssq_od_n (a, i);
1863 gsl_matrix_free (a);
1864 gsl_matrix_free (x);
1866 tab_headers (t, heading_columns, 0, heading_rows, 0);
1868 /* Outline the box */
1875 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1877 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Kaiser-Meyer-Olkin Measure of Sampling Adequacy"));
1879 tab_double (t, 2, 0, 0, sum_ssq_r / (sum_ssq_r + sum_ssq_a), NULL);
1881 tab_text (t, 0, 1, TAT_TITLE | TAB_LEFT, _("Bartlett's Test of Sphericity"));
1883 tab_text (t, 1, 1, TAT_TITLE, _("Approx. Chi-Square"));
1884 tab_text (t, 1, 2, TAT_TITLE, _("df"));
1885 tab_text (t, 1, 3, TAT_TITLE, _("Sig."));
1888 /* The literature doesn't say what to do for the value of W when
1889 missing values are involved. The best thing I can think of
1890 is to take the mean average. */
1892 for (i = 0; i < idata->n->size1; ++i)
1893 w += gsl_matrix_get (idata->n, i, i);
1894 w /= idata->n->size1;
1896 xsq = w - 1 - (2 * factor->n_vars + 5) / 6.0;
1897 xsq *= -log (idata->detR);
1899 tab_double (t, 2, 1, 0, xsq, NULL);
1900 tab_double (t, 2, 2, 0, df, &F_8_0);
1901 tab_double (t, 2, 3, 0, gsl_cdf_chisq_Q (xsq, df), NULL);
1907 show_correlation_matrix (factor, idata);
1908 covariance_destroy (cov);
1911 gsl_matrix *am = matrix_dup (analysis_matrix);
1912 gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
1914 gsl_eigen_symmv (am, idata->eval, idata->evec, workspace);
1916 gsl_eigen_symmv_free (workspace);
1917 gsl_matrix_free (am);
1920 gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
1922 idata->n_extractions = n_extracted_factors (factor, idata);
1924 if (idata->n_extractions == 0)
1926 msg (MW, _("The FACTOR criteria result in zero factors extracted. Therefore no analysis will be performed."));
1930 if (idata->n_extractions > factor->n_vars)
1932 msg (MW, _("The FACTOR criteria result in more factors than variables, which is not meaningful. No analysis will be performed."));
1937 gsl_matrix *rotated_factors = NULL;
1938 gsl_vector *rotated_loadings = NULL;
1940 const gsl_vector *extracted_eigenvalues = NULL;
1941 gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
1942 gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
1944 struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, idata->n_extractions);
1945 gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
1947 if ( factor->extraction == EXTRACTION_PAF)
1949 gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
1950 struct smr_workspace *ws = ws_create (analysis_matrix);
1952 for (i = 0 ; i < factor->n_vars ; ++i)
1954 double r2 = squared_multiple_correlation (analysis_matrix, i, ws);
1956 gsl_vector_set (idata->msr, i, r2);
1960 gsl_vector_memcpy (initial_communalities, idata->msr);
1962 for (i = 0; i < factor->extraction_iterations; ++i)
1965 gsl_vector_memcpy (diff, idata->msr);
1967 iterate_factor_matrix (analysis_matrix, idata->msr, factor_matrix, fmw);
1969 gsl_vector_sub (diff, idata->msr);
1971 gsl_vector_minmax (diff, &min, &max);
1973 if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
1976 gsl_vector_free (diff);
1980 gsl_vector_memcpy (extracted_communalities, idata->msr);
1981 extracted_eigenvalues = fmw->eval;
1983 else if (factor->extraction == EXTRACTION_PC)
1985 for (i = 0; i < factor->n_vars; ++i)
1986 gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
1988 gsl_vector_memcpy (extracted_communalities, initial_communalities);
1990 iterate_factor_matrix (analysis_matrix, extracted_communalities, factor_matrix, fmw);
1993 extracted_eigenvalues = idata->eval;
1997 show_communalities (factor, initial_communalities, extracted_communalities);
2000 if ( factor->rotation != ROT_NONE)
2002 rotated_factors = gsl_matrix_calloc (factor_matrix->size1, factor_matrix->size2);
2003 rotated_loadings = gsl_vector_calloc (factor_matrix->size2);
2005 rotate (factor, factor_matrix, extracted_communalities, rotated_factors, rotated_loadings);
2008 show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues, rotated_loadings);
2010 factor_matrix_workspace_free (fmw);
2012 show_scree (factor, idata);
2014 show_factor_matrix (factor, idata,
2015 factor->extraction == EXTRACTION_PC ? _("Component Matrix") : _("Factor Matrix"),
2018 if ( factor->rotation != ROT_NONE)
2020 show_factor_matrix (factor, idata,
2021 factor->extraction == EXTRACTION_PC ? _("Rotated Component Matrix") : _("Rotated Factor Matrix"),
2024 gsl_matrix_free (rotated_factors);
2029 gsl_matrix_free (factor_matrix);
2030 gsl_vector_free (rotated_loadings);
2031 gsl_vector_free (initial_communalities);
2032 gsl_vector_free (extracted_communalities);
2039 casereader_destroy (r);