FACTOR: Ensure a non-zero number of factors.
[pspp-builds.git] / src / language / stats / factor.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19
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
27 #include <math/covariance.h>
28
29 #include <math/correlation.h>
30 #include <math/moments.h>
31 #include <data/procedure.h>
32 #include <language/lexer/variable-parser.h>
33 #include <language/lexer/value-parser.h>
34 #include <language/command.h>
35 #include <language/lexer/lexer.h>
36
37 #include <data/casegrouper.h>
38 #include <data/casereader.h>
39 #include <data/casewriter.h>
40 #include <data/dictionary.h>
41 #include <data/format.h>
42 #include <data/subcase.h>
43
44 #include <libpspp/misc.h>
45 #include <libpspp/message.h>
46
47 #include <output/table.h>
48
49
50 #include "gettext.h"
51 #define _(msgid) gettext (msgid)
52 #define N_(msgid) msgid
53
54 enum method
55   {
56     METHOD_CORR,
57     METHOD_COV
58   };
59
60 enum missing_type
61   {
62     MISS_LISTWISE,
63     MISS_PAIRWISE,
64     MISS_MEANSUB,
65   };
66
67 enum extraction_method
68   {
69     EXTRACTION_PC,
70     EXTRACTION_PAF,
71   };
72
73 enum print_opts
74   {
75     PRINT_UNIVARIATE  = 0x0001,
76     PRINT_DETERMINANT = 0x0002,
77     PRINT_INV         = 0x0004,
78     PRINT_AIC         = 0x0008,
79     PRINT_SIG         = 0x0010,
80     PRINT_COVARIANCE  = 0x0020,
81     PRINT_CORRELATION = 0x0040,
82     PRINT_ROTATION    = 0x0080,
83     PRINT_EXTRACTION  = 0x0100,
84     PRINT_INITIAL     = 0x0200,
85     PRINT_KMO         = 0x0400,
86     PRINT_REPR        = 0x0800, 
87     PRINT_FSCORE      = 0x1000
88   };
89
90
91 struct cmd_factor 
92 {
93   size_t n_vars;
94   const struct variable **vars;
95
96   const struct variable *wv;
97
98   enum method method;
99   enum missing_type missing_type;
100   enum mv_class exclude;
101   enum print_opts print;
102   enum extraction_method extraction;
103
104   /* Extraction Criteria */
105   int n_factors;
106   double min_eigen;
107   double econverge;
108   int iterations;
109
110   /* Format */
111   double blank;
112   bool sort;
113 };
114
115 struct idata
116 {
117   /* Intermediate values used in calculation */
118
119   const gsl_matrix *corr ;  /* The correlation matrix */
120   const gsl_matrix *cov ;   /* The covariance matrix */
121   const gsl_matrix *n ;     /* Matrix of number of samples */
122
123   gsl_vector *eval ;  /* The eigenvalues */
124   gsl_matrix *evec ;  /* The eigenvectors */
125
126   int n_extractions;
127
128   gsl_vector *msr ;  /* Multiple Squared Regressions */
129 };
130
131 static struct idata *
132 idata_alloc (size_t n_vars)
133 {
134   struct idata *id = xzalloc (sizeof (*id));
135
136   id->n_extractions = 0;
137   id->msr = gsl_vector_alloc (n_vars);
138
139   id->eval = gsl_vector_alloc (n_vars);
140   id->evec = gsl_matrix_alloc (n_vars, n_vars);
141
142   return id;
143 }
144
145 static void
146 idata_free (struct idata *id)
147 {
148   gsl_vector_free (id->msr);
149   gsl_vector_free (id->eval);
150   gsl_matrix_free (id->evec);
151
152   free (id);
153 }
154
155
156 static void
157 dump_matrix (const gsl_matrix *m)
158 {
159   size_t i, j;
160
161   for (i = 0 ; i < m->size1; ++i)
162     {
163       for (j = 0 ; j < m->size2; ++j)
164         printf ("%02f ", gsl_matrix_get (m, i, j));
165       printf ("\n");
166     }
167 }
168
169
170 static void
171 dump_matrix_permute (const gsl_matrix *m, const gsl_permutation *p)
172 {
173   size_t i, j;
174
175   for (i = 0 ; i < m->size1; ++i)
176     {
177       for (j = 0 ; j < m->size2; ++j)
178         printf ("%02f ", gsl_matrix_get (m, gsl_permutation_get (p, i), j));
179       printf ("\n");
180     }
181 }
182
183
184 static void
185 dump_vector (const gsl_vector *v)
186 {
187   size_t i;
188   for (i = 0 ; i < v->size; ++i)
189     {
190       printf ("%02f\n", gsl_vector_get (v, i));
191     }
192   printf ("\n");
193 }
194
195
196 static int 
197 n_extracted_factors (const struct cmd_factor *factor, struct idata *idata)
198 {
199   int i;
200   
201   /* If there is a cached value, then return that. */
202   if ( idata->n_extractions != 0)
203     return idata->n_extractions;
204
205   /* Otherwise, if the number of factors has been explicitly requested,
206      use that. */
207   if (factor->n_factors > 0)
208     {
209       idata->n_extractions = factor->n_factors;
210       goto finish;
211     }
212   
213   /* Use the MIN_EIGEN setting. */
214   for (i = 0 ; i < idata->eval->size; ++i)
215     {
216       double evali = fabs (gsl_vector_get (idata->eval, i));
217
218       idata->n_extractions = i;
219
220       if (evali < factor->min_eigen)
221         goto finish;
222     }
223
224  finish:
225   return idata->n_extractions;
226 }
227
228
229 /* Returns a newly allocated matrix identical to M.
230    It it the callers responsibility to free the returned value.
231 */
232 static gsl_matrix *
233 matrix_dup (const gsl_matrix *m)
234 {
235   gsl_matrix *n =  gsl_matrix_alloc (m->size1, m->size2);
236
237   gsl_matrix_memcpy (n, m);
238
239   return n;
240 }
241
242
243 struct smr_workspace
244 {
245   /* Copy of the subject */
246   gsl_matrix *m;
247   
248   gsl_matrix *inverse;
249
250   gsl_permutation *perm;
251
252   gsl_matrix *result1;
253   gsl_matrix *result2;
254 };
255
256
257 static struct smr_workspace *ws_create (const gsl_matrix *input)
258 {
259   struct smr_workspace *ws = xmalloc (sizeof (*ws));
260   
261   ws->m = gsl_matrix_alloc (input->size1, input->size2);
262   ws->inverse = gsl_matrix_calloc (input->size1 - 1, input->size2 - 1);
263   ws->perm = gsl_permutation_alloc (input->size1 - 1);
264   ws->result1 = gsl_matrix_calloc (input->size1 - 1, 1);
265   ws->result2 = gsl_matrix_calloc (1, 1);
266
267   return ws;
268 }
269
270 static void
271 ws_destroy (struct smr_workspace *ws)
272 {
273   gsl_matrix_free (ws->result2);
274   gsl_matrix_free (ws->result1);
275   gsl_permutation_free (ws->perm);
276   gsl_matrix_free (ws->inverse);
277   gsl_matrix_free (ws->m);
278
279   free (ws);
280 }
281
282
283 /* 
284    Return the square of the regression coefficient for VAR regressed against all other variables.
285  */
286 static double
287 squared_multiple_correlation (const gsl_matrix *corr, int var, struct smr_workspace *ws)
288 {
289   /* For an explanation of what this is doing, see 
290      http://www.visualstatistics.net/Visual%20Statistics%20Multimedia/multiple_regression_analysis.htm
291   */
292
293   int signum = 0;
294   gsl_matrix_view rxx;
295
296   gsl_matrix_memcpy (ws->m, corr);
297
298   gsl_matrix_swap_rows (ws->m, 0, var);
299   gsl_matrix_swap_columns (ws->m, 0, var);
300
301   rxx = gsl_matrix_submatrix (ws->m, 1, 1, ws->m->size1 - 1, ws->m->size1 - 1); 
302
303   gsl_linalg_LU_decomp (&rxx.matrix, ws->perm, &signum);
304
305   gsl_linalg_LU_invert (&rxx.matrix, ws->perm, ws->inverse);
306
307   {
308     gsl_matrix_const_view rxy = gsl_matrix_const_submatrix (ws->m, 1, 0, ws->m->size1 - 1, 1);
309     gsl_matrix_const_view ryx = gsl_matrix_const_submatrix (ws->m, 0, 1, 1, ws->m->size1 - 1);
310
311     gsl_blas_dgemm (CblasNoTrans,  CblasNoTrans,
312                     1.0, ws->inverse, &rxy.matrix, 0.0, ws->result1);
313
314     gsl_blas_dgemm (CblasNoTrans,  CblasNoTrans,
315                     1.0, &ryx.matrix, ws->result1, 0.0, ws->result2);
316   }
317
318   return gsl_matrix_get (ws->result2, 0, 0);
319 }
320
321
322
323 static double the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors);
324
325
326 struct factor_matrix_workspace
327 {
328   size_t n_factors;
329   gsl_eigen_symmv_workspace *eigen_ws;
330
331   gsl_vector *eval ;
332   gsl_matrix *evec ;
333
334   gsl_matrix *gamma ;
335
336   gsl_matrix *r;
337 };
338
339 static struct factor_matrix_workspace *
340 factor_matrix_workspace_alloc (size_t n, size_t nf)
341 {
342   struct factor_matrix_workspace *ws = xmalloc (sizeof (*ws));
343
344   ws->n_factors = nf;
345   ws->gamma = gsl_matrix_calloc (nf, nf);
346   ws->eigen_ws = gsl_eigen_symmv_alloc (n);
347   ws->eval = gsl_vector_alloc (n);
348   ws->evec = gsl_matrix_alloc (n, n);
349   ws->r  = gsl_matrix_alloc (n, n);
350   
351   return ws;
352 }
353
354 static void
355 factor_matrix_workspace_free (struct factor_matrix_workspace *ws)
356 {
357   gsl_eigen_symmv_free (ws->eigen_ws);
358   gsl_vector_free (ws->eval);
359   gsl_matrix_free (ws->evec);
360   gsl_matrix_free (ws->gamma);
361   gsl_matrix_free (ws->r);
362   free (ws);
363 }
364
365 /*
366   Shift P left by OFFSET places, and overwrite TARGET
367   with the shifted result.
368   Positions in TARGET less than OFFSET are unchanged.
369 */
370 static void
371 perm_shift_apply (gsl_permutation *target, const gsl_permutation *p,
372                   size_t offset)
373 {
374   size_t i;
375   assert (target->size == p->size);
376   assert (offset <= target->size);
377
378   for (i = 0; i < target->size - offset; ++i)
379     {
380       target->data[i] = p->data [i + offset];
381     }
382 }
383
384
385 /* 
386    Indirectly sort the rows of matrix INPUT, storing the sort order in PERM.
387    The sort criteria are as follows:
388    
389    Rows are sorted on the first column, until the absolute value of an
390    element in a subsequent column  is greater than that of the first
391    column.  Thereafter, rows will be sorted on the second column,
392    until the absolute value of an element in a subsequent column
393    exceeds that of the second column ...
394 */
395 static void
396 sort_matrix_indirect (const gsl_matrix *input, gsl_permutation *perm)
397 {
398   const size_t n = perm->size;
399   const size_t m = input->size2;
400   int i, j;
401   gsl_matrix *mat ;
402   int column_n = 0;
403   int row_n = 0;
404   gsl_permutation *p;
405
406   assert (perm->size == input->size1);
407
408   p = gsl_permutation_alloc (n);
409
410   /* Copy INPUT into MAT, discarding the sign */
411   mat = gsl_matrix_alloc (n, m);
412   for (i = 0 ; i < mat->size1; ++i)
413     {
414       for (j = 0 ; j < mat->size2; ++j)
415         {
416           double x = gsl_matrix_get (input, i, j);
417           gsl_matrix_set (mat, i, j, fabs (x));
418         }
419     }
420
421   while (column_n < m && row_n < n) 
422     {
423       gsl_vector_const_view columni = gsl_matrix_const_column (mat, column_n);
424       gsl_sort_vector_index (p, &columni.vector);
425
426       for (i = 0 ; i < n; ++i)
427         {
428           gsl_vector_view row = gsl_matrix_row (mat, p->data[n - 1 - i]);
429           size_t maxindex = gsl_vector_max_index (&row.vector);
430           
431           if ( maxindex > column_n )
432             break;
433
434           /* All subsequent elements of this row, are of no interest.
435              So set them all to a highly negative value */
436           for (j = column_n + 1; j < row.vector.size ; ++j)
437             gsl_vector_set (&row.vector, j, -DBL_MAX);
438         }
439
440       perm_shift_apply (perm, p, row_n);
441       row_n += i;
442
443       column_n++;
444     }
445
446   gsl_permutation_free (p);
447   gsl_matrix_free (mat);
448   
449   assert ( 0 == gsl_permutation_valid (perm));
450
451   /* We want the biggest value to be first */
452   gsl_permutation_reverse (perm);    
453 }
454
455
456 /*
457   Get an approximation for the factor matrix into FACTORS, and the communalities into COMMUNALITIES.
458   R is the matrix to be analysed.
459   WS is a pointer to a structure which must have been initialised with factor_matrix_workspace_init.
460  */
461 static void
462 iterate_factor_matrix (const gsl_matrix *r, gsl_vector *communalities, gsl_matrix *factors, struct factor_matrix_workspace *ws)
463 {
464   size_t i;
465   gsl_matrix_view mv ;
466
467   assert (r->size1 == r->size2);
468   assert (r->size1 == communalities->size);
469
470   assert (factors->size1 == r->size1);
471   assert (factors->size2 == ws->n_factors);
472
473   gsl_matrix_memcpy (ws->r, r);
474
475   /* Apply Communalities to diagonal of correlation matrix */
476   for (i = 0 ; i < communalities->size ; ++i)
477     {
478       double *x = gsl_matrix_ptr (ws->r, i, i);
479       *x = gsl_vector_get (communalities, i);
480     }
481
482   gsl_eigen_symmv (ws->r, ws->eval, ws->evec, ws->eigen_ws);
483
484   mv = gsl_matrix_submatrix (ws->evec, 0, 0, ws->evec->size1, ws->n_factors);
485
486   /* Gamma is the diagonal matrix containing the absolute values of the eigenvalues */
487   for (i = 0 ; i < ws->n_factors ; ++i)
488     {
489       double *ptr = gsl_matrix_ptr (ws->gamma, i, i);
490       *ptr = fabs (gsl_vector_get (ws->eval, i));
491     }
492
493   /* Take the square root of gamma */
494   gsl_linalg_cholesky_decomp (ws->gamma);
495
496   gsl_blas_dgemm (CblasNoTrans,  CblasNoTrans,
497                   1.0, &mv.matrix, ws->gamma, 0.0, factors);
498
499   for (i = 0 ; i < r->size1 ; ++i)
500     {
501       double h = the_communality (ws->evec, ws->eval, i, ws->n_factors);
502       gsl_vector_set (communalities, i, h);
503     }
504 }
505
506
507
508 static bool run_factor (struct dataset *ds, const struct cmd_factor *factor);
509
510
511 int
512 cmd_factor (struct lexer *lexer, struct dataset *ds)
513 {
514   bool extraction_seen = false;
515   const struct dictionary *dict = dataset_dict (ds);
516
517   struct cmd_factor factor;
518   factor.method = METHOD_CORR;
519   factor.missing_type = MISS_LISTWISE;
520   factor.exclude = MV_ANY;
521   factor.print = PRINT_INITIAL | PRINT_EXTRACTION | PRINT_ROTATION;
522   factor.extraction = EXTRACTION_PC;
523   factor.n_factors = 0;
524   factor.min_eigen = SYSMIS;
525   factor.iterations = 25;
526   factor.econverge = 0.001;
527   factor.blank = 0;
528   factor.sort = false;
529
530   factor.wv = dict_get_weight (dict);
531
532   lex_match (lexer, '/');
533
534   if (!lex_force_match_id (lexer, "VARIABLES"))
535     {
536       goto error;
537     }
538
539   lex_match (lexer, '=');
540
541   if (!parse_variables_const (lexer, dict, &factor.vars, &factor.n_vars,
542                               PV_NO_DUPLICATE | PV_NUMERIC))
543     goto error;
544
545   if (factor.n_vars < 2)
546     msg (MW, _("Factor analysis on a single variable is not useful."));
547
548   while (lex_token (lexer) != '.')
549     {
550       lex_match (lexer, '/');
551
552
553       if (lex_match_id (lexer, "PLOT"))
554         {
555           lex_match (lexer, '=');
556           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
557             {
558               if (lex_match_id (lexer, "EIGEN"))
559                 {
560                 }
561 #if FACTOR_FULLY_IMPLEMENTED
562               else if (lex_match_id (lexer, "ROTATION"))
563                 {
564                 }
565 #endif
566               else
567                 {
568                   lex_error (lexer, NULL);
569                   goto error;
570                 }
571             }
572         }
573       else if (lex_match_id (lexer, "METHOD"))
574         {
575           lex_match (lexer, '=');
576           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
577             {
578               if (lex_match_id (lexer, "COVARIANCE"))
579                 {
580                   factor.method = METHOD_COV;
581                 }
582               else if (lex_match_id (lexer, "CORRELATION"))
583                 {
584                   factor.method = METHOD_CORR;
585                 }
586               else
587                 {
588                   lex_error (lexer, NULL);
589                   goto error;
590                 }
591             }
592         }
593 #if FACTOR_FULLY_IMPLEMENTED
594       else if (lex_match_id (lexer, "ROTATION"))
595         {
596           lex_match (lexer, '=');
597           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
598             {
599               if (lex_match_id (lexer, "VARIMAX"))
600                 {
601                 }
602               else if (lex_match_id (lexer, "DEFAULT"))
603                 {
604                 }
605               else
606                 {
607                   lex_error (lexer, NULL);
608                   goto error;
609                 }
610             }
611         }
612 #endif
613       else if (lex_match_id (lexer, "CRITERIA"))
614         {
615           lex_match (lexer, '=');
616           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
617             {
618               if (lex_match_id (lexer, "FACTORS"))
619                 {
620                   if ( lex_force_match (lexer, '('))
621                     {
622                       lex_force_int (lexer);
623                       factor.n_factors = lex_integer (lexer);
624                       lex_get (lexer);
625                       lex_force_match (lexer, ')');
626                     }
627                 }
628               else if (lex_match_id (lexer, "MINEIGEN"))
629                 {
630                   if ( lex_force_match (lexer, '('))
631                     {
632                       lex_force_num (lexer);
633                       factor.min_eigen = lex_number (lexer);
634                       lex_get (lexer);
635                       lex_force_match (lexer, ')');
636                     }
637                 }
638               else if (lex_match_id (lexer, "ECONVERGE"))
639                 {
640                   if ( lex_force_match (lexer, '('))
641                     {
642                       lex_force_num (lexer);
643                       factor.econverge = lex_number (lexer);
644                       lex_get (lexer);
645                       lex_force_match (lexer, ')');
646                     }
647                 }
648               else if (lex_match_id (lexer, "ITERATE"))
649                 {
650                   if ( lex_force_match (lexer, '('))
651                     {
652                       lex_force_int (lexer);
653                       factor.iterations = lex_integer (lexer);
654                       lex_get (lexer);
655                       lex_force_match (lexer, ')');
656                     }
657                 }
658               else if (lex_match_id (lexer, "DEFAULT"))
659                 {
660                   factor.n_factors = 0;
661                   factor.min_eigen = 1;
662                   factor.iterations = 25;
663                 }
664               else
665                 {
666                   lex_error (lexer, NULL);
667                   goto error;
668                 }
669             }
670         }
671       else if (lex_match_id (lexer, "EXTRACTION"))
672         {
673           extraction_seen = true;
674           lex_match (lexer, '=');
675           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
676             {
677               if (lex_match_id (lexer, "PAF"))
678                 {
679                   factor.extraction = EXTRACTION_PAF;
680                 }
681               else if (lex_match_id (lexer, "PC"))
682                 {
683                   factor.extraction = EXTRACTION_PC;
684                 }
685               else if (lex_match_id (lexer, "PA1"))
686                 {
687                   factor.extraction = EXTRACTION_PC;
688                 }
689               else if (lex_match_id (lexer, "DEFAULT"))
690                 {
691                   factor.extraction = EXTRACTION_PC;
692                 }
693               else
694                 {
695                   lex_error (lexer, NULL);
696                   goto error;
697                 }
698             }
699         }
700       else if (lex_match_id (lexer, "FORMAT"))
701         {
702           lex_match (lexer, '=');
703           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
704             {
705               if (lex_match_id (lexer, "SORT"))
706                 {
707                   factor.sort = true;
708                 }
709               else if (lex_match_id (lexer, "BLANK"))
710                 {
711                   if ( lex_force_match (lexer, '('))
712                     {
713                       lex_force_num (lexer);
714                       factor.blank = lex_number (lexer);
715                       lex_get (lexer);
716                       lex_force_match (lexer, ')');
717                     }
718                 }
719               else if (lex_match_id (lexer, "DEFAULT"))
720                 {
721                   factor.blank = 0;
722                   factor.sort = false;
723                 }
724               else
725                 {
726                   lex_error (lexer, NULL);
727                   goto error;
728                 }
729             }
730         }
731       else if (lex_match_id (lexer, "PRINT"))
732         {
733           factor.print = 0;
734           lex_match (lexer, '=');
735           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
736             {
737               if (lex_match_id (lexer, "UNIVARIATE"))
738                 {
739                   factor.print |= PRINT_UNIVARIATE;
740                 }
741               else if (lex_match_id (lexer, "DET"))
742                 {
743                   factor.print |= PRINT_DETERMINANT;
744                 }
745 #if FACTOR_FULLY_IMPLEMENTED
746               else if (lex_match_id (lexer, "INV"))
747                 {
748                 }
749               else if (lex_match_id (lexer, "AIC"))
750                 {
751                 }
752 #endif
753               else if (lex_match_id (lexer, "SIG"))
754                 {
755                   factor.print |= PRINT_SIG;
756                 }
757               else if (lex_match_id (lexer, "CORRELATION"))
758                 {
759                   factor.print |= PRINT_CORRELATION;
760                 }
761 #if FACTOR_FULLY_IMPLEMENTED
762               else if (lex_match_id (lexer, "COVARIANCE"))
763                 {
764                 }
765 #endif
766               else if (lex_match_id (lexer, "ROTATION"))
767                 {
768                   factor.print |= PRINT_ROTATION;
769                 }
770               else if (lex_match_id (lexer, "EXTRACTION"))
771                 {
772                   factor.print |= PRINT_EXTRACTION;
773                 }
774               else if (lex_match_id (lexer, "INITIAL"))
775                 {
776                   factor.print |= PRINT_INITIAL;
777                 }
778 #if FACTOR_FULLY_IMPLEMENTED
779               else if (lex_match_id (lexer, "KMO"))
780                 {
781                 }
782               else if (lex_match_id (lexer, "REPR"))
783                 {
784                 }
785               else if (lex_match_id (lexer, "FSCORE"))
786                 {
787                 }
788 #endif
789               else if (lex_match (lexer, T_ALL))
790                 {
791                   factor.print = 0xFFFF;
792                 }
793               else if (lex_match_id (lexer, "DEFAULT"))
794                 {
795                   factor.print |= PRINT_INITIAL ;
796                   factor.print |= PRINT_EXTRACTION ;
797                   factor.print |= PRINT_ROTATION ;
798                 }
799               else
800                 {
801                   lex_error (lexer, NULL);
802                   goto error;
803                 }
804             }
805         }
806       else if (lex_match_id (lexer, "MISSING"))
807         {
808           lex_match (lexer, '=');
809           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
810             {
811               if (lex_match_id (lexer, "INCLUDE"))
812                 {
813                   factor.exclude = MV_SYSTEM;
814                 }
815               else if (lex_match_id (lexer, "EXCLUDE"))
816                 {
817                   factor.exclude = MV_ANY;
818                 }
819               else if (lex_match_id (lexer, "LISTWISE"))
820                 {
821                   factor.missing_type = MISS_LISTWISE;
822                 }
823               else if (lex_match_id (lexer, "PAIRWISE"))
824                 {
825                   factor.missing_type = MISS_PAIRWISE;
826                 }
827               else if (lex_match_id (lexer, "MEANSUB"))
828                 {
829                   factor.missing_type = MISS_MEANSUB;
830                 }
831               else
832                 {
833                   lex_error (lexer, NULL);
834                   goto error;
835                 }
836             }
837         }
838       else
839         {
840           lex_error (lexer, NULL);
841           goto error;
842         }
843     }
844
845   if ( ! run_factor (ds, &factor)) 
846     goto error;
847
848   free (factor.vars);
849   return CMD_SUCCESS;
850
851  error:
852   free (factor.vars);
853   return CMD_FAILURE;
854 }
855
856 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
857
858
859 static bool
860 run_factor (struct dataset *ds, const struct cmd_factor *factor)
861 {
862   struct dictionary *dict = dataset_dict (ds);
863   bool ok;
864   struct casereader *group;
865
866   struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
867
868   while (casegrouper_get_next_group (grouper, &group))
869     {
870       if ( factor->missing_type == MISS_LISTWISE )
871         group  = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
872                                                    factor->exclude,
873                                                    NULL,  NULL);
874       do_factor (factor, group);
875     }
876
877   ok = casegrouper_destroy (grouper);
878   ok = proc_commit (ds) && ok;
879
880   return ok;
881 }
882
883
884 /* Return the communality of variable N, calculated to N_FACTORS */
885 static double
886 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
887 {
888   size_t i;
889
890   double comm = 0;
891
892   assert (n >= 0);
893   assert (n < eval->size);
894   assert (n < evec->size1);
895   assert (n_factors <= eval->size);
896
897   for (i = 0 ; i < n_factors; ++i)
898     {
899       double evali = fabs (gsl_vector_get (eval, i));
900
901       double eveci = gsl_matrix_get (evec, n, i);
902
903       comm += pow2 (eveci) * evali;
904     }
905
906   return comm;
907 }
908
909 /* Return the communality of variable N, calculated to N_FACTORS */
910 static double
911 communality (struct idata *idata, int n, int n_factors)
912 {
913   return the_communality (idata->evec, idata->eval, n, n_factors);
914 }
915
916
917
918 static void
919 show_communalities (const struct cmd_factor * factor,
920                     const gsl_vector *initial, const gsl_vector *extracted)
921 {
922   int i;
923   int c = 0;
924   const int heading_columns = 1;
925   int nc = heading_columns;
926   const int heading_rows = 1;
927   const int nr = heading_rows + factor->n_vars;
928   struct tab_table *t;
929
930   if (factor->print & PRINT_EXTRACTION)
931     nc++;
932
933   if (factor->print & PRINT_INITIAL)
934     nc++;
935
936   /* No point having a table with only headings */
937   if (nc <= 1)
938     return;
939
940   t = tab_create (nc, nr, 0);
941
942   tab_title (t, _("Communalities"));
943
944   tab_dim (t, tab_natural_dimensions, NULL);
945
946   tab_headers (t, heading_columns, 0, heading_rows, 0);
947
948   c = 1;
949   if (factor->print & PRINT_INITIAL)
950     tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
951
952   if (factor->print & PRINT_EXTRACTION)
953     tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
954
955   /* Outline the box */
956   tab_box (t,
957            TAL_2, TAL_2,
958            -1, -1,
959            0, 0,
960            nc - 1, nr - 1);
961
962   /* Vertical lines */
963   tab_box (t,
964            -1, -1,
965            -1, TAL_1,
966            heading_columns, 0,
967            nc - 1, nr - 1);
968
969   tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
970   tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
971
972   for (i = 0 ; i < factor->n_vars; ++i)
973     {
974       c = 0;
975       tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
976
977       if (factor->print & PRINT_INITIAL)
978         tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL);
979
980       if (factor->print & PRINT_EXTRACTION)
981         tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL);
982     }
983
984   tab_submit (t);
985 }
986
987
988 static void
989 show_factor_matrix (const struct cmd_factor *factor, struct idata *idata, const gsl_matrix *fm)
990 {
991   int i;
992   const int n_factors = idata->n_extractions;
993
994   const int heading_columns = 1;
995   const int heading_rows = 2;
996   const int nr = heading_rows + factor->n_vars;
997   const int nc = heading_columns + n_factors;
998   gsl_permutation *perm;
999
1000   struct tab_table *t = tab_create (nc, nr, 0);
1001
1002   if ( factor->extraction == EXTRACTION_PC )
1003     tab_title (t, _("Component Matrix"));
1004   else 
1005     tab_title (t, _("Factor Matrix"));
1006
1007   tab_dim (t, tab_natural_dimensions, NULL);
1008
1009   tab_headers (t, heading_columns, 0, heading_rows, 0);
1010
1011   if ( factor->extraction == EXTRACTION_PC )
1012     tab_joint_text (t,
1013                     1, 0,
1014                     nc - 1, 0,
1015                     TAB_CENTER | TAT_TITLE, _("Component"));
1016   else
1017     tab_joint_text (t,
1018                     1, 0,
1019                     nc - 1, 0,
1020                     TAB_CENTER | TAT_TITLE, _("Factor"));
1021
1022
1023   tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1024
1025
1026   /* Outline the box */
1027   tab_box (t,
1028            TAL_2, TAL_2,
1029            -1, -1,
1030            0, 0,
1031            nc - 1, nr - 1);
1032
1033   /* Vertical lines */
1034   tab_box (t,
1035            -1, -1,
1036            -1, TAL_1,
1037            heading_columns, 1,
1038            nc - 1, nr - 1);
1039
1040   tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1041   tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1042
1043
1044   /* Initialise to the identity permutation */
1045   perm = gsl_permutation_calloc (factor->n_vars);
1046
1047   if ( factor->sort)
1048     sort_matrix_indirect (fm, perm);
1049
1050   for (i = 0 ; i < n_factors; ++i)
1051     {
1052       tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1053     }
1054
1055   for (i = 0 ; i < factor->n_vars; ++i)
1056     {
1057       int j;
1058       const int matrix_row = perm->data[i];
1059       tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1060
1061       for (j = 0 ; j < n_factors; ++j)
1062         {
1063           double x = gsl_matrix_get (fm, matrix_row, j);
1064
1065           if ( fabs (x) < factor->blank)
1066             continue;
1067
1068           tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL);
1069         }
1070     }
1071
1072   gsl_permutation_free (perm);
1073
1074   tab_submit (t);
1075 }
1076
1077
1078 static void
1079 show_explained_variance (const struct cmd_factor * factor, struct idata *idata,
1080                          const gsl_vector *initial_eigenvalues,
1081                          const gsl_vector *extracted_eigenvalues)
1082 {
1083   size_t i;
1084   int c = 0;
1085   const int heading_columns = 1;
1086   const int heading_rows = 2;
1087   const int nr = heading_rows + factor->n_vars;
1088
1089   struct tab_table *t ;
1090
1091   double i_total = 0.0;
1092   double i_cum = 0.0;
1093
1094   double e_total = 0.0;
1095   double e_cum = 0.0;
1096
1097   int nc = heading_columns;
1098
1099   if (factor->print & PRINT_EXTRACTION)
1100     nc += 3;
1101
1102   if (factor->print & PRINT_INITIAL)
1103     nc += 3;
1104
1105   if (factor->print & PRINT_ROTATION)
1106     nc += 3;
1107
1108   /* No point having a table with only headings */
1109   if ( nc <= heading_columns)
1110     return;
1111
1112   t = tab_create (nc, nr, 0);
1113
1114   tab_title (t, _("Total Variance Explained"));
1115
1116   tab_dim (t, tab_natural_dimensions, NULL);
1117
1118   tab_headers (t, heading_columns, 0, heading_rows, 0);
1119
1120   /* Outline the box */
1121   tab_box (t,
1122            TAL_2, TAL_2,
1123            -1, -1,
1124            0, 0,
1125            nc - 1, nr - 1);
1126
1127   /* Vertical lines */
1128   tab_box (t,
1129            -1, -1,
1130            -1, TAL_1,
1131            heading_columns, 0,
1132            nc - 1, nr - 1);
1133
1134   tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1135   tab_hline (t, TAL_1, 1, nc - 1, 1);
1136
1137   tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1138
1139
1140   if ( factor->extraction == EXTRACTION_PC)
1141     tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1142   else
1143     tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1144
1145   c = 1;
1146   if (factor->print & PRINT_INITIAL)
1147     {
1148       tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1149       c += 3;
1150     }
1151
1152   if (factor->print & PRINT_EXTRACTION)
1153     {
1154       tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1155       c += 3;
1156     }
1157
1158   if (factor->print & PRINT_ROTATION)
1159     {
1160       tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1161       c += 3;
1162     }
1163
1164   for (i = 0; i < (nc - heading_columns) / 3 ; ++i)
1165     {
1166       tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1167       tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1168       tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1169
1170       tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1171     }
1172
1173   for (i = 0 ; i < initial_eigenvalues->size; ++i)
1174     i_total += gsl_vector_get (initial_eigenvalues, i);
1175
1176   if ( factor->extraction == EXTRACTION_PAF)
1177     {
1178       e_total = factor->n_vars;
1179     }
1180   else
1181     {
1182       e_total = i_total;
1183     }
1184
1185
1186   for (i = 0 ; i < factor->n_vars; ++i)
1187     {
1188       const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1189       double i_percent = 100.0 * i_lambda / i_total ;
1190
1191       const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1192       double e_percent = 100.0 * e_lambda / e_total ;
1193
1194       c = 0;
1195
1196       tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%d"), i + 1);
1197
1198       i_cum += i_percent;
1199       e_cum += e_percent;
1200
1201       /* Initial Eigenvalues */
1202       if (factor->print & PRINT_INITIAL)
1203       {
1204         tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL);
1205         tab_double (t, c++, i + heading_rows, 0, i_percent, NULL);
1206         tab_double (t, c++, i + heading_rows, 0, i_cum, NULL);
1207       }
1208
1209       if (factor->print & PRINT_EXTRACTION)
1210         {
1211           if (i < idata->n_extractions)
1212             {
1213               /* Sums of squared loadings */
1214               tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL);
1215               tab_double (t, c++, i + heading_rows, 0, e_percent, NULL);
1216               tab_double (t, c++, i + heading_rows, 0, e_cum, NULL);
1217             }
1218         }
1219     }
1220
1221   tab_submit (t);
1222 }
1223
1224
1225 static void
1226 show_correlation_matrix (const struct cmd_factor *factor, const struct idata *idata)
1227 {
1228   struct tab_table *t ;
1229   size_t i, j;
1230   int y_pos_corr = -1;
1231   int y_pos_sig = -1;
1232   int suffix_rows = 0;
1233
1234   const int heading_rows = 1;
1235   const int heading_columns = 2;
1236
1237   int nc = heading_columns ;
1238   int nr = heading_rows ;
1239   int n_data_sets = 0;
1240
1241   if (factor->print & PRINT_CORRELATION)
1242     {
1243       y_pos_corr = n_data_sets;
1244       n_data_sets++;
1245       nc = heading_columns + factor->n_vars;
1246     }
1247
1248   if (factor->print & PRINT_SIG)
1249     {
1250       y_pos_sig = n_data_sets;
1251       n_data_sets++;
1252       nc = heading_columns + factor->n_vars;
1253     }
1254
1255   nr += n_data_sets * factor->n_vars;
1256
1257   if (factor->print & PRINT_DETERMINANT)
1258     suffix_rows = 1;
1259
1260   /* If the table would contain only headings, don't bother rendering it */
1261   if (nr <= heading_rows && suffix_rows == 0)
1262     return;
1263
1264   t = tab_create (nc, nr + suffix_rows, 0);
1265
1266   tab_title (t, _("Correlation Matrix"));
1267
1268   tab_dim (t, tab_natural_dimensions, NULL);
1269
1270   tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1271
1272   if (nr > heading_rows)
1273     {
1274       tab_headers (t, heading_columns, 0, heading_rows, 0);
1275
1276       tab_vline (t, TAL_2, 2, 0, nr - 1);
1277
1278       /* Outline the box */
1279       tab_box (t,
1280                TAL_2, TAL_2,
1281                -1, -1,
1282                0, 0,
1283                nc - 1, nr - 1);
1284
1285       /* Vertical lines */
1286       tab_box (t,
1287                -1, -1,
1288                -1, TAL_1,
1289                heading_columns, 0,
1290                nc - 1, nr - 1);
1291
1292
1293       for (i = 0; i < factor->n_vars; ++i)
1294         tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
1295
1296
1297       for (i = 0 ; i < n_data_sets; ++i)
1298         {
1299           int y = heading_rows + i * factor->n_vars;
1300           size_t v;
1301           for (v = 0; v < factor->n_vars; ++v)
1302             tab_text (t, 1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
1303
1304           tab_hline (t, TAL_1, 0, nc - 1, y);
1305         }
1306
1307       if (factor->print & PRINT_CORRELATION)
1308         {
1309           const double y = heading_rows + y_pos_corr;
1310           tab_text (t, 0, y, TAT_TITLE, _("Correlations"));
1311
1312           for (i = 0; i < factor->n_vars; ++i)
1313             {
1314               for (j = 0; j < factor->n_vars; ++j)
1315                 tab_double (t, heading_columns + i,  y + j, 0, gsl_matrix_get (idata->corr, i, j), NULL);
1316             }
1317         }
1318
1319       if (factor->print & PRINT_SIG)
1320         {
1321           const double y = heading_rows + y_pos_sig * factor->n_vars;
1322           tab_text (t, 0, y, TAT_TITLE, _("Sig. 1-tailed"));
1323
1324           for (i = 0; i < factor->n_vars; ++i)
1325             {
1326               for (j = 0; j < factor->n_vars; ++j)
1327                 {
1328                   double rho = gsl_matrix_get (idata->corr, i, j);
1329                   double w = gsl_matrix_get (idata->n, i, j);
1330
1331                   if (i == j)
1332                     continue;
1333
1334                   tab_double (t, heading_columns + i,  y + j, 0, significance_of_correlation (rho, w), NULL);
1335                 }
1336             }
1337         }
1338     }
1339
1340   if (factor->print & PRINT_DETERMINANT)
1341     {
1342       int sign = 0;
1343       double det = 0.0;
1344
1345       const int size = idata->corr->size1;
1346       gsl_permutation *p = gsl_permutation_calloc (size);
1347       gsl_matrix *tmp = gsl_matrix_calloc (size, size);
1348       gsl_matrix_memcpy (tmp, idata->corr);
1349
1350       gsl_linalg_LU_decomp (tmp, p, &sign);
1351       det = gsl_linalg_LU_det (tmp, sign);
1352       gsl_permutation_free (p);
1353       gsl_matrix_free (tmp);
1354
1355
1356       tab_text (t, 0, nr, TAB_LEFT | TAT_TITLE, _("Determinant"));
1357       tab_double (t, 1, nr, 0, det, NULL);
1358     }
1359
1360   tab_submit (t);
1361 }
1362
1363
1364
1365 static void
1366 do_factor (const struct cmd_factor *factor, struct casereader *r)
1367 {
1368   struct ccase *c;
1369   const gsl_matrix *var_matrix;
1370   const gsl_matrix *mean_matrix;
1371
1372   const gsl_matrix *analysis_matrix;
1373   struct idata *idata = idata_alloc (factor->n_vars);
1374
1375   struct covariance *cov = covariance_create (factor->n_vars, factor->vars,
1376                                               factor->wv, factor->exclude);
1377
1378   for ( ; (c = casereader_read (r) ); case_unref (c))
1379     {
1380       covariance_accumulate (cov, c);
1381     }
1382
1383   idata->cov = covariance_calculate (cov);
1384
1385   var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
1386   mean_matrix = covariance_moments (cov, MOMENT_MEAN);
1387   idata->n = covariance_moments (cov, MOMENT_NONE);
1388
1389   if ( factor->method == METHOD_CORR)
1390     {
1391       idata->corr = correlation_from_covariance (idata->cov, var_matrix);
1392       analysis_matrix = idata->corr;
1393     }
1394   else
1395     analysis_matrix = idata->cov;
1396
1397   if ( factor->print & PRINT_UNIVARIATE)
1398     {
1399       const int nc = 4;
1400       int i;
1401       const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
1402
1403
1404       const int heading_columns = 1;
1405       const int heading_rows = 1;
1406
1407       const int nr = heading_rows + factor->n_vars;
1408
1409       struct tab_table *t = tab_create (nc, nr, 0);
1410       tab_title (t, _("Descriptive Statistics"));
1411       tab_dim (t, tab_natural_dimensions, NULL);
1412
1413       tab_headers (t, heading_columns, 0, heading_rows, 0);
1414
1415       /* Outline the box */
1416       tab_box (t,
1417                TAL_2, TAL_2,
1418                -1, -1,
1419                0, 0,
1420                nc - 1, nr - 1);
1421
1422       /* Vertical lines */
1423       tab_box (t,
1424                -1, -1,
1425                -1, TAL_1,
1426                heading_columns, 0,
1427                nc - 1, nr - 1);
1428
1429       tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1430       tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1431
1432       tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
1433       tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1434       tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
1435
1436       for (i = 0 ; i < factor->n_vars; ++i)
1437         {
1438           const struct variable *v = factor->vars[i];
1439           tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
1440
1441           tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (mean_matrix, i, i), NULL);
1442           tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (var_matrix, i, i)), NULL);
1443           tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (idata->n, i, i), wfmt);
1444         }
1445
1446       tab_submit (t);
1447     }
1448
1449   show_correlation_matrix (factor, idata);
1450
1451 #if 1
1452   {
1453     gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
1454     
1455     gsl_eigen_symmv (matrix_dup (analysis_matrix), idata->eval, idata->evec, workspace);
1456
1457     gsl_eigen_symmv_free (workspace);
1458   }
1459
1460   gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
1461 #endif
1462
1463   idata->n_extractions = n_extracted_factors (factor, idata);
1464
1465   if (idata->n_extractions == 0)
1466     {
1467       msg (MW, _("The FACTOR criteria result in zero factors extracted. Therefore no analysis will be performed."));
1468       goto finish;
1469     }
1470     
1471   {
1472     const gsl_vector *extracted_eigenvalues = NULL;
1473     gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
1474     gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
1475     size_t i;
1476     struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, idata->n_extractions);
1477     gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
1478
1479     if ( factor->extraction == EXTRACTION_PAF)
1480       {
1481         gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
1482         struct smr_workspace *ws = ws_create (analysis_matrix);
1483
1484         for (i = 0 ; i < factor->n_vars ; ++i)
1485           {
1486             double r2 = squared_multiple_correlation (analysis_matrix, i, ws);
1487
1488             gsl_vector_set (idata->msr, i, r2);
1489           }
1490         ws_destroy (ws);
1491
1492         gsl_vector_memcpy (initial_communalities, idata->msr);
1493
1494         for (i = 0; i < factor->iterations; ++i)
1495           {
1496             double min, max;
1497             gsl_vector_memcpy (diff, idata->msr);
1498
1499             iterate_factor_matrix (analysis_matrix, idata->msr, factor_matrix, fmw);
1500       
1501             gsl_vector_sub (diff, idata->msr);
1502
1503             gsl_vector_minmax (diff, &min, &max);
1504       
1505             if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
1506               break;
1507           }
1508         gsl_vector_free (diff);
1509
1510         gsl_vector_memcpy (extracted_communalities, idata->msr);
1511         extracted_eigenvalues = fmw->eval;
1512       }
1513     else if (factor->extraction == EXTRACTION_PC)
1514       {
1515         for (i = 0; i < factor->n_vars; ++i)
1516           gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
1517
1518         gsl_vector_memcpy (extracted_communalities, initial_communalities);
1519
1520         iterate_factor_matrix (analysis_matrix, extracted_communalities, factor_matrix, fmw);
1521         extracted_eigenvalues = idata->eval;
1522       }
1523
1524     show_communalities (factor, initial_communalities, extracted_communalities);
1525
1526     show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues);
1527
1528     factor_matrix_workspace_free (fmw);
1529
1530     show_factor_matrix (factor, idata, factor_matrix);
1531
1532     gsl_vector_free (initial_communalities);
1533     gsl_vector_free (extracted_communalities);
1534   }
1535
1536  finish:
1537
1538   idata_free (idata);
1539
1540   casereader_destroy (r);
1541 }