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 struct covariance *cvm;
249 static struct idata *
250 idata_alloc (size_t n_vars)
252 struct idata *id = xzalloc (sizeof (*id));
254 id->n_extractions = 0;
255 id->msr = gsl_vector_alloc (n_vars);
257 id->eval = gsl_vector_alloc (n_vars);
258 id->evec = gsl_matrix_alloc (n_vars, n_vars);
264 idata_free (struct idata *id)
266 gsl_vector_free (id->msr);
267 gsl_vector_free (id->eval);
268 gsl_matrix_free (id->evec);
275 anti_image (const gsl_matrix *m)
279 assert (m->size1 == m->size2);
281 a = gsl_matrix_alloc (m->size1, m->size2);
283 for (i = 0; i < m->size1; ++i)
285 for (j = 0; j < m->size2; ++j)
287 double *p = gsl_matrix_ptr (a, i, j);
288 *p = gsl_matrix_get (m, i, j);
289 *p /= gsl_matrix_get (m, i, i);
290 *p /= gsl_matrix_get (m, j, j);
298 /* Return the sum of all the elements excluding row N */
300 ssq_od_n (const gsl_matrix *m, int n)
304 assert (m->size1 == m->size2);
306 assert (n < m->size1);
308 for (i = 0; i < m->size1; ++i)
310 if (i == n ) continue;
311 for (j = 0; j < m->size2; ++j)
313 ss += pow2 (gsl_matrix_get (m, i, j));
324 dump_matrix (const gsl_matrix *m)
328 for (i = 0 ; i < m->size1; ++i)
330 for (j = 0 ; j < m->size2; ++j)
331 printf ("%02f ", gsl_matrix_get (m, i, j));
337 dump_matrix_permute (const gsl_matrix *m, const gsl_permutation *p)
341 for (i = 0 ; i < m->size1; ++i)
343 for (j = 0 ; j < m->size2; ++j)
344 printf ("%02f ", gsl_matrix_get (m, gsl_permutation_get (p, i), j));
351 dump_vector (const gsl_vector *v)
354 for (i = 0 ; i < v->size; ++i)
356 printf ("%02f\n", gsl_vector_get (v, i));
364 n_extracted_factors (const struct cmd_factor *factor, struct idata *idata)
368 /* If there is a cached value, then return that. */
369 if ( idata->n_extractions != 0)
370 return idata->n_extractions;
372 /* Otherwise, if the number of factors has been explicitly requested,
374 if (factor->n_factors > 0)
376 idata->n_extractions = factor->n_factors;
380 /* Use the MIN_EIGEN setting. */
381 for (i = 0 ; i < idata->eval->size; ++i)
383 double evali = fabs (gsl_vector_get (idata->eval, i));
385 idata->n_extractions = i;
387 if (evali < factor->min_eigen)
392 return idata->n_extractions;
396 /* Returns a newly allocated matrix identical to M.
397 It it the callers responsibility to free the returned value.
400 matrix_dup (const gsl_matrix *m)
402 gsl_matrix *n = gsl_matrix_alloc (m->size1, m->size2);
404 gsl_matrix_memcpy (n, m);
412 /* Copy of the subject */
417 gsl_permutation *perm;
424 static struct smr_workspace *ws_create (const gsl_matrix *input)
426 struct smr_workspace *ws = xmalloc (sizeof (*ws));
428 ws->m = gsl_matrix_alloc (input->size1, input->size2);
429 ws->inverse = gsl_matrix_calloc (input->size1 - 1, input->size2 - 1);
430 ws->perm = gsl_permutation_alloc (input->size1 - 1);
431 ws->result1 = gsl_matrix_calloc (input->size1 - 1, 1);
432 ws->result2 = gsl_matrix_calloc (1, 1);
438 ws_destroy (struct smr_workspace *ws)
440 gsl_matrix_free (ws->result2);
441 gsl_matrix_free (ws->result1);
442 gsl_permutation_free (ws->perm);
443 gsl_matrix_free (ws->inverse);
444 gsl_matrix_free (ws->m);
451 Return the square of the regression coefficient for VAR regressed against all other variables.
454 squared_multiple_correlation (const gsl_matrix *corr, int var, struct smr_workspace *ws)
456 /* For an explanation of what this is doing, see
457 http://www.visualstatistics.net/Visual%20Statistics%20Multimedia/multiple_regression_analysis.htm
463 gsl_matrix_memcpy (ws->m, corr);
465 gsl_matrix_swap_rows (ws->m, 0, var);
466 gsl_matrix_swap_columns (ws->m, 0, var);
468 rxx = gsl_matrix_submatrix (ws->m, 1, 1, ws->m->size1 - 1, ws->m->size1 - 1);
470 gsl_linalg_LU_decomp (&rxx.matrix, ws->perm, &signum);
472 gsl_linalg_LU_invert (&rxx.matrix, ws->perm, ws->inverse);
475 gsl_matrix_const_view rxy = gsl_matrix_const_submatrix (ws->m, 1, 0, ws->m->size1 - 1, 1);
476 gsl_matrix_const_view ryx = gsl_matrix_const_submatrix (ws->m, 0, 1, 1, ws->m->size1 - 1);
478 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
479 1.0, ws->inverse, &rxy.matrix, 0.0, ws->result1);
481 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
482 1.0, &ryx.matrix, ws->result1, 0.0, ws->result2);
485 return gsl_matrix_get (ws->result2, 0, 0);
490 static double the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors);
493 struct factor_matrix_workspace
496 gsl_eigen_symmv_workspace *eigen_ws;
506 static struct factor_matrix_workspace *
507 factor_matrix_workspace_alloc (size_t n, size_t nf)
509 struct factor_matrix_workspace *ws = xmalloc (sizeof (*ws));
512 ws->gamma = gsl_matrix_calloc (nf, nf);
513 ws->eigen_ws = gsl_eigen_symmv_alloc (n);
514 ws->eval = gsl_vector_alloc (n);
515 ws->evec = gsl_matrix_alloc (n, n);
516 ws->r = gsl_matrix_alloc (n, n);
522 factor_matrix_workspace_free (struct factor_matrix_workspace *ws)
524 gsl_eigen_symmv_free (ws->eigen_ws);
525 gsl_vector_free (ws->eval);
526 gsl_matrix_free (ws->evec);
527 gsl_matrix_free (ws->gamma);
528 gsl_matrix_free (ws->r);
533 Shift P left by OFFSET places, and overwrite TARGET
534 with the shifted result.
535 Positions in TARGET less than OFFSET are unchanged.
538 perm_shift_apply (gsl_permutation *target, const gsl_permutation *p,
542 assert (target->size == p->size);
543 assert (offset <= target->size);
545 for (i = 0; i < target->size - offset; ++i)
547 target->data[i] = p->data [i + offset];
553 Indirectly sort the rows of matrix INPUT, storing the sort order in PERM.
554 The sort criteria are as follows:
556 Rows are sorted on the first column, until the absolute value of an
557 element in a subsequent column is greater than that of the first
558 column. Thereafter, rows will be sorted on the second column,
559 until the absolute value of an element in a subsequent column
560 exceeds that of the second column ...
563 sort_matrix_indirect (const gsl_matrix *input, gsl_permutation *perm)
565 const size_t n = perm->size;
566 const size_t m = input->size2;
573 assert (perm->size == input->size1);
575 p = gsl_permutation_alloc (n);
577 /* Copy INPUT into MAT, discarding the sign */
578 mat = gsl_matrix_alloc (n, m);
579 for (i = 0 ; i < mat->size1; ++i)
581 for (j = 0 ; j < mat->size2; ++j)
583 double x = gsl_matrix_get (input, i, j);
584 gsl_matrix_set (mat, i, j, fabs (x));
588 while (column_n < m && row_n < n)
590 gsl_vector_const_view columni = gsl_matrix_const_column (mat, column_n);
591 gsl_sort_vector_index (p, &columni.vector);
593 for (i = 0 ; i < n; ++i)
595 gsl_vector_view row = gsl_matrix_row (mat, p->data[n - 1 - i]);
596 size_t maxindex = gsl_vector_max_index (&row.vector);
598 if ( maxindex > column_n )
601 /* All subsequent elements of this row, are of no interest.
602 So set them all to a highly negative value */
603 for (j = column_n + 1; j < row.vector.size ; ++j)
604 gsl_vector_set (&row.vector, j, -DBL_MAX);
607 perm_shift_apply (perm, p, row_n);
613 gsl_permutation_free (p);
614 gsl_matrix_free (mat);
616 assert ( 0 == gsl_permutation_valid (perm));
618 /* We want the biggest value to be first */
619 gsl_permutation_reverse (perm);
624 drot_go (double phi, double *l0, double *l1)
626 double r0 = cos (phi) * *l0 + sin (phi) * *l1;
627 double r1 = - sin (phi) * *l0 + cos (phi) * *l1;
635 clone_matrix (const gsl_matrix *m)
638 gsl_matrix *c = gsl_matrix_calloc (m->size1, m->size2);
640 for (j = 0 ; j < c->size1; ++j)
642 for (k = 0 ; k < c->size2; ++k)
644 const double *v = gsl_matrix_const_ptr (m, j, k);
645 gsl_matrix_set (c, j, k, *v);
654 initial_sv (const gsl_matrix *fm)
659 for (j = 0 ; j < fm->size2; ++j)
664 for (k = j + 1 ; k < fm->size2; ++k)
666 double lambda = gsl_matrix_get (fm, k, j);
667 double lambda_sq = lambda * lambda;
668 double lambda_4 = lambda_sq * lambda_sq;
673 sv += ( fm->size1 * l4s - (l2s * l2s) ) / (fm->size1 * fm->size1 );
679 rotate (const struct cmd_factor *cf, const gsl_matrix *unrot,
680 const gsl_vector *communalities,
682 gsl_vector *rotated_loadings,
683 gsl_matrix *pattern_matrix,
684 gsl_matrix *factor_correlation_matrix
691 /* First get a normalised version of UNROT */
692 gsl_matrix *normalised = gsl_matrix_calloc (unrot->size1, unrot->size2);
693 gsl_matrix *h_sqrt = gsl_matrix_calloc (communalities->size, communalities->size);
694 gsl_matrix *h_sqrt_inv ;
696 /* H is the diagonal matrix containing the absolute values of the communalities */
697 for (i = 0 ; i < communalities->size ; ++i)
699 double *ptr = gsl_matrix_ptr (h_sqrt, i, i);
700 *ptr = fabs (gsl_vector_get (communalities, i));
703 /* Take the square root of the communalities */
704 gsl_linalg_cholesky_decomp (h_sqrt);
707 /* Save a copy of h_sqrt and invert it */
708 h_sqrt_inv = clone_matrix (h_sqrt);
709 gsl_linalg_cholesky_decomp (h_sqrt_inv);
710 gsl_linalg_cholesky_invert (h_sqrt_inv);
712 /* normalised vertion is H^{1/2} x UNROT */
713 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, h_sqrt_inv, unrot, 0.0, normalised);
715 gsl_matrix_free (h_sqrt_inv);
718 /* Now perform the rotation iterations */
720 prev_sv = initial_sv (normalised);
721 for (i = 0 ; i < cf->rotation_iterations ; ++i)
724 for (j = 0 ; j < normalised->size2; ++j)
726 /* These variables relate to the convergence criterium */
730 for (k = j + 1 ; k < normalised->size2; ++k)
740 for (p = 0; p < normalised->size1; ++p)
742 double jv = gsl_matrix_get (normalised, p, j);
743 double kv = gsl_matrix_get (normalised, p, k);
745 double u = jv * jv - kv * kv;
746 double v = 2 * jv * kv;
753 rotation_coeff [cf->rotation] (&x, &y, a, b, c, d, normalised);
755 phi = atan2 (x, y) / 4.0 ;
757 /* Don't bother rotating if the angle is small */
758 if ( fabs (sin (phi) ) <= pow (10.0, -15.0))
761 for (p = 0; p < normalised->size1; ++p)
763 double *lambda0 = gsl_matrix_ptr (normalised, p, j);
764 double *lambda1 = gsl_matrix_ptr (normalised, p, k);
765 drot_go (phi, lambda0, lambda1);
768 /* Calculate the convergence criterium */
770 double lambda = gsl_matrix_get (normalised, k, j);
771 double lambda_sq = lambda * lambda;
772 double lambda_4 = lambda_sq * lambda_sq;
778 sv += ( normalised->size1 * l4s - (l2s * l2s) ) / (normalised->size1 * normalised->size1 );
781 if ( fabs (sv - prev_sv) <= cf->rconverge)
787 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0,
788 h_sqrt, normalised, 0.0, result);
790 gsl_matrix_free (h_sqrt);
791 gsl_matrix_free (normalised);
793 if (cf->rotation == ROT_PROMAX)
795 /* general purpose m by m matrix, where m is the number of factors */
796 gsl_matrix *mm1 = gsl_matrix_calloc (unrot->size2, unrot->size2);
797 gsl_matrix *mm2 = gsl_matrix_calloc (unrot->size2, unrot->size2);
799 /* general purpose m by p matrix, where p is the number of variables */
800 gsl_matrix *mp1 = gsl_matrix_calloc (unrot->size2, unrot->size1);
802 gsl_matrix *pm1 = gsl_matrix_calloc (unrot->size1, unrot->size2);
804 gsl_permutation *perm = gsl_permutation_alloc (unrot->size2);
810 /* The following variables follow the notation by SPSS Statistical Algorithms
812 gsl_matrix *L = gsl_matrix_calloc (unrot->size2, unrot->size2);
813 gsl_matrix *P = clone_matrix (result);
818 /* Vector of length p containing (indexed by i)
819 \Sum^m_j {\lambda^2_{ij}} */
820 gsl_vector *rssq = gsl_vector_calloc (unrot->size1);
822 for (i = 0; i < P->size1; ++i)
825 for (j = 0; j < P->size2; ++j)
827 sum += gsl_matrix_get (result, i, j)
828 * gsl_matrix_get (result, i, j);
832 gsl_vector_set (rssq, i, sqrt (sum));
835 for (i = 0; i < P->size1; ++i)
837 for (j = 0; j < P->size2; ++j)
839 double l = gsl_matrix_get (result, i, j);
840 double r = gsl_vector_get (rssq, i);
841 gsl_matrix_set (P, i, j, pow (fabs (l / r), cf->promax_power + 1) * r / l);
845 gsl_vector_free (rssq);
847 gsl_linalg_matmult_mod (result,
848 GSL_LINALG_MOD_TRANSPOSE,
853 gsl_linalg_LU_decomp (mm1, perm, &signum);
854 gsl_linalg_LU_invert (mm1, perm, mm2);
856 gsl_linalg_matmult_mod (mm2, GSL_LINALG_MOD_NONE,
857 result, GSL_LINALG_MOD_TRANSPOSE,
860 gsl_linalg_matmult_mod (mp1, GSL_LINALG_MOD_NONE,
861 P, GSL_LINALG_MOD_NONE,
864 D = diag_rcp_sqrt (L);
865 Q = gsl_matrix_calloc (unrot->size2, unrot->size2);
867 gsl_linalg_matmult_mod (L, GSL_LINALG_MOD_NONE,
868 D, GSL_LINALG_MOD_NONE,
871 gsl_matrix *QQinv = gsl_matrix_calloc (unrot->size2, unrot->size2);
873 gsl_linalg_matmult_mod (Q, GSL_LINALG_MOD_TRANSPOSE,
874 Q, GSL_LINALG_MOD_NONE,
877 gsl_linalg_cholesky_decomp (QQinv);
878 gsl_linalg_cholesky_invert (QQinv);
881 gsl_matrix *C = diag_rcp_inv_sqrt (QQinv);
882 gsl_matrix *Cinv = clone_matrix (C);
884 gsl_linalg_cholesky_decomp (Cinv);
885 gsl_linalg_cholesky_invert (Cinv);
888 gsl_linalg_matmult_mod (result, GSL_LINALG_MOD_NONE,
889 Q, GSL_LINALG_MOD_NONE,
892 gsl_linalg_matmult_mod (pm1, GSL_LINALG_MOD_NONE,
893 Cinv, GSL_LINALG_MOD_NONE,
897 gsl_linalg_matmult_mod (C, GSL_LINALG_MOD_NONE,
898 QQinv, GSL_LINALG_MOD_NONE,
901 gsl_linalg_matmult_mod (mm1, GSL_LINALG_MOD_NONE,
902 C, GSL_LINALG_MOD_TRANSPOSE,
903 factor_correlation_matrix);
905 gsl_linalg_matmult_mod (pattern_matrix, GSL_LINALG_MOD_NONE,
906 factor_correlation_matrix, GSL_LINALG_MOD_NONE,
909 gsl_matrix_memcpy (result, pm1);
912 gsl_matrix_free (QQinv);
914 gsl_matrix_free (Cinv);
921 gsl_permutation_free (perm);
923 gsl_matrix_free (mm1);
924 gsl_matrix_free (mm2);
925 gsl_matrix_free (mp1);
926 gsl_matrix_free (pm1);
930 /* reflect negative sums and populate the rotated loadings vector*/
931 for (i = 0 ; i < result->size2; ++i)
935 for (j = 0 ; j < result->size1; ++j)
937 double s = gsl_matrix_get (result, j, i);
942 gsl_vector_set (rotated_loadings, i, ssq);
945 for (j = 0 ; j < result->size1; ++j)
947 double *lambda = gsl_matrix_ptr (result, j, i);
955 Get an approximation for the factor matrix into FACTORS, and the communalities into COMMUNALITIES.
956 R is the matrix to be analysed.
957 WS is a pointer to a structure which must have been initialised with factor_matrix_workspace_init.
960 iterate_factor_matrix (const gsl_matrix *r, gsl_vector *communalities, gsl_matrix *factors,
961 struct factor_matrix_workspace *ws)
966 assert (r->size1 == r->size2);
967 assert (r->size1 == communalities->size);
969 assert (factors->size1 == r->size1);
970 assert (factors->size2 == ws->n_factors);
972 gsl_matrix_memcpy (ws->r, r);
974 /* Apply Communalities to diagonal of correlation matrix */
975 for (i = 0 ; i < communalities->size ; ++i)
977 double *x = gsl_matrix_ptr (ws->r, i, i);
978 *x = gsl_vector_get (communalities, i);
981 gsl_eigen_symmv (ws->r, ws->eval, ws->evec, ws->eigen_ws);
983 mv = gsl_matrix_submatrix (ws->evec, 0, 0, ws->evec->size1, ws->n_factors);
985 /* Gamma is the diagonal matrix containing the absolute values of the eigenvalues */
986 for (i = 0 ; i < ws->n_factors ; ++i)
988 double *ptr = gsl_matrix_ptr (ws->gamma, i, i);
989 *ptr = fabs (gsl_vector_get (ws->eval, i));
992 /* Take the square root of gamma */
993 gsl_linalg_cholesky_decomp (ws->gamma);
995 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, &mv.matrix, ws->gamma, 0.0, factors);
997 for (i = 0 ; i < r->size1 ; ++i)
999 double h = the_communality (ws->evec, ws->eval, i, ws->n_factors);
1000 gsl_vector_set (communalities, i, h);
1006 static bool run_factor (struct dataset *ds, const struct cmd_factor *factor);
1008 static void do_factor_by_matrix (const struct cmd_factor *factor, struct idata *idata);
1013 cmd_factor (struct lexer *lexer, struct dataset *ds)
1015 struct dictionary *dict = NULL;
1016 int n_iterations = 25;
1017 struct cmd_factor factor;
1020 factor.method = METHOD_CORR;
1021 factor.missing_type = MISS_LISTWISE;
1022 factor.exclude = MV_ANY;
1023 factor.print = PRINT_INITIAL | PRINT_EXTRACTION | PRINT_ROTATION;
1024 factor.extraction = EXTRACTION_PC;
1025 factor.n_factors = 0;
1026 factor.min_eigen = SYSMIS;
1027 factor.extraction_iterations = 25;
1028 factor.rotation_iterations = 25;
1029 factor.econverge = 0.001;
1032 factor.sort = false;
1034 factor.rotation = ROT_VARIMAX;
1037 factor.rconverge = 0.0001;
1039 lex_match (lexer, T_SLASH);
1041 struct matrix_reader *mr = NULL;
1042 struct casereader *matrix_reader = NULL;
1044 if (lex_match_id (lexer, "VARIABLES"))
1046 lex_match (lexer, T_EQUALS);
1047 dict = dataset_dict (ds);
1048 factor.wv = dict_get_weight (dict);
1050 if (!parse_variables_const (lexer, dict, &factor.vars, &factor.n_vars,
1051 PV_NO_DUPLICATE | PV_NUMERIC))
1054 else if (lex_match_id (lexer, "MATRIX"))
1056 lex_match (lexer, T_EQUALS);
1057 if (! lex_force_match_id (lexer, "IN"))
1059 if (!lex_force_match (lexer, T_LPAREN))
1063 if (lex_match_id (lexer, "CORR"))
1066 else if (lex_match_id (lexer, "COV"))
1071 lex_error (lexer, _("Matrix input for %s must be either COV or CORR"), "FACTOR");
1074 if (! lex_force_match (lexer, T_EQUALS))
1076 if (lex_match (lexer, T_ASTERISK))
1078 dict = dataset_dict (ds);
1079 matrix_reader = casereader_clone (dataset_source (ds));
1083 struct file_handle *fh = fh_parse (lexer, FH_REF_FILE, NULL);
1088 = any_reader_open_and_decode (fh, NULL, &dict, NULL);
1090 if (! (matrix_reader && dict))
1096 if (! lex_force_match (lexer, T_RPAREN))
1099 mr = create_matrix_reader_from_case_reader (dict, matrix_reader,
1100 &factor.vars, &factor.n_vars);
1107 while (lex_token (lexer) != T_ENDCMD)
1109 lex_match (lexer, T_SLASH);
1111 if (lex_match_id (lexer, "ANALYSIS"))
1113 struct const_var_set *vs;
1114 const struct variable **vars;
1118 lex_match (lexer, T_EQUALS);
1120 vs = const_var_set_create_from_array (factor.vars, factor.n_vars);
1121 ok = parse_const_var_set_vars (lexer, vs, &vars, &n_vars,
1122 PV_NO_DUPLICATE | PV_NUMERIC);
1123 const_var_set_destroy (vs);
1130 factor.n_vars = n_vars;
1132 else if (lex_match_id (lexer, "PLOT"))
1134 lex_match (lexer, T_EQUALS);
1135 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1137 if (lex_match_id (lexer, "EIGEN"))
1139 factor.plot |= PLOT_SCREE;
1141 #if FACTOR_FULLY_IMPLEMENTED
1142 else if (lex_match_id (lexer, "ROTATION"))
1148 lex_error (lexer, NULL);
1153 else if (lex_match_id (lexer, "METHOD"))
1155 lex_match (lexer, T_EQUALS);
1156 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1158 if (lex_match_id (lexer, "COVARIANCE"))
1160 factor.method = METHOD_COV;
1162 else if (lex_match_id (lexer, "CORRELATION"))
1164 factor.method = METHOD_CORR;
1168 lex_error (lexer, NULL);
1173 else if (lex_match_id (lexer, "ROTATION"))
1175 lex_match (lexer, T_EQUALS);
1176 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1178 /* VARIMAX and DEFAULT are defaults */
1179 if (lex_match_id (lexer, "VARIMAX") || lex_match_id (lexer, "DEFAULT"))
1181 factor.rotation = ROT_VARIMAX;
1183 else if (lex_match_id (lexer, "EQUAMAX"))
1185 factor.rotation = ROT_EQUAMAX;
1187 else if (lex_match_id (lexer, "QUARTIMAX"))
1189 factor.rotation = ROT_QUARTIMAX;
1191 else if (lex_match_id (lexer, "PROMAX"))
1193 factor.promax_power = 5;
1194 if (lex_match (lexer, T_LPAREN)
1195 && lex_force_int (lexer))
1197 factor.promax_power = lex_integer (lexer);
1199 if (! lex_force_match (lexer, T_RPAREN))
1202 factor.rotation = ROT_PROMAX;
1204 else if (lex_match_id (lexer, "NOROTATE"))
1206 factor.rotation = ROT_NONE;
1210 lex_error (lexer, NULL);
1214 factor.rotation_iterations = n_iterations;
1216 else if (lex_match_id (lexer, "CRITERIA"))
1218 lex_match (lexer, T_EQUALS);
1219 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1221 if (lex_match_id (lexer, "FACTORS"))
1223 if ( lex_force_match (lexer, T_LPAREN)
1224 && lex_force_int (lexer))
1226 factor.n_factors = lex_integer (lexer);
1228 if (! lex_force_match (lexer, T_RPAREN))
1232 else if (lex_match_id (lexer, "MINEIGEN"))
1234 if ( lex_force_match (lexer, T_LPAREN)
1235 && lex_force_num (lexer))
1237 factor.min_eigen = lex_number (lexer);
1239 if (! lex_force_match (lexer, T_RPAREN))
1243 else if (lex_match_id (lexer, "ECONVERGE"))
1245 if ( lex_force_match (lexer, T_LPAREN)
1246 && lex_force_num (lexer))
1248 factor.econverge = lex_number (lexer);
1250 if (! lex_force_match (lexer, T_RPAREN))
1254 else if (lex_match_id (lexer, "RCONVERGE"))
1256 if (lex_force_match (lexer, T_LPAREN)
1257 && lex_force_num (lexer))
1259 factor.rconverge = lex_number (lexer);
1261 if (! lex_force_match (lexer, T_RPAREN))
1265 else if (lex_match_id (lexer, "ITERATE"))
1267 if ( lex_force_match (lexer, T_LPAREN)
1268 && lex_force_int (lexer))
1270 n_iterations = lex_integer (lexer);
1272 if (! lex_force_match (lexer, T_RPAREN))
1276 else if (lex_match_id (lexer, "DEFAULT"))
1278 factor.n_factors = 0;
1279 factor.min_eigen = 1;
1284 lex_error (lexer, NULL);
1289 else if (lex_match_id (lexer, "EXTRACTION"))
1291 lex_match (lexer, T_EQUALS);
1292 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1294 if (lex_match_id (lexer, "PAF"))
1296 factor.extraction = EXTRACTION_PAF;
1298 else if (lex_match_id (lexer, "PC"))
1300 factor.extraction = EXTRACTION_PC;
1302 else if (lex_match_id (lexer, "PA1"))
1304 factor.extraction = EXTRACTION_PC;
1306 else if (lex_match_id (lexer, "DEFAULT"))
1308 factor.extraction = EXTRACTION_PC;
1312 lex_error (lexer, NULL);
1316 factor.extraction_iterations = n_iterations;
1318 else if (lex_match_id (lexer, "FORMAT"))
1320 lex_match (lexer, T_EQUALS);
1321 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1323 if (lex_match_id (lexer, "SORT"))
1327 else if (lex_match_id (lexer, "BLANK"))
1329 if ( lex_force_match (lexer, T_LPAREN)
1330 && lex_force_num (lexer))
1332 factor.blank = lex_number (lexer);
1334 if (! lex_force_match (lexer, T_RPAREN))
1338 else if (lex_match_id (lexer, "DEFAULT"))
1341 factor.sort = false;
1345 lex_error (lexer, NULL);
1350 else if (lex_match_id (lexer, "PRINT"))
1353 lex_match (lexer, T_EQUALS);
1354 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1356 if (lex_match_id (lexer, "UNIVARIATE"))
1358 factor.print |= PRINT_UNIVARIATE;
1360 else if (lex_match_id (lexer, "DET"))
1362 factor.print |= PRINT_DETERMINANT;
1364 #if FACTOR_FULLY_IMPLEMENTED
1365 else if (lex_match_id (lexer, "INV"))
1368 else if (lex_match_id (lexer, "AIC"))
1372 else if (lex_match_id (lexer, "SIG"))
1374 factor.print |= PRINT_SIG;
1376 else if (lex_match_id (lexer, "CORRELATION"))
1378 factor.print |= PRINT_CORRELATION;
1380 else if (lex_match_id (lexer, "COVARIANCE"))
1382 factor.print |= PRINT_COVARIANCE;
1384 else if (lex_match_id (lexer, "ROTATION"))
1386 factor.print |= PRINT_ROTATION;
1388 else if (lex_match_id (lexer, "EXTRACTION"))
1390 factor.print |= PRINT_EXTRACTION;
1392 else if (lex_match_id (lexer, "INITIAL"))
1394 factor.print |= PRINT_INITIAL;
1396 else if (lex_match_id (lexer, "KMO"))
1398 factor.print |= PRINT_KMO;
1400 #if FACTOR_FULLY_IMPLEMENTED
1401 else if (lex_match_id (lexer, "REPR"))
1404 else if (lex_match_id (lexer, "FSCORE"))
1408 else if (lex_match (lexer, T_ALL))
1410 factor.print = 0xFFFF;
1412 else if (lex_match_id (lexer, "DEFAULT"))
1414 factor.print |= PRINT_INITIAL ;
1415 factor.print |= PRINT_EXTRACTION ;
1416 factor.print |= PRINT_ROTATION ;
1420 lex_error (lexer, NULL);
1425 else if (lex_match_id (lexer, "MISSING"))
1427 lex_match (lexer, T_EQUALS);
1428 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1430 if (lex_match_id (lexer, "INCLUDE"))
1432 factor.exclude = MV_SYSTEM;
1434 else if (lex_match_id (lexer, "EXCLUDE"))
1436 factor.exclude = MV_ANY;
1438 else if (lex_match_id (lexer, "LISTWISE"))
1440 factor.missing_type = MISS_LISTWISE;
1442 else if (lex_match_id (lexer, "PAIRWISE"))
1444 factor.missing_type = MISS_PAIRWISE;
1446 else if (lex_match_id (lexer, "MEANSUB"))
1448 factor.missing_type = MISS_MEANSUB;
1452 lex_error (lexer, NULL);
1459 lex_error (lexer, NULL);
1464 if ( factor.rotation == ROT_NONE )
1465 factor.print &= ~PRINT_ROTATION;
1467 if (factor.n_vars < 2)
1468 msg (MW, _("Factor analysis on a single variable is not useful."));
1472 struct idata *id = idata_alloc (factor.n_vars);
1474 while (next_matrix_from_reader (&id->mm, mr,
1475 factor.vars, factor.n_vars))
1477 do_factor_by_matrix (&factor, id);
1479 gsl_matrix_free (id->mm.corr);
1481 gsl_matrix_free (id->mm.cov);
1488 if ( ! run_factor (ds, &factor))
1492 destroy_matrix_reader (mr);
1497 destroy_matrix_reader (mr);
1502 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
1506 run_factor (struct dataset *ds, const struct cmd_factor *factor)
1508 struct dictionary *dict = dataset_dict (ds);
1510 struct casereader *group;
1512 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
1514 while (casegrouper_get_next_group (grouper, &group))
1516 if ( factor->missing_type == MISS_LISTWISE )
1517 group = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
1520 do_factor (factor, group);
1523 ok = casegrouper_destroy (grouper);
1524 ok = proc_commit (ds) && ok;
1530 /* Return the communality of variable N, calculated to N_FACTORS */
1532 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
1539 assert (n < eval->size);
1540 assert (n < evec->size1);
1541 assert (n_factors <= eval->size);
1543 for (i = 0 ; i < n_factors; ++i)
1545 double evali = fabs (gsl_vector_get (eval, i));
1547 double eveci = gsl_matrix_get (evec, n, i);
1549 comm += pow2 (eveci) * evali;
1555 /* Return the communality of variable N, calculated to N_FACTORS */
1557 communality (struct idata *idata, int n, int n_factors)
1559 return the_communality (idata->evec, idata->eval, n, n_factors);
1564 show_scree (const struct cmd_factor *f, struct idata *idata)
1569 if ( !(f->plot & PLOT_SCREE) )
1573 label = f->extraction == EXTRACTION_PC ? _("Component Number") : _("Factor Number");
1575 s = scree_create (idata->eval, label);
1581 show_communalities (const struct cmd_factor * factor,
1582 const gsl_vector *initial, const gsl_vector *extracted)
1586 const int heading_columns = 1;
1587 int nc = heading_columns;
1588 const int heading_rows = 1;
1589 const int nr = heading_rows + factor->n_vars;
1590 struct tab_table *t;
1592 if (factor->print & PRINT_EXTRACTION)
1595 if (factor->print & PRINT_INITIAL)
1598 /* No point having a table with only headings */
1602 t = tab_create (nc, nr);
1604 tab_title (t, _("Communalities"));
1606 tab_headers (t, heading_columns, 0, heading_rows, 0);
1609 if (factor->print & PRINT_INITIAL)
1610 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
1612 if (factor->print & PRINT_EXTRACTION)
1613 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
1615 /* Outline the box */
1622 /* Vertical lines */
1629 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1630 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1632 for (i = 0 ; i < factor->n_vars; ++i)
1635 tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
1637 if (factor->print & PRINT_INITIAL)
1638 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL, RC_OTHER);
1640 if (factor->print & PRINT_EXTRACTION)
1641 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL, RC_OTHER);
1649 show_factor_matrix (const struct cmd_factor *factor, struct idata *idata, const char *title, const gsl_matrix *fm)
1653 const int n_factors = idata->n_extractions;
1655 const int heading_columns = 1;
1656 const int heading_rows = 2;
1657 const int nr = heading_rows + factor->n_vars;
1658 const int nc = heading_columns + n_factors;
1659 gsl_permutation *perm;
1661 struct tab_table *t = tab_create (nc, nr);
1664 if ( factor->extraction == EXTRACTION_PC )
1665 tab_title (t, _("Component Matrix"));
1667 tab_title (t, _("Factor Matrix"));
1670 tab_title (t, "%s", title);
1672 tab_headers (t, heading_columns, 0, heading_rows, 0);
1674 if ( factor->extraction == EXTRACTION_PC )
1678 TAB_CENTER | TAT_TITLE, _("Component"));
1683 TAB_CENTER | TAT_TITLE, _("Factor"));
1686 tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1689 /* Outline the box */
1696 /* Vertical lines */
1703 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1704 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1707 /* Initialise to the identity permutation */
1708 perm = gsl_permutation_calloc (factor->n_vars);
1711 sort_matrix_indirect (fm, perm);
1713 for (i = 0 ; i < n_factors; ++i)
1715 tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1718 for (i = 0 ; i < factor->n_vars; ++i)
1721 const int matrix_row = perm->data[i];
1722 tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1724 for (j = 0 ; j < n_factors; ++j)
1726 double x = gsl_matrix_get (fm, matrix_row, j);
1728 if ( fabs (x) < factor->blank)
1731 tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL, RC_OTHER);
1735 gsl_permutation_free (perm);
1742 show_explained_variance (const struct cmd_factor * factor, struct idata *idata,
1743 const gsl_vector *initial_eigenvalues,
1744 const gsl_vector *extracted_eigenvalues,
1745 const gsl_vector *rotated_loadings)
1749 const int heading_columns = 1;
1750 const int heading_rows = 2;
1751 const int nr = heading_rows + factor->n_vars;
1753 struct tab_table *t ;
1755 double i_total = 0.0;
1758 double e_total = 0.0;
1763 int nc = heading_columns;
1765 if (factor->print & PRINT_EXTRACTION)
1768 if (factor->print & PRINT_INITIAL)
1771 if (factor->print & PRINT_ROTATION)
1773 nc += factor->rotation == ROT_PROMAX ? 1 : 3;
1776 /* No point having a table with only headings */
1777 if ( nc <= heading_columns)
1780 t = tab_create (nc, nr);
1782 tab_title (t, _("Total Variance Explained"));
1784 tab_headers (t, heading_columns, 0, heading_rows, 0);
1786 /* Outline the box */
1793 /* Vertical lines */
1800 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1801 tab_hline (t, TAL_1, 1, nc - 1, 1);
1803 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1806 if ( factor->extraction == EXTRACTION_PC)
1807 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1809 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1812 if (factor->print & PRINT_INITIAL)
1814 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1818 if (factor->print & PRINT_EXTRACTION)
1820 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1824 if (factor->print & PRINT_ROTATION)
1826 const int width = factor->rotation == ROT_PROMAX ? 0 : 2;
1827 tab_joint_text (t, c, 0, c + width, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1831 for (i = 0; i < (nc - heading_columns + 2) / 3 ; ++i)
1833 tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1835 tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1837 if (i == 2 && factor->rotation == ROT_PROMAX)
1840 /* xgettext:no-c-format */
1841 tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1842 tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1845 for (i = 0 ; i < initial_eigenvalues->size; ++i)
1846 i_total += gsl_vector_get (initial_eigenvalues, i);
1848 if ( factor->extraction == EXTRACTION_PAF)
1850 e_total = factor->n_vars;
1857 for (i = 0 ; i < factor->n_vars; ++i)
1859 const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1860 double i_percent = 100.0 * i_lambda / i_total ;
1862 const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1863 double e_percent = 100.0 * e_lambda / e_total ;
1867 tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%zu"), i + 1);
1872 /* Initial Eigenvalues */
1873 if (factor->print & PRINT_INITIAL)
1875 tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL, RC_OTHER);
1876 tab_double (t, c++, i + heading_rows, 0, i_percent, NULL, RC_OTHER);
1877 tab_double (t, c++, i + heading_rows, 0, i_cum, NULL, RC_OTHER);
1881 if (factor->print & PRINT_EXTRACTION)
1883 if (i < idata->n_extractions)
1885 /* Sums of squared loadings */
1886 tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL, RC_OTHER);
1887 tab_double (t, c++, i + heading_rows, 0, e_percent, NULL, RC_OTHER);
1888 tab_double (t, c++, i + heading_rows, 0, e_cum, NULL, RC_OTHER);
1892 if (rotated_loadings != NULL)
1894 const double r_lambda = gsl_vector_get (rotated_loadings, i);
1895 double r_percent = 100.0 * r_lambda / e_total ;
1897 if (factor->print & PRINT_ROTATION)
1899 if (i < idata->n_extractions)
1902 tab_double (t, c++, i + heading_rows, 0, r_lambda, NULL, RC_OTHER);
1903 if (factor->rotation != ROT_PROMAX)
1905 tab_double (t, c++, i + heading_rows, 0, r_percent, NULL, RC_OTHER);
1906 tab_double (t, c++, i + heading_rows, 0, r_cum, NULL, RC_OTHER);
1918 show_factor_correlation (const struct cmd_factor * factor, const gsl_matrix *fcm)
1921 const int heading_columns = 1;
1922 const int heading_rows = 1;
1923 const int nr = heading_rows + fcm->size2;
1924 const int nc = heading_columns + fcm->size1;
1925 struct tab_table *t = tab_create (nc, nr);
1927 tab_title (t, _("Factor Correlation Matrix"));
1929 tab_headers (t, heading_columns, 0, heading_rows, 0);
1931 /* Outline the box */
1938 /* Vertical lines */
1945 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1946 tab_hline (t, TAL_1, 1, nc - 1, 1);
1948 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1951 if ( factor->extraction == EXTRACTION_PC)
1952 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Component"));
1954 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Factor"));
1956 for (i = 0 ; i < fcm->size1; ++i)
1958 tab_text_format (t, heading_columns + i, 0, TAB_CENTER | TAT_TITLE, _("%zu"), i + 1);
1961 for (i = 0 ; i < fcm->size2; ++i)
1963 tab_text_format (t, 0, heading_rows + i, TAB_CENTER | TAT_TITLE, _("%zu"), i + 1);
1967 for (i = 0 ; i < fcm->size1; ++i)
1969 for (j = 0 ; j < fcm->size2; ++j)
1970 tab_double (t, heading_columns + j, heading_rows + i, 0,
1971 gsl_matrix_get (fcm, i, j), NULL, RC_OTHER);
1979 show_correlation_matrix (const struct cmd_factor *factor, const struct idata *idata)
1981 struct tab_table *t ;
1983 int y_pos_corr = -1;
1985 int suffix_rows = 0;
1987 const int heading_rows = 1;
1988 const int heading_columns = 2;
1990 int nc = heading_columns ;
1991 int nr = heading_rows ;
1992 int n_data_sets = 0;
1994 if (factor->print & PRINT_CORRELATION)
1996 y_pos_corr = n_data_sets;
1998 nc = heading_columns + factor->n_vars;
2001 if (factor->print & PRINT_SIG)
2003 y_pos_sig = n_data_sets;
2005 nc = heading_columns + factor->n_vars;
2008 nr += n_data_sets * factor->n_vars;
2010 if (factor->print & PRINT_DETERMINANT)
2013 /* If the table would contain only headings, don't bother rendering it */
2014 if (nr <= heading_rows && suffix_rows == 0)
2017 t = tab_create (nc, nr + suffix_rows);
2019 tab_title (t, _("Correlation Matrix"));
2021 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
2023 if (nr > heading_rows)
2025 tab_headers (t, heading_columns, 0, heading_rows, 0);
2027 tab_vline (t, TAL_2, 2, 0, nr - 1);
2029 /* Outline the box */
2036 /* Vertical lines */
2044 for (i = 0; i < factor->n_vars; ++i)
2045 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
2048 for (i = 0 ; i < n_data_sets; ++i)
2050 int y = heading_rows + i * factor->n_vars;
2052 for (v = 0; v < factor->n_vars; ++v)
2053 tab_text (t, 1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
2055 tab_hline (t, TAL_1, 0, nc - 1, y);
2058 if (factor->print & PRINT_CORRELATION)
2060 const double y = heading_rows + y_pos_corr;
2061 tab_text (t, 0, y, TAT_TITLE, _("Correlations"));
2063 for (i = 0; i < factor->n_vars; ++i)
2065 for (j = 0; j < factor->n_vars; ++j)
2066 tab_double (t, heading_columns + j, y + i, 0, gsl_matrix_get (idata->mm.corr, i, j), NULL, RC_OTHER);
2070 if (factor->print & PRINT_SIG)
2072 const double y = heading_rows + y_pos_sig * factor->n_vars;
2073 tab_text (t, 0, y, TAT_TITLE, _("Sig. (1-tailed)"));
2075 for (i = 0; i < factor->n_vars; ++i)
2077 for (j = 0; j < factor->n_vars; ++j)
2079 double rho = gsl_matrix_get (idata->mm.corr, i, j);
2080 double w = gsl_matrix_get (idata->mm.n, i, j);
2085 tab_double (t, heading_columns + j, y + i, 0, significance_of_correlation (rho, w), NULL, RC_PVALUE);
2091 if (factor->print & PRINT_DETERMINANT)
2093 tab_text (t, 0, nr, TAB_LEFT | TAT_TITLE, _("Determinant"));
2095 tab_double (t, 1, nr, 0, idata->detR, NULL, RC_OTHER);
2102 show_covariance_matrix (const struct cmd_factor *factor, const struct idata *idata)
2104 struct tab_table *t ;
2106 int y_pos_corr = -1;
2107 int suffix_rows = 0;
2109 const int heading_rows = 1;
2110 const int heading_columns = 1;
2112 int nc = heading_columns ;
2113 int nr = heading_rows ;
2114 int n_data_sets = 0;
2116 if (factor->print & PRINT_COVARIANCE)
2118 y_pos_corr = n_data_sets;
2120 nc = heading_columns + factor->n_vars;
2123 nr += n_data_sets * factor->n_vars;
2125 /* If the table would contain only headings, don't bother rendering it */
2126 if (nr <= heading_rows && suffix_rows == 0)
2129 t = tab_create (nc, nr + suffix_rows);
2131 tab_title (t, _("Covariance Matrix"));
2133 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
2135 if (nr > heading_rows)
2137 tab_headers (t, heading_columns, 0, heading_rows, 0);
2139 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
2141 /* Outline the box */
2148 /* Vertical lines */
2156 for (i = 0; i < factor->n_vars; ++i)
2157 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
2160 for (i = 0 ; i < n_data_sets; ++i)
2162 int y = heading_rows + i * factor->n_vars;
2164 for (v = 0; v < factor->n_vars; ++v)
2165 tab_text (t, heading_columns -1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
2167 tab_hline (t, TAL_1, 0, nc - 1, y);
2170 if (factor->print & PRINT_COVARIANCE)
2172 const double y = heading_rows + y_pos_corr;
2174 for (i = 0; i < factor->n_vars; ++i)
2176 for (j = 0; j < factor->n_vars; ++j)
2177 tab_double (t, heading_columns + j, y + i, 0, gsl_matrix_get (idata->mm.cov, i, j), NULL, RC_OTHER);
2187 do_factor (const struct cmd_factor *factor, struct casereader *r)
2190 struct idata *idata = idata_alloc (factor->n_vars);
2192 idata->cvm = covariance_1pass_create (factor->n_vars, factor->vars,
2193 factor->wv, factor->exclude);
2195 for ( ; (c = casereader_read (r) ); case_unref (c))
2197 covariance_accumulate (idata->cvm, c);
2200 idata->mm.cov = covariance_calculate (idata->cvm);
2202 if (idata->mm.cov == NULL)
2204 msg (MW, _("The dataset contains no complete observations. No analysis will be performed."));
2205 covariance_destroy (idata->cvm);
2209 idata->mm.var_matrix = covariance_moments (idata->cvm, MOMENT_VARIANCE);
2210 idata->mm.mean_matrix = covariance_moments (idata->cvm, MOMENT_MEAN);
2211 idata->mm.n = covariance_moments (idata->cvm, MOMENT_NONE);
2213 do_factor_by_matrix (factor, idata);
2216 gsl_matrix_free (idata->mm.corr);
2217 gsl_matrix_free (idata->mm.cov);
2220 casereader_destroy (r);
2224 do_factor_by_matrix (const struct cmd_factor *factor, struct idata *idata)
2226 if (idata->mm.cov && !idata->mm.corr)
2227 idata->mm.corr = correlation_from_covariance (idata->mm.cov, idata->mm.var_matrix);
2228 if (idata->mm.corr && !idata->mm.cov)
2229 idata->mm.cov = covariance_from_correlation (idata->mm.corr, idata->mm.var_matrix);
2230 if (factor->method == METHOD_CORR)
2231 idata->analysis_matrix = idata->mm.corr;
2233 idata->analysis_matrix = idata->mm.cov;
2235 if (factor->print & PRINT_DETERMINANT
2236 || factor->print & PRINT_KMO)
2240 const int size = idata->mm.corr->size1;
2241 gsl_permutation *p = gsl_permutation_calloc (size);
2242 gsl_matrix *tmp = gsl_matrix_calloc (size, size);
2243 gsl_matrix_memcpy (tmp, idata->mm.corr);
2245 gsl_linalg_LU_decomp (tmp, p, &sign);
2246 idata->detR = gsl_linalg_LU_det (tmp, sign);
2247 gsl_permutation_free (p);
2248 gsl_matrix_free (tmp);
2251 if ( factor->print & PRINT_UNIVARIATE)
2253 const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
2257 const int heading_columns = 1;
2258 const int heading_rows = 1;
2260 const int nr = heading_rows + factor->n_vars;
2262 struct tab_table *t = tab_create (nc, nr);
2263 tab_set_format (t, RC_WEIGHT, wfmt);
2264 tab_title (t, _("Descriptive Statistics"));
2266 tab_headers (t, heading_columns, 0, heading_rows, 0);
2268 /* Outline the box */
2275 /* Vertical lines */
2282 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
2283 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
2285 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
2286 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
2287 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
2289 for (i = 0 ; i < factor->n_vars; ++i)
2291 const struct variable *v = factor->vars[i];
2292 tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
2294 tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (idata->mm.mean_matrix, i, i), NULL, RC_OTHER);
2295 tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (idata->mm.var_matrix, i, i)), NULL, RC_OTHER);
2296 tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (idata->mm.n, i, i), NULL, RC_WEIGHT);
2302 if (factor->print & PRINT_KMO)
2305 double sum_ssq_r = 0;
2306 double sum_ssq_a = 0;
2308 double df = factor->n_vars * (factor->n_vars - 1) / 2;
2315 const int heading_columns = 2;
2316 const int heading_rows = 0;
2318 const int nr = heading_rows + 4;
2319 const int nc = heading_columns + 1;
2323 struct tab_table *t = tab_create (nc, nr);
2324 tab_title (t, _("KMO and Bartlett's Test"));
2326 x = clone_matrix (idata->mm.corr);
2327 gsl_linalg_cholesky_decomp (x);
2328 gsl_linalg_cholesky_invert (x);
2332 for (i = 0; i < x->size1; ++i)
2334 sum_ssq_r += ssq_od_n (x, i);
2335 sum_ssq_a += ssq_od_n (a, i);
2338 gsl_matrix_free (a);
2339 gsl_matrix_free (x);
2341 tab_headers (t, heading_columns, 0, heading_rows, 0);
2343 /* Outline the box */
2350 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
2352 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Kaiser-Meyer-Olkin Measure of Sampling Adequacy"));
2354 tab_double (t, 2, 0, 0, sum_ssq_r / (sum_ssq_r + sum_ssq_a), NULL, RC_OTHER);
2356 tab_text (t, 0, 1, TAT_TITLE | TAB_LEFT, _("Bartlett's Test of Sphericity"));
2358 tab_text (t, 1, 1, TAT_TITLE, _("Approx. Chi-Square"));
2359 tab_text (t, 1, 2, TAT_TITLE, _("df"));
2360 tab_text (t, 1, 3, TAT_TITLE, _("Sig."));
2363 /* The literature doesn't say what to do for the value of W when
2364 missing values are involved. The best thing I can think of
2365 is to take the mean average. */
2367 for (i = 0; i < idata->mm.n->size1; ++i)
2368 w += gsl_matrix_get (idata->mm.n, i, i);
2369 w /= idata->mm.n->size1;
2371 xsq = w - 1 - (2 * factor->n_vars + 5) / 6.0;
2372 xsq *= -log (idata->detR);
2374 tab_double (t, 2, 1, 0, xsq, NULL, RC_OTHER);
2375 tab_double (t, 2, 2, 0, df, NULL, RC_INTEGER);
2376 tab_double (t, 2, 3, 0, gsl_cdf_chisq_Q (xsq, df), NULL, RC_PVALUE);
2382 show_correlation_matrix (factor, idata);
2383 show_covariance_matrix (factor, idata);
2385 covariance_destroy (idata->cvm);
2388 gsl_matrix *am = matrix_dup (idata->analysis_matrix);
2389 gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
2391 gsl_eigen_symmv (am, idata->eval, idata->evec, workspace);
2393 gsl_eigen_symmv_free (workspace);
2394 gsl_matrix_free (am);
2397 gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
2399 idata->n_extractions = n_extracted_factors (factor, idata);
2401 if (idata->n_extractions == 0)
2403 msg (MW, _("The %s criteria result in zero factors extracted. Therefore no analysis will be performed."), "FACTOR");
2407 if (idata->n_extractions > factor->n_vars)
2410 _("The %s criteria result in more factors than variables, which is not meaningful. No analysis will be performed."),
2416 gsl_matrix *rotated_factors = NULL;
2417 gsl_matrix *pattern_matrix = NULL;
2418 gsl_matrix *fcm = NULL;
2419 gsl_vector *rotated_loadings = NULL;
2421 const gsl_vector *extracted_eigenvalues = NULL;
2422 gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
2423 gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
2425 struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, idata->n_extractions);
2426 gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
2428 if ( factor->extraction == EXTRACTION_PAF)
2430 gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
2431 struct smr_workspace *ws = ws_create (idata->analysis_matrix);
2433 for (i = 0 ; i < factor->n_vars ; ++i)
2435 double r2 = squared_multiple_correlation (idata->analysis_matrix, i, ws);
2437 gsl_vector_set (idata->msr, i, r2);
2441 gsl_vector_memcpy (initial_communalities, idata->msr);
2443 for (i = 0; i < factor->extraction_iterations; ++i)
2446 gsl_vector_memcpy (diff, idata->msr);
2448 iterate_factor_matrix (idata->analysis_matrix, idata->msr, factor_matrix, fmw);
2450 gsl_vector_sub (diff, idata->msr);
2452 gsl_vector_minmax (diff, &min, &max);
2454 if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
2457 gsl_vector_free (diff);
2461 gsl_vector_memcpy (extracted_communalities, idata->msr);
2462 extracted_eigenvalues = fmw->eval;
2464 else if (factor->extraction == EXTRACTION_PC)
2466 for (i = 0; i < factor->n_vars; ++i)
2467 gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
2469 gsl_vector_memcpy (extracted_communalities, initial_communalities);
2471 iterate_factor_matrix (idata->analysis_matrix, extracted_communalities, factor_matrix, fmw);
2474 extracted_eigenvalues = idata->eval;
2478 show_communalities (factor, initial_communalities, extracted_communalities);
2481 if ( factor->rotation != ROT_NONE)
2483 rotated_factors = gsl_matrix_calloc (factor_matrix->size1, factor_matrix->size2);
2484 rotated_loadings = gsl_vector_calloc (factor_matrix->size2);
2485 if (factor->rotation == ROT_PROMAX)
2487 pattern_matrix = gsl_matrix_calloc (factor_matrix->size1, factor_matrix->size2);
2488 fcm = gsl_matrix_calloc (factor_matrix->size2, factor_matrix->size2);
2492 rotate (factor, factor_matrix, extracted_communalities, rotated_factors, rotated_loadings, pattern_matrix, fcm);
2495 show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues, rotated_loadings);
2497 factor_matrix_workspace_free (fmw);
2499 show_scree (factor, idata);
2501 show_factor_matrix (factor, idata,
2502 factor->extraction == EXTRACTION_PC ? _("Component Matrix") : _("Factor Matrix"),
2505 if ( factor->rotation == ROT_PROMAX)
2507 show_factor_matrix (factor, idata, _("Pattern Matrix"), pattern_matrix);
2508 gsl_matrix_free (pattern_matrix);
2511 if ( factor->rotation != ROT_NONE)
2513 show_factor_matrix (factor, idata,
2514 (factor->rotation == ROT_PROMAX) ? _("Structure Matrix") :
2515 (factor->extraction == EXTRACTION_PC ? _("Rotated Component Matrix") :
2516 _("Rotated Factor Matrix")),
2519 gsl_matrix_free (rotated_factors);
2522 if ( factor->rotation == ROT_PROMAX)
2524 show_factor_correlation (factor, fcm);
2525 gsl_matrix_free (fcm);
2528 gsl_matrix_free (factor_matrix);
2529 gsl_vector_free (rotated_loadings);
2530 gsl_vector_free (initial_communalities);
2531 gsl_vector_free (extracted_communalities);