Reliability: Fix crash on invalid syntax
[pspp] / src / language / stats / reliability.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2011, 2013 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 #include <math.h>
20
21 #include "data/casegrouper.h"
22 #include "data/casereader.h"
23 #include "data/dataset.h"
24 #include "data/dictionary.h"
25 #include "data/format.h"
26 #include "data/missing-values.h"
27 #include "language/command.h"
28 #include "language/lexer/lexer.h"
29 #include "language/lexer/variable-parser.h"
30 #include "libpspp/message.h"
31 #include "libpspp/misc.h"
32 #include "libpspp/str.h"
33 #include "math/moments.h"
34 #include "output/tab.h"
35 #include "output/text-item.h"
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
40
41 struct cronbach
42 {
43   const struct variable **items;
44   size_t n_items;
45   double alpha;
46   double sum_of_variances;
47   double variance_of_sums;
48   int totals_idx;          /* Casereader index into the totals */
49
50   struct moments1 **m ;    /* Moments of the items */
51   struct moments1 *total ; /* Moments of the totals */
52 };
53
54 #if 0
55 static void
56 dump_cronbach (const struct cronbach *s)
57 {
58   int i;
59   printf ("N items %d\n", s->n_items);
60   for (i = 0 ; i < s->n_items; ++i)
61     {
62       printf ("%s\n", var_get_name (s->items[i]));
63     }
64
65   printf ("Totals idx %d\n", s->totals_idx);
66
67   printf ("scale variance %g\n", s->variance_of_sums);
68   printf ("alpha %g\n", s->alpha);
69   putchar ('\n');
70 }
71 #endif
72
73 enum model
74   {
75     MODEL_ALPHA,
76     MODEL_SPLIT
77   };
78
79
80 enum summary_opts
81   {
82     SUMMARY_TOTAL = 0x0001,
83   };
84
85
86 struct reliability
87 {
88   const struct variable **variables;
89   size_t n_variables;
90   enum mv_class exclude;
91
92   struct cronbach *sc;
93   int n_sc;
94
95   int total_start;
96
97   struct string scale_name;
98
99   enum model model;
100   int split_point;
101
102
103   enum summary_opts summary;
104
105   const struct variable *wv;
106 };
107
108
109 static bool run_reliability (struct dataset *ds, const struct reliability *reliability);
110
111 static void
112 reliability_destroy (struct reliability *rel)
113 {
114   int j;
115   ds_destroy (&rel->scale_name);
116   if (rel->sc)
117     for (j = 0; j < rel->n_sc ; ++j)
118       {
119         int x;
120         free (rel->sc[j].items);
121         moments1_destroy (rel->sc[j].total);
122         for (x = 0; x < rel->sc[j].n_items; ++x)
123           free (rel->sc[j].m[x]);
124         free (rel->sc[j].m);
125       }
126
127   free (rel->sc);
128   free (rel->variables);
129 }
130
131 int
132 cmd_reliability (struct lexer *lexer, struct dataset *ds)
133 {
134   const struct dictionary *dict = dataset_dict (ds);
135
136   struct reliability reliability;
137   reliability.n_variables = 0;
138   reliability.variables = NULL;
139   reliability.model = MODEL_ALPHA;
140   reliability.exclude = MV_ANY;
141   reliability.summary = 0;
142   reliability.n_sc = 0;
143   reliability.sc = NULL;
144   reliability.wv = dict_get_weight (dict);
145   reliability.total_start = 0;
146
147   lex_match (lexer, T_SLASH);
148
149   if (!lex_force_match_id (lexer, "VARIABLES"))
150     {
151       goto error;
152     }
153
154   lex_match (lexer, T_EQUALS);
155
156   if (!parse_variables_const (lexer, dict, &reliability.variables, &reliability.n_variables,
157                               PV_NO_DUPLICATE | PV_NUMERIC))
158     goto error;
159
160   if (reliability.n_variables < 2)
161     msg (MW, _("Reliability on a single variable is not useful."));
162
163
164     {
165       int i;
166       struct cronbach *c;
167       /* Create a default Scale */
168
169       reliability.n_sc = 1;
170       reliability.sc = xzalloc (sizeof (struct cronbach) * reliability.n_sc);
171
172       ds_init_cstr (&reliability.scale_name, "ANY");
173
174       c = &reliability.sc[0];
175       c->n_items = reliability.n_variables;
176       c->items = xzalloc (sizeof (struct variable*) * c->n_items);
177
178       for (i = 0 ; i < c->n_items ; ++i)
179         c->items[i] = reliability.variables[i];
180     }
181
182
183
184   while (lex_token (lexer) != T_ENDCMD)
185     {
186       lex_match (lexer, T_SLASH);
187
188       if (lex_match_id (lexer, "SCALE"))
189         {
190           struct const_var_set *vs;
191           if ( ! lex_force_match (lexer, T_LPAREN))
192             goto error;
193
194           if ( ! lex_force_string (lexer) ) 
195             goto error;
196
197           ds_assign_substring (&reliability.scale_name, lex_tokss (lexer));
198
199           lex_get (lexer);
200
201           if ( ! lex_force_match (lexer, T_RPAREN))
202             goto error;
203
204           lex_match (lexer, T_EQUALS);
205
206           vs = const_var_set_create_from_array (reliability.variables, reliability.n_variables);
207
208           free (reliability.sc->items);
209           if (!parse_const_var_set_vars (lexer, vs, &reliability.sc->items, &reliability.sc->n_items, 0))
210             {
211               const_var_set_destroy (vs);
212               goto error;
213             }
214
215           const_var_set_destroy (vs);
216         }
217       else if (lex_match_id (lexer, "MODEL"))
218         {
219           lex_match (lexer, T_EQUALS);
220           if (lex_match_id (lexer, "ALPHA"))
221             {
222               reliability.model = MODEL_ALPHA;
223             }
224           else if (lex_match_id (lexer, "SPLIT"))
225             {
226               reliability.model = MODEL_SPLIT;
227               reliability.split_point = -1;
228
229               if ( lex_match (lexer, T_LPAREN))
230                 {
231                   lex_force_num (lexer);
232                   reliability.split_point = lex_number (lexer);
233                   lex_get (lexer);
234                   lex_force_match (lexer, T_RPAREN);
235                 }
236             }
237           else
238             goto error;
239         }
240       else if (lex_match_id (lexer, "SUMMARY"))
241         {
242           lex_match (lexer, T_EQUALS);
243           if (lex_match_id (lexer, "TOTAL"))
244             {
245               reliability.summary |= SUMMARY_TOTAL;
246             }
247           else if (lex_match (lexer, T_ALL))
248             {
249               reliability.summary = 0xFFFF;
250             }
251           else
252             goto error;
253         }
254       else if (lex_match_id (lexer, "MISSING"))
255         {
256           lex_match (lexer, T_EQUALS);
257           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
258             {
259               if (lex_match_id (lexer, "INCLUDE"))
260                 {
261                   reliability.exclude = MV_SYSTEM;
262                 }
263               else if (lex_match_id (lexer, "EXCLUDE"))
264                 {
265                   reliability.exclude = MV_ANY;
266                 }
267               else
268                 {
269                   lex_error (lexer, NULL);
270                   goto error;
271                 }
272             }
273         }
274       else
275         {
276           lex_error (lexer, NULL);
277           goto error;
278         }
279     }
280
281   if ( reliability.model == MODEL_SPLIT)
282     {
283       int i;
284       const struct cronbach *s;
285
286       if ( reliability.split_point >= reliability.n_variables)
287         {
288           msg (ME, _("The split point must be less than the number of variables"));
289           goto error;
290         }
291
292       reliability.n_sc += 2 ;
293       reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
294
295       s = &reliability.sc[0];
296
297       reliability.sc[1].n_items =
298         (reliability.split_point == -1) ? s->n_items / 2 : reliability.split_point;
299
300       reliability.sc[2].n_items = s->n_items - reliability.sc[1].n_items;
301       reliability.sc[1].items = xzalloc (sizeof (struct variable *)
302                                  * reliability.sc[1].n_items);
303
304       reliability.sc[2].items = xzalloc (sizeof (struct variable *) *
305                                  reliability.sc[2].n_items);
306
307       for  (i = 0; i < reliability.sc[1].n_items ; ++i)
308         reliability.sc[1].items[i] = s->items[i];
309
310       while (i < s->n_items)
311         {
312           reliability.sc[2].items[i - reliability.sc[1].n_items] = s->items[i];
313           i++;
314         }
315     }
316
317   if ( reliability.summary & SUMMARY_TOTAL)
318     {
319       int i;
320       const int base_sc = reliability.n_sc;
321
322       reliability.total_start = base_sc;
323
324       reliability.n_sc +=  reliability.sc[0].n_items ;
325       reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
326
327
328       for (i = 0 ; i < reliability.sc[0].n_items; ++i )
329         {
330           int v_src;
331           int v_dest = 0;
332           struct cronbach *s = &reliability.sc[i + base_sc];
333
334           s->n_items = reliability.sc[0].n_items - 1;
335           s->items = xzalloc (sizeof (struct variable *) * s->n_items);
336           for (v_src = 0 ; v_src < reliability.sc[0].n_items ; ++v_src)
337             {
338               if ( v_src != i)
339                 s->items[v_dest++] = reliability.sc[0].items[v_src];
340             }
341         }
342     }
343
344
345   if ( ! run_reliability (ds, &reliability)) 
346     goto error;
347
348   reliability_destroy (&reliability);
349   return CMD_SUCCESS;
350
351  error:
352   reliability_destroy (&reliability);
353   return CMD_FAILURE;
354 }
355
356
357 static void
358 do_reliability (struct casereader *group, struct dataset *ds,
359                 const struct reliability *rel);
360
361
362 static void reliability_summary_total (const struct reliability *rel);
363
364 static void reliability_statistics (const struct reliability *rel);
365
366
367 static bool
368 run_reliability (struct dataset *ds, const struct reliability *reliability)
369 {
370   struct dictionary *dict = dataset_dict (ds);
371   bool ok;
372   struct casereader *group;
373
374   struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
375
376   while (casegrouper_get_next_group (grouper, &group))
377     {
378       do_reliability (group, ds, reliability);
379
380       reliability_statistics (reliability);
381
382       if (reliability->summary & SUMMARY_TOTAL )
383         reliability_summary_total (reliability);
384     }
385
386   ok = casegrouper_destroy (grouper);
387   ok = proc_commit (ds) && ok;
388
389   return ok;
390 }
391
392
393 \f
394
395
396 /* Return the sum of all the item variables in S */
397 static  double
398 append_sum (const struct ccase *c, casenumber n UNUSED, void *aux)
399 {
400   double sum = 0;
401   const struct cronbach *s = aux;
402
403   int v;
404   for (v = 0 ; v < s->n_items; ++v)
405     {
406       sum += case_data (c, s->items[v])->f;
407     }
408
409   return sum;
410 };
411
412 static void
413 case_processing_summary (casenumber n_valid, casenumber n_missing,
414                          const struct dictionary *dict);
415
416
417 static double
418 alpha (int k, double sum_of_variances, double variance_of_sums)
419 {
420   return k / ( k - 1.0) * ( 1 - sum_of_variances / variance_of_sums);
421 }
422
423 static void
424 do_reliability (struct casereader *input, struct dataset *ds,
425                 const struct reliability *rel)
426 {
427   int i;
428   int si;
429   struct ccase *c;
430   casenumber n_missing ;
431   casenumber n_valid = 0;
432
433
434   for (si = 0 ; si < rel->n_sc; ++si)
435     {
436       struct cronbach *s = &rel->sc[si];
437
438       s->m = xzalloc (sizeof (s->m) * s->n_items);
439       s->total = moments1_create (MOMENT_VARIANCE);
440
441       for (i = 0 ; i < s->n_items ; ++i )
442         s->m[i] = moments1_create (MOMENT_VARIANCE);
443     }
444
445   input = casereader_create_filter_missing (input,
446                                             rel->variables,
447                                             rel->n_variables,
448                                             rel->exclude,
449                                             &n_missing,
450                                             NULL);
451
452   for (si = 0 ; si < rel->n_sc; ++si)
453     {
454       struct cronbach *s = &rel->sc[si];
455
456
457       s->totals_idx = caseproto_get_n_widths (casereader_get_proto (input));
458       input =
459         casereader_create_append_numeric (input, append_sum,
460                                           s, NULL);
461     }
462
463   for (; (c = casereader_read (input)) != NULL; case_unref (c))
464     {
465       double weight = 1.0;
466       n_valid ++;
467
468       for (si = 0; si < rel->n_sc; ++si)
469         {
470           struct cronbach *s = &rel->sc[si];
471
472           for (i = 0 ; i < s->n_items ; ++i )
473             moments1_add (s->m[i], case_data (c, s->items[i])->f, weight);
474
475           moments1_add (s->total, case_data_idx (c, s->totals_idx)->f, weight);
476         }
477     }
478   casereader_destroy (input);
479
480   for (si = 0; si < rel->n_sc; ++si)
481     {
482       struct cronbach *s = &rel->sc[si];
483
484       s->sum_of_variances = 0;
485       for (i = 0 ; i < s->n_items ; ++i )
486         {
487           double weight, mean, variance;
488           moments1_calculate (s->m[i], &weight, &mean, &variance, NULL, NULL);
489
490           s->sum_of_variances += variance;
491         }
492
493       moments1_calculate (s->total, NULL, NULL, &s->variance_of_sums,
494                           NULL, NULL);
495
496       s->alpha =
497         alpha (s->n_items, s->sum_of_variances, s->variance_of_sums);
498     }
499
500   text_item_submit (text_item_create_format (TEXT_ITEM_PARAGRAPH, _("Scale: %s"),
501                                              ds_cstr (&rel->scale_name)));
502
503   case_processing_summary (n_valid, n_missing, dataset_dict (ds));
504 }
505
506
507
508
509
510 static void
511 case_processing_summary (casenumber n_valid, casenumber n_missing,
512                          const struct dictionary *dict)
513 {
514   const struct variable *wv = dict_get_weight (dict);
515   const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
516
517   casenumber total;
518   int n_cols = 4;
519   int n_rows = 4;
520   int heading_columns = 2;
521   int heading_rows = 1;
522   struct tab_table *tbl;
523   tbl = tab_create (n_cols, n_rows);
524   tab_headers (tbl, heading_columns, 0, heading_rows, 0);
525
526   tab_title (tbl, _("Case Processing Summary"));
527
528   /* Vertical lines for the data only */
529   tab_box (tbl,
530            -1, -1,
531            -1, TAL_1,
532            heading_columns, 0,
533            n_cols - 1, n_rows - 1);
534
535   /* Box around table */
536   tab_box (tbl,
537            TAL_2, TAL_2,
538            -1, -1,
539            0, 0,
540            n_cols - 1, n_rows - 1);
541
542
543   tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
544
545   tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
546
547
548   tab_text (tbl, 0, heading_rows, TAB_LEFT | TAT_TITLE,
549                 _("Cases"));
550
551   tab_text (tbl, 1, heading_rows, TAB_LEFT | TAT_TITLE,
552                 _("Valid"));
553
554   tab_text (tbl, 1, heading_rows + 1, TAB_LEFT | TAT_TITLE,
555                 _("Excluded"));
556
557   tab_text (tbl, 1, heading_rows + 2, TAB_LEFT | TAT_TITLE,
558                 _("Total"));
559
560   tab_text (tbl, heading_columns, 0, TAB_CENTER | TAT_TITLE,
561                 _("N"));
562
563   tab_text (tbl, heading_columns + 1, 0, TAB_CENTER | TAT_TITLE, _("%"));
564
565   total = n_missing + n_valid;
566
567   tab_double (tbl, 2, heading_rows, TAB_RIGHT,
568              n_valid, wfmt);
569
570
571   tab_double (tbl, 2, heading_rows + 1, TAB_RIGHT,
572              n_missing, wfmt);
573
574
575   tab_double (tbl, 2, heading_rows + 2, TAB_RIGHT,
576              total, wfmt);
577
578
579   tab_double (tbl, 3, heading_rows, TAB_RIGHT,
580              100 * n_valid / (double) total, NULL);
581
582
583   tab_double (tbl, 3, heading_rows + 1, TAB_RIGHT,
584              100 * n_missing / (double) total, NULL);
585
586
587   tab_double (tbl, 3, heading_rows + 2, TAB_RIGHT,
588              100 * total / (double) total, NULL);
589
590
591   tab_submit (tbl);
592 }
593
594
595
596 static void
597 reliability_summary_total (const struct reliability *rel)
598 {
599   int i;
600   const int n_cols = 5;
601   const int heading_columns = 1;
602   const int heading_rows = 1;
603   const int n_rows = rel->sc[0].n_items + heading_rows ;
604
605   struct tab_table *tbl = tab_create (n_cols, n_rows);
606   tab_headers (tbl, heading_columns, 0, heading_rows, 0);
607
608   tab_title (tbl, _("Item-Total Statistics"));
609
610   /* Vertical lines for the data only */
611   tab_box (tbl,
612            -1, -1,
613            -1, TAL_1,
614            heading_columns, 0,
615            n_cols - 1, n_rows - 1);
616
617   /* Box around table */
618   tab_box (tbl,
619            TAL_2, TAL_2,
620            -1, -1,
621            0, 0,
622            n_cols - 1, n_rows - 1);
623
624
625   tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
626
627   tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
628
629   tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
630             _("Scale Mean if Item Deleted"));
631
632   tab_text (tbl, 2, 0, TAB_CENTER | TAT_TITLE,
633             _("Scale Variance if Item Deleted"));
634
635   tab_text (tbl, 3, 0, TAB_CENTER | TAT_TITLE,
636             _("Corrected Item-Total Correlation"));
637
638   tab_text (tbl, 4, 0, TAB_CENTER | TAT_TITLE,
639             _("Cronbach's Alpha if Item Deleted"));
640
641
642   for (i = 0 ; i < rel->sc[0].n_items; ++i)
643     {
644       double cov, item_to_total_r;
645       double mean, weight, var;
646
647       const struct cronbach *s = &rel->sc[rel->total_start + i];
648       tab_text (tbl, 0, heading_rows + i, TAB_LEFT| TAT_TITLE,
649                 var_to_string (rel->sc[0].items[i]));
650
651       moments1_calculate (s->total, &weight, &mean, &var, 0, 0);
652
653       tab_double (tbl, 1, heading_rows + i, TAB_RIGHT,
654                  mean, NULL);
655
656       tab_double (tbl, 2, heading_rows + i, TAB_RIGHT,
657                  s->variance_of_sums, NULL);
658
659       tab_double (tbl, 4, heading_rows + i, TAB_RIGHT,
660                  s->alpha, NULL);
661
662
663       moments1_calculate (rel->sc[0].m[i], &weight, &mean, &var, 0,0);
664       cov = rel->sc[0].variance_of_sums + var - s->variance_of_sums;
665       cov /= 2.0;
666
667       item_to_total_r = (cov - var) / (sqrt(var) * sqrt (s->variance_of_sums));
668
669
670       tab_double (tbl, 3, heading_rows + i, TAB_RIGHT,
671                  item_to_total_r, NULL);
672     }
673
674
675   tab_submit (tbl);
676 }
677
678
679 static void reliability_statistics_model_alpha (struct tab_table *tbl,
680                                                 const struct reliability *rel);
681
682 static void reliability_statistics_model_split (struct tab_table *tbl,
683                                                 const struct reliability *rel);
684
685
686 struct reliability_output_table
687 {
688   int n_cols;
689   int n_rows;
690   int heading_cols;
691   int heading_rows;
692   void (*populate) (struct tab_table *, const struct reliability *);
693 };
694
695
696 static struct reliability_output_table rol[2] =
697   {
698     { 2, 2, 1, 1, reliability_statistics_model_alpha},
699     { 4, 9, 3, 0, reliability_statistics_model_split}
700   };
701
702 static void
703 reliability_statistics (const struct reliability *rel)
704 {
705   int n_cols = rol[rel->model].n_cols;
706   int n_rows = rol[rel->model].n_rows;
707   int heading_columns = rol[rel->model].heading_cols;
708   int heading_rows = rol[rel->model].heading_rows;
709
710   struct tab_table *tbl = tab_create (n_cols, n_rows);
711   tab_headers (tbl, heading_columns, 0, heading_rows, 0);
712
713   tab_title (tbl, _("Reliability Statistics"));
714
715   /* Vertical lines for the data only */
716   tab_box (tbl,
717            -1, -1,
718            -1, TAL_1,
719            heading_columns, 0,
720            n_cols - 1, n_rows - 1);
721
722   /* Box around table */
723   tab_box (tbl,
724            TAL_2, TAL_2,
725            -1, -1,
726            0, 0,
727            n_cols - 1, n_rows - 1);
728
729
730   tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
731
732   tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
733
734   if ( rel->model == MODEL_ALPHA )
735     reliability_statistics_model_alpha (tbl, rel);
736   else if (rel->model == MODEL_SPLIT )
737     reliability_statistics_model_split (tbl, rel);
738
739   tab_submit (tbl);
740 }
741
742
743 static void
744 reliability_statistics_model_alpha (struct tab_table *tbl,
745                                     const struct reliability *rel)
746 {
747   const struct variable *wv = rel->wv;
748   const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
749
750   const struct cronbach *s = &rel->sc[0];
751
752   tab_text (tbl, 0, 0, TAB_CENTER | TAT_TITLE,
753                 _("Cronbach's Alpha"));
754
755   tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
756                 _("N of Items"));
757
758   tab_double (tbl, 0, 1, TAB_RIGHT, s->alpha, NULL);
759
760   tab_double (tbl, 1, 1, TAB_RIGHT, s->n_items, wfmt);
761 }
762
763
764 static void
765 reliability_statistics_model_split (struct tab_table *tbl,
766                                     const struct reliability *rel)
767 {
768   const struct variable *wv = rel->wv;
769   const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
770
771   tab_text (tbl, 0, 0, TAB_LEFT,
772             _("Cronbach's Alpha"));
773
774   tab_text (tbl, 1, 0, TAB_LEFT,
775             _("Part 1"));
776
777   tab_text (tbl, 2, 0, TAB_LEFT,
778             _("Value"));
779
780   tab_text (tbl, 2, 1, TAB_LEFT,
781             _("N of Items"));
782
783
784
785   tab_text (tbl, 1, 2, TAB_LEFT,
786             _("Part 2"));
787
788   tab_text (tbl, 2, 2, TAB_LEFT,
789             _("Value"));
790
791   tab_text (tbl, 2, 3, TAB_LEFT,
792             _("N of Items"));
793
794
795
796   tab_text (tbl, 1, 4, TAB_LEFT,
797             _("Total N of Items"));
798
799   tab_text (tbl, 0, 5, TAB_LEFT,
800             _("Correlation Between Forms"));
801
802
803   tab_text (tbl, 0, 6, TAB_LEFT,
804             _("Spearman-Brown Coefficient"));
805
806   tab_text (tbl, 1, 6, TAB_LEFT,
807             _("Equal Length"));
808
809   tab_text (tbl, 1, 7, TAB_LEFT,
810             _("Unequal Length"));
811
812
813   tab_text (tbl, 0, 8, TAB_LEFT,
814             _("Guttman Split-Half Coefficient"));
815
816
817
818   tab_double (tbl, 3, 0, TAB_RIGHT, rel->sc[1].alpha, NULL);
819   tab_double (tbl, 3, 2, TAB_RIGHT, rel->sc[2].alpha, NULL);
820
821   tab_double (tbl, 3, 1, TAB_RIGHT, rel->sc[1].n_items, wfmt);
822   tab_double (tbl, 3, 3, TAB_RIGHT, rel->sc[2].n_items, wfmt);
823
824   tab_double (tbl, 3, 4, TAB_RIGHT,
825              rel->sc[1].n_items + rel->sc[2].n_items, wfmt);
826
827   {
828     /* R is the correlation between the two parts */
829     double r = rel->sc[0].variance_of_sums -
830       rel->sc[1].variance_of_sums -
831       rel->sc[2].variance_of_sums ;
832
833     /* Guttman Split Half Coefficient */
834     double g = 2 * r / rel->sc[0].variance_of_sums;
835
836     /* Unequal Length Spearman Brown Coefficient, and
837      intermediate value used in the computation thereof */
838     double uly, tmp;
839
840     r /= sqrt (rel->sc[1].variance_of_sums);
841     r /= sqrt (rel->sc[2].variance_of_sums);
842     r /= 2.0;
843
844     tab_double (tbl, 3, 5, TAB_RIGHT, r, NULL);
845
846     /* Equal length Spearman-Brown Coefficient */
847     tab_double (tbl, 3, 6, TAB_RIGHT, 2 * r / (1.0 + r), NULL);
848
849     tab_double (tbl, 3, 8, TAB_RIGHT, g, NULL);
850
851     tmp = (1.0 - r*r) * rel->sc[1].n_items * rel->sc[2].n_items /
852       pow2 (rel->sc[0].n_items);
853
854     uly = sqrt( pow4 (r) + 4 * pow2 (r) * tmp);
855     uly -= pow2 (r);
856     uly /= 2 * tmp;
857
858     tab_double (tbl, 3, 7, TAB_RIGHT, uly, NULL);
859   }
860 }
861