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;
156 /* Extraction Criteria */
171 /* Intermediate values used in calculation */
173 const gsl_matrix *corr ; /* The correlation matrix */
174 gsl_matrix *cov ; /* The covariance matrix */
175 const gsl_matrix *n ; /* Matrix of number of samples */
177 gsl_vector *eval ; /* The eigenvalues */
178 gsl_matrix *evec ; /* The eigenvectors */
182 gsl_vector *msr ; /* Multiple Squared Regressions */
184 double detR; /* The determinant of the correlation matrix */
187 static struct idata *
188 idata_alloc (size_t n_vars)
190 struct idata *id = xzalloc (sizeof (*id));
192 id->n_extractions = 0;
193 id->msr = gsl_vector_alloc (n_vars);
195 id->eval = gsl_vector_alloc (n_vars);
196 id->evec = gsl_matrix_alloc (n_vars, n_vars);
202 idata_free (struct idata *id)
204 gsl_vector_free (id->msr);
205 gsl_vector_free (id->eval);
206 gsl_matrix_free (id->evec);
208 gsl_matrix_free (id->cov);
209 if (id->corr != NULL)
210 gsl_matrix_free (CONST_CAST (gsl_matrix *, id->corr));
217 anti_image (const gsl_matrix *m)
221 assert (m->size1 == m->size2);
223 a = gsl_matrix_alloc (m->size1, m->size2);
225 for (i = 0; i < m->size1; ++i)
227 for (j = 0; j < m->size2; ++j)
229 double *p = gsl_matrix_ptr (a, i, j);
230 *p = gsl_matrix_get (m, i, j);
231 *p /= gsl_matrix_get (m, i, i);
232 *p /= gsl_matrix_get (m, j, j);
240 /* Return the sum of all the elements excluding row N */
242 ssq_od_n (const gsl_matrix *m, int n)
246 assert (m->size1 == m->size2);
248 assert (n < m->size1);
250 for (i = 0; i < m->size1; ++i)
252 if (i == n ) continue;
253 for (j = 0; j < m->size2; ++j)
255 ss += pow2 (gsl_matrix_get (m, i, j));
266 dump_matrix (const gsl_matrix *m)
270 for (i = 0 ; i < m->size1; ++i)
272 for (j = 0 ; j < m->size2; ++j)
273 printf ("%02f ", gsl_matrix_get (m, i, j));
279 dump_matrix_permute (const gsl_matrix *m, const gsl_permutation *p)
283 for (i = 0 ; i < m->size1; ++i)
285 for (j = 0 ; j < m->size2; ++j)
286 printf ("%02f ", gsl_matrix_get (m, gsl_permutation_get (p, i), j));
293 dump_vector (const gsl_vector *v)
296 for (i = 0 ; i < v->size; ++i)
298 printf ("%02f\n", gsl_vector_get (v, i));
306 n_extracted_factors (const struct cmd_factor *factor, struct idata *idata)
310 /* If there is a cached value, then return that. */
311 if ( idata->n_extractions != 0)
312 return idata->n_extractions;
314 /* Otherwise, if the number of factors has been explicitly requested,
316 if (factor->n_factors > 0)
318 idata->n_extractions = factor->n_factors;
322 /* Use the MIN_EIGEN setting. */
323 for (i = 0 ; i < idata->eval->size; ++i)
325 double evali = fabs (gsl_vector_get (idata->eval, i));
327 idata->n_extractions = i;
329 if (evali < factor->min_eigen)
334 return idata->n_extractions;
338 /* Returns a newly allocated matrix identical to M.
339 It it the callers responsibility to free the returned value.
342 matrix_dup (const gsl_matrix *m)
344 gsl_matrix *n = gsl_matrix_alloc (m->size1, m->size2);
346 gsl_matrix_memcpy (n, m);
354 /* Copy of the subject */
359 gsl_permutation *perm;
366 static struct smr_workspace *ws_create (const gsl_matrix *input)
368 struct smr_workspace *ws = xmalloc (sizeof (*ws));
370 ws->m = gsl_matrix_alloc (input->size1, input->size2);
371 ws->inverse = gsl_matrix_calloc (input->size1 - 1, input->size2 - 1);
372 ws->perm = gsl_permutation_alloc (input->size1 - 1);
373 ws->result1 = gsl_matrix_calloc (input->size1 - 1, 1);
374 ws->result2 = gsl_matrix_calloc (1, 1);
380 ws_destroy (struct smr_workspace *ws)
382 gsl_matrix_free (ws->result2);
383 gsl_matrix_free (ws->result1);
384 gsl_permutation_free (ws->perm);
385 gsl_matrix_free (ws->inverse);
386 gsl_matrix_free (ws->m);
393 Return the square of the regression coefficient for VAR regressed against all other variables.
396 squared_multiple_correlation (const gsl_matrix *corr, int var, struct smr_workspace *ws)
398 /* For an explanation of what this is doing, see
399 http://www.visualstatistics.net/Visual%20Statistics%20Multimedia/multiple_regression_analysis.htm
405 gsl_matrix_memcpy (ws->m, corr);
407 gsl_matrix_swap_rows (ws->m, 0, var);
408 gsl_matrix_swap_columns (ws->m, 0, var);
410 rxx = gsl_matrix_submatrix (ws->m, 1, 1, ws->m->size1 - 1, ws->m->size1 - 1);
412 gsl_linalg_LU_decomp (&rxx.matrix, ws->perm, &signum);
414 gsl_linalg_LU_invert (&rxx.matrix, ws->perm, ws->inverse);
417 gsl_matrix_const_view rxy = gsl_matrix_const_submatrix (ws->m, 1, 0, ws->m->size1 - 1, 1);
418 gsl_matrix_const_view ryx = gsl_matrix_const_submatrix (ws->m, 0, 1, 1, ws->m->size1 - 1);
420 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
421 1.0, ws->inverse, &rxy.matrix, 0.0, ws->result1);
423 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
424 1.0, &ryx.matrix, ws->result1, 0.0, ws->result2);
427 return gsl_matrix_get (ws->result2, 0, 0);
432 static double the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors);
435 struct factor_matrix_workspace
438 gsl_eigen_symmv_workspace *eigen_ws;
448 static struct factor_matrix_workspace *
449 factor_matrix_workspace_alloc (size_t n, size_t nf)
451 struct factor_matrix_workspace *ws = xmalloc (sizeof (*ws));
454 ws->gamma = gsl_matrix_calloc (nf, nf);
455 ws->eigen_ws = gsl_eigen_symmv_alloc (n);
456 ws->eval = gsl_vector_alloc (n);
457 ws->evec = gsl_matrix_alloc (n, n);
458 ws->r = gsl_matrix_alloc (n, n);
464 factor_matrix_workspace_free (struct factor_matrix_workspace *ws)
466 gsl_eigen_symmv_free (ws->eigen_ws);
467 gsl_vector_free (ws->eval);
468 gsl_matrix_free (ws->evec);
469 gsl_matrix_free (ws->gamma);
470 gsl_matrix_free (ws->r);
475 Shift P left by OFFSET places, and overwrite TARGET
476 with the shifted result.
477 Positions in TARGET less than OFFSET are unchanged.
480 perm_shift_apply (gsl_permutation *target, const gsl_permutation *p,
484 assert (target->size == p->size);
485 assert (offset <= target->size);
487 for (i = 0; i < target->size - offset; ++i)
489 target->data[i] = p->data [i + offset];
495 Indirectly sort the rows of matrix INPUT, storing the sort order in PERM.
496 The sort criteria are as follows:
498 Rows are sorted on the first column, until the absolute value of an
499 element in a subsequent column is greater than that of the first
500 column. Thereafter, rows will be sorted on the second column,
501 until the absolute value of an element in a subsequent column
502 exceeds that of the second column ...
505 sort_matrix_indirect (const gsl_matrix *input, gsl_permutation *perm)
507 const size_t n = perm->size;
508 const size_t m = input->size2;
515 assert (perm->size == input->size1);
517 p = gsl_permutation_alloc (n);
519 /* Copy INPUT into MAT, discarding the sign */
520 mat = gsl_matrix_alloc (n, m);
521 for (i = 0 ; i < mat->size1; ++i)
523 for (j = 0 ; j < mat->size2; ++j)
525 double x = gsl_matrix_get (input, i, j);
526 gsl_matrix_set (mat, i, j, fabs (x));
530 while (column_n < m && row_n < n)
532 gsl_vector_const_view columni = gsl_matrix_const_column (mat, column_n);
533 gsl_sort_vector_index (p, &columni.vector);
535 for (i = 0 ; i < n; ++i)
537 gsl_vector_view row = gsl_matrix_row (mat, p->data[n - 1 - i]);
538 size_t maxindex = gsl_vector_max_index (&row.vector);
540 if ( maxindex > column_n )
543 /* All subsequent elements of this row, are of no interest.
544 So set them all to a highly negative value */
545 for (j = column_n + 1; j < row.vector.size ; ++j)
546 gsl_vector_set (&row.vector, j, -DBL_MAX);
549 perm_shift_apply (perm, p, row_n);
555 gsl_permutation_free (p);
556 gsl_matrix_free (mat);
558 assert ( 0 == gsl_permutation_valid (perm));
560 /* We want the biggest value to be first */
561 gsl_permutation_reverse (perm);
566 drot_go (double phi, double *l0, double *l1)
568 double r0 = cos (phi) * *l0 + sin (phi) * *l1;
569 double r1 = - sin (phi) * *l0 + cos (phi) * *l1;
577 clone_matrix (const gsl_matrix *m)
580 gsl_matrix *c = gsl_matrix_calloc (m->size1, m->size2);
582 for (j = 0 ; j < c->size1; ++j)
584 for (k = 0 ; k < c->size2; ++k)
586 const double *v = gsl_matrix_const_ptr (m, j, k);
587 gsl_matrix_set (c, j, k, *v);
596 initial_sv (const gsl_matrix *fm)
601 for (j = 0 ; j < fm->size2; ++j)
606 for (k = j + 1 ; k < fm->size2; ++k)
608 double lambda = gsl_matrix_get (fm, k, j);
609 double lambda_sq = lambda * lambda;
610 double lambda_4 = lambda_sq * lambda_sq;
615 sv += ( fm->size1 * l4s - (l2s * l2s) ) / (fm->size1 * fm->size1 );
621 rotate (const struct cmd_factor *cf, const gsl_matrix *unrot,
622 const gsl_vector *communalities,
624 gsl_vector *rotated_loadings
631 /* First get a normalised version of UNROT */
632 gsl_matrix *normalised = gsl_matrix_calloc (unrot->size1, unrot->size2);
633 gsl_matrix *h_sqrt = gsl_matrix_calloc (communalities->size, communalities->size);
634 gsl_matrix *h_sqrt_inv ;
636 /* H is the diagonal matrix containing the absolute values of the communalities */
637 for (i = 0 ; i < communalities->size ; ++i)
639 double *ptr = gsl_matrix_ptr (h_sqrt, i, i);
640 *ptr = fabs (gsl_vector_get (communalities, i));
643 /* Take the square root of the communalities */
644 gsl_linalg_cholesky_decomp (h_sqrt);
647 /* Save a copy of h_sqrt and invert it */
648 h_sqrt_inv = clone_matrix (h_sqrt);
649 gsl_linalg_cholesky_decomp (h_sqrt_inv);
650 gsl_linalg_cholesky_invert (h_sqrt_inv);
652 /* normalised vertion is H^{1/2} x UNROT */
653 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, h_sqrt_inv, unrot, 0.0, normalised);
655 gsl_matrix_free (h_sqrt_inv);
658 /* Now perform the rotation iterations */
660 prev_sv = initial_sv (normalised);
661 for (i = 0 ; i < cf->iterations ; ++i)
664 for (j = 0 ; j < normalised->size2; ++j)
666 /* These variables relate to the convergence criterium */
670 for (k = j + 1 ; k < normalised->size2; ++k)
680 for (p = 0; p < normalised->size1; ++p)
682 double jv = gsl_matrix_get (normalised, p, j);
683 double kv = gsl_matrix_get (normalised, p, k);
685 double u = jv * jv - kv * kv;
686 double v = 2 * jv * kv;
693 rotation_coeff [cf->rotation] (&x, &y, a, b, c, d, normalised);
695 phi = atan2 (x, y) / 4.0 ;
697 /* Don't bother rotating if the angle is small */
698 if ( fabs (sin (phi) ) <= pow (10.0, -15.0))
701 for (p = 0; p < normalised->size1; ++p)
703 double *lambda0 = gsl_matrix_ptr (normalised, p, j);
704 double *lambda1 = gsl_matrix_ptr (normalised, p, k);
705 drot_go (phi, lambda0, lambda1);
708 /* Calculate the convergence criterium */
710 double lambda = gsl_matrix_get (normalised, k, j);
711 double lambda_sq = lambda * lambda;
712 double lambda_4 = lambda_sq * lambda_sq;
718 sv += ( normalised->size1 * l4s - (l2s * l2s) ) / (normalised->size1 * normalised->size1 );
721 if ( fabs (sv - prev_sv) <= cf->rconverge)
727 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0,
728 h_sqrt, normalised, 0.0, result);
730 gsl_matrix_free (h_sqrt);
731 gsl_matrix_free (normalised);
734 /* reflect negative sums and populate the rotated loadings vector*/
735 for (i = 0 ; i < result->size2; ++i)
739 for (j = 0 ; j < result->size1; ++j)
741 double s = gsl_matrix_get (result, j, i);
743 sum += gsl_matrix_get (result, j, i);
746 gsl_vector_set (rotated_loadings, i, ssq);
749 for (j = 0 ; j < result->size1; ++j)
751 double *lambda = gsl_matrix_ptr (result, j, i);
759 Get an approximation for the factor matrix into FACTORS, and the communalities into COMMUNALITIES.
760 R is the matrix to be analysed.
761 WS is a pointer to a structure which must have been initialised with factor_matrix_workspace_init.
764 iterate_factor_matrix (const gsl_matrix *r, gsl_vector *communalities, gsl_matrix *factors,
765 struct factor_matrix_workspace *ws)
770 assert (r->size1 == r->size2);
771 assert (r->size1 == communalities->size);
773 assert (factors->size1 == r->size1);
774 assert (factors->size2 == ws->n_factors);
776 gsl_matrix_memcpy (ws->r, r);
778 /* Apply Communalities to diagonal of correlation matrix */
779 for (i = 0 ; i < communalities->size ; ++i)
781 double *x = gsl_matrix_ptr (ws->r, i, i);
782 *x = gsl_vector_get (communalities, i);
785 gsl_eigen_symmv (ws->r, ws->eval, ws->evec, ws->eigen_ws);
787 mv = gsl_matrix_submatrix (ws->evec, 0, 0, ws->evec->size1, ws->n_factors);
789 /* Gamma is the diagonal matrix containing the absolute values of the eigenvalues */
790 for (i = 0 ; i < ws->n_factors ; ++i)
792 double *ptr = gsl_matrix_ptr (ws->gamma, i, i);
793 *ptr = fabs (gsl_vector_get (ws->eval, i));
796 /* Take the square root of gamma */
797 gsl_linalg_cholesky_decomp (ws->gamma);
799 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, &mv.matrix, ws->gamma, 0.0, factors);
801 for (i = 0 ; i < r->size1 ; ++i)
803 double h = the_communality (ws->evec, ws->eval, i, ws->n_factors);
804 gsl_vector_set (communalities, i, h);
810 static bool run_factor (struct dataset *ds, const struct cmd_factor *factor);
814 cmd_factor (struct lexer *lexer, struct dataset *ds)
816 const struct dictionary *dict = dataset_dict (ds);
818 struct cmd_factor factor;
821 factor.method = METHOD_CORR;
822 factor.missing_type = MISS_LISTWISE;
823 factor.exclude = MV_ANY;
824 factor.print = PRINT_INITIAL | PRINT_EXTRACTION | PRINT_ROTATION;
825 factor.extraction = EXTRACTION_PC;
826 factor.n_factors = 0;
827 factor.min_eigen = SYSMIS;
828 factor.iterations = 25;
829 factor.econverge = 0.001;
834 factor.rotation = ROT_VARIMAX;
836 factor.rconverge = 0.0001;
838 factor.wv = dict_get_weight (dict);
840 lex_match (lexer, T_SLASH);
842 if (!lex_force_match_id (lexer, "VARIABLES"))
847 lex_match (lexer, T_EQUALS);
849 if (!parse_variables_const (lexer, dict, &factor.vars, &factor.n_vars,
850 PV_NO_DUPLICATE | PV_NUMERIC))
853 if (factor.n_vars < 2)
854 msg (MW, _("Factor analysis on a single variable is not useful."));
856 while (lex_token (lexer) != T_ENDCMD)
858 lex_match (lexer, T_SLASH);
860 if (lex_match_id (lexer, "PLOT"))
862 lex_match (lexer, T_EQUALS);
863 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
865 if (lex_match_id (lexer, "EIGEN"))
867 factor.plot |= PLOT_SCREE;
869 #if FACTOR_FULLY_IMPLEMENTED
870 else if (lex_match_id (lexer, "ROTATION"))
876 lex_error (lexer, NULL);
881 else if (lex_match_id (lexer, "METHOD"))
883 lex_match (lexer, T_EQUALS);
884 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
886 if (lex_match_id (lexer, "COVARIANCE"))
888 factor.method = METHOD_COV;
890 else if (lex_match_id (lexer, "CORRELATION"))
892 factor.method = METHOD_CORR;
896 lex_error (lexer, NULL);
901 else if (lex_match_id (lexer, "ROTATION"))
903 lex_match (lexer, T_EQUALS);
904 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
906 /* VARIMAX and DEFAULT are defaults */
907 if (lex_match_id (lexer, "VARIMAX") || lex_match_id (lexer, "DEFAULT"))
909 factor.rotation = ROT_VARIMAX;
911 else if (lex_match_id (lexer, "EQUAMAX"))
913 factor.rotation = ROT_EQUAMAX;
915 else if (lex_match_id (lexer, "QUARTIMAX"))
917 factor.rotation = ROT_QUARTIMAX;
919 else if (lex_match_id (lexer, "NOROTATE"))
921 factor.rotation = ROT_NONE;
925 lex_error (lexer, NULL);
930 else if (lex_match_id (lexer, "CRITERIA"))
932 lex_match (lexer, T_EQUALS);
933 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
935 if (lex_match_id (lexer, "FACTORS"))
937 if ( lex_force_match (lexer, T_LPAREN))
939 lex_force_int (lexer);
940 factor.n_factors = lex_integer (lexer);
942 lex_force_match (lexer, T_RPAREN);
945 else if (lex_match_id (lexer, "MINEIGEN"))
947 if ( lex_force_match (lexer, T_LPAREN))
949 lex_force_num (lexer);
950 factor.min_eigen = lex_number (lexer);
952 lex_force_match (lexer, T_RPAREN);
955 else if (lex_match_id (lexer, "ECONVERGE"))
957 if ( lex_force_match (lexer, T_LPAREN))
959 lex_force_num (lexer);
960 factor.econverge = lex_number (lexer);
962 lex_force_match (lexer, T_RPAREN);
965 else if (lex_match_id (lexer, "RCONVERGE"))
967 if ( lex_force_match (lexer, T_LPAREN))
969 lex_force_num (lexer);
970 factor.rconverge = lex_number (lexer);
972 lex_force_match (lexer, T_RPAREN);
975 else if (lex_match_id (lexer, "ITERATE"))
977 if ( lex_force_match (lexer, T_LPAREN))
979 lex_force_int (lexer);
980 factor.iterations = lex_integer (lexer);
982 lex_force_match (lexer, T_RPAREN);
985 else if (lex_match_id (lexer, "DEFAULT"))
987 factor.n_factors = 0;
988 factor.min_eigen = 1;
989 factor.iterations = 25;
993 lex_error (lexer, NULL);
998 else if (lex_match_id (lexer, "EXTRACTION"))
1000 lex_match (lexer, T_EQUALS);
1001 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1003 if (lex_match_id (lexer, "PAF"))
1005 factor.extraction = EXTRACTION_PAF;
1007 else if (lex_match_id (lexer, "PC"))
1009 factor.extraction = EXTRACTION_PC;
1011 else if (lex_match_id (lexer, "PA1"))
1013 factor.extraction = EXTRACTION_PC;
1015 else if (lex_match_id (lexer, "DEFAULT"))
1017 factor.extraction = EXTRACTION_PC;
1021 lex_error (lexer, NULL);
1026 else if (lex_match_id (lexer, "FORMAT"))
1028 lex_match (lexer, T_EQUALS);
1029 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1031 if (lex_match_id (lexer, "SORT"))
1035 else if (lex_match_id (lexer, "BLANK"))
1037 if ( lex_force_match (lexer, T_LPAREN))
1039 lex_force_num (lexer);
1040 factor.blank = lex_number (lexer);
1042 lex_force_match (lexer, T_RPAREN);
1045 else if (lex_match_id (lexer, "DEFAULT"))
1048 factor.sort = false;
1052 lex_error (lexer, NULL);
1057 else if (lex_match_id (lexer, "PRINT"))
1060 lex_match (lexer, T_EQUALS);
1061 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1063 if (lex_match_id (lexer, "UNIVARIATE"))
1065 factor.print |= PRINT_UNIVARIATE;
1067 else if (lex_match_id (lexer, "DET"))
1069 factor.print |= PRINT_DETERMINANT;
1071 #if FACTOR_FULLY_IMPLEMENTED
1072 else if (lex_match_id (lexer, "INV"))
1075 else if (lex_match_id (lexer, "AIC"))
1079 else if (lex_match_id (lexer, "SIG"))
1081 factor.print |= PRINT_SIG;
1083 else if (lex_match_id (lexer, "CORRELATION"))
1085 factor.print |= PRINT_CORRELATION;
1087 #if FACTOR_FULLY_IMPLEMENTED
1088 else if (lex_match_id (lexer, "COVARIANCE"))
1092 else if (lex_match_id (lexer, "ROTATION"))
1094 factor.print |= PRINT_ROTATION;
1096 else if (lex_match_id (lexer, "EXTRACTION"))
1098 factor.print |= PRINT_EXTRACTION;
1100 else if (lex_match_id (lexer, "INITIAL"))
1102 factor.print |= PRINT_INITIAL;
1104 else if (lex_match_id (lexer, "KMO"))
1106 factor.print |= PRINT_KMO;
1108 #if FACTOR_FULLY_IMPLEMENTED
1109 else if (lex_match_id (lexer, "REPR"))
1112 else if (lex_match_id (lexer, "FSCORE"))
1116 else if (lex_match (lexer, T_ALL))
1118 factor.print = 0xFFFF;
1120 else if (lex_match_id (lexer, "DEFAULT"))
1122 factor.print |= PRINT_INITIAL ;
1123 factor.print |= PRINT_EXTRACTION ;
1124 factor.print |= PRINT_ROTATION ;
1128 lex_error (lexer, NULL);
1133 else if (lex_match_id (lexer, "MISSING"))
1135 lex_match (lexer, T_EQUALS);
1136 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1138 if (lex_match_id (lexer, "INCLUDE"))
1140 factor.exclude = MV_SYSTEM;
1142 else if (lex_match_id (lexer, "EXCLUDE"))
1144 factor.exclude = MV_ANY;
1146 else if (lex_match_id (lexer, "LISTWISE"))
1148 factor.missing_type = MISS_LISTWISE;
1150 else if (lex_match_id (lexer, "PAIRWISE"))
1152 factor.missing_type = MISS_PAIRWISE;
1154 else if (lex_match_id (lexer, "MEANSUB"))
1156 factor.missing_type = MISS_MEANSUB;
1160 lex_error (lexer, NULL);
1167 lex_error (lexer, NULL);
1172 if ( factor.rotation == ROT_NONE )
1173 factor.print &= ~PRINT_ROTATION;
1175 if ( ! run_factor (ds, &factor))
1186 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
1190 run_factor (struct dataset *ds, const struct cmd_factor *factor)
1192 struct dictionary *dict = dataset_dict (ds);
1194 struct casereader *group;
1196 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
1198 while (casegrouper_get_next_group (grouper, &group))
1200 if ( factor->missing_type == MISS_LISTWISE )
1201 group = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
1204 do_factor (factor, group);
1207 ok = casegrouper_destroy (grouper);
1208 ok = proc_commit (ds) && ok;
1214 /* Return the communality of variable N, calculated to N_FACTORS */
1216 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
1223 assert (n < eval->size);
1224 assert (n < evec->size1);
1225 assert (n_factors <= eval->size);
1227 for (i = 0 ; i < n_factors; ++i)
1229 double evali = fabs (gsl_vector_get (eval, i));
1231 double eveci = gsl_matrix_get (evec, n, i);
1233 comm += pow2 (eveci) * evali;
1239 /* Return the communality of variable N, calculated to N_FACTORS */
1241 communality (struct idata *idata, int n, int n_factors)
1243 return the_communality (idata->evec, idata->eval, n, n_factors);
1248 show_scree (const struct cmd_factor *f, struct idata *idata)
1253 if ( !(f->plot & PLOT_SCREE) )
1257 label = f->extraction == EXTRACTION_PC ? _("Component Number") : _("Factor Number");
1259 s = scree_create (idata->eval, label);
1265 show_communalities (const struct cmd_factor * factor,
1266 const gsl_vector *initial, const gsl_vector *extracted)
1270 const int heading_columns = 1;
1271 int nc = heading_columns;
1272 const int heading_rows = 1;
1273 const int nr = heading_rows + factor->n_vars;
1274 struct tab_table *t;
1276 if (factor->print & PRINT_EXTRACTION)
1279 if (factor->print & PRINT_INITIAL)
1282 /* No point having a table with only headings */
1286 t = tab_create (nc, nr);
1288 tab_title (t, _("Communalities"));
1290 tab_headers (t, heading_columns, 0, heading_rows, 0);
1293 if (factor->print & PRINT_INITIAL)
1294 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
1296 if (factor->print & PRINT_EXTRACTION)
1297 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
1299 /* Outline the box */
1306 /* Vertical lines */
1313 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1314 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1316 for (i = 0 ; i < factor->n_vars; ++i)
1319 tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
1321 if (factor->print & PRINT_INITIAL)
1322 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL);
1324 if (factor->print & PRINT_EXTRACTION)
1325 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL);
1333 show_factor_matrix (const struct cmd_factor *factor, struct idata *idata, const char *title, const gsl_matrix *fm)
1336 const int n_factors = idata->n_extractions;
1338 const int heading_columns = 1;
1339 const int heading_rows = 2;
1340 const int nr = heading_rows + factor->n_vars;
1341 const int nc = heading_columns + n_factors;
1342 gsl_permutation *perm;
1344 struct tab_table *t = tab_create (nc, nr);
1347 if ( factor->extraction == EXTRACTION_PC )
1348 tab_title (t, _("Component Matrix"));
1350 tab_title (t, _("Factor Matrix"));
1353 tab_title (t, "%s", title);
1355 tab_headers (t, heading_columns, 0, heading_rows, 0);
1357 if ( factor->extraction == EXTRACTION_PC )
1361 TAB_CENTER | TAT_TITLE, _("Component"));
1366 TAB_CENTER | TAT_TITLE, _("Factor"));
1369 tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1372 /* Outline the box */
1379 /* Vertical lines */
1386 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1387 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1390 /* Initialise to the identity permutation */
1391 perm = gsl_permutation_calloc (factor->n_vars);
1394 sort_matrix_indirect (fm, perm);
1396 for (i = 0 ; i < n_factors; ++i)
1398 tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1401 for (i = 0 ; i < factor->n_vars; ++i)
1404 const int matrix_row = perm->data[i];
1405 tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1407 for (j = 0 ; j < n_factors; ++j)
1409 double x = gsl_matrix_get (fm, matrix_row, j);
1411 if ( fabs (x) < factor->blank)
1414 tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL);
1418 gsl_permutation_free (perm);
1425 show_explained_variance (const struct cmd_factor * factor, struct idata *idata,
1426 const gsl_vector *initial_eigenvalues,
1427 const gsl_vector *extracted_eigenvalues,
1428 const gsl_vector *rotated_loadings)
1432 const int heading_columns = 1;
1433 const int heading_rows = 2;
1434 const int nr = heading_rows + factor->n_vars;
1436 struct tab_table *t ;
1438 double i_total = 0.0;
1441 double e_total = 0.0;
1446 int nc = heading_columns;
1448 if (factor->print & PRINT_EXTRACTION)
1451 if (factor->print & PRINT_INITIAL)
1454 if (factor->print & PRINT_ROTATION)
1457 /* No point having a table with only headings */
1458 if ( nc <= heading_columns)
1461 t = tab_create (nc, nr);
1463 tab_title (t, _("Total Variance Explained"));
1465 tab_headers (t, heading_columns, 0, heading_rows, 0);
1467 /* Outline the box */
1474 /* Vertical lines */
1481 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1482 tab_hline (t, TAL_1, 1, nc - 1, 1);
1484 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1487 if ( factor->extraction == EXTRACTION_PC)
1488 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1490 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1493 if (factor->print & PRINT_INITIAL)
1495 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1499 if (factor->print & PRINT_EXTRACTION)
1501 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1505 if (factor->print & PRINT_ROTATION)
1507 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1511 for (i = 0; i < (nc - heading_columns) / 3 ; ++i)
1513 tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1514 /* xgettext:no-c-format */
1515 tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1516 tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1518 tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1521 for (i = 0 ; i < initial_eigenvalues->size; ++i)
1522 i_total += gsl_vector_get (initial_eigenvalues, i);
1524 if ( factor->extraction == EXTRACTION_PAF)
1526 e_total = factor->n_vars;
1533 for (i = 0 ; i < factor->n_vars; ++i)
1535 const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1536 double i_percent = 100.0 * i_lambda / i_total ;
1538 const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1539 double e_percent = 100.0 * e_lambda / e_total ;
1543 tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%zu"), i + 1);
1548 /* Initial Eigenvalues */
1549 if (factor->print & PRINT_INITIAL)
1551 tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL);
1552 tab_double (t, c++, i + heading_rows, 0, i_percent, NULL);
1553 tab_double (t, c++, i + heading_rows, 0, i_cum, NULL);
1557 if (factor->print & PRINT_EXTRACTION)
1559 if (i < idata->n_extractions)
1561 /* Sums of squared loadings */
1562 tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL);
1563 tab_double (t, c++, i + heading_rows, 0, e_percent, NULL);
1564 tab_double (t, c++, i + heading_rows, 0, e_cum, NULL);
1568 if (rotated_loadings != NULL)
1570 const double r_lambda = gsl_vector_get (rotated_loadings, i);
1571 double r_percent = 100.0 * r_lambda / e_total ;
1573 if (factor->print & PRINT_ROTATION)
1575 if (i < idata->n_extractions)
1578 tab_double (t, c++, i + heading_rows, 0, r_lambda, NULL);
1579 tab_double (t, c++, i + heading_rows, 0, r_percent, NULL);
1580 tab_double (t, c++, i + heading_rows, 0, r_cum, NULL);
1591 show_correlation_matrix (const struct cmd_factor *factor, const struct idata *idata)
1593 struct tab_table *t ;
1595 int y_pos_corr = -1;
1597 int suffix_rows = 0;
1599 const int heading_rows = 1;
1600 const int heading_columns = 2;
1602 int nc = heading_columns ;
1603 int nr = heading_rows ;
1604 int n_data_sets = 0;
1606 if (factor->print & PRINT_CORRELATION)
1608 y_pos_corr = n_data_sets;
1610 nc = heading_columns + factor->n_vars;
1613 if (factor->print & PRINT_SIG)
1615 y_pos_sig = n_data_sets;
1617 nc = heading_columns + factor->n_vars;
1620 nr += n_data_sets * factor->n_vars;
1622 if (factor->print & PRINT_DETERMINANT)
1625 /* If the table would contain only headings, don't bother rendering it */
1626 if (nr <= heading_rows && suffix_rows == 0)
1629 t = tab_create (nc, nr + suffix_rows);
1631 tab_title (t, _("Correlation Matrix"));
1633 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1635 if (nr > heading_rows)
1637 tab_headers (t, heading_columns, 0, heading_rows, 0);
1639 tab_vline (t, TAL_2, 2, 0, nr - 1);
1641 /* Outline the box */
1648 /* Vertical lines */
1656 for (i = 0; i < factor->n_vars; ++i)
1657 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
1660 for (i = 0 ; i < n_data_sets; ++i)
1662 int y = heading_rows + i * factor->n_vars;
1664 for (v = 0; v < factor->n_vars; ++v)
1665 tab_text (t, 1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
1667 tab_hline (t, TAL_1, 0, nc - 1, y);
1670 if (factor->print & PRINT_CORRELATION)
1672 const double y = heading_rows + y_pos_corr;
1673 tab_text (t, 0, y, TAT_TITLE, _("Correlations"));
1675 for (i = 0; i < factor->n_vars; ++i)
1677 for (j = 0; j < factor->n_vars; ++j)
1678 tab_double (t, heading_columns + i, y + j, 0, gsl_matrix_get (idata->corr, i, j), NULL);
1682 if (factor->print & PRINT_SIG)
1684 const double y = heading_rows + y_pos_sig * factor->n_vars;
1685 tab_text (t, 0, y, TAT_TITLE, _("Sig. (1-tailed)"));
1687 for (i = 0; i < factor->n_vars; ++i)
1689 for (j = 0; j < factor->n_vars; ++j)
1691 double rho = gsl_matrix_get (idata->corr, i, j);
1692 double w = gsl_matrix_get (idata->n, i, j);
1697 tab_double (t, heading_columns + i, y + j, 0, significance_of_correlation (rho, w), NULL);
1703 if (factor->print & PRINT_DETERMINANT)
1705 tab_text (t, 0, nr, TAB_LEFT | TAT_TITLE, _("Determinant"));
1707 tab_double (t, 1, nr, 0, idata->detR, NULL);
1716 do_factor (const struct cmd_factor *factor, struct casereader *r)
1719 const gsl_matrix *var_matrix;
1720 const gsl_matrix *mean_matrix;
1722 const gsl_matrix *analysis_matrix;
1723 struct idata *idata = idata_alloc (factor->n_vars);
1725 struct covariance *cov = covariance_1pass_create (factor->n_vars, factor->vars,
1726 factor->wv, factor->exclude);
1728 for ( ; (c = casereader_read (r) ); case_unref (c))
1730 covariance_accumulate (cov, c);
1733 idata->cov = covariance_calculate (cov);
1735 if (idata->cov == NULL)
1737 msg (MW, _("The dataset contains no complete observations. No analysis will be performed."));
1738 covariance_destroy (cov);
1742 var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
1743 mean_matrix = covariance_moments (cov, MOMENT_MEAN);
1744 idata->n = covariance_moments (cov, MOMENT_NONE);
1747 if ( factor->method == METHOD_CORR)
1749 idata->corr = correlation_from_covariance (idata->cov, var_matrix);
1751 analysis_matrix = idata->corr;
1754 analysis_matrix = idata->cov;
1757 if (factor->print & PRINT_DETERMINANT
1758 || factor->print & PRINT_KMO)
1762 const int size = idata->corr->size1;
1763 gsl_permutation *p = gsl_permutation_calloc (size);
1764 gsl_matrix *tmp = gsl_matrix_calloc (size, size);
1765 gsl_matrix_memcpy (tmp, idata->corr);
1767 gsl_linalg_LU_decomp (tmp, p, &sign);
1768 idata->detR = gsl_linalg_LU_det (tmp, sign);
1769 gsl_permutation_free (p);
1770 gsl_matrix_free (tmp);
1773 if ( factor->print & PRINT_UNIVARIATE)
1775 const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
1779 const int heading_columns = 1;
1780 const int heading_rows = 1;
1782 const int nr = heading_rows + factor->n_vars;
1784 struct tab_table *t = tab_create (nc, nr);
1785 tab_title (t, _("Descriptive Statistics"));
1787 tab_headers (t, heading_columns, 0, heading_rows, 0);
1789 /* Outline the box */
1796 /* Vertical lines */
1803 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1804 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1806 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
1807 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1808 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
1810 for (i = 0 ; i < factor->n_vars; ++i)
1812 const struct variable *v = factor->vars[i];
1813 tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
1815 tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (mean_matrix, i, i), NULL);
1816 tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (var_matrix, i, i)), NULL);
1817 tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (idata->n, i, i), wfmt);
1823 if (factor->print & PRINT_KMO)
1826 double sum_ssq_r = 0;
1827 double sum_ssq_a = 0;
1829 double df = factor->n_vars * ( factor->n_vars - 1) / 2;
1836 const int heading_columns = 2;
1837 const int heading_rows = 0;
1839 const int nr = heading_rows + 4;
1840 const int nc = heading_columns + 1;
1844 struct tab_table *t = tab_create (nc, nr);
1845 tab_title (t, _("KMO and Bartlett's Test"));
1847 x = clone_matrix (idata->corr);
1848 gsl_linalg_cholesky_decomp (x);
1849 gsl_linalg_cholesky_invert (x);
1853 for (i = 0; i < x->size1; ++i)
1855 sum_ssq_r += ssq_od_n (x, i);
1856 sum_ssq_a += ssq_od_n (a, i);
1859 gsl_matrix_free (a);
1860 gsl_matrix_free (x);
1862 tab_headers (t, heading_columns, 0, heading_rows, 0);
1864 /* Outline the box */
1871 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1873 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Kaiser-Meyer-Olkin Measure of Sampling Adequacy"));
1875 tab_double (t, 2, 0, 0, sum_ssq_r / (sum_ssq_r + sum_ssq_a), NULL);
1877 tab_text (t, 0, 1, TAT_TITLE | TAB_LEFT, _("Bartlett's Test of Sphericity"));
1879 tab_text (t, 1, 1, TAT_TITLE, _("Approx. Chi-Square"));
1880 tab_text (t, 1, 2, TAT_TITLE, _("df"));
1881 tab_text (t, 1, 3, TAT_TITLE, _("Sig."));
1884 /* The literature doesn't say what to do for the value of W when
1885 missing values are involved. The best thing I can think of
1886 is to take the mean average. */
1888 for (i = 0; i < idata->n->size1; ++i)
1889 w += gsl_matrix_get (idata->n, i, i);
1890 w /= idata->n->size1;
1892 xsq = w - 1 - (2 * factor->n_vars + 5) / 6.0;
1893 xsq *= -log (idata->detR);
1895 tab_double (t, 2, 1, 0, xsq, NULL);
1896 tab_double (t, 2, 2, 0, df, &F_8_0);
1897 tab_double (t, 2, 3, 0, gsl_cdf_chisq_Q (xsq, df), NULL);
1903 show_correlation_matrix (factor, idata);
1904 covariance_destroy (cov);
1907 gsl_matrix *am = matrix_dup (analysis_matrix);
1908 gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
1910 gsl_eigen_symmv (am, idata->eval, idata->evec, workspace);
1912 gsl_eigen_symmv_free (workspace);
1913 gsl_matrix_free (am);
1916 gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
1918 idata->n_extractions = n_extracted_factors (factor, idata);
1920 if (idata->n_extractions == 0)
1922 msg (MW, _("The FACTOR criteria result in zero factors extracted. Therefore no analysis will be performed."));
1926 if (idata->n_extractions > factor->n_vars)
1928 msg (MW, _("The FACTOR criteria result in more factors than variables, which is not meaningful. No analysis will be performed."));
1933 gsl_matrix *rotated_factors = NULL;
1934 gsl_vector *rotated_loadings = NULL;
1936 const gsl_vector *extracted_eigenvalues = NULL;
1937 gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
1938 gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
1940 struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, idata->n_extractions);
1941 gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
1943 if ( factor->extraction == EXTRACTION_PAF)
1945 gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
1946 struct smr_workspace *ws = ws_create (analysis_matrix);
1948 for (i = 0 ; i < factor->n_vars ; ++i)
1950 double r2 = squared_multiple_correlation (analysis_matrix, i, ws);
1952 gsl_vector_set (idata->msr, i, r2);
1956 gsl_vector_memcpy (initial_communalities, idata->msr);
1958 for (i = 0; i < factor->iterations; ++i)
1961 gsl_vector_memcpy (diff, idata->msr);
1963 iterate_factor_matrix (analysis_matrix, idata->msr, factor_matrix, fmw);
1965 gsl_vector_sub (diff, idata->msr);
1967 gsl_vector_minmax (diff, &min, &max);
1969 if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
1972 gsl_vector_free (diff);
1976 gsl_vector_memcpy (extracted_communalities, idata->msr);
1977 extracted_eigenvalues = fmw->eval;
1979 else if (factor->extraction == EXTRACTION_PC)
1981 for (i = 0; i < factor->n_vars; ++i)
1982 gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
1984 gsl_vector_memcpy (extracted_communalities, initial_communalities);
1986 iterate_factor_matrix (analysis_matrix, extracted_communalities, factor_matrix, fmw);
1989 extracted_eigenvalues = idata->eval;
1993 show_communalities (factor, initial_communalities, extracted_communalities);
1996 if ( factor->rotation != ROT_NONE)
1998 rotated_factors = gsl_matrix_calloc (factor_matrix->size1, factor_matrix->size2);
1999 rotated_loadings = gsl_vector_calloc (factor_matrix->size2);
2001 rotate (factor, factor_matrix, extracted_communalities, rotated_factors, rotated_loadings);
2004 show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues, rotated_loadings);
2006 factor_matrix_workspace_free (fmw);
2008 show_scree (factor, idata);
2010 show_factor_matrix (factor, idata,
2011 factor->extraction == EXTRACTION_PC ? _("Component Matrix") : _("Factor Matrix"),
2014 if ( factor->rotation != ROT_NONE)
2016 show_factor_matrix (factor, idata,
2017 factor->extraction == EXTRACTION_PC ? _("Rotated Component Matrix") : _("Rotated Factor Matrix"),
2020 gsl_matrix_free (rotated_factors);
2025 gsl_matrix_free (factor_matrix);
2026 gsl_vector_free (rotated_loadings);
2027 gsl_vector_free (initial_communalities);
2028 gsl_vector_free (extracted_communalities);
2035 casereader_destroy (r);