1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011 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/message.h"
39 #include "libpspp/misc.h"
40 #include "math/correlation.h"
41 #include "math/covariance.h"
42 #include "math/moments.h"
43 #include "output/chart-item.h"
44 #include "output/charts/scree.h"
45 #include "output/tab.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) msgid
64 enum extraction_method
73 PLOT_ROTATION = 0x0002
78 PRINT_UNIVARIATE = 0x0001,
79 PRINT_DETERMINANT = 0x0002,
83 PRINT_COVARIANCE = 0x0020,
84 PRINT_CORRELATION = 0x0040,
85 PRINT_ROTATION = 0x0080,
86 PRINT_EXTRACTION = 0x0100,
87 PRINT_INITIAL = 0x0200,
101 typedef void (*rotation_coefficients) (double *x, double *y,
102 double a, double b, double c, double d,
103 const gsl_matrix *loadings );
107 varimax_coefficients (double *x, double *y,
108 double a, double b, double c, double d,
109 const gsl_matrix *loadings )
111 *x = d - 2 * a * b / loadings->size1;
112 *y = c - (a * a - b * b) / loadings->size1;
116 equamax_coefficients (double *x, double *y,
117 double a, double b, double c, double d,
118 const gsl_matrix *loadings )
120 *x = d - loadings->size2 * a * b / loadings->size1;
121 *y = c - loadings->size2 * (a * a - b * b) / (2 * loadings->size1);
125 quartimax_coefficients (double *x, double *y,
126 double a UNUSED, double b UNUSED, double c, double d,
127 const gsl_matrix *loadings UNUSED)
133 static const rotation_coefficients rotation_coeff[3] = {
134 varimax_coefficients,
135 equamax_coefficients,
136 quartimax_coefficients
143 const struct variable **vars;
145 const struct variable *wv;
148 enum missing_type missing_type;
149 enum mv_class exclude;
150 enum print_opts print;
151 enum extraction_method extraction;
153 enum rotation_type rotation;
155 /* Extraction Criteria */
170 /* Intermediate values used in calculation */
172 const gsl_matrix *corr ; /* The correlation matrix */
173 gsl_matrix *cov ; /* The covariance matrix */
174 const gsl_matrix *n ; /* Matrix of number of samples */
176 gsl_vector *eval ; /* The eigenvalues */
177 gsl_matrix *evec ; /* The eigenvectors */
181 gsl_vector *msr ; /* Multiple Squared Regressions */
183 double detR; /* The determinant of the correlation matrix */
186 static struct idata *
187 idata_alloc (size_t n_vars)
189 struct idata *id = xzalloc (sizeof (*id));
191 id->n_extractions = 0;
192 id->msr = gsl_vector_alloc (n_vars);
194 id->eval = gsl_vector_alloc (n_vars);
195 id->evec = gsl_matrix_alloc (n_vars, n_vars);
201 idata_free (struct idata *id)
203 gsl_vector_free (id->msr);
204 gsl_vector_free (id->eval);
205 gsl_matrix_free (id->evec);
207 gsl_matrix_free (id->cov);
214 anti_image (const gsl_matrix *m)
218 assert (m->size1 == m->size2);
220 a = gsl_matrix_alloc (m->size1, m->size2);
222 for (i = 0; i < m->size1; ++i)
224 for (j = 0; j < m->size2; ++j)
226 double *p = gsl_matrix_ptr (a, i, j);
227 *p = gsl_matrix_get (m, i, j);
228 *p /= gsl_matrix_get (m, i, i);
229 *p /= gsl_matrix_get (m, j, j);
237 /* Return the sum of all the elements excluding row N */
239 ssq_od_n (const gsl_matrix *m, int n)
243 assert (m->size1 == m->size2);
245 assert (n < m->size1);
247 for (i = 0; i < m->size1; ++i)
249 if (i == n ) continue;
250 for (j = 0; j < m->size2; ++j)
252 ss += pow2 (gsl_matrix_get (m, i, j));
263 dump_matrix (const gsl_matrix *m)
267 for (i = 0 ; i < m->size1; ++i)
269 for (j = 0 ; j < m->size2; ++j)
270 printf ("%02f ", gsl_matrix_get (m, i, j));
276 dump_matrix_permute (const gsl_matrix *m, const gsl_permutation *p)
280 for (i = 0 ; i < m->size1; ++i)
282 for (j = 0 ; j < m->size2; ++j)
283 printf ("%02f ", gsl_matrix_get (m, gsl_permutation_get (p, i), j));
290 dump_vector (const gsl_vector *v)
293 for (i = 0 ; i < v->size; ++i)
295 printf ("%02f\n", gsl_vector_get (v, i));
303 n_extracted_factors (const struct cmd_factor *factor, struct idata *idata)
307 /* If there is a cached value, then return that. */
308 if ( idata->n_extractions != 0)
309 return idata->n_extractions;
311 /* Otherwise, if the number of factors has been explicitly requested,
313 if (factor->n_factors > 0)
315 idata->n_extractions = factor->n_factors;
319 /* Use the MIN_EIGEN setting. */
320 for (i = 0 ; i < idata->eval->size; ++i)
322 double evali = fabs (gsl_vector_get (idata->eval, i));
324 idata->n_extractions = i;
326 if (evali < factor->min_eigen)
331 return idata->n_extractions;
335 /* Returns a newly allocated matrix identical to M.
336 It it the callers responsibility to free the returned value.
339 matrix_dup (const gsl_matrix *m)
341 gsl_matrix *n = gsl_matrix_alloc (m->size1, m->size2);
343 gsl_matrix_memcpy (n, m);
351 /* Copy of the subject */
356 gsl_permutation *perm;
363 static struct smr_workspace *ws_create (const gsl_matrix *input)
365 struct smr_workspace *ws = xmalloc (sizeof (*ws));
367 ws->m = gsl_matrix_alloc (input->size1, input->size2);
368 ws->inverse = gsl_matrix_calloc (input->size1 - 1, input->size2 - 1);
369 ws->perm = gsl_permutation_alloc (input->size1 - 1);
370 ws->result1 = gsl_matrix_calloc (input->size1 - 1, 1);
371 ws->result2 = gsl_matrix_calloc (1, 1);
377 ws_destroy (struct smr_workspace *ws)
379 gsl_matrix_free (ws->result2);
380 gsl_matrix_free (ws->result1);
381 gsl_permutation_free (ws->perm);
382 gsl_matrix_free (ws->inverse);
383 gsl_matrix_free (ws->m);
390 Return the square of the regression coefficient for VAR regressed against all other variables.
393 squared_multiple_correlation (const gsl_matrix *corr, int var, struct smr_workspace *ws)
395 /* For an explanation of what this is doing, see
396 http://www.visualstatistics.net/Visual%20Statistics%20Multimedia/multiple_regression_analysis.htm
402 gsl_matrix_memcpy (ws->m, corr);
404 gsl_matrix_swap_rows (ws->m, 0, var);
405 gsl_matrix_swap_columns (ws->m, 0, var);
407 rxx = gsl_matrix_submatrix (ws->m, 1, 1, ws->m->size1 - 1, ws->m->size1 - 1);
409 gsl_linalg_LU_decomp (&rxx.matrix, ws->perm, &signum);
411 gsl_linalg_LU_invert (&rxx.matrix, ws->perm, ws->inverse);
414 gsl_matrix_const_view rxy = gsl_matrix_const_submatrix (ws->m, 1, 0, ws->m->size1 - 1, 1);
415 gsl_matrix_const_view ryx = gsl_matrix_const_submatrix (ws->m, 0, 1, 1, ws->m->size1 - 1);
417 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
418 1.0, ws->inverse, &rxy.matrix, 0.0, ws->result1);
420 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
421 1.0, &ryx.matrix, ws->result1, 0.0, ws->result2);
424 return gsl_matrix_get (ws->result2, 0, 0);
429 static double the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors);
432 struct factor_matrix_workspace
435 gsl_eigen_symmv_workspace *eigen_ws;
445 static struct factor_matrix_workspace *
446 factor_matrix_workspace_alloc (size_t n, size_t nf)
448 struct factor_matrix_workspace *ws = xmalloc (sizeof (*ws));
451 ws->gamma = gsl_matrix_calloc (nf, nf);
452 ws->eigen_ws = gsl_eigen_symmv_alloc (n);
453 ws->eval = gsl_vector_alloc (n);
454 ws->evec = gsl_matrix_alloc (n, n);
455 ws->r = gsl_matrix_alloc (n, n);
461 factor_matrix_workspace_free (struct factor_matrix_workspace *ws)
463 gsl_eigen_symmv_free (ws->eigen_ws);
464 gsl_vector_free (ws->eval);
465 gsl_matrix_free (ws->evec);
466 gsl_matrix_free (ws->gamma);
467 gsl_matrix_free (ws->r);
472 Shift P left by OFFSET places, and overwrite TARGET
473 with the shifted result.
474 Positions in TARGET less than OFFSET are unchanged.
477 perm_shift_apply (gsl_permutation *target, const gsl_permutation *p,
481 assert (target->size == p->size);
482 assert (offset <= target->size);
484 for (i = 0; i < target->size - offset; ++i)
486 target->data[i] = p->data [i + offset];
492 Indirectly sort the rows of matrix INPUT, storing the sort order in PERM.
493 The sort criteria are as follows:
495 Rows are sorted on the first column, until the absolute value of an
496 element in a subsequent column is greater than that of the first
497 column. Thereafter, rows will be sorted on the second column,
498 until the absolute value of an element in a subsequent column
499 exceeds that of the second column ...
502 sort_matrix_indirect (const gsl_matrix *input, gsl_permutation *perm)
504 const size_t n = perm->size;
505 const size_t m = input->size2;
512 assert (perm->size == input->size1);
514 p = gsl_permutation_alloc (n);
516 /* Copy INPUT into MAT, discarding the sign */
517 mat = gsl_matrix_alloc (n, m);
518 for (i = 0 ; i < mat->size1; ++i)
520 for (j = 0 ; j < mat->size2; ++j)
522 double x = gsl_matrix_get (input, i, j);
523 gsl_matrix_set (mat, i, j, fabs (x));
527 while (column_n < m && row_n < n)
529 gsl_vector_const_view columni = gsl_matrix_const_column (mat, column_n);
530 gsl_sort_vector_index (p, &columni.vector);
532 for (i = 0 ; i < n; ++i)
534 gsl_vector_view row = gsl_matrix_row (mat, p->data[n - 1 - i]);
535 size_t maxindex = gsl_vector_max_index (&row.vector);
537 if ( maxindex > column_n )
540 /* All subsequent elements of this row, are of no interest.
541 So set them all to a highly negative value */
542 for (j = column_n + 1; j < row.vector.size ; ++j)
543 gsl_vector_set (&row.vector, j, -DBL_MAX);
546 perm_shift_apply (perm, p, row_n);
552 gsl_permutation_free (p);
553 gsl_matrix_free (mat);
555 assert ( 0 == gsl_permutation_valid (perm));
557 /* We want the biggest value to be first */
558 gsl_permutation_reverse (perm);
563 drot_go (double phi, double *l0, double *l1)
565 double r0 = cos (phi) * *l0 + sin (phi) * *l1;
566 double r1 = - sin (phi) * *l0 + cos (phi) * *l1;
574 clone_matrix (const gsl_matrix *m)
577 gsl_matrix *c = gsl_matrix_calloc (m->size1, m->size2);
579 for (j = 0 ; j < c->size1; ++j)
581 for (k = 0 ; k < c->size2; ++k)
583 const double *v = gsl_matrix_const_ptr (m, j, k);
584 gsl_matrix_set (c, j, k, *v);
593 initial_sv (const gsl_matrix *fm)
598 for (j = 0 ; j < fm->size2; ++j)
603 for (k = j + 1 ; k < fm->size2; ++k)
605 double lambda = gsl_matrix_get (fm, k, j);
606 double lambda_sq = lambda * lambda;
607 double lambda_4 = lambda_sq * lambda_sq;
612 sv += ( fm->size1 * l4s - (l2s * l2s) ) / (fm->size1 * fm->size1 );
618 rotate (const struct cmd_factor *cf, const gsl_matrix *unrot,
619 const gsl_vector *communalities,
621 gsl_vector *rotated_loadings
628 /* First get a normalised version of UNROT */
629 gsl_matrix *normalised = gsl_matrix_calloc (unrot->size1, unrot->size2);
630 gsl_matrix *h_sqrt = gsl_matrix_calloc (communalities->size, communalities->size);
631 gsl_matrix *h_sqrt_inv ;
633 /* H is the diagonal matrix containing the absolute values of the communalities */
634 for (i = 0 ; i < communalities->size ; ++i)
636 double *ptr = gsl_matrix_ptr (h_sqrt, i, i);
637 *ptr = fabs (gsl_vector_get (communalities, i));
640 /* Take the square root of the communalities */
641 gsl_linalg_cholesky_decomp (h_sqrt);
644 /* Save a copy of h_sqrt and invert it */
645 h_sqrt_inv = clone_matrix (h_sqrt);
646 gsl_linalg_cholesky_decomp (h_sqrt_inv);
647 gsl_linalg_cholesky_invert (h_sqrt_inv);
649 /* normalised vertion is H^{1/2} x UNROT */
650 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, h_sqrt_inv, unrot, 0.0, normalised);
652 gsl_matrix_free (h_sqrt_inv);
655 /* Now perform the rotation iterations */
657 prev_sv = initial_sv (normalised);
658 for (i = 0 ; i < cf->iterations ; ++i)
661 for (j = 0 ; j < normalised->size2; ++j)
663 /* These variables relate to the convergence criterium */
667 for (k = j + 1 ; k < normalised->size2; ++k)
677 for (p = 0; p < normalised->size1; ++p)
679 double jv = gsl_matrix_get (normalised, p, j);
680 double kv = gsl_matrix_get (normalised, p, k);
682 double u = jv * jv - kv * kv;
683 double v = 2 * jv * kv;
690 rotation_coeff [cf->rotation] (&x, &y, a, b, c, d, normalised);
692 phi = atan2 (x, y) / 4.0 ;
694 /* Don't bother rotating if the angle is small */
695 if ( fabs (sin (phi) ) <= pow (10.0, -15.0))
698 for (p = 0; p < normalised->size1; ++p)
700 double *lambda0 = gsl_matrix_ptr (normalised, p, j);
701 double *lambda1 = gsl_matrix_ptr (normalised, p, k);
702 drot_go (phi, lambda0, lambda1);
705 /* Calculate the convergence criterium */
707 double lambda = gsl_matrix_get (normalised, k, j);
708 double lambda_sq = lambda * lambda;
709 double lambda_4 = lambda_sq * lambda_sq;
715 sv += ( normalised->size1 * l4s - (l2s * l2s) ) / (normalised->size1 * normalised->size1 );
718 if ( fabs (sv - prev_sv) <= cf->rconverge)
724 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0,
725 h_sqrt, normalised, 0.0, result);
727 gsl_matrix_free (h_sqrt);
730 /* reflect negative sums and populate the rotated loadings vector*/
731 for (i = 0 ; i < result->size2; ++i)
735 for (j = 0 ; j < result->size1; ++j)
737 double s = gsl_matrix_get (result, j, i);
739 sum += gsl_matrix_get (result, j, i);
742 gsl_vector_set (rotated_loadings, i, ssq);
745 for (j = 0 ; j < result->size1; ++j)
747 double *lambda = gsl_matrix_ptr (result, j, i);
755 Get an approximation for the factor matrix into FACTORS, and the communalities into COMMUNALITIES.
756 R is the matrix to be analysed.
757 WS is a pointer to a structure which must have been initialised with factor_matrix_workspace_init.
760 iterate_factor_matrix (const gsl_matrix *r, gsl_vector *communalities, gsl_matrix *factors,
761 struct factor_matrix_workspace *ws)
766 assert (r->size1 == r->size2);
767 assert (r->size1 == communalities->size);
769 assert (factors->size1 == r->size1);
770 assert (factors->size2 == ws->n_factors);
772 gsl_matrix_memcpy (ws->r, r);
774 /* Apply Communalities to diagonal of correlation matrix */
775 for (i = 0 ; i < communalities->size ; ++i)
777 double *x = gsl_matrix_ptr (ws->r, i, i);
778 *x = gsl_vector_get (communalities, i);
781 gsl_eigen_symmv (ws->r, ws->eval, ws->evec, ws->eigen_ws);
783 mv = gsl_matrix_submatrix (ws->evec, 0, 0, ws->evec->size1, ws->n_factors);
785 /* Gamma is the diagonal matrix containing the absolute values of the eigenvalues */
786 for (i = 0 ; i < ws->n_factors ; ++i)
788 double *ptr = gsl_matrix_ptr (ws->gamma, i, i);
789 *ptr = fabs (gsl_vector_get (ws->eval, i));
792 /* Take the square root of gamma */
793 gsl_linalg_cholesky_decomp (ws->gamma);
795 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, &mv.matrix, ws->gamma, 0.0, factors);
797 for (i = 0 ; i < r->size1 ; ++i)
799 double h = the_communality (ws->evec, ws->eval, i, ws->n_factors);
800 gsl_vector_set (communalities, i, h);
806 static bool run_factor (struct dataset *ds, const struct cmd_factor *factor);
810 cmd_factor (struct lexer *lexer, struct dataset *ds)
812 bool extraction_seen = false;
813 const struct dictionary *dict = dataset_dict (ds);
815 struct cmd_factor factor;
818 factor.method = METHOD_CORR;
819 factor.missing_type = MISS_LISTWISE;
820 factor.exclude = MV_ANY;
821 factor.print = PRINT_INITIAL | PRINT_EXTRACTION | PRINT_ROTATION;
822 factor.extraction = EXTRACTION_PC;
823 factor.n_factors = 0;
824 factor.min_eigen = SYSMIS;
825 factor.iterations = 25;
826 factor.econverge = 0.001;
831 factor.rotation = ROT_VARIMAX;
833 factor.rconverge = 0.0001;
835 factor.wv = dict_get_weight (dict);
837 lex_match (lexer, T_SLASH);
839 if (!lex_force_match_id (lexer, "VARIABLES"))
844 lex_match (lexer, T_EQUALS);
846 if (!parse_variables_const (lexer, dict, &factor.vars, &factor.n_vars,
847 PV_NO_DUPLICATE | PV_NUMERIC))
850 if (factor.n_vars < 2)
851 msg (MW, _("Factor analysis on a single variable is not useful."));
853 while (lex_token (lexer) != T_ENDCMD)
855 lex_match (lexer, T_SLASH);
857 if (lex_match_id (lexer, "PLOT"))
859 lex_match (lexer, T_EQUALS);
860 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
862 if (lex_match_id (lexer, "EIGEN"))
864 factor.plot |= PLOT_SCREE;
866 #if FACTOR_FULLY_IMPLEMENTED
867 else if (lex_match_id (lexer, "ROTATION"))
873 lex_error (lexer, NULL);
878 else if (lex_match_id (lexer, "METHOD"))
880 lex_match (lexer, T_EQUALS);
881 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
883 if (lex_match_id (lexer, "COVARIANCE"))
885 factor.method = METHOD_COV;
887 else if (lex_match_id (lexer, "CORRELATION"))
889 factor.method = METHOD_CORR;
893 lex_error (lexer, NULL);
898 else if (lex_match_id (lexer, "ROTATION"))
900 lex_match (lexer, T_EQUALS);
901 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
903 /* VARIMAX and DEFAULT are defaults */
904 if (lex_match_id (lexer, "VARIMAX") || lex_match_id (lexer, "DEFAULT"))
906 factor.rotation = ROT_VARIMAX;
908 else if (lex_match_id (lexer, "EQUAMAX"))
910 factor.rotation = ROT_EQUAMAX;
912 else if (lex_match_id (lexer, "QUARTIMAX"))
914 factor.rotation = ROT_QUARTIMAX;
916 else if (lex_match_id (lexer, "NOROTATE"))
918 factor.rotation = ROT_NONE;
922 lex_error (lexer, NULL);
927 else if (lex_match_id (lexer, "CRITERIA"))
929 lex_match (lexer, T_EQUALS);
930 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
932 if (lex_match_id (lexer, "FACTORS"))
934 if ( lex_force_match (lexer, T_LPAREN))
936 lex_force_int (lexer);
937 factor.n_factors = lex_integer (lexer);
939 lex_force_match (lexer, T_RPAREN);
942 else if (lex_match_id (lexer, "MINEIGEN"))
944 if ( lex_force_match (lexer, T_LPAREN))
946 lex_force_num (lexer);
947 factor.min_eigen = lex_number (lexer);
949 lex_force_match (lexer, T_RPAREN);
952 else if (lex_match_id (lexer, "ECONVERGE"))
954 if ( lex_force_match (lexer, T_LPAREN))
956 lex_force_num (lexer);
957 factor.econverge = lex_number (lexer);
959 lex_force_match (lexer, T_RPAREN);
962 else if (lex_match_id (lexer, "RCONVERGE"))
964 if ( lex_force_match (lexer, T_LPAREN))
966 lex_force_num (lexer);
967 factor.rconverge = lex_number (lexer);
969 lex_force_match (lexer, T_RPAREN);
972 else if (lex_match_id (lexer, "ITERATE"))
974 if ( lex_force_match (lexer, T_LPAREN))
976 lex_force_int (lexer);
977 factor.iterations = lex_integer (lexer);
979 lex_force_match (lexer, T_RPAREN);
982 else if (lex_match_id (lexer, "DEFAULT"))
984 factor.n_factors = 0;
985 factor.min_eigen = 1;
986 factor.iterations = 25;
990 lex_error (lexer, NULL);
995 else if (lex_match_id (lexer, "EXTRACTION"))
997 extraction_seen = true;
998 lex_match (lexer, T_EQUALS);
999 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1001 if (lex_match_id (lexer, "PAF"))
1003 factor.extraction = EXTRACTION_PAF;
1005 else if (lex_match_id (lexer, "PC"))
1007 factor.extraction = EXTRACTION_PC;
1009 else if (lex_match_id (lexer, "PA1"))
1011 factor.extraction = EXTRACTION_PC;
1013 else if (lex_match_id (lexer, "DEFAULT"))
1015 factor.extraction = EXTRACTION_PC;
1019 lex_error (lexer, NULL);
1024 else if (lex_match_id (lexer, "FORMAT"))
1026 lex_match (lexer, T_EQUALS);
1027 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1029 if (lex_match_id (lexer, "SORT"))
1033 else if (lex_match_id (lexer, "BLANK"))
1035 if ( lex_force_match (lexer, T_LPAREN))
1037 lex_force_num (lexer);
1038 factor.blank = lex_number (lexer);
1040 lex_force_match (lexer, T_RPAREN);
1043 else if (lex_match_id (lexer, "DEFAULT"))
1046 factor.sort = false;
1050 lex_error (lexer, NULL);
1055 else if (lex_match_id (lexer, "PRINT"))
1058 lex_match (lexer, T_EQUALS);
1059 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1061 if (lex_match_id (lexer, "UNIVARIATE"))
1063 factor.print |= PRINT_UNIVARIATE;
1065 else if (lex_match_id (lexer, "DET"))
1067 factor.print |= PRINT_DETERMINANT;
1069 #if FACTOR_FULLY_IMPLEMENTED
1070 else if (lex_match_id (lexer, "INV"))
1073 else if (lex_match_id (lexer, "AIC"))
1077 else if (lex_match_id (lexer, "SIG"))
1079 factor.print |= PRINT_SIG;
1081 else if (lex_match_id (lexer, "CORRELATION"))
1083 factor.print |= PRINT_CORRELATION;
1085 #if FACTOR_FULLY_IMPLEMENTED
1086 else if (lex_match_id (lexer, "COVARIANCE"))
1090 else if (lex_match_id (lexer, "ROTATION"))
1092 factor.print |= PRINT_ROTATION;
1094 else if (lex_match_id (lexer, "EXTRACTION"))
1096 factor.print |= PRINT_EXTRACTION;
1098 else if (lex_match_id (lexer, "INITIAL"))
1100 factor.print |= PRINT_INITIAL;
1102 else if (lex_match_id (lexer, "KMO"))
1104 factor.print |= PRINT_KMO;
1106 #if FACTOR_FULLY_IMPLEMENTED
1107 else if (lex_match_id (lexer, "REPR"))
1110 else if (lex_match_id (lexer, "FSCORE"))
1114 else if (lex_match (lexer, T_ALL))
1116 factor.print = 0xFFFF;
1118 else if (lex_match_id (lexer, "DEFAULT"))
1120 factor.print |= PRINT_INITIAL ;
1121 factor.print |= PRINT_EXTRACTION ;
1122 factor.print |= PRINT_ROTATION ;
1126 lex_error (lexer, NULL);
1131 else if (lex_match_id (lexer, "MISSING"))
1133 lex_match (lexer, T_EQUALS);
1134 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1136 if (lex_match_id (lexer, "INCLUDE"))
1138 factor.exclude = MV_SYSTEM;
1140 else if (lex_match_id (lexer, "EXCLUDE"))
1142 factor.exclude = MV_ANY;
1144 else if (lex_match_id (lexer, "LISTWISE"))
1146 factor.missing_type = MISS_LISTWISE;
1148 else if (lex_match_id (lexer, "PAIRWISE"))
1150 factor.missing_type = MISS_PAIRWISE;
1152 else if (lex_match_id (lexer, "MEANSUB"))
1154 factor.missing_type = MISS_MEANSUB;
1158 lex_error (lexer, NULL);
1165 lex_error (lexer, NULL);
1170 if ( factor.rotation == ROT_NONE )
1171 factor.print &= ~PRINT_ROTATION;
1173 if ( ! run_factor (ds, &factor))
1184 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
1188 run_factor (struct dataset *ds, const struct cmd_factor *factor)
1190 struct dictionary *dict = dataset_dict (ds);
1192 struct casereader *group;
1194 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
1196 while (casegrouper_get_next_group (grouper, &group))
1198 if ( factor->missing_type == MISS_LISTWISE )
1199 group = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
1202 do_factor (factor, group);
1205 ok = casegrouper_destroy (grouper);
1206 ok = proc_commit (ds) && ok;
1212 /* Return the communality of variable N, calculated to N_FACTORS */
1214 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
1221 assert (n < eval->size);
1222 assert (n < evec->size1);
1223 assert (n_factors <= eval->size);
1225 for (i = 0 ; i < n_factors; ++i)
1227 double evali = fabs (gsl_vector_get (eval, i));
1229 double eveci = gsl_matrix_get (evec, n, i);
1231 comm += pow2 (eveci) * evali;
1237 /* Return the communality of variable N, calculated to N_FACTORS */
1239 communality (struct idata *idata, int n, int n_factors)
1241 return the_communality (idata->evec, idata->eval, n, n_factors);
1246 show_scree (const struct cmd_factor *f, struct idata *idata)
1251 if ( !(f->plot & PLOT_SCREE) )
1255 label = f->extraction == EXTRACTION_PC ? _("Component Number") : _("Factor Number");
1257 s = scree_create (idata->eval, label);
1263 show_communalities (const struct cmd_factor * factor,
1264 const gsl_vector *initial, const gsl_vector *extracted)
1268 const int heading_columns = 1;
1269 int nc = heading_columns;
1270 const int heading_rows = 1;
1271 const int nr = heading_rows + factor->n_vars;
1272 struct tab_table *t;
1274 if (factor->print & PRINT_EXTRACTION)
1277 if (factor->print & PRINT_INITIAL)
1280 /* No point having a table with only headings */
1284 t = tab_create (nc, nr);
1286 tab_title (t, _("Communalities"));
1288 tab_headers (t, heading_columns, 0, heading_rows, 0);
1291 if (factor->print & PRINT_INITIAL)
1292 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
1294 if (factor->print & PRINT_EXTRACTION)
1295 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
1297 /* Outline the box */
1304 /* Vertical lines */
1311 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1312 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1314 for (i = 0 ; i < factor->n_vars; ++i)
1317 tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
1319 if (factor->print & PRINT_INITIAL)
1320 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL);
1322 if (factor->print & PRINT_EXTRACTION)
1323 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL);
1331 show_factor_matrix (const struct cmd_factor *factor, struct idata *idata, const char *title, const gsl_matrix *fm)
1334 const int n_factors = idata->n_extractions;
1336 const int heading_columns = 1;
1337 const int heading_rows = 2;
1338 const int nr = heading_rows + factor->n_vars;
1339 const int nc = heading_columns + n_factors;
1340 gsl_permutation *perm;
1342 struct tab_table *t = tab_create (nc, nr);
1345 if ( factor->extraction == EXTRACTION_PC )
1346 tab_title (t, _("Component Matrix"));
1348 tab_title (t, _("Factor Matrix"));
1351 tab_title (t, "%s", title);
1353 tab_headers (t, heading_columns, 0, heading_rows, 0);
1355 if ( factor->extraction == EXTRACTION_PC )
1359 TAB_CENTER | TAT_TITLE, _("Component"));
1364 TAB_CENTER | TAT_TITLE, _("Factor"));
1367 tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1370 /* Outline the box */
1377 /* Vertical lines */
1384 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1385 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1388 /* Initialise to the identity permutation */
1389 perm = gsl_permutation_calloc (factor->n_vars);
1392 sort_matrix_indirect (fm, perm);
1394 for (i = 0 ; i < n_factors; ++i)
1396 tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1399 for (i = 0 ; i < factor->n_vars; ++i)
1402 const int matrix_row = perm->data[i];
1403 tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1405 for (j = 0 ; j < n_factors; ++j)
1407 double x = gsl_matrix_get (fm, matrix_row, j);
1409 if ( fabs (x) < factor->blank)
1412 tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL);
1416 gsl_permutation_free (perm);
1423 show_explained_variance (const struct cmd_factor * factor, struct idata *idata,
1424 const gsl_vector *initial_eigenvalues,
1425 const gsl_vector *extracted_eigenvalues,
1426 const gsl_vector *rotated_loadings)
1430 const int heading_columns = 1;
1431 const int heading_rows = 2;
1432 const int nr = heading_rows + factor->n_vars;
1434 struct tab_table *t ;
1436 double i_total = 0.0;
1439 double e_total = 0.0;
1444 int nc = heading_columns;
1446 if (factor->print & PRINT_EXTRACTION)
1449 if (factor->print & PRINT_INITIAL)
1452 if (factor->print & PRINT_ROTATION)
1455 /* No point having a table with only headings */
1456 if ( nc <= heading_columns)
1459 t = tab_create (nc, nr);
1461 tab_title (t, _("Total Variance Explained"));
1463 tab_headers (t, heading_columns, 0, heading_rows, 0);
1465 /* Outline the box */
1472 /* Vertical lines */
1479 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1480 tab_hline (t, TAL_1, 1, nc - 1, 1);
1482 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1485 if ( factor->extraction == EXTRACTION_PC)
1486 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1488 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1491 if (factor->print & PRINT_INITIAL)
1493 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1497 if (factor->print & PRINT_EXTRACTION)
1499 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1503 if (factor->print & PRINT_ROTATION)
1505 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1509 for (i = 0; i < (nc - heading_columns) / 3 ; ++i)
1511 tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1512 /* xgettext:no-c-format */
1513 tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1514 tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1516 tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1519 for (i = 0 ; i < initial_eigenvalues->size; ++i)
1520 i_total += gsl_vector_get (initial_eigenvalues, i);
1522 if ( factor->extraction == EXTRACTION_PAF)
1524 e_total = factor->n_vars;
1531 for (i = 0 ; i < factor->n_vars; ++i)
1533 const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1534 double i_percent = 100.0 * i_lambda / i_total ;
1536 const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1537 double e_percent = 100.0 * e_lambda / e_total ;
1539 const double r_lambda = gsl_vector_get (rotated_loadings, i);
1540 double r_percent = 100.0 * r_lambda / e_total ;
1544 tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%zu"), i + 1);
1550 /* Initial Eigenvalues */
1551 if (factor->print & PRINT_INITIAL)
1553 tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL);
1554 tab_double (t, c++, i + heading_rows, 0, i_percent, NULL);
1555 tab_double (t, c++, i + heading_rows, 0, i_cum, NULL);
1559 if (factor->print & PRINT_EXTRACTION)
1561 if (i < idata->n_extractions)
1563 /* Sums of squared loadings */
1564 tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL);
1565 tab_double (t, c++, i + heading_rows, 0, e_percent, NULL);
1566 tab_double (t, c++, i + heading_rows, 0, e_cum, NULL);
1570 if (factor->print & PRINT_ROTATION)
1572 if (i < idata->n_extractions)
1574 tab_double (t, c++, i + heading_rows, 0, r_lambda, NULL);
1575 tab_double (t, c++, i + heading_rows, 0, r_percent, NULL);
1576 tab_double (t, c++, i + heading_rows, 0, r_cum, NULL);
1587 show_correlation_matrix (const struct cmd_factor *factor, const struct idata *idata)
1589 struct tab_table *t ;
1591 int y_pos_corr = -1;
1593 int suffix_rows = 0;
1595 const int heading_rows = 1;
1596 const int heading_columns = 2;
1598 int nc = heading_columns ;
1599 int nr = heading_rows ;
1600 int n_data_sets = 0;
1602 if (factor->print & PRINT_CORRELATION)
1604 y_pos_corr = n_data_sets;
1606 nc = heading_columns + factor->n_vars;
1609 if (factor->print & PRINT_SIG)
1611 y_pos_sig = n_data_sets;
1613 nc = heading_columns + factor->n_vars;
1616 nr += n_data_sets * factor->n_vars;
1618 if (factor->print & PRINT_DETERMINANT)
1621 /* If the table would contain only headings, don't bother rendering it */
1622 if (nr <= heading_rows && suffix_rows == 0)
1625 t = tab_create (nc, nr + suffix_rows);
1627 tab_title (t, _("Correlation Matrix"));
1629 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1631 if (nr > heading_rows)
1633 tab_headers (t, heading_columns, 0, heading_rows, 0);
1635 tab_vline (t, TAL_2, 2, 0, nr - 1);
1637 /* Outline the box */
1644 /* Vertical lines */
1652 for (i = 0; i < factor->n_vars; ++i)
1653 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
1656 for (i = 0 ; i < n_data_sets; ++i)
1658 int y = heading_rows + i * factor->n_vars;
1660 for (v = 0; v < factor->n_vars; ++v)
1661 tab_text (t, 1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
1663 tab_hline (t, TAL_1, 0, nc - 1, y);
1666 if (factor->print & PRINT_CORRELATION)
1668 const double y = heading_rows + y_pos_corr;
1669 tab_text (t, 0, y, TAT_TITLE, _("Correlations"));
1671 for (i = 0; i < factor->n_vars; ++i)
1673 for (j = 0; j < factor->n_vars; ++j)
1674 tab_double (t, heading_columns + i, y + j, 0, gsl_matrix_get (idata->corr, i, j), NULL);
1678 if (factor->print & PRINT_SIG)
1680 const double y = heading_rows + y_pos_sig * factor->n_vars;
1681 tab_text (t, 0, y, TAT_TITLE, _("Sig. (1-tailed)"));
1683 for (i = 0; i < factor->n_vars; ++i)
1685 for (j = 0; j < factor->n_vars; ++j)
1687 double rho = gsl_matrix_get (idata->corr, i, j);
1688 double w = gsl_matrix_get (idata->n, i, j);
1693 tab_double (t, heading_columns + i, y + j, 0, significance_of_correlation (rho, w), NULL);
1699 if (factor->print & PRINT_DETERMINANT)
1701 tab_text (t, 0, nr, TAB_LEFT | TAT_TITLE, _("Determinant"));
1703 tab_double (t, 1, nr, 0, idata->detR, NULL);
1712 do_factor (const struct cmd_factor *factor, struct casereader *r)
1715 const gsl_matrix *var_matrix;
1716 const gsl_matrix *mean_matrix;
1718 const gsl_matrix *analysis_matrix;
1719 struct idata *idata = idata_alloc (factor->n_vars);
1721 struct covariance *cov = covariance_1pass_create (factor->n_vars, factor->vars,
1722 factor->wv, factor->exclude);
1724 for ( ; (c = casereader_read (r) ); case_unref (c))
1726 covariance_accumulate (cov, c);
1729 idata->cov = covariance_calculate (cov);
1731 if (idata->cov == NULL)
1733 msg (MW, _("The dataset contains no complete observations. No analysis will be performed."));
1737 var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
1738 mean_matrix = covariance_moments (cov, MOMENT_MEAN);
1739 idata->n = covariance_moments (cov, MOMENT_NONE);
1741 if ( factor->method == METHOD_CORR)
1743 idata->corr = correlation_from_covariance (idata->cov, var_matrix);
1745 analysis_matrix = idata->corr;
1748 analysis_matrix = idata->cov;
1750 if (factor->print & PRINT_DETERMINANT
1751 || factor->print & PRINT_KMO)
1755 const int size = idata->corr->size1;
1756 gsl_permutation *p = gsl_permutation_calloc (size);
1757 gsl_matrix *tmp = gsl_matrix_calloc (size, size);
1758 gsl_matrix_memcpy (tmp, idata->corr);
1760 gsl_linalg_LU_decomp (tmp, p, &sign);
1761 idata->detR = gsl_linalg_LU_det (tmp, sign);
1762 gsl_permutation_free (p);
1763 gsl_matrix_free (tmp);
1766 if ( factor->print & PRINT_UNIVARIATE)
1768 const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
1772 const int heading_columns = 1;
1773 const int heading_rows = 1;
1775 const int nr = heading_rows + factor->n_vars;
1777 struct tab_table *t = tab_create (nc, nr);
1778 tab_title (t, _("Descriptive Statistics"));
1780 tab_headers (t, heading_columns, 0, heading_rows, 0);
1782 /* Outline the box */
1789 /* Vertical lines */
1796 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1797 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1799 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
1800 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1801 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
1803 for (i = 0 ; i < factor->n_vars; ++i)
1805 const struct variable *v = factor->vars[i];
1806 tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
1808 tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (mean_matrix, i, i), NULL);
1809 tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (var_matrix, i, i)), NULL);
1810 tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (idata->n, i, i), wfmt);
1816 if (factor->print & PRINT_KMO)
1819 double sum_ssq_r = 0;
1820 double sum_ssq_a = 0;
1822 double df = factor->n_vars * ( factor->n_vars - 1) / 2;
1829 const int heading_columns = 2;
1830 const int heading_rows = 0;
1832 const int nr = heading_rows + 4;
1833 const int nc = heading_columns + 1;
1837 struct tab_table *t = tab_create (nc, nr);
1838 tab_title (t, _("KMO and Bartlett's Test"));
1840 x = clone_matrix (idata->corr);
1841 gsl_linalg_cholesky_decomp (x);
1842 gsl_linalg_cholesky_invert (x);
1846 for (i = 0; i < x->size1; ++i)
1848 sum_ssq_r += ssq_od_n (x, i);
1849 sum_ssq_a += ssq_od_n (a, i);
1852 gsl_matrix_free (a);
1853 gsl_matrix_free (x);
1855 tab_headers (t, heading_columns, 0, heading_rows, 0);
1857 /* Outline the box */
1864 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1866 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Kaiser-Meyer-Olkin Measure of Sampling Adequacy"));
1868 tab_double (t, 2, 0, 0, sum_ssq_r / (sum_ssq_r + sum_ssq_a), NULL);
1870 tab_text (t, 0, 1, TAT_TITLE | TAB_LEFT, _("Bartlett's Test of Sphericity"));
1872 tab_text (t, 1, 1, TAT_TITLE, _("Approx. Chi-Square"));
1873 tab_text (t, 1, 2, TAT_TITLE, _("df"));
1874 tab_text (t, 1, 3, TAT_TITLE, _("Sig."));
1877 /* The literature doesn't say what to do for the value of W when
1878 missing values are involved. The best thing I can think of
1879 is to take the mean average. */
1881 for (i = 0; i < idata->n->size1; ++i)
1882 w += gsl_matrix_get (idata->n, i, i);
1883 w /= idata->n->size1;
1885 xsq = w - 1 - (2 * factor->n_vars + 5) / 6.0;
1886 xsq *= -log (idata->detR);
1888 tab_double (t, 2, 1, 0, xsq, NULL);
1889 tab_double (t, 2, 2, 0, df, &F_8_0);
1890 tab_double (t, 2, 3, 0, gsl_cdf_chisq_Q (xsq, df), NULL);
1896 show_correlation_matrix (factor, idata);
1899 gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
1901 gsl_eigen_symmv (matrix_dup (analysis_matrix), idata->eval, idata->evec, workspace);
1903 gsl_eigen_symmv_free (workspace);
1906 gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
1908 idata->n_extractions = n_extracted_factors (factor, idata);
1910 if (idata->n_extractions == 0)
1912 msg (MW, _("The FACTOR criteria result in zero factors extracted. Therefore no analysis will be performed."));
1916 if (idata->n_extractions > factor->n_vars)
1918 msg (MW, _("The FACTOR criteria result in more factors than variables, which is not meaningful. No analysis will be performed."));
1923 gsl_matrix *rotated_factors = NULL;
1924 gsl_vector *rotated_loadings = NULL;
1926 const gsl_vector *extracted_eigenvalues = NULL;
1927 gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
1928 gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
1930 struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, idata->n_extractions);
1931 gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
1933 if ( factor->extraction == EXTRACTION_PAF)
1935 gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
1936 struct smr_workspace *ws = ws_create (analysis_matrix);
1938 for (i = 0 ; i < factor->n_vars ; ++i)
1940 double r2 = squared_multiple_correlation (analysis_matrix, i, ws);
1942 gsl_vector_set (idata->msr, i, r2);
1946 gsl_vector_memcpy (initial_communalities, idata->msr);
1948 for (i = 0; i < factor->iterations; ++i)
1951 gsl_vector_memcpy (diff, idata->msr);
1953 iterate_factor_matrix (analysis_matrix, idata->msr, factor_matrix, fmw);
1955 gsl_vector_sub (diff, idata->msr);
1957 gsl_vector_minmax (diff, &min, &max);
1959 if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
1962 gsl_vector_free (diff);
1966 gsl_vector_memcpy (extracted_communalities, idata->msr);
1967 extracted_eigenvalues = fmw->eval;
1969 else if (factor->extraction == EXTRACTION_PC)
1971 for (i = 0; i < factor->n_vars; ++i)
1972 gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
1974 gsl_vector_memcpy (extracted_communalities, initial_communalities);
1976 iterate_factor_matrix (analysis_matrix, extracted_communalities, factor_matrix, fmw);
1979 extracted_eigenvalues = idata->eval;
1983 show_communalities (factor, initial_communalities, extracted_communalities);
1986 if ( factor->rotation != ROT_NONE)
1988 rotated_factors = gsl_matrix_calloc (factor_matrix->size1, factor_matrix->size2);
1989 rotated_loadings = gsl_vector_calloc (factor_matrix->size2);
1991 rotate (factor, factor_matrix, extracted_communalities, rotated_factors, rotated_loadings);
1994 show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues, rotated_loadings);
1996 factor_matrix_workspace_free (fmw);
1998 show_scree (factor, idata);
2000 show_factor_matrix (factor, idata,
2001 factor->extraction == EXTRACTION_PC ? _("Component Matrix") : _("Factor Matrix"),
2004 if ( factor->rotation != ROT_NONE)
2006 show_factor_matrix (factor, idata,
2007 factor->extraction == EXTRACTION_PC ? _("Rotated Component Matrix") : _("Rotated Factor Matrix"),
2010 gsl_matrix_free (rotated_factors);
2015 gsl_vector_free (initial_communalities);
2016 gsl_vector_free (extracted_communalities);
2023 casereader_destroy (r);