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