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);
269 if (id->mm.cov != NULL)
270 gsl_matrix_free (id->mm.cov);
271 if (id->mm.corr != NULL)
272 gsl_matrix_free (CONST_CAST (gsl_matrix *, id->mm.corr));
279 anti_image (const gsl_matrix *m)
283 assert (m->size1 == m->size2);
285 a = gsl_matrix_alloc (m->size1, m->size2);
287 for (i = 0; i < m->size1; ++i)
289 for (j = 0; j < m->size2; ++j)
291 double *p = gsl_matrix_ptr (a, i, j);
292 *p = gsl_matrix_get (m, i, j);
293 *p /= gsl_matrix_get (m, i, i);
294 *p /= gsl_matrix_get (m, j, j);
302 /* Return the sum of all the elements excluding row N */
304 ssq_od_n (const gsl_matrix *m, int n)
308 assert (m->size1 == m->size2);
310 assert (n < m->size1);
312 for (i = 0; i < m->size1; ++i)
314 if (i == n ) continue;
315 for (j = 0; j < m->size2; ++j)
317 ss += pow2 (gsl_matrix_get (m, i, j));
328 dump_matrix (const gsl_matrix *m)
332 for (i = 0 ; i < m->size1; ++i)
334 for (j = 0 ; j < m->size2; ++j)
335 printf ("%02f ", gsl_matrix_get (m, i, j));
341 dump_matrix_permute (const gsl_matrix *m, const gsl_permutation *p)
345 for (i = 0 ; i < m->size1; ++i)
347 for (j = 0 ; j < m->size2; ++j)
348 printf ("%02f ", gsl_matrix_get (m, gsl_permutation_get (p, i), j));
355 dump_vector (const gsl_vector *v)
358 for (i = 0 ; i < v->size; ++i)
360 printf ("%02f\n", gsl_vector_get (v, i));
368 n_extracted_factors (const struct cmd_factor *factor, struct idata *idata)
372 /* If there is a cached value, then return that. */
373 if ( idata->n_extractions != 0)
374 return idata->n_extractions;
376 /* Otherwise, if the number of factors has been explicitly requested,
378 if (factor->n_factors > 0)
380 idata->n_extractions = factor->n_factors;
384 /* Use the MIN_EIGEN setting. */
385 for (i = 0 ; i < idata->eval->size; ++i)
387 double evali = fabs (gsl_vector_get (idata->eval, i));
389 idata->n_extractions = i;
391 if (evali < factor->min_eigen)
396 return idata->n_extractions;
400 /* Returns a newly allocated matrix identical to M.
401 It it the callers responsibility to free the returned value.
404 matrix_dup (const gsl_matrix *m)
406 gsl_matrix *n = gsl_matrix_alloc (m->size1, m->size2);
408 gsl_matrix_memcpy (n, m);
416 /* Copy of the subject */
421 gsl_permutation *perm;
428 static struct smr_workspace *ws_create (const gsl_matrix *input)
430 struct smr_workspace *ws = xmalloc (sizeof (*ws));
432 ws->m = gsl_matrix_alloc (input->size1, input->size2);
433 ws->inverse = gsl_matrix_calloc (input->size1 - 1, input->size2 - 1);
434 ws->perm = gsl_permutation_alloc (input->size1 - 1);
435 ws->result1 = gsl_matrix_calloc (input->size1 - 1, 1);
436 ws->result2 = gsl_matrix_calloc (1, 1);
442 ws_destroy (struct smr_workspace *ws)
444 gsl_matrix_free (ws->result2);
445 gsl_matrix_free (ws->result1);
446 gsl_permutation_free (ws->perm);
447 gsl_matrix_free (ws->inverse);
448 gsl_matrix_free (ws->m);
455 Return the square of the regression coefficient for VAR regressed against all other variables.
458 squared_multiple_correlation (const gsl_matrix *corr, int var, struct smr_workspace *ws)
460 /* For an explanation of what this is doing, see
461 http://www.visualstatistics.net/Visual%20Statistics%20Multimedia/multiple_regression_analysis.htm
467 gsl_matrix_memcpy (ws->m, corr);
469 gsl_matrix_swap_rows (ws->m, 0, var);
470 gsl_matrix_swap_columns (ws->m, 0, var);
472 rxx = gsl_matrix_submatrix (ws->m, 1, 1, ws->m->size1 - 1, ws->m->size1 - 1);
474 gsl_linalg_LU_decomp (&rxx.matrix, ws->perm, &signum);
476 gsl_linalg_LU_invert (&rxx.matrix, ws->perm, ws->inverse);
479 gsl_matrix_const_view rxy = gsl_matrix_const_submatrix (ws->m, 1, 0, ws->m->size1 - 1, 1);
480 gsl_matrix_const_view ryx = gsl_matrix_const_submatrix (ws->m, 0, 1, 1, ws->m->size1 - 1);
482 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
483 1.0, ws->inverse, &rxy.matrix, 0.0, ws->result1);
485 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
486 1.0, &ryx.matrix, ws->result1, 0.0, ws->result2);
489 return gsl_matrix_get (ws->result2, 0, 0);
494 static double the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors);
497 struct factor_matrix_workspace
500 gsl_eigen_symmv_workspace *eigen_ws;
510 static struct factor_matrix_workspace *
511 factor_matrix_workspace_alloc (size_t n, size_t nf)
513 struct factor_matrix_workspace *ws = xmalloc (sizeof (*ws));
516 ws->gamma = gsl_matrix_calloc (nf, nf);
517 ws->eigen_ws = gsl_eigen_symmv_alloc (n);
518 ws->eval = gsl_vector_alloc (n);
519 ws->evec = gsl_matrix_alloc (n, n);
520 ws->r = gsl_matrix_alloc (n, n);
526 factor_matrix_workspace_free (struct factor_matrix_workspace *ws)
528 gsl_eigen_symmv_free (ws->eigen_ws);
529 gsl_vector_free (ws->eval);
530 gsl_matrix_free (ws->evec);
531 gsl_matrix_free (ws->gamma);
532 gsl_matrix_free (ws->r);
537 Shift P left by OFFSET places, and overwrite TARGET
538 with the shifted result.
539 Positions in TARGET less than OFFSET are unchanged.
542 perm_shift_apply (gsl_permutation *target, const gsl_permutation *p,
546 assert (target->size == p->size);
547 assert (offset <= target->size);
549 for (i = 0; i < target->size - offset; ++i)
551 target->data[i] = p->data [i + offset];
557 Indirectly sort the rows of matrix INPUT, storing the sort order in PERM.
558 The sort criteria are as follows:
560 Rows are sorted on the first column, until the absolute value of an
561 element in a subsequent column is greater than that of the first
562 column. Thereafter, rows will be sorted on the second column,
563 until the absolute value of an element in a subsequent column
564 exceeds that of the second column ...
567 sort_matrix_indirect (const gsl_matrix *input, gsl_permutation *perm)
569 const size_t n = perm->size;
570 const size_t m = input->size2;
577 assert (perm->size == input->size1);
579 p = gsl_permutation_alloc (n);
581 /* Copy INPUT into MAT, discarding the sign */
582 mat = gsl_matrix_alloc (n, m);
583 for (i = 0 ; i < mat->size1; ++i)
585 for (j = 0 ; j < mat->size2; ++j)
587 double x = gsl_matrix_get (input, i, j);
588 gsl_matrix_set (mat, i, j, fabs (x));
592 while (column_n < m && row_n < n)
594 gsl_vector_const_view columni = gsl_matrix_const_column (mat, column_n);
595 gsl_sort_vector_index (p, &columni.vector);
597 for (i = 0 ; i < n; ++i)
599 gsl_vector_view row = gsl_matrix_row (mat, p->data[n - 1 - i]);
600 size_t maxindex = gsl_vector_max_index (&row.vector);
602 if ( maxindex > column_n )
605 /* All subsequent elements of this row, are of no interest.
606 So set them all to a highly negative value */
607 for (j = column_n + 1; j < row.vector.size ; ++j)
608 gsl_vector_set (&row.vector, j, -DBL_MAX);
611 perm_shift_apply (perm, p, row_n);
617 gsl_permutation_free (p);
618 gsl_matrix_free (mat);
620 assert ( 0 == gsl_permutation_valid (perm));
622 /* We want the biggest value to be first */
623 gsl_permutation_reverse (perm);
628 drot_go (double phi, double *l0, double *l1)
630 double r0 = cos (phi) * *l0 + sin (phi) * *l1;
631 double r1 = - sin (phi) * *l0 + cos (phi) * *l1;
639 clone_matrix (const gsl_matrix *m)
642 gsl_matrix *c = gsl_matrix_calloc (m->size1, m->size2);
644 for (j = 0 ; j < c->size1; ++j)
646 for (k = 0 ; k < c->size2; ++k)
648 const double *v = gsl_matrix_const_ptr (m, j, k);
649 gsl_matrix_set (c, j, k, *v);
658 initial_sv (const gsl_matrix *fm)
663 for (j = 0 ; j < fm->size2; ++j)
668 for (k = j + 1 ; k < fm->size2; ++k)
670 double lambda = gsl_matrix_get (fm, k, j);
671 double lambda_sq = lambda * lambda;
672 double lambda_4 = lambda_sq * lambda_sq;
677 sv += ( fm->size1 * l4s - (l2s * l2s) ) / (fm->size1 * fm->size1 );
683 rotate (const struct cmd_factor *cf, const gsl_matrix *unrot,
684 const gsl_vector *communalities,
686 gsl_vector *rotated_loadings,
687 gsl_matrix *pattern_matrix,
688 gsl_matrix *factor_correlation_matrix
695 /* First get a normalised version of UNROT */
696 gsl_matrix *normalised = gsl_matrix_calloc (unrot->size1, unrot->size2);
697 gsl_matrix *h_sqrt = gsl_matrix_calloc (communalities->size, communalities->size);
698 gsl_matrix *h_sqrt_inv ;
700 /* H is the diagonal matrix containing the absolute values of the communalities */
701 for (i = 0 ; i < communalities->size ; ++i)
703 double *ptr = gsl_matrix_ptr (h_sqrt, i, i);
704 *ptr = fabs (gsl_vector_get (communalities, i));
707 /* Take the square root of the communalities */
708 gsl_linalg_cholesky_decomp (h_sqrt);
711 /* Save a copy of h_sqrt and invert it */
712 h_sqrt_inv = clone_matrix (h_sqrt);
713 gsl_linalg_cholesky_decomp (h_sqrt_inv);
714 gsl_linalg_cholesky_invert (h_sqrt_inv);
716 /* normalised vertion is H^{1/2} x UNROT */
717 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, h_sqrt_inv, unrot, 0.0, normalised);
719 gsl_matrix_free (h_sqrt_inv);
722 /* Now perform the rotation iterations */
724 prev_sv = initial_sv (normalised);
725 for (i = 0 ; i < cf->rotation_iterations ; ++i)
728 for (j = 0 ; j < normalised->size2; ++j)
730 /* These variables relate to the convergence criterium */
734 for (k = j + 1 ; k < normalised->size2; ++k)
744 for (p = 0; p < normalised->size1; ++p)
746 double jv = gsl_matrix_get (normalised, p, j);
747 double kv = gsl_matrix_get (normalised, p, k);
749 double u = jv * jv - kv * kv;
750 double v = 2 * jv * kv;
757 rotation_coeff [cf->rotation] (&x, &y, a, b, c, d, normalised);
759 phi = atan2 (x, y) / 4.0 ;
761 /* Don't bother rotating if the angle is small */
762 if ( fabs (sin (phi) ) <= pow (10.0, -15.0))
765 for (p = 0; p < normalised->size1; ++p)
767 double *lambda0 = gsl_matrix_ptr (normalised, p, j);
768 double *lambda1 = gsl_matrix_ptr (normalised, p, k);
769 drot_go (phi, lambda0, lambda1);
772 /* Calculate the convergence criterium */
774 double lambda = gsl_matrix_get (normalised, k, j);
775 double lambda_sq = lambda * lambda;
776 double lambda_4 = lambda_sq * lambda_sq;
782 sv += ( normalised->size1 * l4s - (l2s * l2s) ) / (normalised->size1 * normalised->size1 );
785 if ( fabs (sv - prev_sv) <= cf->rconverge)
791 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0,
792 h_sqrt, normalised, 0.0, result);
794 gsl_matrix_free (h_sqrt);
795 gsl_matrix_free (normalised);
797 if (cf->rotation == ROT_PROMAX)
799 /* general purpose m by m matrix, where m is the number of factors */
800 gsl_matrix *mm1 = gsl_matrix_calloc (unrot->size2, unrot->size2);
801 gsl_matrix *mm2 = gsl_matrix_calloc (unrot->size2, unrot->size2);
803 /* general purpose m by p matrix, where p is the number of variables */
804 gsl_matrix *mp1 = gsl_matrix_calloc (unrot->size2, unrot->size1);
806 gsl_matrix *pm1 = gsl_matrix_calloc (unrot->size1, unrot->size2);
808 gsl_permutation *perm = gsl_permutation_alloc (unrot->size2);
814 /* The following variables follow the notation by SPSS Statistical Algorithms
816 gsl_matrix *L = gsl_matrix_calloc (unrot->size2, unrot->size2);
817 gsl_matrix *P = clone_matrix (result);
822 /* Vector of length p containing (indexed by i)
823 \Sum^m_j {\lambda^2_{ij}} */
824 gsl_vector *rssq = gsl_vector_calloc (unrot->size1);
826 for (i = 0; i < P->size1; ++i)
829 for (j = 0; j < P->size2; ++j)
831 sum += gsl_matrix_get (result, i, j)
832 * gsl_matrix_get (result, i, j);
836 gsl_vector_set (rssq, i, sqrt (sum));
839 for (i = 0; i < P->size1; ++i)
841 for (j = 0; j < P->size2; ++j)
843 double l = gsl_matrix_get (result, i, j);
844 double r = gsl_vector_get (rssq, i);
845 gsl_matrix_set (P, i, j, pow (fabs (l / r), cf->promax_power + 1) * r / l);
849 gsl_vector_free (rssq);
851 gsl_linalg_matmult_mod (result,
852 GSL_LINALG_MOD_TRANSPOSE,
857 gsl_linalg_LU_decomp (mm1, perm, &signum);
858 gsl_linalg_LU_invert (mm1, perm, mm2);
860 gsl_linalg_matmult_mod (mm2, GSL_LINALG_MOD_NONE,
861 result, GSL_LINALG_MOD_TRANSPOSE,
864 gsl_linalg_matmult_mod (mp1, GSL_LINALG_MOD_NONE,
865 P, GSL_LINALG_MOD_NONE,
868 D = diag_rcp_sqrt (L);
869 Q = gsl_matrix_calloc (unrot->size2, unrot->size2);
871 gsl_linalg_matmult_mod (L, GSL_LINALG_MOD_NONE,
872 D, GSL_LINALG_MOD_NONE,
875 gsl_matrix *QQinv = gsl_matrix_calloc (unrot->size2, unrot->size2);
877 gsl_linalg_matmult_mod (Q, GSL_LINALG_MOD_TRANSPOSE,
878 Q, GSL_LINALG_MOD_NONE,
881 gsl_linalg_cholesky_decomp (QQinv);
882 gsl_linalg_cholesky_invert (QQinv);
885 gsl_matrix *C = diag_rcp_inv_sqrt (QQinv);
886 gsl_matrix *Cinv = clone_matrix (C);
888 gsl_linalg_cholesky_decomp (Cinv);
889 gsl_linalg_cholesky_invert (Cinv);
892 gsl_linalg_matmult_mod (result, GSL_LINALG_MOD_NONE,
893 Q, GSL_LINALG_MOD_NONE,
896 gsl_linalg_matmult_mod (pm1, GSL_LINALG_MOD_NONE,
897 Cinv, GSL_LINALG_MOD_NONE,
901 gsl_linalg_matmult_mod (C, GSL_LINALG_MOD_NONE,
902 QQinv, GSL_LINALG_MOD_NONE,
905 gsl_linalg_matmult_mod (mm1, GSL_LINALG_MOD_NONE,
906 C, GSL_LINALG_MOD_TRANSPOSE,
907 factor_correlation_matrix);
909 gsl_linalg_matmult_mod (pattern_matrix, GSL_LINALG_MOD_NONE,
910 factor_correlation_matrix, GSL_LINALG_MOD_NONE,
913 gsl_matrix_memcpy (result, pm1);
916 gsl_matrix_free (QQinv);
918 gsl_matrix_free (Cinv);
925 gsl_permutation_free (perm);
927 gsl_matrix_free (mm1);
928 gsl_matrix_free (mm2);
929 gsl_matrix_free (mp1);
930 gsl_matrix_free (pm1);
934 /* reflect negative sums and populate the rotated loadings vector*/
935 for (i = 0 ; i < result->size2; ++i)
939 for (j = 0 ; j < result->size1; ++j)
941 double s = gsl_matrix_get (result, j, i);
946 gsl_vector_set (rotated_loadings, i, ssq);
949 for (j = 0 ; j < result->size1; ++j)
951 double *lambda = gsl_matrix_ptr (result, j, i);
959 Get an approximation for the factor matrix into FACTORS, and the communalities into COMMUNALITIES.
960 R is the matrix to be analysed.
961 WS is a pointer to a structure which must have been initialised with factor_matrix_workspace_init.
964 iterate_factor_matrix (const gsl_matrix *r, gsl_vector *communalities, gsl_matrix *factors,
965 struct factor_matrix_workspace *ws)
970 assert (r->size1 == r->size2);
971 assert (r->size1 == communalities->size);
973 assert (factors->size1 == r->size1);
974 assert (factors->size2 == ws->n_factors);
976 gsl_matrix_memcpy (ws->r, r);
978 /* Apply Communalities to diagonal of correlation matrix */
979 for (i = 0 ; i < communalities->size ; ++i)
981 double *x = gsl_matrix_ptr (ws->r, i, i);
982 *x = gsl_vector_get (communalities, i);
985 gsl_eigen_symmv (ws->r, ws->eval, ws->evec, ws->eigen_ws);
987 mv = gsl_matrix_submatrix (ws->evec, 0, 0, ws->evec->size1, ws->n_factors);
989 /* Gamma is the diagonal matrix containing the absolute values of the eigenvalues */
990 for (i = 0 ; i < ws->n_factors ; ++i)
992 double *ptr = gsl_matrix_ptr (ws->gamma, i, i);
993 *ptr = fabs (gsl_vector_get (ws->eval, i));
996 /* Take the square root of gamma */
997 gsl_linalg_cholesky_decomp (ws->gamma);
999 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, &mv.matrix, ws->gamma, 0.0, factors);
1001 for (i = 0 ; i < r->size1 ; ++i)
1003 double h = the_communality (ws->evec, ws->eval, i, ws->n_factors);
1004 gsl_vector_set (communalities, i, h);
1010 static bool run_factor (struct dataset *ds, const struct cmd_factor *factor);
1012 static void do_factor_by_matrix (const struct cmd_factor *factor, struct idata *idata);
1017 cmd_factor (struct lexer *lexer, struct dataset *ds)
1019 struct dictionary *dict = NULL;
1020 int n_iterations = 25;
1021 struct cmd_factor factor;
1024 factor.method = METHOD_CORR;
1025 factor.missing_type = MISS_LISTWISE;
1026 factor.exclude = MV_ANY;
1027 factor.print = PRINT_INITIAL | PRINT_EXTRACTION | PRINT_ROTATION;
1028 factor.extraction = EXTRACTION_PC;
1029 factor.n_factors = 0;
1030 factor.min_eigen = SYSMIS;
1031 factor.extraction_iterations = 25;
1032 factor.rotation_iterations = 25;
1033 factor.econverge = 0.001;
1036 factor.sort = false;
1038 factor.rotation = ROT_VARIMAX;
1041 factor.rconverge = 0.0001;
1043 lex_match (lexer, T_SLASH);
1045 struct matrix_reader *mr = NULL;
1046 struct casereader *matrix_reader = NULL;
1048 if (lex_match_id (lexer, "VARIABLES"))
1050 lex_match (lexer, T_EQUALS);
1051 dict = dataset_dict (ds);
1052 factor.wv = dict_get_weight (dict);
1054 if (!parse_variables_const (lexer, dict, &factor.vars, &factor.n_vars,
1055 PV_NO_DUPLICATE | PV_NUMERIC))
1058 else if (lex_match_id (lexer, "MATRIX"))
1060 if (! lex_force_match_id (lexer, "IN"))
1062 if (!lex_force_match (lexer, T_LPAREN))
1066 if (lex_match_id (lexer, "CORR"))
1069 else if (lex_match_id (lexer, "COV"))
1074 lex_error (lexer, _("Matrix input for %s must be either COV or CORR"), "FACTOR");
1077 if (! lex_force_match (lexer, T_EQUALS))
1079 if (lex_match (lexer, T_ASTERISK))
1081 dict = dataset_dict (ds);
1082 matrix_reader = casereader_clone (dataset_source (ds));
1086 struct file_handle *fh = fh_parse (lexer, FH_REF_FILE, NULL);
1091 = any_reader_open_and_decode (fh, NULL, &dict, NULL);
1093 if (! (matrix_reader && dict))
1099 if (! lex_force_match (lexer, T_RPAREN))
1102 mr = create_matrix_reader_from_case_reader (dict, matrix_reader,
1103 &factor.vars, &factor.n_vars);
1110 while (lex_token (lexer) != T_ENDCMD)
1112 lex_match (lexer, T_SLASH);
1114 if (lex_match_id (lexer, "ANALYSIS"))
1116 struct const_var_set *vs;
1117 const struct variable **vars;
1121 lex_match (lexer, T_EQUALS);
1123 vs = const_var_set_create_from_array (factor.vars, factor.n_vars);
1124 ok = parse_const_var_set_vars (lexer, vs, &vars, &n_vars,
1125 PV_NO_DUPLICATE | PV_NUMERIC);
1126 const_var_set_destroy (vs);
1133 factor.n_vars = n_vars;
1135 else if (lex_match_id (lexer, "PLOT"))
1137 lex_match (lexer, T_EQUALS);
1138 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1140 if (lex_match_id (lexer, "EIGEN"))
1142 factor.plot |= PLOT_SCREE;
1144 #if FACTOR_FULLY_IMPLEMENTED
1145 else if (lex_match_id (lexer, "ROTATION"))
1151 lex_error (lexer, NULL);
1156 else if (lex_match_id (lexer, "METHOD"))
1158 lex_match (lexer, T_EQUALS);
1159 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1161 if (lex_match_id (lexer, "COVARIANCE"))
1163 factor.method = METHOD_COV;
1165 else if (lex_match_id (lexer, "CORRELATION"))
1167 factor.method = METHOD_CORR;
1171 lex_error (lexer, NULL);
1176 else if (lex_match_id (lexer, "ROTATION"))
1178 lex_match (lexer, T_EQUALS);
1179 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1181 /* VARIMAX and DEFAULT are defaults */
1182 if (lex_match_id (lexer, "VARIMAX") || lex_match_id (lexer, "DEFAULT"))
1184 factor.rotation = ROT_VARIMAX;
1186 else if (lex_match_id (lexer, "EQUAMAX"))
1188 factor.rotation = ROT_EQUAMAX;
1190 else if (lex_match_id (lexer, "QUARTIMAX"))
1192 factor.rotation = ROT_QUARTIMAX;
1194 else if (lex_match_id (lexer, "PROMAX"))
1196 factor.promax_power = 5;
1197 if (lex_match (lexer, T_LPAREN)
1198 && lex_force_int (lexer))
1200 factor.promax_power = lex_integer (lexer);
1202 if (! lex_force_match (lexer, T_RPAREN))
1205 factor.rotation = ROT_PROMAX;
1207 else if (lex_match_id (lexer, "NOROTATE"))
1209 factor.rotation = ROT_NONE;
1213 lex_error (lexer, NULL);
1217 factor.rotation_iterations = n_iterations;
1219 else if (lex_match_id (lexer, "CRITERIA"))
1221 lex_match (lexer, T_EQUALS);
1222 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1224 if (lex_match_id (lexer, "FACTORS"))
1226 if ( lex_force_match (lexer, T_LPAREN)
1227 && lex_force_int (lexer))
1229 factor.n_factors = lex_integer (lexer);
1231 if (! lex_force_match (lexer, T_RPAREN))
1235 else if (lex_match_id (lexer, "MINEIGEN"))
1237 if ( lex_force_match (lexer, T_LPAREN)
1238 && lex_force_num (lexer))
1240 factor.min_eigen = lex_number (lexer);
1242 if (! lex_force_match (lexer, T_RPAREN))
1246 else if (lex_match_id (lexer, "ECONVERGE"))
1248 if ( lex_force_match (lexer, T_LPAREN)
1249 && lex_force_num (lexer))
1251 factor.econverge = lex_number (lexer);
1253 if (! lex_force_match (lexer, T_RPAREN))
1257 else if (lex_match_id (lexer, "RCONVERGE"))
1259 if (lex_force_match (lexer, T_LPAREN)
1260 && lex_force_num (lexer))
1262 factor.rconverge = lex_number (lexer);
1264 if (! lex_force_match (lexer, T_RPAREN))
1268 else if (lex_match_id (lexer, "ITERATE"))
1270 if ( lex_force_match (lexer, T_LPAREN)
1271 && lex_force_int (lexer))
1273 n_iterations = lex_integer (lexer);
1275 if (! lex_force_match (lexer, T_RPAREN))
1279 else if (lex_match_id (lexer, "DEFAULT"))
1281 factor.n_factors = 0;
1282 factor.min_eigen = 1;
1287 lex_error (lexer, NULL);
1292 else if (lex_match_id (lexer, "EXTRACTION"))
1294 lex_match (lexer, T_EQUALS);
1295 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1297 if (lex_match_id (lexer, "PAF"))
1299 factor.extraction = EXTRACTION_PAF;
1301 else if (lex_match_id (lexer, "PC"))
1303 factor.extraction = EXTRACTION_PC;
1305 else if (lex_match_id (lexer, "PA1"))
1307 factor.extraction = EXTRACTION_PC;
1309 else if (lex_match_id (lexer, "DEFAULT"))
1311 factor.extraction = EXTRACTION_PC;
1315 lex_error (lexer, NULL);
1319 factor.extraction_iterations = n_iterations;
1321 else if (lex_match_id (lexer, "FORMAT"))
1323 lex_match (lexer, T_EQUALS);
1324 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1326 if (lex_match_id (lexer, "SORT"))
1330 else if (lex_match_id (lexer, "BLANK"))
1332 if ( lex_force_match (lexer, T_LPAREN)
1333 && lex_force_num (lexer))
1335 factor.blank = lex_number (lexer);
1337 if (! lex_force_match (lexer, T_RPAREN))
1341 else if (lex_match_id (lexer, "DEFAULT"))
1344 factor.sort = false;
1348 lex_error (lexer, NULL);
1353 else if (lex_match_id (lexer, "PRINT"))
1356 lex_match (lexer, T_EQUALS);
1357 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1359 if (lex_match_id (lexer, "UNIVARIATE"))
1361 factor.print |= PRINT_UNIVARIATE;
1363 else if (lex_match_id (lexer, "DET"))
1365 factor.print |= PRINT_DETERMINANT;
1367 #if FACTOR_FULLY_IMPLEMENTED
1368 else if (lex_match_id (lexer, "INV"))
1371 else if (lex_match_id (lexer, "AIC"))
1375 else if (lex_match_id (lexer, "SIG"))
1377 factor.print |= PRINT_SIG;
1379 else if (lex_match_id (lexer, "CORRELATION"))
1381 factor.print |= PRINT_CORRELATION;
1383 #if FACTOR_FULLY_IMPLEMENTED
1384 else if (lex_match_id (lexer, "COVARIANCE"))
1388 else if (lex_match_id (lexer, "ROTATION"))
1390 factor.print |= PRINT_ROTATION;
1392 else if (lex_match_id (lexer, "EXTRACTION"))
1394 factor.print |= PRINT_EXTRACTION;
1396 else if (lex_match_id (lexer, "INITIAL"))
1398 factor.print |= PRINT_INITIAL;
1400 else if (lex_match_id (lexer, "KMO"))
1402 factor.print |= PRINT_KMO;
1404 #if FACTOR_FULLY_IMPLEMENTED
1405 else if (lex_match_id (lexer, "REPR"))
1408 else if (lex_match_id (lexer, "FSCORE"))
1412 else if (lex_match (lexer, T_ALL))
1414 factor.print = 0xFFFF;
1416 else if (lex_match_id (lexer, "DEFAULT"))
1418 factor.print |= PRINT_INITIAL ;
1419 factor.print |= PRINT_EXTRACTION ;
1420 factor.print |= PRINT_ROTATION ;
1424 lex_error (lexer, NULL);
1429 else if (lex_match_id (lexer, "MISSING"))
1431 lex_match (lexer, T_EQUALS);
1432 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
1434 if (lex_match_id (lexer, "INCLUDE"))
1436 factor.exclude = MV_SYSTEM;
1438 else if (lex_match_id (lexer, "EXCLUDE"))
1440 factor.exclude = MV_ANY;
1442 else if (lex_match_id (lexer, "LISTWISE"))
1444 factor.missing_type = MISS_LISTWISE;
1446 else if (lex_match_id (lexer, "PAIRWISE"))
1448 factor.missing_type = MISS_PAIRWISE;
1450 else if (lex_match_id (lexer, "MEANSUB"))
1452 factor.missing_type = MISS_MEANSUB;
1456 lex_error (lexer, NULL);
1463 lex_error (lexer, NULL);
1468 if ( factor.rotation == ROT_NONE )
1469 factor.print &= ~PRINT_ROTATION;
1471 if (factor.n_vars < 2)
1472 msg (MW, _("Factor analysis on a single variable is not useful."));
1476 struct idata *id = idata_alloc (factor.n_vars);
1478 while (next_matrix_from_reader (&id->mm, mr,
1479 factor.vars, factor.n_vars))
1481 do_factor_by_matrix (&factor, id);
1490 if ( ! run_factor (ds, &factor))
1494 destroy_matrix_reader (mr);
1499 destroy_matrix_reader (mr);
1504 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
1508 run_factor (struct dataset *ds, const struct cmd_factor *factor)
1510 struct dictionary *dict = dataset_dict (ds);
1512 struct casereader *group;
1514 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
1516 while (casegrouper_get_next_group (grouper, &group))
1518 if ( factor->missing_type == MISS_LISTWISE )
1519 group = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
1522 do_factor (factor, group);
1525 ok = casegrouper_destroy (grouper);
1526 ok = proc_commit (ds) && ok;
1532 /* Return the communality of variable N, calculated to N_FACTORS */
1534 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
1541 assert (n < eval->size);
1542 assert (n < evec->size1);
1543 assert (n_factors <= eval->size);
1545 for (i = 0 ; i < n_factors; ++i)
1547 double evali = fabs (gsl_vector_get (eval, i));
1549 double eveci = gsl_matrix_get (evec, n, i);
1551 comm += pow2 (eveci) * evali;
1557 /* Return the communality of variable N, calculated to N_FACTORS */
1559 communality (struct idata *idata, int n, int n_factors)
1561 return the_communality (idata->evec, idata->eval, n, n_factors);
1566 show_scree (const struct cmd_factor *f, struct idata *idata)
1571 if ( !(f->plot & PLOT_SCREE) )
1575 label = f->extraction == EXTRACTION_PC ? _("Component Number") : _("Factor Number");
1577 s = scree_create (idata->eval, label);
1583 show_communalities (const struct cmd_factor * factor,
1584 const gsl_vector *initial, const gsl_vector *extracted)
1588 const int heading_columns = 1;
1589 int nc = heading_columns;
1590 const int heading_rows = 1;
1591 const int nr = heading_rows + factor->n_vars;
1592 struct tab_table *t;
1594 if (factor->print & PRINT_EXTRACTION)
1597 if (factor->print & PRINT_INITIAL)
1600 /* No point having a table with only headings */
1604 t = tab_create (nc, nr);
1606 tab_title (t, _("Communalities"));
1608 tab_headers (t, heading_columns, 0, heading_rows, 0);
1611 if (factor->print & PRINT_INITIAL)
1612 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
1614 if (factor->print & PRINT_EXTRACTION)
1615 tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
1617 /* Outline the box */
1624 /* Vertical lines */
1631 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1632 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1634 for (i = 0 ; i < factor->n_vars; ++i)
1637 tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
1639 if (factor->print & PRINT_INITIAL)
1640 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL, RC_OTHER);
1642 if (factor->print & PRINT_EXTRACTION)
1643 tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL, RC_OTHER);
1651 show_factor_matrix (const struct cmd_factor *factor, struct idata *idata, const char *title, const gsl_matrix *fm)
1655 const int n_factors = idata->n_extractions;
1657 const int heading_columns = 1;
1658 const int heading_rows = 2;
1659 const int nr = heading_rows + factor->n_vars;
1660 const int nc = heading_columns + n_factors;
1661 gsl_permutation *perm;
1663 struct tab_table *t = tab_create (nc, nr);
1666 if ( factor->extraction == EXTRACTION_PC )
1667 tab_title (t, _("Component Matrix"));
1669 tab_title (t, _("Factor Matrix"));
1672 tab_title (t, "%s", title);
1674 tab_headers (t, heading_columns, 0, heading_rows, 0);
1676 if ( factor->extraction == EXTRACTION_PC )
1680 TAB_CENTER | TAT_TITLE, _("Component"));
1685 TAB_CENTER | TAT_TITLE, _("Factor"));
1688 tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1691 /* Outline the box */
1698 /* Vertical lines */
1705 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1706 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1709 /* Initialise to the identity permutation */
1710 perm = gsl_permutation_calloc (factor->n_vars);
1713 sort_matrix_indirect (fm, perm);
1715 for (i = 0 ; i < n_factors; ++i)
1717 tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1720 for (i = 0 ; i < factor->n_vars; ++i)
1723 const int matrix_row = perm->data[i];
1724 tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1726 for (j = 0 ; j < n_factors; ++j)
1728 double x = gsl_matrix_get (fm, matrix_row, j);
1730 if ( fabs (x) < factor->blank)
1733 tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL, RC_OTHER);
1737 gsl_permutation_free (perm);
1744 show_explained_variance (const struct cmd_factor * factor, struct idata *idata,
1745 const gsl_vector *initial_eigenvalues,
1746 const gsl_vector *extracted_eigenvalues,
1747 const gsl_vector *rotated_loadings)
1751 const int heading_columns = 1;
1752 const int heading_rows = 2;
1753 const int nr = heading_rows + factor->n_vars;
1755 struct tab_table *t ;
1757 double i_total = 0.0;
1760 double e_total = 0.0;
1765 int nc = heading_columns;
1767 if (factor->print & PRINT_EXTRACTION)
1770 if (factor->print & PRINT_INITIAL)
1773 if (factor->print & PRINT_ROTATION)
1775 nc += factor->rotation == ROT_PROMAX ? 1 : 3;
1778 /* No point having a table with only headings */
1779 if ( nc <= heading_columns)
1782 t = tab_create (nc, nr);
1784 tab_title (t, _("Total Variance Explained"));
1786 tab_headers (t, heading_columns, 0, heading_rows, 0);
1788 /* Outline the box */
1795 /* Vertical lines */
1802 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1803 tab_hline (t, TAL_1, 1, nc - 1, 1);
1805 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1808 if ( factor->extraction == EXTRACTION_PC)
1809 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1811 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1814 if (factor->print & PRINT_INITIAL)
1816 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1820 if (factor->print & PRINT_EXTRACTION)
1822 tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1826 if (factor->print & PRINT_ROTATION)
1828 const int width = factor->rotation == ROT_PROMAX ? 0 : 2;
1829 tab_joint_text (t, c, 0, c + width, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1833 for (i = 0; i < (nc - heading_columns + 2) / 3 ; ++i)
1835 tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1837 tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1839 if (i == 2 && factor->rotation == ROT_PROMAX)
1842 /* xgettext:no-c-format */
1843 tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1844 tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1847 for (i = 0 ; i < initial_eigenvalues->size; ++i)
1848 i_total += gsl_vector_get (initial_eigenvalues, i);
1850 if ( factor->extraction == EXTRACTION_PAF)
1852 e_total = factor->n_vars;
1859 for (i = 0 ; i < factor->n_vars; ++i)
1861 const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1862 double i_percent = 100.0 * i_lambda / i_total ;
1864 const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1865 double e_percent = 100.0 * e_lambda / e_total ;
1869 tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%zu"), i + 1);
1874 /* Initial Eigenvalues */
1875 if (factor->print & PRINT_INITIAL)
1877 tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL, RC_OTHER);
1878 tab_double (t, c++, i + heading_rows, 0, i_percent, NULL, RC_OTHER);
1879 tab_double (t, c++, i + heading_rows, 0, i_cum, NULL, RC_OTHER);
1883 if (factor->print & PRINT_EXTRACTION)
1885 if (i < idata->n_extractions)
1887 /* Sums of squared loadings */
1888 tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL, RC_OTHER);
1889 tab_double (t, c++, i + heading_rows, 0, e_percent, NULL, RC_OTHER);
1890 tab_double (t, c++, i + heading_rows, 0, e_cum, NULL, RC_OTHER);
1894 if (rotated_loadings != NULL)
1896 const double r_lambda = gsl_vector_get (rotated_loadings, i);
1897 double r_percent = 100.0 * r_lambda / e_total ;
1899 if (factor->print & PRINT_ROTATION)
1901 if (i < idata->n_extractions)
1904 tab_double (t, c++, i + heading_rows, 0, r_lambda, NULL, RC_OTHER);
1905 if (factor->rotation != ROT_PROMAX)
1907 tab_double (t, c++, i + heading_rows, 0, r_percent, NULL, RC_OTHER);
1908 tab_double (t, c++, i + heading_rows, 0, r_cum, NULL, RC_OTHER);
1920 show_factor_correlation (const struct cmd_factor * factor, const gsl_matrix *fcm)
1923 const int heading_columns = 1;
1924 const int heading_rows = 1;
1925 const int nr = heading_rows + fcm->size2;
1926 const int nc = heading_columns + fcm->size1;
1927 struct tab_table *t = tab_create (nc, nr);
1929 tab_title (t, _("Factor Correlation Matrix"));
1931 tab_headers (t, heading_columns, 0, heading_rows, 0);
1933 /* Outline the box */
1940 /* Vertical lines */
1947 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1948 tab_hline (t, TAL_1, 1, nc - 1, 1);
1950 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1953 if ( factor->extraction == EXTRACTION_PC)
1954 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Component"));
1956 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Factor"));
1958 for (i = 0 ; i < fcm->size1; ++i)
1960 tab_text_format (t, heading_columns + i, 0, TAB_CENTER | TAT_TITLE, _("%zu"), i + 1);
1963 for (i = 0 ; i < fcm->size2; ++i)
1965 tab_text_format (t, 0, heading_rows + i, TAB_CENTER | TAT_TITLE, _("%zu"), i + 1);
1969 for (i = 0 ; i < fcm->size1; ++i)
1971 for (j = 0 ; j < fcm->size2; ++j)
1972 tab_double (t, heading_columns + i, heading_rows +j, 0,
1973 gsl_matrix_get (fcm, i, j), NULL, RC_OTHER);
1981 show_correlation_matrix (const struct cmd_factor *factor, const struct idata *idata)
1983 struct tab_table *t ;
1985 int y_pos_corr = -1;
1987 int suffix_rows = 0;
1989 const int heading_rows = 1;
1990 const int heading_columns = 2;
1992 int nc = heading_columns ;
1993 int nr = heading_rows ;
1994 int n_data_sets = 0;
1996 if (factor->print & PRINT_CORRELATION)
1998 y_pos_corr = n_data_sets;
2000 nc = heading_columns + factor->n_vars;
2003 if (factor->print & PRINT_SIG)
2005 y_pos_sig = n_data_sets;
2007 nc = heading_columns + factor->n_vars;
2010 nr += n_data_sets * factor->n_vars;
2012 if (factor->print & PRINT_DETERMINANT)
2015 /* If the table would contain only headings, don't bother rendering it */
2016 if (nr <= heading_rows && suffix_rows == 0)
2019 t = tab_create (nc, nr + suffix_rows);
2021 tab_title (t, _("Correlation Matrix"));
2023 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
2025 if (nr > heading_rows)
2027 tab_headers (t, heading_columns, 0, heading_rows, 0);
2029 tab_vline (t, TAL_2, 2, 0, nr - 1);
2031 /* Outline the box */
2038 /* Vertical lines */
2046 for (i = 0; i < factor->n_vars; ++i)
2047 tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
2050 for (i = 0 ; i < n_data_sets; ++i)
2052 int y = heading_rows + i * factor->n_vars;
2054 for (v = 0; v < factor->n_vars; ++v)
2055 tab_text (t, 1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
2057 tab_hline (t, TAL_1, 0, nc - 1, y);
2060 if (factor->print & PRINT_CORRELATION)
2062 const double y = heading_rows + y_pos_corr;
2063 tab_text (t, 0, y, TAT_TITLE, _("Correlations"));
2065 for (i = 0; i < factor->n_vars; ++i)
2067 for (j = 0; j < factor->n_vars; ++j)
2068 tab_double (t, heading_columns + i, y + j, 0, gsl_matrix_get (idata->mm.corr, i, j), NULL, RC_OTHER);
2072 if (factor->print & PRINT_SIG)
2074 const double y = heading_rows + y_pos_sig * factor->n_vars;
2075 tab_text (t, 0, y, TAT_TITLE, _("Sig. (1-tailed)"));
2077 for (i = 0; i < factor->n_vars; ++i)
2079 for (j = 0; j < factor->n_vars; ++j)
2081 double rho = gsl_matrix_get (idata->mm.corr, i, j);
2082 double w = gsl_matrix_get (idata->mm.n, i, j);
2087 tab_double (t, heading_columns + i, y + j, 0, significance_of_correlation (rho, w), NULL, RC_PVALUE);
2093 if (factor->print & PRINT_DETERMINANT)
2095 tab_text (t, 0, nr, TAB_LEFT | TAT_TITLE, _("Determinant"));
2097 tab_double (t, 1, nr, 0, idata->detR, NULL, RC_OTHER);
2105 do_factor (const struct cmd_factor *factor, struct casereader *r)
2108 struct idata *idata = idata_alloc (factor->n_vars);
2110 idata->cvm = covariance_1pass_create (factor->n_vars, factor->vars,
2111 factor->wv, factor->exclude);
2113 for ( ; (c = casereader_read (r) ); case_unref (c))
2115 covariance_accumulate (idata->cvm, c);
2118 idata->mm.cov = covariance_calculate (idata->cvm);
2120 if (idata->mm.cov == NULL)
2122 msg (MW, _("The dataset contains no complete observations. No analysis will be performed."));
2123 covariance_destroy (idata->cvm);
2127 idata->mm.var_matrix = covariance_moments (idata->cvm, MOMENT_VARIANCE);
2128 idata->mm.mean_matrix = covariance_moments (idata->cvm, MOMENT_MEAN);
2129 idata->mm.n = covariance_moments (idata->cvm, MOMENT_NONE);
2131 do_factor_by_matrix (factor, idata);
2135 casereader_destroy (r);
2139 do_factor_by_matrix (const struct cmd_factor *factor, struct idata *idata)
2141 if (idata->mm.cov && !idata->mm.corr)
2142 idata->mm.corr = correlation_from_covariance (idata->mm.cov, idata->mm.var_matrix);
2143 if (idata->mm.corr && !idata->mm.cov)
2144 idata->mm.cov = covariance_from_correlation (idata->mm.corr, idata->mm.var_matrix);
2145 if (factor->method == METHOD_CORR)
2146 idata->analysis_matrix = idata->mm.corr;
2148 idata->analysis_matrix = idata->mm.cov;
2150 if (factor->print & PRINT_DETERMINANT
2151 || factor->print & PRINT_KMO)
2155 const int size = idata->mm.corr->size1;
2156 gsl_permutation *p = gsl_permutation_calloc (size);
2157 gsl_matrix *tmp = gsl_matrix_calloc (size, size);
2158 gsl_matrix_memcpy (tmp, idata->mm.corr);
2160 gsl_linalg_LU_decomp (tmp, p, &sign);
2161 idata->detR = gsl_linalg_LU_det (tmp, sign);
2162 gsl_permutation_free (p);
2163 gsl_matrix_free (tmp);
2166 if ( factor->print & PRINT_UNIVARIATE)
2168 const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
2172 const int heading_columns = 1;
2173 const int heading_rows = 1;
2175 const int nr = heading_rows + factor->n_vars;
2177 struct tab_table *t = tab_create (nc, nr);
2178 tab_set_format (t, RC_WEIGHT, wfmt);
2179 tab_title (t, _("Descriptive Statistics"));
2181 tab_headers (t, heading_columns, 0, heading_rows, 0);
2183 /* Outline the box */
2190 /* Vertical lines */
2197 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
2198 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
2200 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
2201 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
2202 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
2204 for (i = 0 ; i < factor->n_vars; ++i)
2206 const struct variable *v = factor->vars[i];
2207 tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
2209 tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (idata->mm.mean_matrix, i, i), NULL, RC_OTHER);
2210 tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (idata->mm.var_matrix, i, i)), NULL, RC_OTHER);
2211 tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (idata->mm.n, i, i), NULL, RC_WEIGHT);
2217 if (factor->print & PRINT_KMO)
2220 double sum_ssq_r = 0;
2221 double sum_ssq_a = 0;
2223 double df = factor->n_vars * (factor->n_vars - 1) / 2;
2230 const int heading_columns = 2;
2231 const int heading_rows = 0;
2233 const int nr = heading_rows + 4;
2234 const int nc = heading_columns + 1;
2238 struct tab_table *t = tab_create (nc, nr);
2239 tab_title (t, _("KMO and Bartlett's Test"));
2241 x = clone_matrix (idata->mm.corr);
2242 gsl_linalg_cholesky_decomp (x);
2243 gsl_linalg_cholesky_invert (x);
2247 for (i = 0; i < x->size1; ++i)
2249 sum_ssq_r += ssq_od_n (x, i);
2250 sum_ssq_a += ssq_od_n (a, i);
2253 gsl_matrix_free (a);
2254 gsl_matrix_free (x);
2256 tab_headers (t, heading_columns, 0, heading_rows, 0);
2258 /* Outline the box */
2265 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
2267 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Kaiser-Meyer-Olkin Measure of Sampling Adequacy"));
2269 tab_double (t, 2, 0, 0, sum_ssq_r / (sum_ssq_r + sum_ssq_a), NULL, RC_OTHER);
2271 tab_text (t, 0, 1, TAT_TITLE | TAB_LEFT, _("Bartlett's Test of Sphericity"));
2273 tab_text (t, 1, 1, TAT_TITLE, _("Approx. Chi-Square"));
2274 tab_text (t, 1, 2, TAT_TITLE, _("df"));
2275 tab_text (t, 1, 3, TAT_TITLE, _("Sig."));
2278 /* The literature doesn't say what to do for the value of W when
2279 missing values are involved. The best thing I can think of
2280 is to take the mean average. */
2282 for (i = 0; i < idata->mm.n->size1; ++i)
2283 w += gsl_matrix_get (idata->mm.n, i, i);
2284 w /= idata->mm.n->size1;
2286 xsq = w - 1 - (2 * factor->n_vars + 5) / 6.0;
2287 xsq *= -log (idata->detR);
2289 tab_double (t, 2, 1, 0, xsq, NULL, RC_OTHER);
2290 tab_double (t, 2, 2, 0, df, NULL, RC_INTEGER);
2291 tab_double (t, 2, 3, 0, gsl_cdf_chisq_Q (xsq, df), NULL, RC_PVALUE);
2297 show_correlation_matrix (factor, idata);
2299 covariance_destroy (idata->cvm);
2302 gsl_matrix *am = matrix_dup (idata->analysis_matrix);
2303 gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
2305 gsl_eigen_symmv (am, idata->eval, idata->evec, workspace);
2307 gsl_eigen_symmv_free (workspace);
2308 gsl_matrix_free (am);
2311 gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
2313 idata->n_extractions = n_extracted_factors (factor, idata);
2315 if (idata->n_extractions == 0)
2317 msg (MW, _("The %s criteria result in zero factors extracted. Therefore no analysis will be performed."), "FACTOR");
2321 if (idata->n_extractions > factor->n_vars)
2324 _("The %s criteria result in more factors than variables, which is not meaningful. No analysis will be performed."),
2330 gsl_matrix *rotated_factors = NULL;
2331 gsl_matrix *pattern_matrix = NULL;
2332 gsl_matrix *fcm = NULL;
2333 gsl_vector *rotated_loadings = NULL;
2335 const gsl_vector *extracted_eigenvalues = NULL;
2336 gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
2337 gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
2339 struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, idata->n_extractions);
2340 gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
2342 if ( factor->extraction == EXTRACTION_PAF)
2344 gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
2345 struct smr_workspace *ws = ws_create (idata->analysis_matrix);
2347 for (i = 0 ; i < factor->n_vars ; ++i)
2349 double r2 = squared_multiple_correlation (idata->analysis_matrix, i, ws);
2351 gsl_vector_set (idata->msr, i, r2);
2355 gsl_vector_memcpy (initial_communalities, idata->msr);
2357 for (i = 0; i < factor->extraction_iterations; ++i)
2360 gsl_vector_memcpy (diff, idata->msr);
2362 iterate_factor_matrix (idata->analysis_matrix, idata->msr, factor_matrix, fmw);
2364 gsl_vector_sub (diff, idata->msr);
2366 gsl_vector_minmax (diff, &min, &max);
2368 if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
2371 gsl_vector_free (diff);
2375 gsl_vector_memcpy (extracted_communalities, idata->msr);
2376 extracted_eigenvalues = fmw->eval;
2378 else if (factor->extraction == EXTRACTION_PC)
2380 for (i = 0; i < factor->n_vars; ++i)
2381 gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
2383 gsl_vector_memcpy (extracted_communalities, initial_communalities);
2385 iterate_factor_matrix (idata->analysis_matrix, extracted_communalities, factor_matrix, fmw);
2388 extracted_eigenvalues = idata->eval;
2392 show_communalities (factor, initial_communalities, extracted_communalities);
2395 if ( factor->rotation != ROT_NONE)
2397 rotated_factors = gsl_matrix_calloc (factor_matrix->size1, factor_matrix->size2);
2398 rotated_loadings = gsl_vector_calloc (factor_matrix->size2);
2399 if (factor->rotation == ROT_PROMAX)
2401 pattern_matrix = gsl_matrix_calloc (factor_matrix->size1, factor_matrix->size2);
2402 fcm = gsl_matrix_calloc (factor_matrix->size2, factor_matrix->size2);
2406 rotate (factor, factor_matrix, extracted_communalities, rotated_factors, rotated_loadings, pattern_matrix, fcm);
2409 show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues, rotated_loadings);
2411 factor_matrix_workspace_free (fmw);
2413 show_scree (factor, idata);
2415 show_factor_matrix (factor, idata,
2416 factor->extraction == EXTRACTION_PC ? _("Component Matrix") : _("Factor Matrix"),
2419 if ( factor->rotation == ROT_PROMAX)
2421 show_factor_matrix (factor, idata, _("Pattern Matrix"), pattern_matrix);
2422 gsl_matrix_free (pattern_matrix);
2425 if ( factor->rotation != ROT_NONE)
2427 show_factor_matrix (factor, idata,
2428 (factor->rotation == ROT_PROMAX) ? _("Structure Matrix") :
2429 (factor->extraction == EXTRACTION_PC ? _("Rotated Component Matrix") :
2430 _("Rotated Factor Matrix")),
2433 gsl_matrix_free (rotated_factors);
2436 if ( factor->rotation == ROT_PROMAX)
2438 show_factor_correlation (factor, fcm);
2439 gsl_matrix_free (fcm);
2442 gsl_matrix_free (factor_matrix);
2443 gsl_vector_free (rotated_loadings);
2444 gsl_vector_free (initial_communalities);
2445 gsl_vector_free (extracted_communalities);