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