1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011, 2012, 2014, 2015,
3 2016, 2017 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <gsl/gsl_vector.h>
21 #include <gsl/gsl_linalg.h>
22 #include <gsl/gsl_matrix.h>
23 #include <gsl/gsl_eigen.h>
24 #include <gsl/gsl_blas.h>
25 #include <gsl/gsl_sort_vector.h>
26 #include <gsl/gsl_cdf.h>
28 #include "data/any-reader.h"
29 #include "data/casegrouper.h"
30 #include "data/casereader.h"
31 #include "data/casewriter.h"
32 #include "data/dataset.h"
33 #include "data/dictionary.h"
34 #include "data/format.h"
35 #include "data/subcase.h"
36 #include "language/command.h"
37 #include "language/lexer/lexer.h"
38 #include "language/lexer/value-parser.h"
39 #include "language/lexer/variable-parser.h"
40 #include "language/data-io/file-handle.h"
41 #include "language/data-io/matrix-reader.h"
42 #include "libpspp/cast.h"
43 #include "libpspp/message.h"
44 #include "libpspp/misc.h"
45 #include "math/correlation.h"
46 #include "math/covariance.h"
47 #include "math/moments.h"
48 #include "output/chart-item.h"
49 #include "output/charts/scree.h"
50 #include "output/tab.h"
54 #define _(msgid) gettext (msgid)
55 #define N_(msgid) msgid
70 enum extraction_method
79 PLOT_ROTATION = 0x0002
84 PRINT_UNIVARIATE = 0x0001,
85 PRINT_DETERMINANT = 0x0002,
89 PRINT_COVARIANCE = 0x0020,
90 PRINT_CORRELATION = 0x0040,
91 PRINT_ROTATION = 0x0080,
92 PRINT_EXTRACTION = 0x0100,
93 PRINT_INITIAL = 0x0200,
108 typedef void (*rotation_coefficients) (double *x, double *y,
109 double a, double b, double c, double d,
110 const gsl_matrix *loadings );
114 varimax_coefficients (double *x, double *y,
115 double a, double b, double c, double d,
116 const gsl_matrix *loadings )
118 *x = d - 2 * a * b / loadings->size1;
119 *y = c - (a * a - b * b) / loadings->size1;
123 equamax_coefficients (double *x, double *y,
124 double a, double b, double c, double d,
125 const gsl_matrix *loadings )
127 *x = d - loadings->size2 * a * b / loadings->size1;
128 *y = c - loadings->size2 * (a * a - b * b) / (2 * loadings->size1);
132 quartimax_coefficients (double *x, double *y,
133 double a UNUSED, double b UNUSED, double c, double d,
134 const gsl_matrix *loadings UNUSED)
140 static const rotation_coefficients rotation_coeff[] = {
141 varimax_coefficients,
142 equamax_coefficients,
143 quartimax_coefficients,
144 varimax_coefficients /* PROMAX is identical to VARIMAX */
148 /* return diag (C'C) ^ {-0.5} */
150 diag_rcp_sqrt (const gsl_matrix *C)
153 gsl_matrix *d = gsl_matrix_calloc (C->size1, C->size2);
154 gsl_matrix *r = gsl_matrix_calloc (C->size1, C->size2);
156 assert (C->size1 == C->size2);
158 gsl_linalg_matmult_mod (C, GSL_LINALG_MOD_TRANSPOSE,
159 C, GSL_LINALG_MOD_NONE,
162 for (j = 0 ; j < d->size2; ++j)
164 double e = gsl_matrix_get (d, j, j);
166 gsl_matrix_set (r, j, j, e);
176 /* return diag ((C'C)^-1) ^ {-0.5} */
178 diag_rcp_inv_sqrt (const gsl_matrix *CCinv)
181 gsl_matrix *r = gsl_matrix_calloc (CCinv->size1, CCinv->size2);
183 assert (CCinv->size1 == CCinv->size2);
185 for (j = 0 ; j < CCinv->size2; ++j)
187 double e = gsl_matrix_get (CCinv, j, j);
189 gsl_matrix_set (r, j, j, e);
202 const struct variable **vars;
204 const struct variable *wv;
207 enum missing_type missing_type;
208 enum mv_class exclude;
209 enum print_opts print;
210 enum extraction_method extraction;
212 enum rotation_type rotation;
213 int rotation_iterations;
216 /* Extraction Criteria */
220 int extraction_iterations;
232 /* Intermediate values used in calculation */
233 struct matrix_material mm;
235 gsl_matrix *analysis_matrix; /* A pointer to either mm.corr or mm.cov */
237 gsl_vector *eval ; /* The eigenvalues */
238 gsl_matrix *evec ; /* The eigenvectors */
242 gsl_vector *msr ; /* Multiple Squared Regressions */
244 double detR; /* The determinant of the correlation matrix */
246 gsl_matrix *ai_cov; /* The anti-image covariance matrix */
247 gsl_matrix *ai_cor; /* The anti-image correlation matrix */
248 struct covariance *cvm;
251 static struct idata *
252 idata_alloc (size_t n_vars)
254 struct idata *id = xzalloc (sizeof (*id));
256 id->n_extractions = 0;
257 id->msr = gsl_vector_alloc (n_vars);
259 id->eval = gsl_vector_alloc (n_vars);
260 id->evec = gsl_matrix_alloc (n_vars, n_vars);
266 idata_free (struct idata *id)
268 gsl_vector_free (id->msr);
269 gsl_vector_free (id->eval);
270 gsl_matrix_free (id->evec);
271 gsl_matrix_free (id->ai_cov);
272 gsl_matrix_free (id->ai_cor);
277 /* Return the sum of squares of all the elements in row J excluding column J */
279 ssq_row_od_n (const gsl_matrix *m, int j)
283 assert (m->size1 == m->size2);
285 assert (j < m->size1);
287 for (i = 0; i < m->size1; ++i)
289 if (i == j ) continue;
290 ss += pow2 (gsl_matrix_get (m, i, j));
296 /* Return the sum of squares of all the elements excluding row N */
298 ssq_od_n (const gsl_matrix *m, int n)
302 assert (m->size1 == m->size2);
304 assert (n < m->size1);
306 for (i = 0; i < m->size1; ++i)
308 for (j = 0; j < m->size2; ++j)
310 if (i == j) continue;
311 ss += pow2 (gsl_matrix_get (m, i, j));
320 anti_image_corr (const gsl_matrix *m, const struct idata *idata)
324 assert (m->size1 == m->size2);
326 a = gsl_matrix_alloc (m->size1, m->size2);
328 for (i = 0; i < m->size1; ++i)
330 for (j = 0; j < m->size2; ++j)
332 double *p = gsl_matrix_ptr (a, i, j);
333 *p = gsl_matrix_get (m, i, j);
334 *p /= sqrt (gsl_matrix_get (m, i, i) *
335 gsl_matrix_get (m, j, j));
339 for (i = 0; i < m->size1; ++i)
341 double r = ssq_row_od_n (idata->mm.corr, i);
342 double u = ssq_row_od_n (a, i);
343 gsl_matrix_set (a, i, i, r / (r + u));
350 anti_image_cov (const gsl_matrix *m)
354 assert (m->size1 == m->size2);
356 a = gsl_matrix_alloc (m->size1, m->size2);
358 for (i = 0; i < m->size1; ++i)
360 for (j = 0; j < m->size2; ++j)
362 double *p = gsl_matrix_ptr (a, i, j);
363 *p = gsl_matrix_get (m, i, j);
364 *p /= gsl_matrix_get (m, i, i);
365 *p /= gsl_matrix_get (m, j, j);
374 dump_matrix (const gsl_matrix *m)
378 for (i = 0 ; i < m->size1; ++i)
380 for (j = 0 ; j < m->size2; ++j)
381 printf ("%02f ", gsl_matrix_get (m, i, j));
387 dump_matrix_permute (const gsl_matrix *m, const gsl_permutation *p)
391 for (i = 0 ; i < m->size1; ++i)
393 for (j = 0 ; j < m->size2; ++j)
394 printf ("%02f ", gsl_matrix_get (m, gsl_permutation_get (p, i), j));
401 dump_vector (const gsl_vector *v)
404 for (i = 0 ; i < v->size; ++i)
406 printf ("%02f\n", gsl_vector_get (v, i));
414 n_extracted_factors (const struct cmd_factor *factor, struct idata *idata)
418 /* If there is a cached value, then return that. */
419 if ( idata->n_extractions != 0)
420 return idata->n_extractions;
422 /* Otherwise, if the number of factors has been explicitly requested,
424 if (factor->n_factors > 0)
426 idata->n_extractions = factor->n_factors;
430 /* Use the MIN_EIGEN setting. */
431 for (i = 0 ; i < idata->eval->size; ++i)
433 double evali = fabs (gsl_vector_get (idata->eval, i));
435 idata->n_extractions = i;
437 if (evali < factor->min_eigen)
442 return idata->n_extractions;
446 /* Returns a newly allocated matrix identical to M.
447 It it the callers responsibility to free the returned value.
450 matrix_dup (const gsl_matrix *m)
452 gsl_matrix *n = gsl_matrix_alloc (m->size1, m->size2);
454 gsl_matrix_memcpy (n, m);
462 /* Copy of the subject */
467 gsl_permutation *perm;
474 static struct smr_workspace *ws_create (const gsl_matrix *input)
476 struct smr_workspace *ws = xmalloc (sizeof (*ws));
478 ws->m = gsl_matrix_alloc (input->size1, input->size2);
479 ws->inverse = gsl_matrix_calloc (input->size1 - 1, input->size2 - 1);
480 ws->perm = gsl_permutation_alloc (input->size1 - 1);
481 ws->result1 = gsl_matrix_calloc (input->size1 - 1, 1);
482 ws->result2 = gsl_matrix_calloc (1, 1);
488 ws_destroy (struct smr_workspace *ws)
490 gsl_matrix_free (ws->result2);
491 gsl_matrix_free (ws->result1);
492 gsl_permutation_free (ws->perm);
493 gsl_matrix_free (ws->inverse);
494 gsl_matrix_free (ws->m);
501 Return the square of the regression coefficient for VAR regressed against all other variables.
504 squared_multiple_correlation (const gsl_matrix *corr, int var, struct smr_workspace *ws)
506 /* For an explanation of what this is doing, see
507 http://www.visualstatistics.net/Visual%20Statistics%20Multimedia/multiple_regression_analysis.htm
513 gsl_matrix_memcpy (ws->m, corr);
515 gsl_matrix_swap_rows (ws->m, 0, var);
516 gsl_matrix_swap_columns (ws->m, 0, var);
518 rxx = gsl_matrix_submatrix (ws->m, 1, 1, ws->m->size1 - 1, ws->m->size1 - 1);
520 gsl_linalg_LU_decomp (&rxx.matrix, ws->perm, &signum);
522 gsl_linalg_LU_invert (&rxx.matrix, ws->perm, ws->inverse);
525 gsl_matrix_const_view rxy = gsl_matrix_const_submatrix (ws->m, 1, 0, ws->m->size1 - 1, 1);
526 gsl_matrix_const_view ryx = gsl_matrix_const_submatrix (ws->m, 0, 1, 1, ws->m->size1 - 1);
528 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
529 1.0, ws->inverse, &rxy.matrix, 0.0, ws->result1);
531 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
532 1.0, &ryx.matrix, ws->result1, 0.0, ws->result2);
535 return gsl_matrix_get (ws->result2, 0, 0);
540 static double the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors);
543 struct factor_matrix_workspace
546 gsl_eigen_symmv_workspace *eigen_ws;
556 static struct factor_matrix_workspace *
557 factor_matrix_workspace_alloc (size_t n, size_t nf)
559 struct factor_matrix_workspace *ws = xmalloc (sizeof (*ws));
562 ws->gamma = gsl_matrix_calloc (nf, nf);
563 ws->eigen_ws = gsl_eigen_symmv_alloc (n);
564 ws->eval = gsl_vector_alloc (n);
565 ws->evec = gsl_matrix_alloc (n, n);
566 ws->r = gsl_matrix_alloc (n, n);
572 factor_matrix_workspace_free (struct factor_matrix_workspace *ws)
574 gsl_eigen_symmv_free (ws->eigen_ws);
575 gsl_vector_free (ws->eval);
576 gsl_matrix_free (ws->evec);
577 gsl_matrix_free (ws->gamma);
578 gsl_matrix_free (ws->r);
583 Shift P left by OFFSET places, and overwrite TARGET
584 with the shifted result.
585 Positions in TARGET less than OFFSET are unchanged.
588 perm_shift_apply (gsl_permutation *target, const gsl_permutation *p,
592 assert (target->size == p->size);
593 assert (offset <= target->size);
595 for (i = 0; i < target->size - offset; ++i)
597 target->data[i] = p->data [i + offset];
603 Indirectly sort the rows of matrix INPUT, storing the sort order in PERM.
604 The sort criteria are as follows:
606 Rows are sorted on the first column, until the absolute value of an
607 element in a subsequent column is greater than that of the first
608 column. Thereafter, rows will be sorted on the second column,
609 until the absolute value of an element in a subsequent column
610 exceeds that of the second column ...
613 sort_matrix_indirect (const gsl_matrix *input, gsl_permutation *perm)
615 const size_t n = perm->size;
616 const size_t m = input->size2;
623 assert (perm->size == input->size1);
625 p = gsl_permutation_alloc (n);
627 /* Copy INPUT into MAT, discarding the sign */
628 mat = gsl_matrix_alloc (n, m);
629 for (i = 0 ; i < mat->size1; ++i)
631 for (j = 0 ; j < mat->size2; ++j)
633 double x = gsl_matrix_get (input, i, j);
634 gsl_matrix_set (mat, i, j, fabs (x));
638 while (column_n < m && row_n < n)
640 gsl_vector_const_view columni = gsl_matrix_const_column (mat, column_n);
641 gsl_sort_vector_index (p, &columni.vector);
643 for (i = 0 ; i < n; ++i)
645 gsl_vector_view row = gsl_matrix_row (mat, p->data[n - 1 - i]);
646 size_t maxindex = gsl_vector_max_index (&row.vector);
648 if ( maxindex > column_n )
651 /* All subsequent elements of this row, are of no interest.
652 So set them all to a highly negative value */
653 for (j = column_n + 1; j < row.vector.size ; ++j)
654 gsl_vector_set (&row.vector, j, -DBL_MAX);
657 perm_shift_apply (perm, p, row_n);
663 gsl_permutation_free (p);
664 gsl_matrix_free (mat);
666 assert ( 0 == gsl_permutation_valid (perm));
668 /* We want the biggest value to be first */
669 gsl_permutation_reverse (perm);
674 drot_go (double phi, double *l0, double *l1)
676 double r0 = cos (phi) * *l0 + sin (phi) * *l1;
677 double r1 = - sin (phi) * *l0 + cos (phi) * *l1;
685 clone_matrix (const gsl_matrix *m)
688 gsl_matrix *c = gsl_matrix_calloc (m->size1, m->size2);
690 for (j = 0 ; j < c->size1; ++j)
692 for (k = 0 ; k < c->size2; ++k)
694 const double *v = gsl_matrix_const_ptr (m, j, k);
695 gsl_matrix_set (c, j, k, *v);
704 initial_sv (const gsl_matrix *fm)
709 for (j = 0 ; j < fm->size2; ++j)
714 for (k = j + 1 ; k < fm->size2; ++k)
716 double lambda = gsl_matrix_get (fm, k, j);
717 double lambda_sq = lambda * lambda;
718 double lambda_4 = lambda_sq * lambda_sq;
723 sv += ( fm->size1 * l4s - (l2s * l2s) ) / (fm->size1 * fm->size1 );
729 rotate (const struct cmd_factor *cf, const gsl_matrix *unrot,
730 const gsl_vector *communalities,
732 gsl_vector *rotated_loadings,
733 gsl_matrix *pattern_matrix,
734 gsl_matrix *factor_correlation_matrix
741 /* First get a normalised version of UNROT */
742 gsl_matrix *normalised = gsl_matrix_calloc (unrot->size1, unrot->size2);
743 gsl_matrix *h_sqrt = gsl_matrix_calloc (communalities->size, communalities->size);
744 gsl_matrix *h_sqrt_inv ;
746 /* H is the diagonal matrix containing the absolute values of the communalities */
747 for (i = 0 ; i < communalities->size ; ++i)
749 double *ptr = gsl_matrix_ptr (h_sqrt, i, i);
750 *ptr = fabs (gsl_vector_get (communalities, i));
753 /* Take the square root of the communalities */
754 gsl_linalg_cholesky_decomp (h_sqrt);
757 /* Save a copy of h_sqrt and invert it */
758 h_sqrt_inv = clone_matrix (h_sqrt);
759 gsl_linalg_cholesky_decomp (h_sqrt_inv);
760 gsl_linalg_cholesky_invert (h_sqrt_inv);
762 /* normalised vertion is H^{1/2} x UNROT */
763 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, h_sqrt_inv, unrot, 0.0, normalised);
765 gsl_matrix_free (h_sqrt_inv);
768 /* Now perform the rotation iterations */
770 prev_sv = initial_sv (normalised);
771 for (i = 0 ; i < cf->rotation_iterations ; ++i)
774 for (j = 0 ; j < normalised->size2; ++j)
776 /* These variables relate to the convergence criterium */
780 for (k = j + 1 ; k < normalised->size2; ++k)
790 for (p = 0; p < normalised->size1; ++p)
792 double jv = gsl_matrix_get (normalised, p, j);
793 double kv = gsl_matrix_get (normalised, p, k);
795 double u = jv * jv - kv * kv;
796 double v = 2 * jv * kv;
803 rotation_coeff [cf->rotation] (&x, &y, a, b, c, d, normalised);
805 phi = atan2 (x, y) / 4.0 ;
807 /* Don't bother rotating if the angle is small */
808 if ( fabs (sin (phi) ) <= pow (10.0, -15.0))
811 for (p = 0; p < normalised->size1; ++p)
813 double *lambda0 = gsl_matrix_ptr (normalised, p, j);
814 double *lambda1 = gsl_matrix_ptr (normalised, p, k);
815 drot_go (phi, lambda0, lambda1);
818 /* Calculate the convergence criterium */
820 double lambda = gsl_matrix_get (normalised, k, j);
821 double lambda_sq = lambda * lambda;
822 double lambda_4 = lambda_sq * lambda_sq;
828 sv += ( normalised->size1 * l4s - (l2s * l2s) ) / (normalised->size1 * normalised->size1 );
831 if ( fabs (sv - prev_sv) <= cf->rconverge)
837 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0,
838 h_sqrt, normalised, 0.0, result);
840 gsl_matrix_free (h_sqrt);
841 gsl_matrix_free (normalised);
843 if (cf->rotation == ROT_PROMAX)
845 /* general purpose m by m matrix, where m is the number of factors */
846 gsl_matrix *mm1 = gsl_matrix_calloc (unrot->size2, unrot->size2);
847 gsl_matrix *mm2 = gsl_matrix_calloc (unrot->size2, unrot->size2);
849 /* general purpose m by p matrix, where p is the number of variables */
850 gsl_matrix *mp1 = gsl_matrix_calloc (unrot->size2, unrot->size1);
852 gsl_matrix *pm1 = gsl_matrix_calloc (unrot->size1, unrot->size2);
854 gsl_permutation *perm = gsl_permutation_alloc (unrot->size2);
860 /* The following variables follow the notation by SPSS Statistical Algorithms
862 gsl_matrix *L = gsl_matrix_calloc (unrot->size2, unrot->size2);
863 gsl_matrix *P = clone_matrix (result);
868 /* Vector of length p containing (indexed by i)
869 \Sum^m_j {\lambda^2_{ij}} */
870 gsl_vector *rssq = gsl_vector_calloc (unrot->size1);
872 for (i = 0; i < P->size1; ++i)
875 for (j = 0; j < P->size2; ++j)
877 sum += gsl_matrix_get (result, i, j)
878 * gsl_matrix_get (result, i, j);
882 gsl_vector_set (rssq, i, sqrt (sum));
885 for (i = 0; i < P->size1; ++i)
887 for (j = 0; j < P->size2; ++j)
889 double l = gsl_matrix_get (result, i, j);
890 double r = gsl_vector_get (rssq, i);
891 gsl_matrix_set (P, i, j, pow (fabs (l / r), cf->promax_power + 1) * r / l);
895 gsl_vector_free (rssq);
897 gsl_linalg_matmult_mod (result,
898 GSL_LINALG_MOD_TRANSPOSE,
903 gsl_linalg_LU_decomp (mm1, perm, &signum);
904 gsl_linalg_LU_invert (mm1, perm, mm2);
906 gsl_linalg_matmult_mod (mm2, GSL_LINALG_MOD_NONE,
907 result, GSL_LINALG_MOD_TRANSPOSE,
910 gsl_linalg_matmult_mod (mp1, GSL_LINALG_MOD_NONE,
911 P, GSL_LINALG_MOD_NONE,
914 D = diag_rcp_sqrt (L);
915 Q = gsl_matrix_calloc (unrot->size2, unrot->size2);
917 gsl_linalg_matmult_mod (L, GSL_LINALG_MOD_NONE,
918 D, GSL_LINALG_MOD_NONE,
921 gsl_matrix *QQinv = gsl_matrix_calloc (unrot->size2, unrot->size2);
923 gsl_linalg_matmult_mod (Q, GSL_LINALG_MOD_TRANSPOSE,
924 Q, GSL_LINALG_MOD_NONE,
927 gsl_linalg_cholesky_decomp (QQinv);
928 gsl_linalg_cholesky_invert (QQinv);
931 gsl_matrix *C = diag_rcp_inv_sqrt (QQinv);
932 gsl_matrix *Cinv = clone_matrix (C);
934 gsl_linalg_cholesky_decomp (Cinv);
935 gsl_linalg_cholesky_invert (Cinv);
938 gsl_linalg_matmult_mod (result, GSL_LINALG_MOD_NONE,
939 Q, GSL_LINALG_MOD_NONE,
942 gsl_linalg_matmult_mod (pm1, GSL_LINALG_MOD_NONE,
943 Cinv, GSL_LINALG_MOD_NONE,
947 gsl_linalg_matmult_mod (C, GSL_LINALG_MOD_NONE,
948 QQinv, GSL_LINALG_MOD_NONE,
951 gsl_linalg_matmult_mod (mm1, GSL_LINALG_MOD_NONE,
952 C, GSL_LINALG_MOD_TRANSPOSE,
953 factor_correlation_matrix);
955 gsl_linalg_matmult_mod (pattern_matrix, GSL_LINALG_MOD_NONE,
956 factor_correlation_matrix, GSL_LINALG_MOD_NONE,
959 gsl_matrix_memcpy (result, pm1);
962 gsl_matrix_free (QQinv);
964 gsl_matrix_free (Cinv);
971 gsl_permutation_free (perm);
973 gsl_matrix_free (mm1);
974 gsl_matrix_free (mm2);
975 gsl_matrix_free (mp1);
976 gsl_matrix_free (pm1);
980 /* reflect negative sums and populate the rotated loadings vector*/
981 for (i = 0 ; i < result->size2; ++i)
985 for (j = 0 ; j < result->size1; ++j)
987 double s = gsl_matrix_get (result, j, i);
992 gsl_vector_set (rotated_loadings, i, ssq);
995 for (j = 0 ; j < result->size1; ++j)
997 double *lambda = gsl_matrix_ptr (result, j, i);
1005 Get an approximation for the factor matrix into FACTORS, and the communalities into COMMUNALITIES.
1006 R is the matrix to be analysed.
1007 WS is a pointer to a structure which must have been initialised with factor_matrix_workspace_init.
1010 iterate_factor_matrix (const gsl_matrix *r, gsl_vector *communalities, gsl_matrix *factors,
1011 struct factor_matrix_workspace *ws)
1014 gsl_matrix_view mv ;
1016 assert (r->size1 == r->size2);
1017 assert (r->size1 == communalities->size);
1019 assert (factors->size1 == r->size1);
1020 assert (factors->size2 == ws->n_factors);
1022 gsl_matrix_memcpy (ws->r, r);
1024 /* Apply Communalities to diagonal of correlation matrix */
1025 for (i = 0 ; i < communalities->size ; ++i)
1027 double *x = gsl_matrix_ptr (ws->r, i, i);
1028 *x = gsl_vector_get (communalities, i);
1031 gsl_eigen_symmv (ws->r, ws->eval, ws->evec, ws->eigen_ws);
1033 mv = gsl_matrix_submatrix (ws->evec, 0, 0, ws->evec->size1, ws->n_factors);
1035 /* Gamma is the diagonal matrix containing the absolute values of the eigenvalues */
1036 for (i = 0 ; i < ws->n_factors ; ++i)
1038 double *ptr = gsl_matrix_ptr (ws->gamma, i, i);
1039 *ptr = fabs (gsl_vector_get (ws->eval, i));
1042 /* Take the square root of gamma */
1043 gsl_linalg_cholesky_decomp (ws->gamma);
1045 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, &mv.matrix, ws->gamma, 0.0, factors);
1047 for (i = 0 ; i < r->size1 ; ++i)
1049 double h = the_communality (ws->evec, ws->eval, i, ws->n_factors);
1050 gsl_vector_set (communalities, i, h);
1056 static bool run_factor (struct dataset *ds, const struct cmd_factor *factor);
1058 static void do_factor_by_matrix (const struct cmd_factor *factor, struct idata *idata);
1063 cmd_factor (struct lexer *lexer, struct dataset *ds)
1065 struct dictionary *dict = NULL;
1066 int n_iterations = 25;
1067 struct cmd_factor factor;
1070 factor.method = METHOD_CORR;
1071 factor.missing_type = MISS_LISTWISE;
1072 factor.exclude = MV_ANY;
1073 factor.print = PRINT_INITIAL | PRINT_EXTRACTION | PRINT_ROTATION;
1074 factor.extraction = EXTRACTION_PC;
1075 factor.n_factors = 0;
1076 factor.min_eigen = SYSMIS;
1077 factor.extraction_iterations = 25;
1078 factor.rotation_iterations = 25;
1079 factor.econverge = 0.001;
1082 factor.sort = false;
1084 factor.rotation = ROT_VARIMAX;
1087 factor.rconverge = 0.0001;
1089 lex_match (lexer, T_SLASH);
1091 struct matrix_reader *mr = NULL;
1092 struct casereader *matrix_reader = NULL;
1094 if (lex_match_id (lexer, "VARIABLES"))
1096 lex_match (lexer, T_EQUALS);
1097 dict = dataset_dict (ds);
1098 factor.wv = dict_get_weight (dict);
1100 if (!parse_variables_const (lexer, dict, &factor.vars, &factor.n_vars,
1101 PV_NO_DUPLICATE | PV_NUMERIC))
1104 else if (lex_match_id (lexer, "MATRIX"))
1106 lex_match (lexer, T_EQUALS);
1107 if (! lex_force_match_id (lexer, "IN"))
1109 if (!lex_force_match (lexer, T_LPAREN))
1113 if (lex_match_id (lexer, "CORR"))
1116 else if (lex_match_id (lexer, "COV"))
1121 lex_error (lexer, _("Matrix input for %s must be either COV or CORR"), "FACTOR");
1124 if (! lex_force_match (lexer, T_EQUALS))
1126 if (lex_match (lexer, T_ASTERISK))
1128 dict = dataset_dict (ds);
1129 matrix_reader = casereader_clone (dataset_source (ds));
1133 struct file_handle *fh = fh_parse (lexer, FH_REF_FILE, NULL);
1138 = any_reader_open_and_decode (fh, NULL, &dict, NULL);
1140 if (! (matrix_reader && dict))
1146 if (! lex_force_match (lexer, T_RPAREN))
1149 mr = create_matrix_reader_from_case_reader (dict, matrix_reader,
1150 &factor.vars, &factor.n_vars);
1157 while (lex_token (lexer) != T_ENDCMD)
1159 lex_match (lexer, T_SLASH);
1161 if (lex_match_id (lexer, "ANALYSIS"))
1163 struct const_var_set *vs;
1164 const struct variable **vars;
1168 lex_match (lexer, T_EQUALS);
1170 vs = const_var_set_create_from_array (factor.vars, factor.n_vars);
1171 ok = parse_const_var_set_vars (lexer, vs, &vars, &n_vars,
1172 PV_NO_DUPLICATE | PV_NUMERIC);
1173 const_var_set_destroy (vs);
1180 factor.n_vars = n_vars;
1182 else if (lex_match_id (lexer, "PLOT"))
1184 lex_match (lexer, T_EQUALS);
1185 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1187 if (lex_match_id (lexer, "EIGEN"))
1189 factor.plot |= PLOT_SCREE;
1191 #if FACTOR_FULLY_IMPLEMENTED
1192 else if (lex_match_id (lexer, "ROTATION"))
1198 lex_error (lexer, NULL);
1203 else if (lex_match_id (lexer, "METHOD"))
1205 lex_match (lexer, T_EQUALS);
1206 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1208 if (lex_match_id (lexer, "COVARIANCE"))
1210 factor.method = METHOD_COV;
1212 else if (lex_match_id (lexer, "CORRELATION"))
1214 factor.method = METHOD_CORR;
1218 lex_error (lexer, NULL);
1223 else if (lex_match_id (lexer, "ROTATION"))
1225 lex_match (lexer, T_EQUALS);
1226 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1228 /* VARIMAX and DEFAULT are defaults */
1229 if (lex_match_id (lexer, "VARIMAX") || lex_match_id (lexer, "DEFAULT"))
1231 factor.rotation = ROT_VARIMAX;
1233 else if (lex_match_id (lexer, "EQUAMAX"))
1235 factor.rotation = ROT_EQUAMAX;
1237 else if (lex_match_id (lexer, "QUARTIMAX"))
1239 factor.rotation = ROT_QUARTIMAX;
1241 else if (lex_match_id (lexer, "PROMAX"))
1243 factor.promax_power = 5;
1244 if (lex_match (lexer, T_LPAREN)
1245 && lex_force_int (lexer))
1247 factor.promax_power = lex_integer (lexer);
1249 if (! lex_force_match (lexer, T_RPAREN))
1252 factor.rotation = ROT_PROMAX;
1254 else if (lex_match_id (lexer, "NOROTATE"))
1256 factor.rotation = ROT_NONE;
1260 lex_error (lexer, NULL);
1264 factor.rotation_iterations = n_iterations;
1266 else if (lex_match_id (lexer, "CRITERIA"))
1268 lex_match (lexer, T_EQUALS);
1269 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1271 if (lex_match_id (lexer, "FACTORS"))
1273 if ( lex_force_match (lexer, T_LPAREN)
1274 && lex_force_int (lexer))
1276 factor.n_factors = lex_integer (lexer);
1278 if (! lex_force_match (lexer, T_RPAREN))
1282 else if (lex_match_id (lexer, "MINEIGEN"))
1284 if ( lex_force_match (lexer, T_LPAREN)
1285 && lex_force_num (lexer))
1287 factor.min_eigen = lex_number (lexer);
1289 if (! lex_force_match (lexer, T_RPAREN))
1293 else if (lex_match_id (lexer, "ECONVERGE"))
1295 if ( lex_force_match (lexer, T_LPAREN)
1296 && lex_force_num (lexer))
1298 factor.econverge = lex_number (lexer);
1300 if (! lex_force_match (lexer, T_RPAREN))
1304 else if (lex_match_id (lexer, "RCONVERGE"))
1306 if (lex_force_match (lexer, T_LPAREN)
1307 && lex_force_num (lexer))
1309 factor.rconverge = lex_number (lexer);
1311 if (! lex_force_match (lexer, T_RPAREN))
1315 else if (lex_match_id (lexer, "ITERATE"))
1317 if ( lex_force_match (lexer, T_LPAREN)
1318 && lex_force_int (lexer))
1320 n_iterations = lex_integer (lexer);
1322 if (! lex_force_match (lexer, T_RPAREN))
1326 else if (lex_match_id (lexer, "DEFAULT"))
1328 factor.n_factors = 0;
1329 factor.min_eigen = 1;
1334 lex_error (lexer, NULL);
1339 else if (lex_match_id (lexer, "EXTRACTION"))
1341 lex_match (lexer, T_EQUALS);
1342 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1344 if (lex_match_id (lexer, "PAF"))
1346 factor.extraction = EXTRACTION_PAF;
1348 else if (lex_match_id (lexer, "PC"))
1350 factor.extraction = EXTRACTION_PC;
1352 else if (lex_match_id (lexer, "PA1"))
1354 factor.extraction = EXTRACTION_PC;
1356 else if (lex_match_id (lexer, "DEFAULT"))
1358 factor.extraction = EXTRACTION_PC;
1362 lex_error (lexer, NULL);
1366 factor.extraction_iterations = n_iterations;
1368 else if (lex_match_id (lexer, "FORMAT"))
1370 lex_match (lexer, T_EQUALS);
1371 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1373 if (lex_match_id (lexer, "SORT"))
1377 else if (lex_match_id (lexer, "BLANK"))
1379 if ( lex_force_match (lexer, T_LPAREN)
1380 && lex_force_num (lexer))
1382 factor.blank = lex_number (lexer);
1384 if (! lex_force_match (lexer, T_RPAREN))
1388 else if (lex_match_id (lexer, "DEFAULT"))
1391 factor.sort = false;
1395 lex_error (lexer, NULL);
1400 else if (lex_match_id (lexer, "PRINT"))
1403 lex_match (lexer, T_EQUALS);
1404 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1406 if (lex_match_id (lexer, "UNIVARIATE"))
1408 factor.print |= PRINT_UNIVARIATE;
1410 else if (lex_match_id (lexer, "DET"))
1412 factor.print |= PRINT_DETERMINANT;
1414 #if FACTOR_FULLY_IMPLEMENTED
1415 else if (lex_match_id (lexer, "INV"))
1419 else if (lex_match_id (lexer, "AIC"))
1421 factor.print |= PRINT_AIC;
1423 else if (lex_match_id (lexer, "SIG"))
1425 factor.print |= PRINT_SIG;
1427 else if (lex_match_id (lexer, "CORRELATION"))
1429 factor.print |= PRINT_CORRELATION;
1431 else if (lex_match_id (lexer, "COVARIANCE"))
1433 factor.print |= PRINT_COVARIANCE;
1435 else if (lex_match_id (lexer, "ROTATION"))
1437 factor.print |= PRINT_ROTATION;
1439 else if (lex_match_id (lexer, "EXTRACTION"))
1441 factor.print |= PRINT_EXTRACTION;
1443 else if (lex_match_id (lexer, "INITIAL"))
1445 factor.print |= PRINT_INITIAL;
1447 else if (lex_match_id (lexer, "KMO"))
1449 factor.print |= PRINT_KMO;
1451 #if FACTOR_FULLY_IMPLEMENTED
1452 else if (lex_match_id (lexer, "REPR"))
1455 else if (lex_match_id (lexer, "FSCORE"))
1459 else if (lex_match (lexer, T_ALL))
1461 factor.print = 0xFFFF;
1463 else if (lex_match_id (lexer, "DEFAULT"))
1465 factor.print |= PRINT_INITIAL ;
1466 factor.print |= PRINT_EXTRACTION ;
1467 factor.print |= PRINT_ROTATION ;
1471 lex_error (lexer, NULL);
1476 else if (lex_match_id (lexer, "MISSING"))
1478 lex_match (lexer, T_EQUALS);
1479 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1481 if (lex_match_id (lexer, "INCLUDE"))
1483 factor.exclude = MV_SYSTEM;
1485 else if (lex_match_id (lexer, "EXCLUDE"))
1487 factor.exclude = MV_ANY;
1489 else if (lex_match_id (lexer, "LISTWISE"))
1491 factor.missing_type = MISS_LISTWISE;
1493 else if (lex_match_id (lexer, "PAIRWISE"))
1495 factor.missing_type = MISS_PAIRWISE;
1497 else if (lex_match_id (lexer, "MEANSUB"))
1499 factor.missing_type = MISS_MEANSUB;
1503 lex_error (lexer, NULL);
1510 lex_error (lexer, NULL);
1515 if ( factor.rotation == ROT_NONE )
1516 factor.print &= ~PRINT_ROTATION;
1518 if (factor.n_vars < 2)
1519 msg (MW, _("Factor analysis on a single variable is not useful."));
1521 if (factor.n_vars < 1)
1523 msg (ME, _("Factor analysis without variables is not possible."));
1529 struct idata *id = idata_alloc (factor.n_vars);
1531 while (next_matrix_from_reader (&id->mm, mr,
1532 factor.vars, factor.n_vars))
1534 do_factor_by_matrix (&factor, id);
1536 gsl_matrix_free (id->mm.corr);
1538 gsl_matrix_free (id->mm.cov);
1545 if ( ! run_factor (ds, &factor))
1549 destroy_matrix_reader (mr);
1554 destroy_matrix_reader (mr);
1559 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
1563 run_factor (struct dataset *ds, const struct cmd_factor *factor)
1565 struct dictionary *dict = dataset_dict (ds);
1567 struct casereader *group;
1569 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
1571 while (casegrouper_get_next_group (grouper, &group))
1573 if ( factor->missing_type == MISS_LISTWISE )
1574 group = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
1577 do_factor (factor, group);
1580 ok = casegrouper_destroy (grouper);
1581 ok = proc_commit (ds) && ok;
1587 /* Return the communality of variable N, calculated to N_FACTORS */
1589 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
1596 assert (n < eval->size);
1597 assert (n < evec->size1);
1598 assert (n_factors <= eval->size);
1600 for (i = 0 ; i < n_factors; ++i)
1602 double evali = fabs (gsl_vector_get (eval, i));
1604 double eveci = gsl_matrix_get (evec, n, i);
1606 comm += pow2 (eveci) * evali;
1612 /* Return the communality of variable N, calculated to N_FACTORS */
1614 communality (const struct idata *idata, int n, int n_factors)
1616 return the_communality (idata->evec, idata->eval, n, n_factors);
1621 show_scree (const struct cmd_factor *f, const struct idata *idata)
1626 if ( !(f->plot & PLOT_SCREE) )
1630 label = f->extraction == EXTRACTION_PC ? _("Component Number") : _("Factor Number");
1632 s = scree_create (idata->eval, label);
1638 show_communalities (const struct cmd_factor * factor,
1639 const gsl_vector *initial, const gsl_vector *extracted)
1643 const int heading_columns = 1;
1644 int nc = heading_columns;
1645 const int heading_rows = 1;
1646 const int nr = heading_rows + factor->n_vars;
1647 struct tab_table *t;
1649 if (factor->print & PRINT_EXTRACTION)
1652 if (factor->print & PRINT_INITIAL)
1655 /* No point having a table with only headings */
1659 t = tab_create (nc, nr);
1661 tab_title (t, _("Communalities"));
1663 tab_headers (t, heading_columns, 0, heading_rows, 0);
1666 if (factor->print & PRINT_INITIAL)
1667 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
1669 if (factor->print & PRINT_EXTRACTION)
1670 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
1672 /* Outline the box */
1679 /* Vertical lines */
1686 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1687 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1689 for (i = 0 ; i < factor->n_vars; ++i)
1692 tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
1694 if (factor->print & PRINT_INITIAL)
1695 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL, RC_OTHER);
1697 if (factor->print & PRINT_EXTRACTION)
1698 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL, RC_OTHER);
1706 show_factor_matrix (const struct cmd_factor *factor, const struct idata *idata, const char *title, const gsl_matrix *fm)
1710 const int n_factors = idata->n_extractions;
1712 const int heading_columns = 1;
1713 const int heading_rows = 2;
1714 const int nr = heading_rows + factor->n_vars;
1715 const int nc = heading_columns + n_factors;
1716 gsl_permutation *perm;
1718 struct tab_table *t = tab_create (nc, nr);
1721 if ( factor->extraction == EXTRACTION_PC )
1722 tab_title (t, _("Component Matrix"));
1724 tab_title (t, _("Factor Matrix"));
1727 tab_title (t, "%s", title);
1729 tab_headers (t, heading_columns, 0, heading_rows, 0);
1731 if ( factor->extraction == EXTRACTION_PC )
1735 TAB_CENTER | TAT_TITLE, _("Component"));
1740 TAB_CENTER | TAT_TITLE, _("Factor"));
1743 tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1746 /* Outline the box */
1753 /* Vertical lines */
1760 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1761 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1764 /* Initialise to the identity permutation */
1765 perm = gsl_permutation_calloc (factor->n_vars);
1768 sort_matrix_indirect (fm, perm);
1770 for (i = 0 ; i < n_factors; ++i)
1772 tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1775 for (i = 0 ; i < factor->n_vars; ++i)
1778 const int matrix_row = perm->data[i];
1779 tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1781 for (j = 0 ; j < n_factors; ++j)
1783 double x = gsl_matrix_get (fm, matrix_row, j);
1785 if ( fabs (x) < factor->blank)
1788 tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL, RC_OTHER);
1792 gsl_permutation_free (perm);
1799 show_explained_variance (const struct cmd_factor * factor,
1800 const struct idata *idata,
1801 const gsl_vector *initial_eigenvalues,
1802 const gsl_vector *extracted_eigenvalues,
1803 const gsl_vector *rotated_loadings)
1807 const int heading_columns = 1;
1808 const int heading_rows = 2;
1809 const int nr = heading_rows + factor->n_vars;
1811 struct tab_table *t ;
1813 double i_total = 0.0;
1816 double e_total = 0.0;
1821 int nc = heading_columns;
1823 if (factor->print & PRINT_EXTRACTION)
1826 if (factor->print & PRINT_INITIAL)
1829 if (factor->print & PRINT_ROTATION)
1831 nc += factor->rotation == ROT_PROMAX ? 1 : 3;
1834 /* No point having a table with only headings */
1835 if ( nc <= heading_columns)
1838 t = tab_create (nc, nr);
1840 tab_title (t, _("Total Variance Explained"));
1842 tab_headers (t, heading_columns, 0, heading_rows, 0);
1844 /* Outline the box */
1851 /* Vertical lines */
1858 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1859 tab_hline (t, TAL_1, 1, nc - 1, 1);
1861 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1864 if ( factor->extraction == EXTRACTION_PC)
1865 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1867 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1870 if (factor->print & PRINT_INITIAL)
1872 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1876 if (factor->print & PRINT_EXTRACTION)
1878 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1882 if (factor->print & PRINT_ROTATION)
1884 const int width = factor->rotation == ROT_PROMAX ? 0 : 2;
1885 tab_joint_text (t, c, 0, c + width, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1889 for (i = 0; i < (nc - heading_columns + 2) / 3 ; ++i)
1891 tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1893 tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1895 if (i == 2 && factor->rotation == ROT_PROMAX)
1898 /* xgettext:no-c-format */
1899 tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1900 tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1903 for (i = 0 ; i < initial_eigenvalues->size; ++i)
1904 i_total += gsl_vector_get (initial_eigenvalues, i);
1906 if ( factor->extraction == EXTRACTION_PAF)
1908 e_total = factor->n_vars;
1915 for (i = 0 ; i < factor->n_vars; ++i)
1917 const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1918 double i_percent = 100.0 * i_lambda / i_total ;
1920 const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1921 double e_percent = 100.0 * e_lambda / e_total ;
1925 tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%zu"), i + 1);
1930 /* Initial Eigenvalues */
1931 if (factor->print & PRINT_INITIAL)
1933 tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL, RC_OTHER);
1934 tab_double (t, c++, i + heading_rows, 0, i_percent, NULL, RC_OTHER);
1935 tab_double (t, c++, i + heading_rows, 0, i_cum, NULL, RC_OTHER);
1939 if (factor->print & PRINT_EXTRACTION)
1941 if (i < idata->n_extractions)
1943 /* Sums of squared loadings */
1944 tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL, RC_OTHER);
1945 tab_double (t, c++, i + heading_rows, 0, e_percent, NULL, RC_OTHER);
1946 tab_double (t, c++, i + heading_rows, 0, e_cum, NULL, RC_OTHER);
1950 if (rotated_loadings != NULL)
1952 const double r_lambda = gsl_vector_get (rotated_loadings, i);
1953 double r_percent = 100.0 * r_lambda / e_total ;
1955 if (factor->print & PRINT_ROTATION)
1957 if (i < idata->n_extractions)
1960 tab_double (t, c++, i + heading_rows, 0, r_lambda, NULL, RC_OTHER);
1961 if (factor->rotation != ROT_PROMAX)
1963 tab_double (t, c++, i + heading_rows, 0, r_percent, NULL, RC_OTHER);
1964 tab_double (t, c++, i + heading_rows, 0, r_cum, NULL, RC_OTHER);
1976 show_factor_correlation (const struct cmd_factor * factor, const gsl_matrix *fcm)
1979 const int heading_columns = 1;
1980 const int heading_rows = 1;
1981 const int nr = heading_rows + fcm->size2;
1982 const int nc = heading_columns + fcm->size1;
1983 struct tab_table *t = tab_create (nc, nr);
1985 tab_title (t, _("Factor Correlation Matrix"));
1987 tab_headers (t, heading_columns, 0, heading_rows, 0);
1989 /* Outline the box */
1996 /* Vertical lines */
2003 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
2004 tab_hline (t, TAL_1, 1, nc - 1, 1);
2006 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
2009 if ( factor->extraction == EXTRACTION_PC)
2010 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Component"));
2012 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Factor"));
2014 for (i = 0 ; i < fcm->size1; ++i)
2016 tab_text_format (t, heading_columns + i, 0, TAB_CENTER | TAT_TITLE, _("%zu"), i + 1);
2019 for (i = 0 ; i < fcm->size2; ++i)
2021 tab_text_format (t, 0, heading_rows + i, TAB_CENTER | TAT_TITLE, _("%zu"), i + 1);
2025 for (i = 0 ; i < fcm->size1; ++i)
2027 for (j = 0 ; j < fcm->size2; ++j)
2028 tab_double (t, heading_columns + j, heading_rows + i, 0,
2029 gsl_matrix_get (fcm, i, j), NULL, RC_OTHER);
2036 show_aic (const struct cmd_factor *factor, const struct idata *idata)
2038 struct tab_table *t ;
2041 const int heading_rows = 1;
2042 const int heading_columns = 2;
2044 const int nc = heading_columns + factor->n_vars;
2045 const int nr = heading_rows + 2 * factor->n_vars;
2047 if ((factor->print & PRINT_AIC) == 0)
2050 t = tab_create (nc, nr);
2052 tab_title (t, _("Anti-Image Matrices"));
2054 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
2056 tab_headers (t, heading_columns, 0, heading_rows, 0);
2058 tab_vline (t, TAL_2, 2, 0, nr - 1);
2060 /* Outline the box */
2067 /* Vertical lines */
2075 for (i = 0; i < factor->n_vars; ++i)
2076 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
2078 tab_text (t, 0, heading_rows, TAT_TITLE, _("Anti-image Covariance"));
2079 tab_hline (t, TAL_1, 0, nc - 1, heading_rows + factor->n_vars);
2080 tab_text (t, 0, heading_rows + factor->n_vars, TAT_TITLE, _("Anti-image Correlation"));
2082 for (i = 0; i < factor->n_vars; ++i)
2084 tab_text (t, 1, i + heading_rows, TAT_TITLE,
2085 var_to_string (factor->vars[i]));
2087 tab_text (t, 1, factor->n_vars + i + heading_rows, TAT_TITLE,
2088 var_to_string (factor->vars[i]));
2091 for (i = 0; i < factor->n_vars; ++i)
2094 for (j = 0; j < factor->n_vars; ++j)
2096 tab_double (t, heading_columns + i, heading_rows + j, 0,
2097 gsl_matrix_get (idata->ai_cov, i, j), NULL, RC_OTHER);
2101 for (j = 0; j < factor->n_vars; ++j)
2103 tab_double (t, heading_columns + i, factor->n_vars + heading_rows + j, 0,
2104 gsl_matrix_get (idata->ai_cor, i, j), NULL, RC_OTHER);
2112 show_correlation_matrix (const struct cmd_factor *factor, const struct idata *idata)
2114 struct tab_table *t ;
2116 int y_pos_corr = -1;
2118 int suffix_rows = 0;
2120 const int heading_rows = 1;
2121 const int heading_columns = 2;
2123 int nc = heading_columns ;
2124 int nr = heading_rows ;
2125 int n_data_sets = 0;
2127 if (factor->print & PRINT_CORRELATION)
2129 y_pos_corr = n_data_sets;
2131 nc = heading_columns + factor->n_vars;
2134 if (factor->print & PRINT_SIG)
2136 y_pos_sig = n_data_sets;
2138 nc = heading_columns + factor->n_vars;
2141 nr += n_data_sets * factor->n_vars;
2143 if (factor->print & PRINT_DETERMINANT)
2146 /* If the table would contain only headings, don't bother rendering it */
2147 if (nr <= heading_rows && suffix_rows == 0)
2150 t = tab_create (nc, nr + suffix_rows);
2152 tab_title (t, _("Correlation Matrix"));
2154 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
2156 if (nr > heading_rows)
2158 tab_headers (t, heading_columns, 0, heading_rows, 0);
2160 tab_vline (t, TAL_2, 2, 0, nr - 1);
2162 /* Outline the box */
2169 /* Vertical lines */
2177 for (i = 0; i < factor->n_vars; ++i)
2178 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
2181 for (i = 0 ; i < n_data_sets; ++i)
2183 int y = heading_rows + i * factor->n_vars;
2185 for (v = 0; v < factor->n_vars; ++v)
2186 tab_text (t, 1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
2188 tab_hline (t, TAL_1, 0, nc - 1, y);
2191 if (factor->print & PRINT_CORRELATION)
2193 const double y = heading_rows + y_pos_corr;
2194 tab_text (t, 0, y, TAT_TITLE, _("Correlations"));
2196 for (i = 0; i < factor->n_vars; ++i)
2198 for (j = 0; j < factor->n_vars; ++j)
2199 tab_double (t, heading_columns + j, y + i, 0, gsl_matrix_get (idata->mm.corr, i, j), NULL, RC_OTHER);
2203 if (factor->print & PRINT_SIG)
2205 const double y = heading_rows + y_pos_sig * factor->n_vars;
2206 tab_text (t, 0, y, TAT_TITLE, _("Sig. (1-tailed)"));
2208 for (i = 0; i < factor->n_vars; ++i)
2210 for (j = 0; j < factor->n_vars; ++j)
2212 double rho = gsl_matrix_get (idata->mm.corr, i, j);
2213 double w = gsl_matrix_get (idata->mm.n, i, j);
2218 tab_double (t, heading_columns + j, y + i, 0, significance_of_correlation (rho, w), NULL, RC_PVALUE);
2224 if (factor->print & PRINT_DETERMINANT)
2226 tab_text (t, 0, nr, TAB_LEFT | TAT_TITLE, _("Determinant"));
2228 tab_double (t, 1, nr, 0, idata->detR, NULL, RC_OTHER);
2235 show_covariance_matrix (const struct cmd_factor *factor, const struct idata *idata)
2237 struct tab_table *t ;
2239 int y_pos_corr = -1;
2240 int suffix_rows = 0;
2242 const int heading_rows = 1;
2243 const int heading_columns = 1;
2245 int nc = heading_columns ;
2246 int nr = heading_rows ;
2247 int n_data_sets = 0;
2249 if (factor->print & PRINT_COVARIANCE)
2251 y_pos_corr = n_data_sets;
2253 nc = heading_columns + factor->n_vars;
2256 nr += n_data_sets * factor->n_vars;
2258 /* If the table would contain only headings, don't bother rendering it */
2259 if (nr <= heading_rows && suffix_rows == 0)
2262 t = tab_create (nc, nr + suffix_rows);
2264 tab_title (t, _("Covariance Matrix"));
2266 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
2268 if (nr > heading_rows)
2270 tab_headers (t, heading_columns, 0, heading_rows, 0);
2272 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
2274 /* Outline the box */
2281 /* Vertical lines */
2289 for (i = 0; i < factor->n_vars; ++i)
2290 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
2293 for (i = 0 ; i < n_data_sets; ++i)
2295 int y = heading_rows + i * factor->n_vars;
2297 for (v = 0; v < factor->n_vars; ++v)
2298 tab_text (t, heading_columns -1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
2300 tab_hline (t, TAL_1, 0, nc - 1, y);
2303 if (factor->print & PRINT_COVARIANCE)
2305 const double y = heading_rows + y_pos_corr;
2307 for (i = 0; i < factor->n_vars; ++i)
2309 for (j = 0; j < factor->n_vars; ++j)
2310 tab_double (t, heading_columns + j, y + i, 0, gsl_matrix_get (idata->mm.cov, i, j), NULL, RC_OTHER);
2320 do_factor (const struct cmd_factor *factor, struct casereader *r)
2323 struct idata *idata = idata_alloc (factor->n_vars);
2325 idata->cvm = covariance_1pass_create (factor->n_vars, factor->vars,
2326 factor->wv, factor->exclude, true);
2328 for ( ; (c = casereader_read (r) ); case_unref (c))
2330 covariance_accumulate (idata->cvm, c);
2333 idata->mm.cov = covariance_calculate (idata->cvm);
2335 if (idata->mm.cov == NULL)
2337 msg (MW, _("The dataset contains no complete observations. No analysis will be performed."));
2338 covariance_destroy (idata->cvm);
2342 idata->mm.var_matrix = covariance_moments (idata->cvm, MOMENT_VARIANCE);
2343 idata->mm.mean_matrix = covariance_moments (idata->cvm, MOMENT_MEAN);
2344 idata->mm.n = covariance_moments (idata->cvm, MOMENT_NONE);
2346 do_factor_by_matrix (factor, idata);
2349 gsl_matrix_free (idata->mm.corr);
2350 gsl_matrix_free (idata->mm.cov);
2353 casereader_destroy (r);
2357 do_factor_by_matrix (const struct cmd_factor *factor, struct idata *idata)
2359 if (!idata->mm.cov && !idata->mm.corr)
2361 msg (ME, _("The dataset has no complete covariance or correlation matrix."));
2365 if (idata->mm.cov && !idata->mm.corr)
2366 idata->mm.corr = correlation_from_covariance (idata->mm.cov, idata->mm.var_matrix);
2367 if (idata->mm.corr && !idata->mm.cov)
2368 idata->mm.cov = covariance_from_correlation (idata->mm.corr, idata->mm.var_matrix);
2369 if (factor->method == METHOD_CORR)
2370 idata->analysis_matrix = idata->mm.corr;
2372 idata->analysis_matrix = idata->mm.cov;
2375 r_inv = clone_matrix (idata->mm.corr);
2376 gsl_linalg_cholesky_decomp (r_inv);
2377 gsl_linalg_cholesky_invert (r_inv);
2379 idata->ai_cov = anti_image_cov (r_inv);
2380 idata->ai_cor = anti_image_corr (r_inv, idata);
2383 double sum_ssq_r = 0;
2384 double sum_ssq_a = 0;
2385 for (i = 0; i < r_inv->size1; ++i)
2387 sum_ssq_r += ssq_od_n (idata->mm.corr, i);
2388 sum_ssq_a += ssq_od_n (idata->ai_cor, i);
2391 gsl_matrix_free (r_inv);
2393 if (factor->print & PRINT_DETERMINANT
2394 || factor->print & PRINT_KMO)
2398 const int size = idata->mm.corr->size1;
2399 gsl_permutation *p = gsl_permutation_calloc (size);
2400 gsl_matrix *tmp = gsl_matrix_calloc (size, size);
2401 gsl_matrix_memcpy (tmp, idata->mm.corr);
2403 gsl_linalg_LU_decomp (tmp, p, &sign);
2404 idata->detR = gsl_linalg_LU_det (tmp, sign);
2405 gsl_permutation_free (p);
2406 gsl_matrix_free (tmp);
2409 if ( factor->print & PRINT_UNIVARIATE)
2411 const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
2415 const int heading_columns = 1;
2416 const int heading_rows = 1;
2418 const int nr = heading_rows + factor->n_vars;
2420 struct tab_table *t = tab_create (nc, nr);
2421 tab_set_format (t, RC_WEIGHT, wfmt);
2422 tab_title (t, _("Descriptive Statistics"));
2424 tab_headers (t, heading_columns, 0, heading_rows, 0);
2426 /* Outline the box */
2433 /* Vertical lines */
2440 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
2441 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
2443 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
2444 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
2445 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
2447 for (i = 0 ; i < factor->n_vars; ++i)
2449 const struct variable *v = factor->vars[i];
2450 tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
2452 tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (idata->mm.mean_matrix, i, i), NULL, RC_OTHER);
2453 tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (idata->mm.var_matrix, i, i)), NULL, RC_OTHER);
2454 tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (idata->mm.n, i, i), NULL, RC_WEIGHT);
2460 if (factor->print & PRINT_KMO)
2463 double df = factor->n_vars * (factor->n_vars - 1) / 2;
2470 const int heading_columns = 2;
2471 const int heading_rows = 0;
2473 const int nr = heading_rows + 4;
2474 const int nc = heading_columns + 1;
2478 struct tab_table *t = tab_create (nc, nr);
2479 tab_title (t, _("KMO and Bartlett's Test"));
2482 tab_headers (t, heading_columns, 0, heading_rows, 0);
2484 /* Outline the box */
2491 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
2493 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Kaiser-Meyer-Olkin Measure of Sampling Adequacy"));
2495 tab_double (t, 2, 0, 0, sum_ssq_r / (sum_ssq_r + sum_ssq_a), NULL, RC_OTHER);
2497 tab_text (t, 0, 1, TAT_TITLE | TAB_LEFT, _("Bartlett's Test of Sphericity"));
2499 tab_text (t, 1, 1, TAT_TITLE, _("Approx. Chi-Square"));
2500 tab_text (t, 1, 2, TAT_TITLE, _("df"));
2501 tab_text (t, 1, 3, TAT_TITLE, _("Sig."));
2504 /* The literature doesn't say what to do for the value of W when
2505 missing values are involved. The best thing I can think of
2506 is to take the mean average. */
2508 for (i = 0; i < idata->mm.n->size1; ++i)
2509 w += gsl_matrix_get (idata->mm.n, i, i);
2510 w /= idata->mm.n->size1;
2512 xsq = w - 1 - (2 * factor->n_vars + 5) / 6.0;
2513 xsq *= -log (idata->detR);
2515 tab_double (t, 2, 1, 0, xsq, NULL, RC_OTHER);
2516 tab_double (t, 2, 2, 0, df, NULL, RC_INTEGER);
2517 tab_double (t, 2, 3, 0, gsl_cdf_chisq_Q (xsq, df), NULL, RC_PVALUE);
2523 show_correlation_matrix (factor, idata);
2524 show_covariance_matrix (factor, idata);
2526 covariance_destroy (idata->cvm);
2529 gsl_matrix *am = matrix_dup (idata->analysis_matrix);
2530 gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
2532 gsl_eigen_symmv (am, idata->eval, idata->evec, workspace);
2534 gsl_eigen_symmv_free (workspace);
2535 gsl_matrix_free (am);
2538 gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
2540 idata->n_extractions = n_extracted_factors (factor, idata);
2542 if (idata->n_extractions == 0)
2544 msg (MW, _("The %s criteria result in zero factors extracted. Therefore no analysis will be performed."), "FACTOR");
2548 if (idata->n_extractions > factor->n_vars)
2551 _("The %s criteria result in more factors than variables, which is not meaningful. No analysis will be performed."),
2557 gsl_matrix *rotated_factors = NULL;
2558 gsl_matrix *pattern_matrix = NULL;
2559 gsl_matrix *fcm = NULL;
2560 gsl_vector *rotated_loadings = NULL;
2562 const gsl_vector *extracted_eigenvalues = NULL;
2563 gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
2564 gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
2566 struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, idata->n_extractions);
2567 gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
2569 if ( factor->extraction == EXTRACTION_PAF)
2571 gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
2572 struct smr_workspace *ws = ws_create (idata->analysis_matrix);
2574 for (i = 0 ; i < factor->n_vars ; ++i)
2576 double r2 = squared_multiple_correlation (idata->analysis_matrix, i, ws);
2578 gsl_vector_set (idata->msr, i, r2);
2582 gsl_vector_memcpy (initial_communalities, idata->msr);
2584 for (i = 0; i < factor->extraction_iterations; ++i)
2587 gsl_vector_memcpy (diff, idata->msr);
2589 iterate_factor_matrix (idata->analysis_matrix, idata->msr, factor_matrix, fmw);
2591 gsl_vector_sub (diff, idata->msr);
2593 gsl_vector_minmax (diff, &min, &max);
2595 if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
2598 gsl_vector_free (diff);
2602 gsl_vector_memcpy (extracted_communalities, idata->msr);
2603 extracted_eigenvalues = fmw->eval;
2605 else if (factor->extraction == EXTRACTION_PC)
2607 for (i = 0; i < factor->n_vars; ++i)
2608 gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
2610 gsl_vector_memcpy (extracted_communalities, initial_communalities);
2612 iterate_factor_matrix (idata->analysis_matrix, extracted_communalities, factor_matrix, fmw);
2615 extracted_eigenvalues = idata->eval;
2619 show_aic (factor, idata);
2620 show_communalities (factor, initial_communalities, extracted_communalities);
2622 if ( factor->rotation != ROT_NONE)
2624 rotated_factors = gsl_matrix_calloc (factor_matrix->size1, factor_matrix->size2);
2625 rotated_loadings = gsl_vector_calloc (factor_matrix->size2);
2626 if (factor->rotation == ROT_PROMAX)
2628 pattern_matrix = gsl_matrix_calloc (factor_matrix->size1, factor_matrix->size2);
2629 fcm = gsl_matrix_calloc (factor_matrix->size2, factor_matrix->size2);
2633 rotate (factor, factor_matrix, extracted_communalities, rotated_factors, rotated_loadings, pattern_matrix, fcm);
2636 show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues, rotated_loadings);
2638 factor_matrix_workspace_free (fmw);
2640 show_scree (factor, idata);
2642 show_factor_matrix (factor, idata,
2643 factor->extraction == EXTRACTION_PC ? _("Component Matrix") : _("Factor Matrix"),
2646 if ( factor->rotation == ROT_PROMAX)
2648 show_factor_matrix (factor, idata, _("Pattern Matrix"), pattern_matrix);
2649 gsl_matrix_free (pattern_matrix);
2652 if ( factor->rotation != ROT_NONE)
2654 show_factor_matrix (factor, idata,
2655 (factor->rotation == ROT_PROMAX) ? _("Structure Matrix") :
2656 (factor->extraction == EXTRACTION_PC ? _("Rotated Component Matrix") :
2657 _("Rotated Factor Matrix")),
2660 gsl_matrix_free (rotated_factors);
2663 if ( factor->rotation == ROT_PROMAX)
2665 show_factor_correlation (factor, fcm);
2666 gsl_matrix_free (fcm);
2669 gsl_matrix_free (factor_matrix);
2670 gsl_vector_free (rotated_loadings);
2671 gsl_vector_free (initial_communalities);
2672 gsl_vector_free (extracted_communalities);