1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011, 2012, 2014, 2015, 2016 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <gsl/gsl_vector.h>
20 #include <gsl/gsl_linalg.h>
21 #include <gsl/gsl_matrix.h>
22 #include <gsl/gsl_eigen.h>
23 #include <gsl/gsl_blas.h>
24 #include <gsl/gsl_sort_vector.h>
25 #include <gsl/gsl_cdf.h>
27 #include "data/casegrouper.h"
28 #include "data/casereader.h"
29 #include "data/casewriter.h"
30 #include "data/dataset.h"
31 #include "data/dictionary.h"
32 #include "data/format.h"
33 #include "data/subcase.h"
34 #include "language/command.h"
35 #include "language/lexer/lexer.h"
36 #include "language/lexer/value-parser.h"
37 #include "language/lexer/variable-parser.h"
38 #include "libpspp/cast.h"
39 #include "libpspp/message.h"
40 #include "libpspp/misc.h"
41 #include "math/correlation.h"
42 #include "math/covariance.h"
43 #include "math/moments.h"
44 #include "output/chart-item.h"
45 #include "output/charts/scree.h"
46 #include "output/tab.h"
49 #define _(msgid) gettext (msgid)
50 #define N_(msgid) msgid
65 enum extraction_method
74 PLOT_ROTATION = 0x0002
79 PRINT_UNIVARIATE = 0x0001,
80 PRINT_DETERMINANT = 0x0002,
84 PRINT_COVARIANCE = 0x0020,
85 PRINT_CORRELATION = 0x0040,
86 PRINT_ROTATION = 0x0080,
87 PRINT_EXTRACTION = 0x0100,
88 PRINT_INITIAL = 0x0200,
103 typedef void (*rotation_coefficients) (double *x, double *y,
104 double a, double b, double c, double d,
105 const gsl_matrix *loadings );
109 varimax_coefficients (double *x, double *y,
110 double a, double b, double c, double d,
111 const gsl_matrix *loadings )
113 *x = d - 2 * a * b / loadings->size1;
114 *y = c - (a * a - b * b) / loadings->size1;
118 equamax_coefficients (double *x, double *y,
119 double a, double b, double c, double d,
120 const gsl_matrix *loadings )
122 *x = d - loadings->size2 * a * b / loadings->size1;
123 *y = c - loadings->size2 * (a * a - b * b) / (2 * loadings->size1);
127 quartimax_coefficients (double *x, double *y,
128 double a UNUSED, double b UNUSED, double c, double d,
129 const gsl_matrix *loadings UNUSED)
135 static const rotation_coefficients rotation_coeff[] = {
136 varimax_coefficients,
137 equamax_coefficients,
138 quartimax_coefficients,
139 varimax_coefficients /* PROMAX is identical to VARIMAX */
143 /* return diag (C'C) ^ {-0.5} */
145 diag_rcp_sqrt (const gsl_matrix *C)
148 gsl_matrix *d = gsl_matrix_calloc (C->size1, C->size2);
149 gsl_matrix *r = gsl_matrix_calloc (C->size1, C->size2);
151 assert (C->size1 == C->size2);
153 gsl_linalg_matmult_mod (C, GSL_LINALG_MOD_TRANSPOSE,
154 C, GSL_LINALG_MOD_NONE,
157 for (j = 0 ; j < d->size2; ++j)
159 double e = gsl_matrix_get (d, j, j);
161 gsl_matrix_set (r, j, j, e);
171 /* return diag ((C'C)^-1) ^ {-0.5} */
173 diag_rcp_inv_sqrt (const gsl_matrix *CCinv)
176 gsl_matrix *r = gsl_matrix_calloc (CCinv->size1, CCinv->size2);
178 assert (CCinv->size1 == CCinv->size2);
180 for (j = 0 ; j < CCinv->size2; ++j)
182 double e = gsl_matrix_get (CCinv, j, j);
184 gsl_matrix_set (r, j, j, e);
197 const struct variable **vars;
199 const struct variable *wv;
202 enum missing_type missing_type;
203 enum mv_class exclude;
204 enum print_opts print;
205 enum extraction_method extraction;
207 enum rotation_type rotation;
208 int rotation_iterations;
211 /* Extraction Criteria */
215 int extraction_iterations;
226 /* Intermediate values used in calculation */
228 const gsl_matrix *corr ; /* The correlation matrix */
229 gsl_matrix *cov ; /* The covariance matrix */
230 const gsl_matrix *n ; /* Matrix of number of samples */
232 gsl_vector *eval ; /* The eigenvalues */
233 gsl_matrix *evec ; /* The eigenvectors */
237 gsl_vector *msr ; /* Multiple Squared Regressions */
239 double detR; /* The determinant of the correlation matrix */
242 static struct idata *
243 idata_alloc (size_t n_vars)
245 struct idata *id = xzalloc (sizeof (*id));
247 id->n_extractions = 0;
248 id->msr = gsl_vector_alloc (n_vars);
250 id->eval = gsl_vector_alloc (n_vars);
251 id->evec = gsl_matrix_alloc (n_vars, n_vars);
257 idata_free (struct idata *id)
259 gsl_vector_free (id->msr);
260 gsl_vector_free (id->eval);
261 gsl_matrix_free (id->evec);
263 gsl_matrix_free (id->cov);
264 if (id->corr != NULL)
265 gsl_matrix_free (CONST_CAST (gsl_matrix *, id->corr));
272 anti_image (const gsl_matrix *m)
276 assert (m->size1 == m->size2);
278 a = gsl_matrix_alloc (m->size1, m->size2);
280 for (i = 0; i < m->size1; ++i)
282 for (j = 0; j < m->size2; ++j)
284 double *p = gsl_matrix_ptr (a, i, j);
285 *p = gsl_matrix_get (m, i, j);
286 *p /= gsl_matrix_get (m, i, i);
287 *p /= gsl_matrix_get (m, j, j);
295 /* Return the sum of all the elements excluding row N */
297 ssq_od_n (const gsl_matrix *m, int n)
301 assert (m->size1 == m->size2);
303 assert (n < m->size1);
305 for (i = 0; i < m->size1; ++i)
307 if (i == n ) continue;
308 for (j = 0; j < m->size2; ++j)
310 ss += pow2 (gsl_matrix_get (m, i, j));
321 dump_matrix (const gsl_matrix *m)
325 for (i = 0 ; i < m->size1; ++i)
327 for (j = 0 ; j < m->size2; ++j)
328 printf ("%02f ", gsl_matrix_get (m, i, j));
334 dump_matrix_permute (const gsl_matrix *m, const gsl_permutation *p)
338 for (i = 0 ; i < m->size1; ++i)
340 for (j = 0 ; j < m->size2; ++j)
341 printf ("%02f ", gsl_matrix_get (m, gsl_permutation_get (p, i), j));
348 dump_vector (const gsl_vector *v)
351 for (i = 0 ; i < v->size; ++i)
353 printf ("%02f\n", gsl_vector_get (v, i));
361 n_extracted_factors (const struct cmd_factor *factor, struct idata *idata)
365 /* If there is a cached value, then return that. */
366 if ( idata->n_extractions != 0)
367 return idata->n_extractions;
369 /* Otherwise, if the number of factors has been explicitly requested,
371 if (factor->n_factors > 0)
373 idata->n_extractions = factor->n_factors;
377 /* Use the MIN_EIGEN setting. */
378 for (i = 0 ; i < idata->eval->size; ++i)
380 double evali = fabs (gsl_vector_get (idata->eval, i));
382 idata->n_extractions = i;
384 if (evali < factor->min_eigen)
389 return idata->n_extractions;
393 /* Returns a newly allocated matrix identical to M.
394 It it the callers responsibility to free the returned value.
397 matrix_dup (const gsl_matrix *m)
399 gsl_matrix *n = gsl_matrix_alloc (m->size1, m->size2);
401 gsl_matrix_memcpy (n, m);
409 /* Copy of the subject */
414 gsl_permutation *perm;
421 static struct smr_workspace *ws_create (const gsl_matrix *input)
423 struct smr_workspace *ws = xmalloc (sizeof (*ws));
425 ws->m = gsl_matrix_alloc (input->size1, input->size2);
426 ws->inverse = gsl_matrix_calloc (input->size1 - 1, input->size2 - 1);
427 ws->perm = gsl_permutation_alloc (input->size1 - 1);
428 ws->result1 = gsl_matrix_calloc (input->size1 - 1, 1);
429 ws->result2 = gsl_matrix_calloc (1, 1);
435 ws_destroy (struct smr_workspace *ws)
437 gsl_matrix_free (ws->result2);
438 gsl_matrix_free (ws->result1);
439 gsl_permutation_free (ws->perm);
440 gsl_matrix_free (ws->inverse);
441 gsl_matrix_free (ws->m);
448 Return the square of the regression coefficient for VAR regressed against all other variables.
451 squared_multiple_correlation (const gsl_matrix *corr, int var, struct smr_workspace *ws)
453 /* For an explanation of what this is doing, see
454 http://www.visualstatistics.net/Visual%20Statistics%20Multimedia/multiple_regression_analysis.htm
460 gsl_matrix_memcpy (ws->m, corr);
462 gsl_matrix_swap_rows (ws->m, 0, var);
463 gsl_matrix_swap_columns (ws->m, 0, var);
465 rxx = gsl_matrix_submatrix (ws->m, 1, 1, ws->m->size1 - 1, ws->m->size1 - 1);
467 gsl_linalg_LU_decomp (&rxx.matrix, ws->perm, &signum);
469 gsl_linalg_LU_invert (&rxx.matrix, ws->perm, ws->inverse);
472 gsl_matrix_const_view rxy = gsl_matrix_const_submatrix (ws->m, 1, 0, ws->m->size1 - 1, 1);
473 gsl_matrix_const_view ryx = gsl_matrix_const_submatrix (ws->m, 0, 1, 1, ws->m->size1 - 1);
475 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
476 1.0, ws->inverse, &rxy.matrix, 0.0, ws->result1);
478 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
479 1.0, &ryx.matrix, ws->result1, 0.0, ws->result2);
482 return gsl_matrix_get (ws->result2, 0, 0);
487 static double the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors);
490 struct factor_matrix_workspace
493 gsl_eigen_symmv_workspace *eigen_ws;
503 static struct factor_matrix_workspace *
504 factor_matrix_workspace_alloc (size_t n, size_t nf)
506 struct factor_matrix_workspace *ws = xmalloc (sizeof (*ws));
509 ws->gamma = gsl_matrix_calloc (nf, nf);
510 ws->eigen_ws = gsl_eigen_symmv_alloc (n);
511 ws->eval = gsl_vector_alloc (n);
512 ws->evec = gsl_matrix_alloc (n, n);
513 ws->r = gsl_matrix_alloc (n, n);
519 factor_matrix_workspace_free (struct factor_matrix_workspace *ws)
521 gsl_eigen_symmv_free (ws->eigen_ws);
522 gsl_vector_free (ws->eval);
523 gsl_matrix_free (ws->evec);
524 gsl_matrix_free (ws->gamma);
525 gsl_matrix_free (ws->r);
530 Shift P left by OFFSET places, and overwrite TARGET
531 with the shifted result.
532 Positions in TARGET less than OFFSET are unchanged.
535 perm_shift_apply (gsl_permutation *target, const gsl_permutation *p,
539 assert (target->size == p->size);
540 assert (offset <= target->size);
542 for (i = 0; i < target->size - offset; ++i)
544 target->data[i] = p->data [i + offset];
550 Indirectly sort the rows of matrix INPUT, storing the sort order in PERM.
551 The sort criteria are as follows:
553 Rows are sorted on the first column, until the absolute value of an
554 element in a subsequent column is greater than that of the first
555 column. Thereafter, rows will be sorted on the second column,
556 until the absolute value of an element in a subsequent column
557 exceeds that of the second column ...
560 sort_matrix_indirect (const gsl_matrix *input, gsl_permutation *perm)
562 const size_t n = perm->size;
563 const size_t m = input->size2;
570 assert (perm->size == input->size1);
572 p = gsl_permutation_alloc (n);
574 /* Copy INPUT into MAT, discarding the sign */
575 mat = gsl_matrix_alloc (n, m);
576 for (i = 0 ; i < mat->size1; ++i)
578 for (j = 0 ; j < mat->size2; ++j)
580 double x = gsl_matrix_get (input, i, j);
581 gsl_matrix_set (mat, i, j, fabs (x));
585 while (column_n < m && row_n < n)
587 gsl_vector_const_view columni = gsl_matrix_const_column (mat, column_n);
588 gsl_sort_vector_index (p, &columni.vector);
590 for (i = 0 ; i < n; ++i)
592 gsl_vector_view row = gsl_matrix_row (mat, p->data[n - 1 - i]);
593 size_t maxindex = gsl_vector_max_index (&row.vector);
595 if ( maxindex > column_n )
598 /* All subsequent elements of this row, are of no interest.
599 So set them all to a highly negative value */
600 for (j = column_n + 1; j < row.vector.size ; ++j)
601 gsl_vector_set (&row.vector, j, -DBL_MAX);
604 perm_shift_apply (perm, p, row_n);
610 gsl_permutation_free (p);
611 gsl_matrix_free (mat);
613 assert ( 0 == gsl_permutation_valid (perm));
615 /* We want the biggest value to be first */
616 gsl_permutation_reverse (perm);
621 drot_go (double phi, double *l0, double *l1)
623 double r0 = cos (phi) * *l0 + sin (phi) * *l1;
624 double r1 = - sin (phi) * *l0 + cos (phi) * *l1;
632 clone_matrix (const gsl_matrix *m)
635 gsl_matrix *c = gsl_matrix_calloc (m->size1, m->size2);
637 for (j = 0 ; j < c->size1; ++j)
639 for (k = 0 ; k < c->size2; ++k)
641 const double *v = gsl_matrix_const_ptr (m, j, k);
642 gsl_matrix_set (c, j, k, *v);
651 initial_sv (const gsl_matrix *fm)
656 for (j = 0 ; j < fm->size2; ++j)
661 for (k = j + 1 ; k < fm->size2; ++k)
663 double lambda = gsl_matrix_get (fm, k, j);
664 double lambda_sq = lambda * lambda;
665 double lambda_4 = lambda_sq * lambda_sq;
670 sv += ( fm->size1 * l4s - (l2s * l2s) ) / (fm->size1 * fm->size1 );
676 rotate (const struct cmd_factor *cf, const gsl_matrix *unrot,
677 const gsl_vector *communalities,
679 gsl_vector *rotated_loadings,
680 gsl_matrix *pattern_matrix,
681 gsl_matrix *factor_correlation_matrix
688 /* First get a normalised version of UNROT */
689 gsl_matrix *normalised = gsl_matrix_calloc (unrot->size1, unrot->size2);
690 gsl_matrix *h_sqrt = gsl_matrix_calloc (communalities->size, communalities->size);
691 gsl_matrix *h_sqrt_inv ;
693 /* H is the diagonal matrix containing the absolute values of the communalities */
694 for (i = 0 ; i < communalities->size ; ++i)
696 double *ptr = gsl_matrix_ptr (h_sqrt, i, i);
697 *ptr = fabs (gsl_vector_get (communalities, i));
700 /* Take the square root of the communalities */
701 gsl_linalg_cholesky_decomp (h_sqrt);
704 /* Save a copy of h_sqrt and invert it */
705 h_sqrt_inv = clone_matrix (h_sqrt);
706 gsl_linalg_cholesky_decomp (h_sqrt_inv);
707 gsl_linalg_cholesky_invert (h_sqrt_inv);
709 /* normalised vertion is H^{1/2} x UNROT */
710 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, h_sqrt_inv, unrot, 0.0, normalised);
712 gsl_matrix_free (h_sqrt_inv);
715 /* Now perform the rotation iterations */
717 prev_sv = initial_sv (normalised);
718 for (i = 0 ; i < cf->rotation_iterations ; ++i)
721 for (j = 0 ; j < normalised->size2; ++j)
723 /* These variables relate to the convergence criterium */
727 for (k = j + 1 ; k < normalised->size2; ++k)
737 for (p = 0; p < normalised->size1; ++p)
739 double jv = gsl_matrix_get (normalised, p, j);
740 double kv = gsl_matrix_get (normalised, p, k);
742 double u = jv * jv - kv * kv;
743 double v = 2 * jv * kv;
750 rotation_coeff [cf->rotation] (&x, &y, a, b, c, d, normalised);
752 phi = atan2 (x, y) / 4.0 ;
754 /* Don't bother rotating if the angle is small */
755 if ( fabs (sin (phi) ) <= pow (10.0, -15.0))
758 for (p = 0; p < normalised->size1; ++p)
760 double *lambda0 = gsl_matrix_ptr (normalised, p, j);
761 double *lambda1 = gsl_matrix_ptr (normalised, p, k);
762 drot_go (phi, lambda0, lambda1);
765 /* Calculate the convergence criterium */
767 double lambda = gsl_matrix_get (normalised, k, j);
768 double lambda_sq = lambda * lambda;
769 double lambda_4 = lambda_sq * lambda_sq;
775 sv += ( normalised->size1 * l4s - (l2s * l2s) ) / (normalised->size1 * normalised->size1 );
778 if ( fabs (sv - prev_sv) <= cf->rconverge)
784 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0,
785 h_sqrt, normalised, 0.0, result);
787 gsl_matrix_free (h_sqrt);
788 gsl_matrix_free (normalised);
790 if (cf->rotation == ROT_PROMAX)
792 /* general purpose m by m matrix, where m is the number of factors */
793 gsl_matrix *mm1 = gsl_matrix_calloc (unrot->size2, unrot->size2);
794 gsl_matrix *mm2 = gsl_matrix_calloc (unrot->size2, unrot->size2);
796 /* general purpose m by p matrix, where p is the number of variables */
797 gsl_matrix *mp1 = gsl_matrix_calloc (unrot->size2, unrot->size1);
799 gsl_matrix *pm1 = gsl_matrix_calloc (unrot->size1, unrot->size2);
801 gsl_permutation *perm = gsl_permutation_alloc (unrot->size2);
807 /* The following variables follow the notation by SPSS Statistical Algorithms
809 gsl_matrix *L = gsl_matrix_calloc (unrot->size2, unrot->size2);
810 gsl_matrix *P = clone_matrix (result);
815 /* Vector of length p containing (indexed by i)
816 \Sum^m_j {\lambda^2_{ij}} */
817 gsl_vector *rssq = gsl_vector_calloc (unrot->size1);
819 for (i = 0; i < P->size1; ++i)
822 for (j = 0; j < P->size2; ++j)
824 sum += gsl_matrix_get (result, i, j)
825 * gsl_matrix_get (result, i, j);
829 gsl_vector_set (rssq, i, sqrt (sum));
832 for (i = 0; i < P->size1; ++i)
834 for (j = 0; j < P->size2; ++j)
836 double l = gsl_matrix_get (result, i, j);
837 double r = gsl_vector_get (rssq, i);
838 gsl_matrix_set (P, i, j, pow (fabs (l / r), cf->promax_power + 1) * r / l);
842 gsl_vector_free (rssq);
844 gsl_linalg_matmult_mod (result,
845 GSL_LINALG_MOD_TRANSPOSE,
850 gsl_linalg_LU_decomp (mm1, perm, &signum);
851 gsl_linalg_LU_invert (mm1, perm, mm2);
853 gsl_linalg_matmult_mod (mm2, GSL_LINALG_MOD_NONE,
854 result, GSL_LINALG_MOD_TRANSPOSE,
857 gsl_linalg_matmult_mod (mp1, GSL_LINALG_MOD_NONE,
858 P, GSL_LINALG_MOD_NONE,
861 D = diag_rcp_sqrt (L);
862 Q = gsl_matrix_calloc (unrot->size2, unrot->size2);
864 gsl_linalg_matmult_mod (L, GSL_LINALG_MOD_NONE,
865 D, GSL_LINALG_MOD_NONE,
868 gsl_matrix *QQinv = gsl_matrix_calloc (unrot->size2, unrot->size2);
870 gsl_linalg_matmult_mod (Q, GSL_LINALG_MOD_TRANSPOSE,
871 Q, GSL_LINALG_MOD_NONE,
874 gsl_linalg_cholesky_decomp (QQinv);
875 gsl_linalg_cholesky_invert (QQinv);
878 gsl_matrix *C = diag_rcp_inv_sqrt (QQinv);
879 gsl_matrix *Cinv = clone_matrix (C);
881 gsl_linalg_cholesky_decomp (Cinv);
882 gsl_linalg_cholesky_invert (Cinv);
885 gsl_linalg_matmult_mod (result, GSL_LINALG_MOD_NONE,
886 Q, GSL_LINALG_MOD_NONE,
889 gsl_linalg_matmult_mod (pm1, GSL_LINALG_MOD_NONE,
890 Cinv, GSL_LINALG_MOD_NONE,
894 gsl_linalg_matmult_mod (C, GSL_LINALG_MOD_NONE,
895 QQinv, GSL_LINALG_MOD_NONE,
898 gsl_linalg_matmult_mod (mm1, GSL_LINALG_MOD_NONE,
899 C, GSL_LINALG_MOD_TRANSPOSE,
900 factor_correlation_matrix);
902 gsl_linalg_matmult_mod (pattern_matrix, GSL_LINALG_MOD_NONE,
903 factor_correlation_matrix, GSL_LINALG_MOD_NONE,
906 gsl_matrix_memcpy (result, pm1);
909 gsl_matrix_free (QQinv);
911 gsl_matrix_free (Cinv);
918 gsl_permutation_free (perm);
920 gsl_matrix_free (mm1);
921 gsl_matrix_free (mm2);
922 gsl_matrix_free (mp1);
923 gsl_matrix_free (pm1);
927 /* reflect negative sums and populate the rotated loadings vector*/
928 for (i = 0 ; i < result->size2; ++i)
932 for (j = 0 ; j < result->size1; ++j)
934 double s = gsl_matrix_get (result, j, i);
939 gsl_vector_set (rotated_loadings, i, ssq);
942 for (j = 0 ; j < result->size1; ++j)
944 double *lambda = gsl_matrix_ptr (result, j, i);
952 Get an approximation for the factor matrix into FACTORS, and the communalities into COMMUNALITIES.
953 R is the matrix to be analysed.
954 WS is a pointer to a structure which must have been initialised with factor_matrix_workspace_init.
957 iterate_factor_matrix (const gsl_matrix *r, gsl_vector *communalities, gsl_matrix *factors,
958 struct factor_matrix_workspace *ws)
963 assert (r->size1 == r->size2);
964 assert (r->size1 == communalities->size);
966 assert (factors->size1 == r->size1);
967 assert (factors->size2 == ws->n_factors);
969 gsl_matrix_memcpy (ws->r, r);
971 /* Apply Communalities to diagonal of correlation matrix */
972 for (i = 0 ; i < communalities->size ; ++i)
974 double *x = gsl_matrix_ptr (ws->r, i, i);
975 *x = gsl_vector_get (communalities, i);
978 gsl_eigen_symmv (ws->r, ws->eval, ws->evec, ws->eigen_ws);
980 mv = gsl_matrix_submatrix (ws->evec, 0, 0, ws->evec->size1, ws->n_factors);
982 /* Gamma is the diagonal matrix containing the absolute values of the eigenvalues */
983 for (i = 0 ; i < ws->n_factors ; ++i)
985 double *ptr = gsl_matrix_ptr (ws->gamma, i, i);
986 *ptr = fabs (gsl_vector_get (ws->eval, i));
989 /* Take the square root of gamma */
990 gsl_linalg_cholesky_decomp (ws->gamma);
992 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, &mv.matrix, ws->gamma, 0.0, factors);
994 for (i = 0 ; i < r->size1 ; ++i)
996 double h = the_communality (ws->evec, ws->eval, i, ws->n_factors);
997 gsl_vector_set (communalities, i, h);
1003 static bool run_factor (struct dataset *ds, const struct cmd_factor *factor);
1007 cmd_factor (struct lexer *lexer, struct dataset *ds)
1009 const struct dictionary *dict = dataset_dict (ds);
1010 int n_iterations = 25;
1011 struct cmd_factor factor;
1014 factor.method = METHOD_CORR;
1015 factor.missing_type = MISS_LISTWISE;
1016 factor.exclude = MV_ANY;
1017 factor.print = PRINT_INITIAL | PRINT_EXTRACTION | PRINT_ROTATION;
1018 factor.extraction = EXTRACTION_PC;
1019 factor.n_factors = 0;
1020 factor.min_eigen = SYSMIS;
1021 factor.extraction_iterations = 25;
1022 factor.rotation_iterations = 25;
1023 factor.econverge = 0.001;
1026 factor.sort = false;
1028 factor.rotation = ROT_VARIMAX;
1030 factor.rconverge = 0.0001;
1032 factor.wv = dict_get_weight (dict);
1034 lex_match (lexer, T_SLASH);
1036 if (!lex_force_match_id (lexer, "VARIABLES"))
1041 lex_match (lexer, T_EQUALS);
1043 if (!parse_variables_const (lexer, dict, &factor.vars, &factor.n_vars,
1044 PV_NO_DUPLICATE | PV_NUMERIC))
1047 if (factor.n_vars < 2)
1048 msg (MW, _("Factor analysis on a single variable is not useful."));
1050 while (lex_token (lexer) != T_ENDCMD)
1052 lex_match (lexer, T_SLASH);
1054 if (lex_match_id (lexer, "ANALYSIS"))
1056 struct const_var_set *vs;
1057 const struct variable **vars;
1061 lex_match (lexer, T_EQUALS);
1063 vs = const_var_set_create_from_array (factor.vars, factor.n_vars);
1064 ok = parse_const_var_set_vars (lexer, vs, &vars, &n_vars,
1065 PV_NO_DUPLICATE | PV_NUMERIC);
1066 const_var_set_destroy (vs);
1073 factor.n_vars = n_vars;
1075 else if (lex_match_id (lexer, "PLOT"))
1077 lex_match (lexer, T_EQUALS);
1078 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1080 if (lex_match_id (lexer, "EIGEN"))
1082 factor.plot |= PLOT_SCREE;
1084 #if FACTOR_FULLY_IMPLEMENTED
1085 else if (lex_match_id (lexer, "ROTATION"))
1091 lex_error (lexer, NULL);
1096 else if (lex_match_id (lexer, "METHOD"))
1098 lex_match (lexer, T_EQUALS);
1099 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1101 if (lex_match_id (lexer, "COVARIANCE"))
1103 factor.method = METHOD_COV;
1105 else if (lex_match_id (lexer, "CORRELATION"))
1107 factor.method = METHOD_CORR;
1111 lex_error (lexer, NULL);
1116 else if (lex_match_id (lexer, "ROTATION"))
1118 lex_match (lexer, T_EQUALS);
1119 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1121 /* VARIMAX and DEFAULT are defaults */
1122 if (lex_match_id (lexer, "VARIMAX") || lex_match_id (lexer, "DEFAULT"))
1124 factor.rotation = ROT_VARIMAX;
1126 else if (lex_match_id (lexer, "EQUAMAX"))
1128 factor.rotation = ROT_EQUAMAX;
1130 else if (lex_match_id (lexer, "QUARTIMAX"))
1132 factor.rotation = ROT_QUARTIMAX;
1134 else if (lex_match_id (lexer, "PROMAX"))
1136 factor.promax_power = 5;
1137 if (lex_match (lexer, T_LPAREN)
1138 && lex_force_int (lexer))
1140 factor.promax_power = lex_integer (lexer);
1142 if (! lex_force_match (lexer, T_RPAREN))
1145 factor.rotation = ROT_PROMAX;
1147 else if (lex_match_id (lexer, "NOROTATE"))
1149 factor.rotation = ROT_NONE;
1153 lex_error (lexer, NULL);
1157 factor.rotation_iterations = n_iterations;
1159 else if (lex_match_id (lexer, "CRITERIA"))
1161 lex_match (lexer, T_EQUALS);
1162 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1164 if (lex_match_id (lexer, "FACTORS"))
1166 if ( lex_force_match (lexer, T_LPAREN)
1167 && lex_force_int (lexer))
1169 factor.n_factors = lex_integer (lexer);
1171 if (! lex_force_match (lexer, T_RPAREN))
1175 else if (lex_match_id (lexer, "MINEIGEN"))
1177 if ( lex_force_match (lexer, T_LPAREN)
1178 && lex_force_num (lexer))
1180 factor.min_eigen = lex_number (lexer);
1182 if (! lex_force_match (lexer, T_RPAREN))
1186 else if (lex_match_id (lexer, "ECONVERGE"))
1188 if ( lex_force_match (lexer, T_LPAREN)
1189 && lex_force_num (lexer))
1191 factor.econverge = lex_number (lexer);
1193 if (! lex_force_match (lexer, T_RPAREN))
1197 else if (lex_match_id (lexer, "RCONVERGE"))
1199 if (lex_force_match (lexer, T_LPAREN)
1200 && lex_force_num (lexer))
1202 factor.rconverge = lex_number (lexer);
1204 if (! lex_force_match (lexer, T_RPAREN))
1208 else if (lex_match_id (lexer, "ITERATE"))
1210 if ( lex_force_match (lexer, T_LPAREN)
1211 && lex_force_int (lexer))
1213 n_iterations = lex_integer (lexer);
1215 if (! lex_force_match (lexer, T_RPAREN))
1219 else if (lex_match_id (lexer, "DEFAULT"))
1221 factor.n_factors = 0;
1222 factor.min_eigen = 1;
1227 lex_error (lexer, NULL);
1232 else if (lex_match_id (lexer, "EXTRACTION"))
1234 lex_match (lexer, T_EQUALS);
1235 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1237 if (lex_match_id (lexer, "PAF"))
1239 factor.extraction = EXTRACTION_PAF;
1241 else if (lex_match_id (lexer, "PC"))
1243 factor.extraction = EXTRACTION_PC;
1245 else if (lex_match_id (lexer, "PA1"))
1247 factor.extraction = EXTRACTION_PC;
1249 else if (lex_match_id (lexer, "DEFAULT"))
1251 factor.extraction = EXTRACTION_PC;
1255 lex_error (lexer, NULL);
1259 factor.extraction_iterations = n_iterations;
1261 else if (lex_match_id (lexer, "FORMAT"))
1263 lex_match (lexer, T_EQUALS);
1264 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1266 if (lex_match_id (lexer, "SORT"))
1270 else if (lex_match_id (lexer, "BLANK"))
1272 if ( lex_force_match (lexer, T_LPAREN)
1273 && lex_force_num (lexer))
1275 factor.blank = lex_number (lexer);
1277 if (! lex_force_match (lexer, T_RPAREN))
1281 else if (lex_match_id (lexer, "DEFAULT"))
1284 factor.sort = false;
1288 lex_error (lexer, NULL);
1293 else if (lex_match_id (lexer, "PRINT"))
1296 lex_match (lexer, T_EQUALS);
1297 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1299 if (lex_match_id (lexer, "UNIVARIATE"))
1301 factor.print |= PRINT_UNIVARIATE;
1303 else if (lex_match_id (lexer, "DET"))
1305 factor.print |= PRINT_DETERMINANT;
1307 #if FACTOR_FULLY_IMPLEMENTED
1308 else if (lex_match_id (lexer, "INV"))
1311 else if (lex_match_id (lexer, "AIC"))
1315 else if (lex_match_id (lexer, "SIG"))
1317 factor.print |= PRINT_SIG;
1319 else if (lex_match_id (lexer, "CORRELATION"))
1321 factor.print |= PRINT_CORRELATION;
1323 #if FACTOR_FULLY_IMPLEMENTED
1324 else if (lex_match_id (lexer, "COVARIANCE"))
1328 else if (lex_match_id (lexer, "ROTATION"))
1330 factor.print |= PRINT_ROTATION;
1332 else if (lex_match_id (lexer, "EXTRACTION"))
1334 factor.print |= PRINT_EXTRACTION;
1336 else if (lex_match_id (lexer, "INITIAL"))
1338 factor.print |= PRINT_INITIAL;
1340 else if (lex_match_id (lexer, "KMO"))
1342 factor.print |= PRINT_KMO;
1344 #if FACTOR_FULLY_IMPLEMENTED
1345 else if (lex_match_id (lexer, "REPR"))
1348 else if (lex_match_id (lexer, "FSCORE"))
1352 else if (lex_match (lexer, T_ALL))
1354 factor.print = 0xFFFF;
1356 else if (lex_match_id (lexer, "DEFAULT"))
1358 factor.print |= PRINT_INITIAL ;
1359 factor.print |= PRINT_EXTRACTION ;
1360 factor.print |= PRINT_ROTATION ;
1364 lex_error (lexer, NULL);
1369 else if (lex_match_id (lexer, "MISSING"))
1371 lex_match (lexer, T_EQUALS);
1372 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1374 if (lex_match_id (lexer, "INCLUDE"))
1376 factor.exclude = MV_SYSTEM;
1378 else if (lex_match_id (lexer, "EXCLUDE"))
1380 factor.exclude = MV_ANY;
1382 else if (lex_match_id (lexer, "LISTWISE"))
1384 factor.missing_type = MISS_LISTWISE;
1386 else if (lex_match_id (lexer, "PAIRWISE"))
1388 factor.missing_type = MISS_PAIRWISE;
1390 else if (lex_match_id (lexer, "MEANSUB"))
1392 factor.missing_type = MISS_MEANSUB;
1396 lex_error (lexer, NULL);
1403 lex_error (lexer, NULL);
1408 if ( factor.rotation == ROT_NONE )
1409 factor.print &= ~PRINT_ROTATION;
1411 if ( ! run_factor (ds, &factor))
1422 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
1426 run_factor (struct dataset *ds, const struct cmd_factor *factor)
1428 struct dictionary *dict = dataset_dict (ds);
1430 struct casereader *group;
1432 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
1434 while (casegrouper_get_next_group (grouper, &group))
1436 if ( factor->missing_type == MISS_LISTWISE )
1437 group = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
1440 do_factor (factor, group);
1443 ok = casegrouper_destroy (grouper);
1444 ok = proc_commit (ds) && ok;
1450 /* Return the communality of variable N, calculated to N_FACTORS */
1452 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
1459 assert (n < eval->size);
1460 assert (n < evec->size1);
1461 assert (n_factors <= eval->size);
1463 for (i = 0 ; i < n_factors; ++i)
1465 double evali = fabs (gsl_vector_get (eval, i));
1467 double eveci = gsl_matrix_get (evec, n, i);
1469 comm += pow2 (eveci) * evali;
1475 /* Return the communality of variable N, calculated to N_FACTORS */
1477 communality (struct idata *idata, int n, int n_factors)
1479 return the_communality (idata->evec, idata->eval, n, n_factors);
1484 show_scree (const struct cmd_factor *f, struct idata *idata)
1489 if ( !(f->plot & PLOT_SCREE) )
1493 label = f->extraction == EXTRACTION_PC ? _("Component Number") : _("Factor Number");
1495 s = scree_create (idata->eval, label);
1501 show_communalities (const struct cmd_factor * factor,
1502 const gsl_vector *initial, const gsl_vector *extracted)
1506 const int heading_columns = 1;
1507 int nc = heading_columns;
1508 const int heading_rows = 1;
1509 const int nr = heading_rows + factor->n_vars;
1510 struct tab_table *t;
1512 if (factor->print & PRINT_EXTRACTION)
1515 if (factor->print & PRINT_INITIAL)
1518 /* No point having a table with only headings */
1522 t = tab_create (nc, nr);
1524 tab_title (t, _("Communalities"));
1526 tab_headers (t, heading_columns, 0, heading_rows, 0);
1529 if (factor->print & PRINT_INITIAL)
1530 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
1532 if (factor->print & PRINT_EXTRACTION)
1533 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
1535 /* Outline the box */
1542 /* Vertical lines */
1549 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1550 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1552 for (i = 0 ; i < factor->n_vars; ++i)
1555 tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
1557 if (factor->print & PRINT_INITIAL)
1558 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL, RC_OTHER);
1560 if (factor->print & PRINT_EXTRACTION)
1561 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL, RC_OTHER);
1569 show_factor_matrix (const struct cmd_factor *factor, struct idata *idata, const char *title, const gsl_matrix *fm)
1573 const int n_factors = idata->n_extractions;
1575 const int heading_columns = 1;
1576 const int heading_rows = 2;
1577 const int nr = heading_rows + factor->n_vars;
1578 const int nc = heading_columns + n_factors;
1579 gsl_permutation *perm;
1581 struct tab_table *t = tab_create (nc, nr);
1584 if ( factor->extraction == EXTRACTION_PC )
1585 tab_title (t, _("Component Matrix"));
1587 tab_title (t, _("Factor Matrix"));
1590 tab_title (t, "%s", title);
1592 tab_headers (t, heading_columns, 0, heading_rows, 0);
1594 if ( factor->extraction == EXTRACTION_PC )
1598 TAB_CENTER | TAT_TITLE, _("Component"));
1603 TAB_CENTER | TAT_TITLE, _("Factor"));
1606 tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1609 /* Outline the box */
1616 /* Vertical lines */
1623 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1624 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1627 /* Initialise to the identity permutation */
1628 perm = gsl_permutation_calloc (factor->n_vars);
1631 sort_matrix_indirect (fm, perm);
1633 for (i = 0 ; i < n_factors; ++i)
1635 tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1638 for (i = 0 ; i < factor->n_vars; ++i)
1641 const int matrix_row = perm->data[i];
1642 tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1644 for (j = 0 ; j < n_factors; ++j)
1646 double x = gsl_matrix_get (fm, matrix_row, j);
1648 if ( fabs (x) < factor->blank)
1651 tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL, RC_OTHER);
1655 gsl_permutation_free (perm);
1662 show_explained_variance (const struct cmd_factor * factor, struct idata *idata,
1663 const gsl_vector *initial_eigenvalues,
1664 const gsl_vector *extracted_eigenvalues,
1665 const gsl_vector *rotated_loadings)
1669 const int heading_columns = 1;
1670 const int heading_rows = 2;
1671 const int nr = heading_rows + factor->n_vars;
1673 struct tab_table *t ;
1675 double i_total = 0.0;
1678 double e_total = 0.0;
1683 int nc = heading_columns;
1685 if (factor->print & PRINT_EXTRACTION)
1688 if (factor->print & PRINT_INITIAL)
1691 if (factor->print & PRINT_ROTATION)
1693 nc += factor->rotation == ROT_PROMAX ? 1 : 3;
1696 /* No point having a table with only headings */
1697 if ( nc <= heading_columns)
1700 t = tab_create (nc, nr);
1702 tab_title (t, _("Total Variance Explained"));
1704 tab_headers (t, heading_columns, 0, heading_rows, 0);
1706 /* Outline the box */
1713 /* Vertical lines */
1720 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1721 tab_hline (t, TAL_1, 1, nc - 1, 1);
1723 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1726 if ( factor->extraction == EXTRACTION_PC)
1727 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1729 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1732 if (factor->print & PRINT_INITIAL)
1734 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1738 if (factor->print & PRINT_EXTRACTION)
1740 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1744 if (factor->print & PRINT_ROTATION)
1746 const int width = factor->rotation == ROT_PROMAX ? 0 : 2;
1747 tab_joint_text (t, c, 0, c + width, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1751 for (i = 0; i < (nc - heading_columns + 2) / 3 ; ++i)
1753 tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1755 tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1757 if (i == 2 && factor->rotation == ROT_PROMAX)
1760 /* xgettext:no-c-format */
1761 tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1762 tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1765 for (i = 0 ; i < initial_eigenvalues->size; ++i)
1766 i_total += gsl_vector_get (initial_eigenvalues, i);
1768 if ( factor->extraction == EXTRACTION_PAF)
1770 e_total = factor->n_vars;
1777 for (i = 0 ; i < factor->n_vars; ++i)
1779 const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1780 double i_percent = 100.0 * i_lambda / i_total ;
1782 const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1783 double e_percent = 100.0 * e_lambda / e_total ;
1787 tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%zu"), i + 1);
1792 /* Initial Eigenvalues */
1793 if (factor->print & PRINT_INITIAL)
1795 tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL, RC_OTHER);
1796 tab_double (t, c++, i + heading_rows, 0, i_percent, NULL, RC_OTHER);
1797 tab_double (t, c++, i + heading_rows, 0, i_cum, NULL, RC_OTHER);
1801 if (factor->print & PRINT_EXTRACTION)
1803 if (i < idata->n_extractions)
1805 /* Sums of squared loadings */
1806 tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL, RC_OTHER);
1807 tab_double (t, c++, i + heading_rows, 0, e_percent, NULL, RC_OTHER);
1808 tab_double (t, c++, i + heading_rows, 0, e_cum, NULL, RC_OTHER);
1812 if (rotated_loadings != NULL)
1814 const double r_lambda = gsl_vector_get (rotated_loadings, i);
1815 double r_percent = 100.0 * r_lambda / e_total ;
1817 if (factor->print & PRINT_ROTATION)
1819 if (i < idata->n_extractions)
1822 tab_double (t, c++, i + heading_rows, 0, r_lambda, NULL, RC_OTHER);
1823 if (factor->rotation != ROT_PROMAX)
1825 tab_double (t, c++, i + heading_rows, 0, r_percent, NULL, RC_OTHER);
1826 tab_double (t, c++, i + heading_rows, 0, r_cum, NULL, RC_OTHER);
1838 show_factor_correlation (const struct cmd_factor * factor, const gsl_matrix *fcm)
1841 const int heading_columns = 1;
1842 const int heading_rows = 1;
1843 const int nr = heading_rows + fcm->size2;
1844 const int nc = heading_columns + fcm->size1;
1845 struct tab_table *t = tab_create (nc, nr);
1847 tab_title (t, _("Factor Correlation Matrix"));
1849 tab_headers (t, heading_columns, 0, heading_rows, 0);
1851 /* Outline the box */
1858 /* Vertical lines */
1865 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1866 tab_hline (t, TAL_1, 1, nc - 1, 1);
1868 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1871 if ( factor->extraction == EXTRACTION_PC)
1872 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Component"));
1874 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Factor"));
1876 for (i = 0 ; i < fcm->size1; ++i)
1878 tab_text_format (t, heading_columns + i, 0, TAB_CENTER | TAT_TITLE, _("%zu"), i + 1);
1881 for (i = 0 ; i < fcm->size2; ++i)
1883 tab_text_format (t, 0, heading_rows + i, TAB_CENTER | TAT_TITLE, _("%zu"), i + 1);
1887 for (i = 0 ; i < fcm->size1; ++i)
1889 for (j = 0 ; j < fcm->size2; ++j)
1890 tab_double (t, heading_columns + i, heading_rows +j, 0,
1891 gsl_matrix_get (fcm, i, j), NULL, RC_OTHER);
1899 show_correlation_matrix (const struct cmd_factor *factor, const struct idata *idata)
1901 struct tab_table *t ;
1903 int y_pos_corr = -1;
1905 int suffix_rows = 0;
1907 const int heading_rows = 1;
1908 const int heading_columns = 2;
1910 int nc = heading_columns ;
1911 int nr = heading_rows ;
1912 int n_data_sets = 0;
1914 if (factor->print & PRINT_CORRELATION)
1916 y_pos_corr = n_data_sets;
1918 nc = heading_columns + factor->n_vars;
1921 if (factor->print & PRINT_SIG)
1923 y_pos_sig = n_data_sets;
1925 nc = heading_columns + factor->n_vars;
1928 nr += n_data_sets * factor->n_vars;
1930 if (factor->print & PRINT_DETERMINANT)
1933 /* If the table would contain only headings, don't bother rendering it */
1934 if (nr <= heading_rows && suffix_rows == 0)
1937 t = tab_create (nc, nr + suffix_rows);
1939 tab_title (t, _("Correlation Matrix"));
1941 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1943 if (nr > heading_rows)
1945 tab_headers (t, heading_columns, 0, heading_rows, 0);
1947 tab_vline (t, TAL_2, 2, 0, nr - 1);
1949 /* Outline the box */
1956 /* Vertical lines */
1964 for (i = 0; i < factor->n_vars; ++i)
1965 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
1968 for (i = 0 ; i < n_data_sets; ++i)
1970 int y = heading_rows + i * factor->n_vars;
1972 for (v = 0; v < factor->n_vars; ++v)
1973 tab_text (t, 1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
1975 tab_hline (t, TAL_1, 0, nc - 1, y);
1978 if (factor->print & PRINT_CORRELATION)
1980 const double y = heading_rows + y_pos_corr;
1981 tab_text (t, 0, y, TAT_TITLE, _("Correlations"));
1983 for (i = 0; i < factor->n_vars; ++i)
1985 for (j = 0; j < factor->n_vars; ++j)
1986 tab_double (t, heading_columns + i, y + j, 0, gsl_matrix_get (idata->corr, i, j), NULL, RC_OTHER);
1990 if (factor->print & PRINT_SIG)
1992 const double y = heading_rows + y_pos_sig * factor->n_vars;
1993 tab_text (t, 0, y, TAT_TITLE, _("Sig. (1-tailed)"));
1995 for (i = 0; i < factor->n_vars; ++i)
1997 for (j = 0; j < factor->n_vars; ++j)
1999 double rho = gsl_matrix_get (idata->corr, i, j);
2000 double w = gsl_matrix_get (idata->n, i, j);
2005 tab_double (t, heading_columns + i, y + j, 0, significance_of_correlation (rho, w), NULL, RC_PVALUE);
2011 if (factor->print & PRINT_DETERMINANT)
2013 tab_text (t, 0, nr, TAB_LEFT | TAT_TITLE, _("Determinant"));
2015 tab_double (t, 1, nr, 0, idata->detR, NULL, RC_OTHER);
2024 do_factor (const struct cmd_factor *factor, struct casereader *r)
2027 const gsl_matrix *var_matrix;
2028 const gsl_matrix *mean_matrix;
2030 const gsl_matrix *analysis_matrix;
2031 struct idata *idata = idata_alloc (factor->n_vars);
2033 struct covariance *cov = covariance_1pass_create (factor->n_vars, factor->vars,
2034 factor->wv, factor->exclude);
2036 for ( ; (c = casereader_read (r) ); case_unref (c))
2038 covariance_accumulate (cov, c);
2041 idata->cov = covariance_calculate (cov);
2043 if (idata->cov == NULL)
2045 msg (MW, _("The dataset contains no complete observations. No analysis will be performed."));
2046 covariance_destroy (cov);
2050 var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
2051 mean_matrix = covariance_moments (cov, MOMENT_MEAN);
2052 idata->n = covariance_moments (cov, MOMENT_NONE);
2055 if ( factor->method == METHOD_CORR)
2057 idata->corr = correlation_from_covariance (idata->cov, var_matrix);
2059 analysis_matrix = idata->corr;
2062 analysis_matrix = idata->cov;
2065 if (factor->print & PRINT_DETERMINANT
2066 || factor->print & PRINT_KMO)
2070 const int size = idata->corr->size1;
2071 gsl_permutation *p = gsl_permutation_calloc (size);
2072 gsl_matrix *tmp = gsl_matrix_calloc (size, size);
2073 gsl_matrix_memcpy (tmp, idata->corr);
2075 gsl_linalg_LU_decomp (tmp, p, &sign);
2076 idata->detR = gsl_linalg_LU_det (tmp, sign);
2077 gsl_permutation_free (p);
2078 gsl_matrix_free (tmp);
2081 if ( factor->print & PRINT_UNIVARIATE)
2083 const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
2087 const int heading_columns = 1;
2088 const int heading_rows = 1;
2090 const int nr = heading_rows + factor->n_vars;
2092 struct tab_table *t = tab_create (nc, nr);
2093 tab_set_format (t, RC_WEIGHT, wfmt);
2094 tab_title (t, _("Descriptive Statistics"));
2096 tab_headers (t, heading_columns, 0, heading_rows, 0);
2098 /* Outline the box */
2105 /* Vertical lines */
2112 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
2113 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
2115 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
2116 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
2117 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
2119 for (i = 0 ; i < factor->n_vars; ++i)
2121 const struct variable *v = factor->vars[i];
2122 tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
2124 tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (mean_matrix, i, i), NULL, RC_OTHER);
2125 tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (var_matrix, i, i)), NULL, RC_OTHER);
2126 tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (idata->n, i, i), NULL, RC_WEIGHT);
2132 if (factor->print & PRINT_KMO)
2135 double sum_ssq_r = 0;
2136 double sum_ssq_a = 0;
2138 double df = factor->n_vars * ( factor->n_vars - 1) / 2;
2145 const int heading_columns = 2;
2146 const int heading_rows = 0;
2148 const int nr = heading_rows + 4;
2149 const int nc = heading_columns + 1;
2153 struct tab_table *t = tab_create (nc, nr);
2154 tab_title (t, _("KMO and Bartlett's Test"));
2156 x = clone_matrix (idata->corr);
2157 gsl_linalg_cholesky_decomp (x);
2158 gsl_linalg_cholesky_invert (x);
2162 for (i = 0; i < x->size1; ++i)
2164 sum_ssq_r += ssq_od_n (x, i);
2165 sum_ssq_a += ssq_od_n (a, i);
2168 gsl_matrix_free (a);
2169 gsl_matrix_free (x);
2171 tab_headers (t, heading_columns, 0, heading_rows, 0);
2173 /* Outline the box */
2180 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
2182 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Kaiser-Meyer-Olkin Measure of Sampling Adequacy"));
2184 tab_double (t, 2, 0, 0, sum_ssq_r / (sum_ssq_r + sum_ssq_a), NULL, RC_OTHER);
2186 tab_text (t, 0, 1, TAT_TITLE | TAB_LEFT, _("Bartlett's Test of Sphericity"));
2188 tab_text (t, 1, 1, TAT_TITLE, _("Approx. Chi-Square"));
2189 tab_text (t, 1, 2, TAT_TITLE, _("df"));
2190 tab_text (t, 1, 3, TAT_TITLE, _("Sig."));
2193 /* The literature doesn't say what to do for the value of W when
2194 missing values are involved. The best thing I can think of
2195 is to take the mean average. */
2197 for (i = 0; i < idata->n->size1; ++i)
2198 w += gsl_matrix_get (idata->n, i, i);
2199 w /= idata->n->size1;
2201 xsq = w - 1 - (2 * factor->n_vars + 5) / 6.0;
2202 xsq *= -log (idata->detR);
2204 tab_double (t, 2, 1, 0, xsq, NULL, RC_OTHER);
2205 tab_double (t, 2, 2, 0, df, NULL, RC_INTEGER);
2206 tab_double (t, 2, 3, 0, gsl_cdf_chisq_Q (xsq, df), NULL, RC_PVALUE);
2212 show_correlation_matrix (factor, idata);
2213 covariance_destroy (cov);
2216 gsl_matrix *am = matrix_dup (analysis_matrix);
2217 gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
2219 gsl_eigen_symmv (am, idata->eval, idata->evec, workspace);
2221 gsl_eigen_symmv_free (workspace);
2222 gsl_matrix_free (am);
2225 gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
2227 idata->n_extractions = n_extracted_factors (factor, idata);
2229 if (idata->n_extractions == 0)
2231 msg (MW, _("The %s criteria result in zero factors extracted. Therefore no analysis will be performed."), "FACTOR");
2235 if (idata->n_extractions > factor->n_vars)
2238 _("The %s criteria result in more factors than variables, which is not meaningful. No analysis will be performed."),
2244 gsl_matrix *rotated_factors = NULL;
2245 gsl_matrix *pattern_matrix = NULL;
2246 gsl_matrix *fcm = NULL;
2247 gsl_vector *rotated_loadings = NULL;
2249 const gsl_vector *extracted_eigenvalues = NULL;
2250 gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
2251 gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
2253 struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, idata->n_extractions);
2254 gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
2256 if ( factor->extraction == EXTRACTION_PAF)
2258 gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
2259 struct smr_workspace *ws = ws_create (analysis_matrix);
2261 for (i = 0 ; i < factor->n_vars ; ++i)
2263 double r2 = squared_multiple_correlation (analysis_matrix, i, ws);
2265 gsl_vector_set (idata->msr, i, r2);
2269 gsl_vector_memcpy (initial_communalities, idata->msr);
2271 for (i = 0; i < factor->extraction_iterations; ++i)
2274 gsl_vector_memcpy (diff, idata->msr);
2276 iterate_factor_matrix (analysis_matrix, idata->msr, factor_matrix, fmw);
2278 gsl_vector_sub (diff, idata->msr);
2280 gsl_vector_minmax (diff, &min, &max);
2282 if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
2285 gsl_vector_free (diff);
2289 gsl_vector_memcpy (extracted_communalities, idata->msr);
2290 extracted_eigenvalues = fmw->eval;
2292 else if (factor->extraction == EXTRACTION_PC)
2294 for (i = 0; i < factor->n_vars; ++i)
2295 gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
2297 gsl_vector_memcpy (extracted_communalities, initial_communalities);
2299 iterate_factor_matrix (analysis_matrix, extracted_communalities, factor_matrix, fmw);
2302 extracted_eigenvalues = idata->eval;
2306 show_communalities (factor, initial_communalities, extracted_communalities);
2309 if ( factor->rotation != ROT_NONE)
2311 rotated_factors = gsl_matrix_calloc (factor_matrix->size1, factor_matrix->size2);
2312 rotated_loadings = gsl_vector_calloc (factor_matrix->size2);
2313 if (factor->rotation == ROT_PROMAX)
2315 pattern_matrix = gsl_matrix_calloc (factor_matrix->size1, factor_matrix->size2);
2316 fcm = gsl_matrix_calloc (factor_matrix->size2, factor_matrix->size2);
2320 rotate (factor, factor_matrix, extracted_communalities, rotated_factors, rotated_loadings, pattern_matrix, fcm);
2323 show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues, rotated_loadings);
2325 factor_matrix_workspace_free (fmw);
2327 show_scree (factor, idata);
2329 show_factor_matrix (factor, idata,
2330 factor->extraction == EXTRACTION_PC ? _("Component Matrix") : _("Factor Matrix"),
2333 if ( factor->rotation == ROT_PROMAX)
2335 show_factor_matrix (factor, idata, _("Pattern Matrix"), pattern_matrix);
2336 gsl_matrix_free (pattern_matrix);
2339 if ( factor->rotation != ROT_NONE)
2341 show_factor_matrix (factor, idata,
2342 (factor->rotation == ROT_PROMAX) ? _("Structure Matrix") :
2343 (factor->extraction == EXTRACTION_PC ? _("Rotated Component Matrix") : _("Rotated Factor Matrix")),
2346 gsl_matrix_free (rotated_factors);
2349 if ( factor->rotation == ROT_PROMAX)
2351 show_factor_correlation (factor, fcm);
2352 gsl_matrix_free (fcm);
2355 gsl_matrix_free (factor_matrix);
2356 gsl_vector_free (rotated_loadings);
2357 gsl_vector_free (initial_communalities);
2358 gsl_vector_free (extracted_communalities);
2365 casereader_destroy (r);