FACTOR: Added "Scree Plots"
[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 #include <output/charts/scree.h>
50 #include <output/chart.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   while (lex_token (lexer) != '.')
556     {
557       lex_match (lexer, '/');
558
559       if (lex_match_id (lexer, "PLOT"))
560         {
561           lex_match (lexer, '=');
562           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
563             {
564               if (lex_match_id (lexer, "EIGEN"))
565                 {
566                   factor.plot |= PLOT_SCREE;
567                 }
568 #if FACTOR_FULLY_IMPLEMENTED
569               else if (lex_match_id (lexer, "ROTATION"))
570                 {
571                 }
572 #endif
573               else
574                 {
575                   lex_error (lexer, NULL);
576                   goto error;
577                 }
578             }
579         }
580       else if (lex_match_id (lexer, "METHOD"))
581         {
582           lex_match (lexer, '=');
583           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
584             {
585               if (lex_match_id (lexer, "COVARIANCE"))
586                 {
587                   factor.method = METHOD_COV;
588                 }
589               else if (lex_match_id (lexer, "CORRELATION"))
590                 {
591                   factor.method = METHOD_CORR;
592                 }
593               else
594                 {
595                   lex_error (lexer, NULL);
596                   goto error;
597                 }
598             }
599         }
600 #if FACTOR_FULLY_IMPLEMENTED
601       else if (lex_match_id (lexer, "ROTATION"))
602         {
603           lex_match (lexer, '=');
604           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
605             {
606               if (lex_match_id (lexer, "VARIMAX"))
607                 {
608                 }
609               else if (lex_match_id (lexer, "DEFAULT"))
610                 {
611                 }
612               else
613                 {
614                   lex_error (lexer, NULL);
615                   goto error;
616                 }
617             }
618         }
619 #endif
620       else if (lex_match_id (lexer, "CRITERIA"))
621         {
622           lex_match (lexer, '=');
623           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
624             {
625               if (lex_match_id (lexer, "FACTORS"))
626                 {
627                   if ( lex_force_match (lexer, '('))
628                     {
629                       lex_force_int (lexer);
630                       factor.n_factors = lex_integer (lexer);
631                       lex_get (lexer);
632                       lex_force_match (lexer, ')');
633                     }
634                 }
635               else if (lex_match_id (lexer, "MINEIGEN"))
636                 {
637                   if ( lex_force_match (lexer, '('))
638                     {
639                       lex_force_num (lexer);
640                       factor.min_eigen = lex_number (lexer);
641                       lex_get (lexer);
642                       lex_force_match (lexer, ')');
643                     }
644                 }
645               else if (lex_match_id (lexer, "ECONVERGE"))
646                 {
647                   if ( lex_force_match (lexer, '('))
648                     {
649                       lex_force_num (lexer);
650                       factor.econverge = lex_number (lexer);
651                       lex_get (lexer);
652                       lex_force_match (lexer, ')');
653                     }
654                 }
655               else if (lex_match_id (lexer, "ITERATE"))
656                 {
657                   if ( lex_force_match (lexer, '('))
658                     {
659                       lex_force_int (lexer);
660                       factor.iterations = lex_integer (lexer);
661                       lex_get (lexer);
662                       lex_force_match (lexer, ')');
663                     }
664                 }
665               else if (lex_match_id (lexer, "DEFAULT"))
666                 {
667                   factor.n_factors = 0;
668                   factor.min_eigen = 1;
669                   factor.iterations = 25;
670                 }
671               else
672                 {
673                   lex_error (lexer, NULL);
674                   goto error;
675                 }
676             }
677         }
678       else if (lex_match_id (lexer, "EXTRACTION"))
679         {
680           extraction_seen = true;
681           lex_match (lexer, '=');
682           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
683             {
684               if (lex_match_id (lexer, "PAF"))
685                 {
686                   factor.extraction = EXTRACTION_PAF;
687                 }
688               else if (lex_match_id (lexer, "PC"))
689                 {
690                   factor.extraction = EXTRACTION_PC;
691                 }
692               else if (lex_match_id (lexer, "PA1"))
693                 {
694                   factor.extraction = EXTRACTION_PC;
695                 }
696               else if (lex_match_id (lexer, "DEFAULT"))
697                 {
698                   factor.extraction = EXTRACTION_PC;
699                 }
700               else
701                 {
702                   lex_error (lexer, NULL);
703                   goto error;
704                 }
705             }
706         }
707       else if (lex_match_id (lexer, "FORMAT"))
708         {
709           lex_match (lexer, '=');
710           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
711             {
712               if (lex_match_id (lexer, "SORT"))
713                 {
714                   factor.sort = true;
715                 }
716               else if (lex_match_id (lexer, "BLANK"))
717                 {
718                   if ( lex_force_match (lexer, '('))
719                     {
720                       lex_force_num (lexer);
721                       factor.blank = lex_number (lexer);
722                       lex_get (lexer);
723                       lex_force_match (lexer, ')');
724                     }
725                 }
726               else if (lex_match_id (lexer, "DEFAULT"))
727                 {
728                   factor.blank = 0;
729                   factor.sort = false;
730                 }
731               else
732                 {
733                   lex_error (lexer, NULL);
734                   goto error;
735                 }
736             }
737         }
738       else if (lex_match_id (lexer, "PRINT"))
739         {
740           factor.print = 0;
741           lex_match (lexer, '=');
742           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
743             {
744               if (lex_match_id (lexer, "UNIVARIATE"))
745                 {
746                   factor.print |= PRINT_UNIVARIATE;
747                 }
748               else if (lex_match_id (lexer, "DET"))
749                 {
750                   factor.print |= PRINT_DETERMINANT;
751                 }
752 #if FACTOR_FULLY_IMPLEMENTED
753               else if (lex_match_id (lexer, "INV"))
754                 {
755                 }
756               else if (lex_match_id (lexer, "AIC"))
757                 {
758                 }
759 #endif
760               else if (lex_match_id (lexer, "SIG"))
761                 {
762                   factor.print |= PRINT_SIG;
763                 }
764               else if (lex_match_id (lexer, "CORRELATION"))
765                 {
766                   factor.print |= PRINT_CORRELATION;
767                 }
768 #if FACTOR_FULLY_IMPLEMENTED
769               else if (lex_match_id (lexer, "COVARIANCE"))
770                 {
771                 }
772 #endif
773               else if (lex_match_id (lexer, "ROTATION"))
774                 {
775                   factor.print |= PRINT_ROTATION;
776                 }
777               else if (lex_match_id (lexer, "EXTRACTION"))
778                 {
779                   factor.print |= PRINT_EXTRACTION;
780                 }
781               else if (lex_match_id (lexer, "INITIAL"))
782                 {
783                   factor.print |= PRINT_INITIAL;
784                 }
785 #if FACTOR_FULLY_IMPLEMENTED
786               else if (lex_match_id (lexer, "KMO"))
787                 {
788                 }
789               else if (lex_match_id (lexer, "REPR"))
790                 {
791                 }
792               else if (lex_match_id (lexer, "FSCORE"))
793                 {
794                 }
795 #endif
796               else if (lex_match (lexer, T_ALL))
797                 {
798                   factor.print = 0xFFFF;
799                 }
800               else if (lex_match_id (lexer, "DEFAULT"))
801                 {
802                   factor.print |= PRINT_INITIAL ;
803                   factor.print |= PRINT_EXTRACTION ;
804                   factor.print |= PRINT_ROTATION ;
805                 }
806               else
807                 {
808                   lex_error (lexer, NULL);
809                   goto error;
810                 }
811             }
812         }
813       else if (lex_match_id (lexer, "MISSING"))
814         {
815           lex_match (lexer, '=');
816           while (lex_token (lexer) != '.' && lex_token (lexer) != '/')
817             {
818               if (lex_match_id (lexer, "INCLUDE"))
819                 {
820                   factor.exclude = MV_SYSTEM;
821                 }
822               else if (lex_match_id (lexer, "EXCLUDE"))
823                 {
824                   factor.exclude = MV_ANY;
825                 }
826               else if (lex_match_id (lexer, "LISTWISE"))
827                 {
828                   factor.missing_type = MISS_LISTWISE;
829                 }
830               else if (lex_match_id (lexer, "PAIRWISE"))
831                 {
832                   factor.missing_type = MISS_PAIRWISE;
833                 }
834               else if (lex_match_id (lexer, "MEANSUB"))
835                 {
836                   factor.missing_type = MISS_MEANSUB;
837                 }
838               else
839                 {
840                   lex_error (lexer, NULL);
841                   goto error;
842                 }
843             }
844         }
845       else
846         {
847           lex_error (lexer, NULL);
848           goto error;
849         }
850     }
851
852   if ( ! run_factor (ds, &factor)) 
853     goto error;
854
855   free (factor.vars);
856   return CMD_SUCCESS;
857
858  error:
859   free (factor.vars);
860   return CMD_FAILURE;
861 }
862
863 static void do_factor (const struct cmd_factor *factor, struct casereader *group);
864
865
866 static bool
867 run_factor (struct dataset *ds, const struct cmd_factor *factor)
868 {
869   struct dictionary *dict = dataset_dict (ds);
870   bool ok;
871   struct casereader *group;
872
873   struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
874
875   while (casegrouper_get_next_group (grouper, &group))
876     {
877       if ( factor->missing_type == MISS_LISTWISE )
878         group  = casereader_create_filter_missing (group, factor->vars, factor->n_vars,
879                                                    factor->exclude,
880                                                    NULL,  NULL);
881       do_factor (factor, group);
882     }
883
884   ok = casegrouper_destroy (grouper);
885   ok = proc_commit (ds) && ok;
886
887   return ok;
888 }
889
890
891 /* Return the communality of variable N, calculated to N_FACTORS */
892 static double
893 the_communality (const gsl_matrix *evec, const gsl_vector *eval, int n, int n_factors)
894 {
895   size_t i;
896
897   double comm = 0;
898
899   assert (n >= 0);
900   assert (n < eval->size);
901   assert (n < evec->size1);
902   assert (n_factors <= eval->size);
903
904   for (i = 0 ; i < n_factors; ++i)
905     {
906       double evali = fabs (gsl_vector_get (eval, i));
907
908       double eveci = gsl_matrix_get (evec, n, i);
909
910       comm += pow2 (eveci) * evali;
911     }
912
913   return comm;
914 }
915
916 /* Return the communality of variable N, calculated to N_FACTORS */
917 static double
918 communality (struct idata *idata, int n, int n_factors)
919 {
920   return the_communality (idata->evec, idata->eval, n, n_factors);
921 }
922
923
924 static void
925 show_scree (const struct cmd_factor *f, struct idata *idata)
926 {
927   struct scree *s;
928   const char *label ;
929
930   if ( !(f->plot & PLOT_SCREE) )
931     return;
932
933
934   label = f->extraction == EXTRACTION_PC ? _("Component Number") : _("Factor Number");
935
936   s = scree_create (idata->eval, label);
937
938   chart_submit (scree_get_chart (s));
939 }
940
941 static void
942 show_communalities (const struct cmd_factor * factor,
943                     const gsl_vector *initial, const gsl_vector *extracted)
944 {
945   int i;
946   int c = 0;
947   const int heading_columns = 1;
948   int nc = heading_columns;
949   const int heading_rows = 1;
950   const int nr = heading_rows + factor->n_vars;
951   struct tab_table *t;
952
953   if (factor->print & PRINT_EXTRACTION)
954     nc++;
955
956   if (factor->print & PRINT_INITIAL)
957     nc++;
958
959   /* No point having a table with only headings */
960   if (nc <= 1)
961     return;
962
963   t = tab_create (nc, nr);
964
965   tab_title (t, _("Communalities"));
966
967   tab_dim (t, tab_natural_dimensions, NULL, NULL);
968
969   tab_headers (t, heading_columns, 0, heading_rows, 0);
970
971   c = 1;
972   if (factor->print & PRINT_INITIAL)
973     tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Initial"));
974
975   if (factor->print & PRINT_EXTRACTION)
976     tab_text (t, c++, 0, TAB_CENTER | TAT_TITLE, _("Extraction"));
977
978   /* Outline the box */
979   tab_box (t,
980            TAL_2, TAL_2,
981            -1, -1,
982            0, 0,
983            nc - 1, nr - 1);
984
985   /* Vertical lines */
986   tab_box (t,
987            -1, -1,
988            -1, TAL_1,
989            heading_columns, 0,
990            nc - 1, nr - 1);
991
992   tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
993   tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
994
995   for (i = 0 ; i < factor->n_vars; ++i)
996     {
997       c = 0;
998       tab_text (t, c++, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[i]));
999
1000       if (factor->print & PRINT_INITIAL)
1001         tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (initial, i), NULL);
1002
1003       if (factor->print & PRINT_EXTRACTION)
1004         tab_double (t, c++, i + heading_rows, 0, gsl_vector_get (extracted, i), NULL);
1005     }
1006
1007   tab_submit (t);
1008 }
1009
1010
1011 static void
1012 show_factor_matrix (const struct cmd_factor *factor, struct idata *idata, const gsl_matrix *fm)
1013 {
1014   int i;
1015   const int n_factors = n_extracted_factors (factor, idata);
1016
1017   const int heading_columns = 1;
1018   const int heading_rows = 2;
1019   const int nr = heading_rows + factor->n_vars;
1020   const int nc = heading_columns + n_factors;
1021   gsl_permutation *perm;
1022
1023   struct tab_table *t = tab_create (nc, nr);
1024
1025   if ( factor->extraction == EXTRACTION_PC )
1026     tab_title (t, _("Component Matrix"));
1027   else 
1028     tab_title (t, _("Factor Matrix"));
1029
1030   tab_dim (t, tab_natural_dimensions, NULL, NULL);
1031
1032   tab_headers (t, heading_columns, 0, heading_rows, 0);
1033
1034   if ( factor->extraction == EXTRACTION_PC )
1035     tab_joint_text (t,
1036                     1, 0,
1037                     nc - 1, 0,
1038                     TAB_CENTER | TAT_TITLE, _("Component"));
1039   else
1040     tab_joint_text (t,
1041                     1, 0,
1042                     nc - 1, 0,
1043                     TAB_CENTER | TAT_TITLE, _("Factor"));
1044
1045
1046   tab_hline (t, TAL_1, heading_columns, nc - 1, 1);
1047
1048
1049   /* Outline the box */
1050   tab_box (t,
1051            TAL_2, TAL_2,
1052            -1, -1,
1053            0, 0,
1054            nc - 1, nr - 1);
1055
1056   /* Vertical lines */
1057   tab_box (t,
1058            -1, -1,
1059            -1, TAL_1,
1060            heading_columns, 1,
1061            nc - 1, nr - 1);
1062
1063   tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1064   tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1065
1066
1067   /* Initialise to the identity permutation */
1068   perm = gsl_permutation_calloc (factor->n_vars);
1069
1070   if ( factor->sort)
1071     sort_matrix_indirect (fm, perm);
1072
1073   for (i = 0 ; i < n_factors; ++i)
1074     {
1075       tab_text_format (t, heading_columns + i, 1, TAB_CENTER | TAT_TITLE, _("%d"), i + 1);
1076     }
1077
1078   for (i = 0 ; i < factor->n_vars; ++i)
1079     {
1080       int j;
1081       const int matrix_row = perm->data[i];
1082       tab_text (t, 0, i + heading_rows, TAT_TITLE, var_to_string (factor->vars[matrix_row]));
1083
1084       for (j = 0 ; j < n_factors; ++j)
1085         {
1086           double x = gsl_matrix_get (fm, matrix_row, j);
1087
1088           if ( fabs (x) < factor->blank)
1089             continue;
1090
1091           tab_double (t, heading_columns + j, heading_rows + i, 0, x, NULL);
1092         }
1093     }
1094
1095   gsl_permutation_free (perm);
1096
1097   tab_submit (t);
1098 }
1099
1100
1101 static void
1102 show_explained_variance (const struct cmd_factor * factor, struct idata *idata,
1103                          const gsl_vector *initial_eigenvalues,
1104                          const gsl_vector *extracted_eigenvalues)
1105 {
1106   size_t i;
1107   int c = 0;
1108   const int heading_columns = 1;
1109   const int heading_rows = 2;
1110   const int nr = heading_rows + factor->n_vars;
1111
1112   struct tab_table *t ;
1113
1114   double i_total = 0.0;
1115   double i_cum = 0.0;
1116
1117   double e_total = 0.0;
1118   double e_cum = 0.0;
1119
1120   int nc = heading_columns;
1121
1122   if (factor->print & PRINT_EXTRACTION)
1123     nc += 3;
1124
1125   if (factor->print & PRINT_INITIAL)
1126     nc += 3;
1127
1128   if (factor->print & PRINT_ROTATION)
1129     nc += 3;
1130
1131   /* No point having a table with only headings */
1132   if ( nc <= heading_columns)
1133     return;
1134
1135   t = tab_create (nc, nr);
1136
1137   tab_title (t, _("Total Variance Explained"));
1138
1139   tab_dim (t, tab_natural_dimensions, NULL, NULL);
1140
1141   tab_headers (t, heading_columns, 0, heading_rows, 0);
1142
1143   /* Outline the box */
1144   tab_box (t,
1145            TAL_2, TAL_2,
1146            -1, -1,
1147            0, 0,
1148            nc - 1, nr - 1);
1149
1150   /* Vertical lines */
1151   tab_box (t,
1152            -1, -1,
1153            -1, TAL_1,
1154            heading_columns, 0,
1155            nc - 1, nr - 1);
1156
1157   tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1158   tab_hline (t, TAL_1, 1, nc - 1, 1);
1159
1160   tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1161
1162
1163   if ( factor->extraction == EXTRACTION_PC)
1164     tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Component"));
1165   else
1166     tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Factor"));
1167
1168   c = 1;
1169   if (factor->print & PRINT_INITIAL)
1170     {
1171       tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Initial Eigenvalues"));
1172       c += 3;
1173     }
1174
1175   if (factor->print & PRINT_EXTRACTION)
1176     {
1177       tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Extraction Sums of Squared Loadings"));
1178       c += 3;
1179     }
1180
1181   if (factor->print & PRINT_ROTATION)
1182     {
1183       tab_joint_text (t, c, 0, c + 2, 0, TAB_CENTER | TAT_TITLE, _("Rotation Sums of Squared Loadings"));
1184       c += 3;
1185     }
1186
1187   for (i = 0; i < (nc - heading_columns) / 3 ; ++i)
1188     {
1189       tab_text (t, i * 3 + 1, 1, TAB_CENTER | TAT_TITLE, _("Total"));
1190       tab_text (t, i * 3 + 2, 1, TAB_CENTER | TAT_TITLE, _("% of Variance"));
1191       tab_text (t, i * 3 + 3, 1, TAB_CENTER | TAT_TITLE, _("Cumulative %"));
1192
1193       tab_vline (t, TAL_2, heading_columns + i * 3, 0, nr - 1);
1194     }
1195
1196   for (i = 0 ; i < initial_eigenvalues->size; ++i)
1197     i_total += gsl_vector_get (initial_eigenvalues, i);
1198
1199   if ( factor->extraction == EXTRACTION_PAF)
1200     {
1201       e_total = factor->n_vars;
1202     }
1203   else
1204     {
1205       e_total = i_total;
1206     }
1207
1208
1209   for (i = 0 ; i < factor->n_vars; ++i)
1210     {
1211       const double i_lambda = gsl_vector_get (initial_eigenvalues, i);
1212       double i_percent = 100.0 * i_lambda / i_total ;
1213
1214       const double e_lambda = gsl_vector_get (extracted_eigenvalues, i);
1215       double e_percent = 100.0 * e_lambda / e_total ;
1216
1217       c = 0;
1218
1219       tab_text_format (t, c++, i + heading_rows, TAB_LEFT | TAT_TITLE, _("%d"), i + 1);
1220
1221       i_cum += i_percent;
1222       e_cum += e_percent;
1223
1224       /* Initial Eigenvalues */
1225       if (factor->print & PRINT_INITIAL)
1226       {
1227         tab_double (t, c++, i + heading_rows, 0, i_lambda, NULL);
1228         tab_double (t, c++, i + heading_rows, 0, i_percent, NULL);
1229         tab_double (t, c++, i + heading_rows, 0, i_cum, NULL);
1230       }
1231
1232       if (factor->print & PRINT_EXTRACTION)
1233         {
1234           if ( i < n_extracted_factors (factor, idata))
1235             {
1236               /* Sums of squared loadings */
1237               tab_double (t, c++, i + heading_rows, 0, e_lambda, NULL);
1238               tab_double (t, c++, i + heading_rows, 0, e_percent, NULL);
1239               tab_double (t, c++, i + heading_rows, 0, e_cum, NULL);
1240             }
1241         }
1242     }
1243
1244   tab_submit (t);
1245 }
1246
1247
1248 static void
1249 show_correlation_matrix (const struct cmd_factor *factor, const struct idata *idata)
1250 {
1251   struct tab_table *t ;
1252   size_t i, j;
1253   int y_pos_corr = -1;
1254   int y_pos_sig = -1;
1255   int suffix_rows = 0;
1256
1257   const int heading_rows = 1;
1258   const int heading_columns = 2;
1259
1260   int nc = heading_columns ;
1261   int nr = heading_rows ;
1262   int n_data_sets = 0;
1263
1264   if (factor->print & PRINT_CORRELATION)
1265     {
1266       y_pos_corr = n_data_sets;
1267       n_data_sets++;
1268       nc = heading_columns + factor->n_vars;
1269     }
1270
1271   if (factor->print & PRINT_SIG)
1272     {
1273       y_pos_sig = n_data_sets;
1274       n_data_sets++;
1275       nc = heading_columns + factor->n_vars;
1276     }
1277
1278   nr += n_data_sets * factor->n_vars;
1279
1280   if (factor->print & PRINT_DETERMINANT)
1281     suffix_rows = 1;
1282
1283   /* If the table would contain only headings, don't bother rendering it */
1284   if (nr <= heading_rows && suffix_rows == 0)
1285     return;
1286
1287   t = tab_create (nc, nr + suffix_rows);
1288
1289   tab_title (t, _("Correlation Matrix"));
1290
1291   tab_dim (t, tab_natural_dimensions, NULL, NULL);
1292
1293   tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1294
1295   if (nr > heading_rows)
1296     {
1297       tab_headers (t, heading_columns, 0, heading_rows, 0);
1298
1299       tab_vline (t, TAL_2, 2, 0, nr - 1);
1300
1301       /* Outline the box */
1302       tab_box (t,
1303                TAL_2, TAL_2,
1304                -1, -1,
1305                0, 0,
1306                nc - 1, nr - 1);
1307
1308       /* Vertical lines */
1309       tab_box (t,
1310                -1, -1,
1311                -1, TAL_1,
1312                heading_columns, 0,
1313                nc - 1, nr - 1);
1314
1315
1316       for (i = 0; i < factor->n_vars; ++i)
1317         tab_text (t, heading_columns + i, 0, TAT_TITLE, var_to_string (factor->vars[i]));
1318
1319
1320       for (i = 0 ; i < n_data_sets; ++i)
1321         {
1322           int y = heading_rows + i * factor->n_vars;
1323           size_t v;
1324           for (v = 0; v < factor->n_vars; ++v)
1325             tab_text (t, 1, y + v, TAT_TITLE, var_to_string (factor->vars[v]));
1326
1327           tab_hline (t, TAL_1, 0, nc - 1, y);
1328         }
1329
1330       if (factor->print & PRINT_CORRELATION)
1331         {
1332           const double y = heading_rows + y_pos_corr;
1333           tab_text (t, 0, y, TAT_TITLE, _("Correlations"));
1334
1335           for (i = 0; i < factor->n_vars; ++i)
1336             {
1337               for (j = 0; j < factor->n_vars; ++j)
1338                 tab_double (t, heading_columns + i,  y + j, 0, gsl_matrix_get (idata->corr, i, j), NULL);
1339             }
1340         }
1341
1342       if (factor->print & PRINT_SIG)
1343         {
1344           const double y = heading_rows + y_pos_sig * factor->n_vars;
1345           tab_text (t, 0, y, TAT_TITLE, _("Sig. 1-tailed"));
1346
1347           for (i = 0; i < factor->n_vars; ++i)
1348             {
1349               for (j = 0; j < factor->n_vars; ++j)
1350                 {
1351                   double rho = gsl_matrix_get (idata->corr, i, j);
1352                   double w = gsl_matrix_get (idata->n, i, j);
1353
1354                   if (i == j)
1355                     continue;
1356
1357                   tab_double (t, heading_columns + i,  y + j, 0, significance_of_correlation (rho, w), NULL);
1358                 }
1359             }
1360         }
1361     }
1362
1363   if (factor->print & PRINT_DETERMINANT)
1364     {
1365       int sign = 0;
1366       double det = 0.0;
1367
1368       const int size = idata->corr->size1;
1369       gsl_permutation *p = gsl_permutation_calloc (size);
1370       gsl_matrix *tmp = gsl_matrix_calloc (size, size);
1371       gsl_matrix_memcpy (tmp, idata->corr);
1372
1373       gsl_linalg_LU_decomp (tmp, p, &sign);
1374       det = gsl_linalg_LU_det (tmp, sign);
1375       gsl_permutation_free (p);
1376       gsl_matrix_free (tmp);
1377
1378
1379       tab_text (t, 0, nr, TAB_LEFT | TAT_TITLE, _("Determinant"));
1380       tab_double (t, 1, nr, 0, det, NULL);
1381     }
1382
1383   tab_submit (t);
1384 }
1385
1386
1387
1388 static void
1389 do_factor (const struct cmd_factor *factor, struct casereader *r)
1390 {
1391   struct ccase *c;
1392   const gsl_matrix *var_matrix;
1393   const gsl_matrix *mean_matrix;
1394
1395   const gsl_matrix *analysis_matrix;
1396   struct idata *idata = idata_alloc (factor->n_vars);
1397
1398   struct covariance *cov = covariance_create (factor->n_vars, factor->vars,
1399                                               factor->wv, factor->exclude);
1400
1401   for ( ; (c = casereader_read (r) ); case_unref (c))
1402     {
1403       covariance_accumulate (cov, c);
1404     }
1405
1406   idata->cov = covariance_calculate (cov);
1407
1408   var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
1409   mean_matrix = covariance_moments (cov, MOMENT_MEAN);
1410   idata->n = covariance_moments (cov, MOMENT_NONE);
1411
1412   if ( factor->method == METHOD_CORR)
1413     {
1414       idata->corr = correlation_from_covariance (idata->cov, var_matrix);
1415       analysis_matrix = idata->corr;
1416     }
1417   else
1418     analysis_matrix = idata->cov;
1419
1420   if ( factor->print & PRINT_UNIVARIATE)
1421     {
1422       const int nc = 4;
1423       int i;
1424       const struct fmt_spec *wfmt = factor->wv ? var_get_print_format (factor->wv) : & F_8_0;
1425
1426
1427       const int heading_columns = 1;
1428       const int heading_rows = 1;
1429
1430       const int nr = heading_rows + factor->n_vars;
1431
1432       struct tab_table *t = tab_create (nc, nr);
1433       tab_title (t, _("Descriptive Statistics"));
1434       tab_dim (t, tab_natural_dimensions, NULL, NULL);
1435
1436       tab_headers (t, heading_columns, 0, heading_rows, 0);
1437
1438       /* Outline the box */
1439       tab_box (t,
1440                TAL_2, TAL_2,
1441                -1, -1,
1442                0, 0,
1443                nc - 1, nr - 1);
1444
1445       /* Vertical lines */
1446       tab_box (t,
1447                -1, -1,
1448                -1, TAL_1,
1449                heading_columns, 0,
1450                nc - 1, nr - 1);
1451
1452       tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
1453       tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1454
1455       tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
1456       tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
1457       tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Analysis N"));
1458
1459       for (i = 0 ; i < factor->n_vars; ++i)
1460         {
1461           const struct variable *v = factor->vars[i];
1462           tab_text (t, 0, i + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
1463
1464           tab_double (t, 1, i + heading_rows, 0, gsl_matrix_get (mean_matrix, i, i), NULL);
1465           tab_double (t, 2, i + heading_rows, 0, sqrt (gsl_matrix_get (var_matrix, i, i)), NULL);
1466           tab_double (t, 3, i + heading_rows, 0, gsl_matrix_get (idata->n, i, i), wfmt);
1467         }
1468
1469       tab_submit (t);
1470     }
1471
1472   show_correlation_matrix (factor, idata);
1473
1474 #if 1
1475   {
1476     gsl_eigen_symmv_workspace *workspace = gsl_eigen_symmv_alloc (factor->n_vars);
1477     
1478     gsl_eigen_symmv (matrix_dup (analysis_matrix), idata->eval, idata->evec, workspace);
1479
1480     gsl_eigen_symmv_free (workspace);
1481   }
1482
1483   gsl_eigen_symmv_sort (idata->eval, idata->evec, GSL_EIGEN_SORT_ABS_DESC);
1484 #endif
1485
1486   {
1487     const gsl_vector *extracted_eigenvalues = NULL;
1488     gsl_vector *initial_communalities = gsl_vector_alloc (factor->n_vars);
1489     gsl_vector *extracted_communalities = gsl_vector_alloc (factor->n_vars);
1490     size_t i;
1491     struct factor_matrix_workspace *fmw = factor_matrix_workspace_alloc (idata->msr->size, n_extracted_factors (factor, idata));
1492     gsl_matrix *factor_matrix = gsl_matrix_calloc (factor->n_vars, fmw->n_factors);
1493
1494     if ( factor->extraction == EXTRACTION_PAF)
1495       {
1496         gsl_vector *diff = gsl_vector_alloc (idata->msr->size);
1497         struct smr_workspace *ws = ws_create (analysis_matrix);
1498
1499         for (i = 0 ; i < factor->n_vars ; ++i)
1500           {
1501             double r2 = squared_multiple_correlation (analysis_matrix, i, ws);
1502
1503             gsl_vector_set (idata->msr, i, r2);
1504           }
1505         ws_destroy (ws);
1506
1507         gsl_vector_memcpy (initial_communalities, idata->msr);
1508
1509         for (i = 0; i < factor->iterations; ++i)
1510           {
1511             double min, max;
1512             gsl_vector_memcpy (diff, idata->msr);
1513
1514             iterate_factor_matrix (analysis_matrix, idata->msr, factor_matrix, fmw);
1515       
1516             gsl_vector_sub (diff, idata->msr);
1517
1518             gsl_vector_minmax (diff, &min, &max);
1519       
1520             if ( fabs (min) < factor->econverge && fabs (max) < factor->econverge)
1521               break;
1522           }
1523         gsl_vector_free (diff);
1524
1525         gsl_vector_memcpy (extracted_communalities, idata->msr);
1526         extracted_eigenvalues = fmw->eval;
1527       }
1528     else if (factor->extraction == EXTRACTION_PC)
1529       {
1530         for (i = 0 ; i < factor->n_vars; ++i)
1531           {
1532             gsl_vector_set (initial_communalities, i, communality (idata, i, factor->n_vars));
1533           }
1534         gsl_vector_memcpy (extracted_communalities, initial_communalities);
1535
1536         iterate_factor_matrix (analysis_matrix, extracted_communalities, factor_matrix, fmw);
1537         extracted_eigenvalues = idata->eval;
1538       }
1539
1540     show_communalities (factor, initial_communalities, extracted_communalities);
1541
1542     show_explained_variance (factor, idata, idata->eval, extracted_eigenvalues);
1543
1544     factor_matrix_workspace_free (fmw);
1545
1546     show_scree (factor, idata);
1547
1548     show_factor_matrix (factor, idata, factor_matrix);
1549
1550     gsl_vector_free (initial_communalities);
1551     gsl_vector_free (extracted_communalities);
1552   }
1553
1554   idata_free (idata);
1555
1556   casereader_destroy (r);
1557 }