1 /* PSPP - computes sample statistics. -*-c-*-
3 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 Written by John Williams <johnr.williams@stonebow.otago.ac.nz>.
5 Almost completly re-written by John Darrington 2004
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 #include "dcdflib/cdflib.h"
36 #include "value-labels.h"
47 variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
49 +missing=miss:!analysis/listwise,
50 incl:include/!exclude;
51 format=fmt:!labels/nolabels;
52 criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
57 static struct cmd_t_test cmd;
61 static struct pool *t_test_pool ;
63 /* Variable for the GROUPS subcommand, if given. */
64 static struct variable *groups;
66 /* GROUPS: Number of values specified by the user; the values
68 static int n_groups_values;
69 static union value groups_values[2];
71 /* PAIRS: Number of pairs to be compared ; each pair. */
72 static int n_pairs = 0 ;
75 /* The variables comprising the pair */
76 struct variable *v[2];
78 /* The correlation coefficient between the variables */
81 /* The sum of the differences */
84 /* The mean of the differences */
87 /* The sum of the squares of the differences */
90 /* The std deviation of the differences */
93 static struct pair *pairs=0;
96 static int parse_value (union value * v, int type) ;
99 /* Structures and Functions for the Statistics Summary Box */
101 typedef void populate_ssbox_func(struct ssbox *ssb,
102 struct cmd_t_test *cmd);
103 typedef void finalize_ssbox_func(struct ssbox *ssb);
109 populate_ssbox_func *populate;
110 finalize_ssbox_func *finalize;
115 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
117 /* Populate a ssbox according to cmd */
118 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
120 /* Submit and destroy a ssbox */
121 void ssbox_finalize(struct ssbox *ssb);
123 /* A function to create, populate and submit the Paired Samples Correlation
125 void pscbox(struct cmd_t_test *cmd);
128 /* Structures and Functions for the Test Results Box */
131 typedef void populate_trbox_func(struct trbox *trb,
132 struct cmd_t_test *cmd);
133 typedef void finalize_trbox_func(struct trbox *trb);
137 populate_trbox_func *populate;
138 finalize_trbox_func *finalize;
142 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
144 /* Populate a ssbox according to cmd */
145 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
147 /* Submit and destroy a ssbox */
148 void trbox_finalize(struct trbox *trb);
150 /* Which mode was T-TEST invoked */
158 static int common_calc (struct ccase *);
159 static void common_precalc (void);
160 static void common_postcalc (void);
162 static int one_sample_calc (struct ccase *);
163 static void one_sample_precalc (void);
164 static void one_sample_postcalc (void);
166 static int paired_calc (struct ccase *);
167 static void paired_precalc (void);
168 static void paired_postcalc (void);
170 static int compare_var_name (const void *a_, const void *b_, void *v_ unused);
171 static unsigned hash_var_name (const void *a_, void *v_ unused);
179 struct ssbox stat_summary_box;
180 struct trbox test_results_box;
182 if (!lex_force_match_id ("T"))
186 lex_match_id ("TEST");
188 if ( !parse_t_test(&cmd) )
191 if (! cmd.sbc_criteria)
196 if (cmd.sbc_testval) ++m;
197 if (cmd.sbc_groups) ++m;
198 if (cmd.sbc_pairs) ++m;
203 _("TESTVAL, GROUPS and PAIRS subcommands are mutually exclusive.")
211 else if (cmd.sbc_groups)
216 if ( mode == T_PAIRED)
218 if (cmd.sbc_variables)
220 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
225 /* Iterate through the pairs and put each variable that is a
226 member of a pair into cmd.v_variables */
229 struct hsh_iterator hi;
230 struct hsh_table *hash;
233 hash=hsh_create(n_pairs,compare_var_name,hash_var_name,0,0);
235 for (i=0; i < n_pairs; ++i)
237 hsh_insert(hash,pairs[i].v[0]);
238 hsh_insert(hash,pairs[i].v[1]);
241 assert(cmd.n_variables == 0);
242 cmd.n_variables = hsh_count(hash);
244 cmd.v_variables = xrealloc(cmd.v_variables,
245 sizeof(struct variable) * cmd.n_variables);
246 /* Iterate through the hash */
247 for (i=0,v = (struct variable *) hsh_first(hash,&hi);
249 v=hsh_next(hash,&hi) )
250 cmd.v_variables[i++]=v;
257 procedure(common_precalc,common_calc,common_postcalc);
262 procedure(one_sample_precalc,one_sample_calc,one_sample_postcalc);
265 procedure(paired_precalc,paired_calc,paired_postcalc);
270 t_test_pool = pool_create ();
272 ssbox_create(&stat_summary_box,&cmd,mode);
273 ssbox_populate(&stat_summary_box,&cmd);
274 ssbox_finalize(&stat_summary_box);
276 if ( mode == T_PAIRED)
281 trbox_create(&test_results_box,&cmd,mode);
282 trbox_populate(&test_results_box,&cmd);
283 trbox_finalize(&test_results_box);
285 pool_destroy (t_test_pool);
298 tts_custom_groups (struct cmd_t_test *cmd unused)
302 if (token != T_ALL &&
303 (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
306 msg(SE,_("`%s' is not a variable name"),tokid);
310 groups = parse_variable ();
313 lex_error ("expecting variable name in GROUPS subcommand");
317 if (groups->type == T_STRING && groups->width > MAX_SHORT_STRING)
319 msg (SE, _("Long string variable %s is not valid here."),
324 if (!lex_match ('('))
326 if (groups->type == NUMERIC)
329 groups_values[0].f = 1;
330 groups_values[1].f = 2;
335 msg (SE, _("When applying GROUPS to a string variable, at "
336 "least one value must be specified."));
341 if (!parse_value (&groups_values[0],groups->type))
349 if (!parse_value (&groups_values[1],groups->type))
353 if (!lex_force_match (')'))
363 tts_custom_pairs (struct cmd_t_test *cmd unused)
365 struct variable **vars;
370 int n_after_WITH = -1;
371 int paired ; /* Was the PAIRED keyword given ? */
375 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
378 msg(SE,_("`%s' is not a variable name"),tokid);
383 if (!parse_variables (default_dict, &vars, &n_vars,
384 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
392 if (lex_match (T_WITH))
394 n_before_WITH = n_vars;
395 if (!parse_variables (default_dict, &vars, &n_vars,
396 PV_DUPLICATE | PV_APPEND
397 | PV_NUMERIC | PV_NO_SCRATCH))
402 n_after_WITH = n_vars - n_before_WITH;
405 paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
407 /* Determine the number of pairs needed */
410 if (n_before_WITH != n_after_WITH)
413 msg (SE, _("PAIRED was specified but the number of variables "
414 "preceding WITH (%d) did not match the number "
416 n_before_WITH, n_after_WITH );
419 n_pairs_local=n_before_WITH;
421 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
423 n_pairs_local=n_before_WITH * n_after_WITH ;
425 else /* Neither WITH nor PAIRED keyword given */
430 msg (SE, _("At least two variables must be specified "
435 /* how many ways can you pick 2 from n_vars ? */
436 n_pairs_local = n_vars * (n_vars -1 ) /2 ;
440 /* Allocate storage for the pairs */
441 pairs = xrealloc(pairs, sizeof(struct pair) * (n_pairs + n_pairs_local) );
443 /* Populate the pairs with the appropriate variables */
448 assert(n_pairs_local == n_vars/2);
449 for (i = 0; i < n_pairs_local ; ++i)
451 pairs[i].v[n_pairs+0] = vars[i];
452 pairs[i].v[n_pairs+1] = vars[i+n_pairs_local];
455 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
460 for(i=0 ; i < n_before_WITH ; ++i )
462 for(j=0 ; j < n_after_WITH ; ++j)
464 pairs[p].v[0] = vars[i];
465 pairs[p].v[1] = vars[j+n_before_WITH];
470 else /* Neither WITH nor PAIRED given */
475 for(i=0 ; i < n_vars ; ++i )
477 for(j=i+1 ; j < n_vars ; ++j)
479 pairs[p].v[0] = vars[i];
480 pairs[p].v[1] = vars[j];
486 n_pairs+=n_pairs_local;
491 /* Parses the current token (numeric or string, depending on type)
492 value v and returns success. */
494 parse_value (union value * v, int type )
498 if (!lex_force_num ())
504 if (!lex_force_string ())
506 strncpy (v->s, ds_value (&tokstr), ds_length (&tokstr));
515 /* Implementation of the SSBOX object */
517 void ssbox_base_init(struct ssbox *this, int cols,int rows);
519 void ssbox_base_finalize(struct ssbox *ssb);
521 void ssbox_one_sample_init(struct ssbox *this,
522 struct cmd_t_test *cmd );
524 void ssbox_independent_samples_init(struct ssbox *this,
525 struct cmd_t_test *cmd);
527 void ssbox_paired_init(struct ssbox *this,
528 struct cmd_t_test *cmd);
530 /* Factory to create an ssbox */
532 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
537 ssbox_one_sample_init(ssb,cmd);
540 ssbox_independent_samples_init(ssb,cmd);
543 ssbox_paired_init(ssb,cmd);
551 /* Despatcher for the populate method */
553 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
555 ssb->populate(ssb,cmd);
559 /* Despatcher for finalize */
561 ssbox_finalize(struct ssbox *ssb)
567 /* Submit the box and clear up */
569 ssbox_base_finalize(struct ssbox *ssb)
574 /* Initialize a ssbox struct */
576 ssbox_base_init(struct ssbox *this, int cols,int rows)
578 this->finalize = ssbox_base_finalize;
579 this->t = tab_create (cols, rows, 0);
581 tab_columns (this->t, SOM_COL_DOWN, 1);
582 tab_headers (this->t,0,0,1,0);
583 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
584 tab_hline(this->t, TAL_2,0,cols-1,1);
585 tab_dim (this->t, tab_natural_dimensions);
588 void ssbox_one_sample_populate(struct ssbox *ssb,
589 struct cmd_t_test *cmd);
591 /* Initialize the one_sample ssbox */
593 ssbox_one_sample_init(struct ssbox *this,
594 struct cmd_t_test *cmd )
597 const int vsize=cmd->n_variables+1;
599 this->populate = ssbox_one_sample_populate;
601 ssbox_base_init(this, hsize,vsize);
602 tab_title (this->t, 0, _("One-Sample Statistics"));
603 tab_vline(this->t, TAL_2, 1,0,vsize);
604 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
605 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
606 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
607 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
610 void ssbox_independent_samples_populate(struct ssbox *ssb,
611 struct cmd_t_test *cmd);
613 /* Initialize the independent samples ssbox */
615 ssbox_independent_samples_init(struct ssbox *this,
616 struct cmd_t_test *cmd)
619 int vsize = cmd->n_variables*2 +1;
621 this->populate = ssbox_independent_samples_populate;
623 ssbox_base_init(this, hsize,vsize);
624 tab_title (this->t, 0, _("Group Statistics"));
625 tab_vline(this->t,0,1,0,vsize);
626 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, groups->name);
627 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
628 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
629 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
630 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
634 /* Populate the ssbox for independent samples */
636 ssbox_independent_samples_populate(struct ssbox *ssb,
637 struct cmd_t_test *cmd)
644 if ( groups->type == NUMERIC )
646 val_lab1 = val_labs_find( groups->val_labs,groups_values[0]);
647 val_lab2 = val_labs_find( groups->val_labs,groups_values[1]);
651 val_lab1 = groups_values[0].s;
652 val_lab2 = groups_values[1].s;
657 for (i=0; i < cmd->n_variables; ++i)
659 tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
662 tab_text (ssb->t, 1, i*2+1, TAB_LEFT, val_lab1);
664 tab_float(ssb->t, 1 ,i*2+1, TAB_LEFT, groups_values[0].f, 2,0);
667 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT, val_lab2);
669 tab_float(ssb->t, 1 ,i*2+1+1, TAB_LEFT, groups_values[1].f,2,0);
674 void ssbox_paired_populate(struct ssbox *ssb,
675 struct cmd_t_test *cmd);
677 /* Initialize the paired values ssbox */
679 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd unused)
683 int vsize = n_pairs*2+1;
685 this->populate = ssbox_paired_populate;
687 ssbox_base_init(this, hsize,vsize);
688 tab_title (this->t, 0, _("Paired Sample Statistics"));
689 tab_vline(this->t,TAL_0,1,0,vsize-1);
690 tab_vline(this->t,TAL_2,2,0,vsize-1);
691 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
692 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
693 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
694 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
698 /* Populate the ssbox for paired values */
700 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd unused)
706 for (i=0; i < n_pairs; ++i)
710 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
712 for (j=0 ; j < 2 ; ++j)
714 struct t_test_proc *ttp;
716 ttp=&pairs[i].v[j]->p.t_t;
720 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT, pairs[i].v[j]->name);
723 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, ttp->mean, 8, 2);
724 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, ttp->n, 2, 0);
725 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, ttp->std_dev, 8, 3);
726 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, ttp->se_mean, 8, 3);
734 /* Populate the one sample ssbox */
736 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
742 for (i=0; i < cmd->n_variables; ++i)
744 struct t_test_proc *ttp;
745 ttp= &cmd->v_variables[i]->p.t_t;
747 tab_text (ssb->t, 0, i+1, TAB_LEFT, cmd->v_variables[i]->name);
748 tab_float (ssb->t,1, i+1, TAB_RIGHT, ttp->n, 2, 0);
749 tab_float (ssb->t,2, i+1, TAB_RIGHT, ttp->mean, 8, 2);
750 tab_float (ssb->t,3, i+1, TAB_RIGHT, ttp->std_dev, 8, 2);
751 tab_float (ssb->t,4, i+1, TAB_RIGHT, ttp->se_mean, 8, 3);
758 /* Implementation of the Test Results box struct */
760 void trbox_base_init(struct trbox *self,int n_vars, int cols);
761 void trbox_base_finalize(struct trbox *trb);
763 void trbox_independent_samples_init(struct trbox *trb,
764 struct cmd_t_test *cmd );
766 void trbox_independent_samples_populate(struct trbox *trb,
767 struct cmd_t_test *cmd);
769 void trbox_one_sample_init(struct trbox *self,
770 struct cmd_t_test *cmd );
772 void trbox_one_sample_populate(struct trbox *trb,
773 struct cmd_t_test *cmd);
775 void trbox_paired_init(struct trbox *self,
776 struct cmd_t_test *cmd );
778 void trbox_paired_populate(struct trbox *trb,
779 struct cmd_t_test *cmd);
783 /* Create a trbox according to mode*/
785 trbox_create(struct trbox *trb,
786 struct cmd_t_test *cmd, int mode)
791 trbox_one_sample_init(trb,cmd);
794 trbox_independent_samples_init(trb,cmd);
797 trbox_paired_init(trb,cmd);
804 /* Populate a trbox according to cmd */
806 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
808 trb->populate(trb,cmd);
811 /* Submit and destroy a trbox */
813 trbox_finalize(struct trbox *trb)
818 /* Initialize the independent samples trbox */
820 trbox_independent_samples_init(struct trbox *self,
821 struct cmd_t_test *cmd unused)
824 const int vsize=cmd->n_variables*2+3;
827 self->populate = trbox_independent_samples_populate;
829 trbox_base_init(self,cmd->n_variables*2,hsize);
830 tab_title(self->t,0,_("Independent Samples Test"));
831 tab_hline(self->t,TAL_1,2,hsize-1,1);
832 tab_vline(self->t,TAL_2,2,0,vsize-1);
833 tab_vline(self->t,TAL_1,4,0,vsize-1);
834 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
835 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
836 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
837 tab_joint_text(self->t, 2, 0, 3, 0,
838 TAB_CENTER,_("Levine's Test for Equality of Variances"));
839 tab_joint_text(self->t, 4,0,hsize-1,0,
840 TAB_CENTER,_("t-test for Equality of Means"));
842 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
843 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
844 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
845 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
846 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
847 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
848 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
849 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
850 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
852 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
853 _("%d%% Confidence Interval of the Difference"),
854 (int)round(cmd->criteria*100.0));
858 /* Populate the independent samples trbox */
860 trbox_independent_samples_populate(struct trbox *self,
861 struct cmd_t_test *cmd )
866 for (i=0; i < cmd->n_variables; ++i)
868 tab_text (self->t, 0, i*2+3, TAB_LEFT, cmd->v_variables[i]->name);
870 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
872 tab_text (self->t, 1, i*2+3+1,
873 TAB_LEFT, _("Equal variances not assumed"));
877 /* Initialize the paired samples trbox */
879 trbox_paired_init(struct trbox *self,
880 struct cmd_t_test *cmd unused)
884 const int vsize=n_pairs+3;
886 self->populate = trbox_paired_populate;
888 trbox_base_init(self,n_pairs,hsize);
889 tab_title (self->t, 0, _("Paired Samples Test"));
890 tab_hline(self->t,TAL_1,2,6,1);
891 tab_vline(self->t,TAL_2,2,0,vsize);
892 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
893 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
894 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
895 tab_hline(self->t,TAL_1,5,6, 2);
896 tab_vline(self->t,TAL_0,6,0,1);
898 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
899 _("%d%% Confidence Interval of the Difference"),
900 (int)round(cmd->criteria*100.0));
902 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
903 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
904 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
905 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
906 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
907 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
908 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
909 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
912 /* Populate the paired samples trbox */
914 trbox_paired_populate(struct trbox *trb,
915 struct cmd_t_test *cmd unused)
919 for (i=0; i < n_pairs; ++i)
927 struct variable *v0 = pairs[i].v[0];
928 struct variable *v1 = pairs[i].v[1];
930 struct t_test_proc *ttp0 = &v0->p.t_t;
931 struct t_test_proc *ttp1 = &v1->p.t_t;
937 tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
939 tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
940 pairs[i].v[0]->name, pairs[i].v[1]->name);
942 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
944 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
947 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
948 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
950 /* Now work out the confidence interval */
951 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
953 which=2; /* Calc T from p,q and df */
954 cdft(&which, &p, &q, &t, &df, &status, &bound);
958 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
961 tab_float(trb->t, 5, i+3, TAB_RIGHT,
962 pairs[i].mean_diff - t * se_mean , 8, 4);
964 tab_float(trb->t, 6, i+3, TAB_RIGHT,
965 pairs[i].mean_diff + t * se_mean , 8, 4);
967 t = ( ttp0->mean - ttp1->mean)
969 ( sqr(ttp0->s_std_dev) + sqr(ttp1->s_std_dev) -
970 2 * pairs[i].correlation * ttp0->s_std_dev * ttp1->s_std_dev )
974 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
976 /* Degrees of freedom */
977 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
980 cdft(&which, &p, &q, &t, &df, &status, &bound);
984 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
988 tab_float(trb->t, 9, i+3, TAB_RIGHT, p*2.0 , 8, 3);
995 /* Initialize the one sample trbox */
997 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
1000 const int vsize=cmd->n_variables+3;
1002 self->populate = trbox_one_sample_populate;
1004 trbox_base_init(self, cmd->n_variables,hsize);
1005 tab_title (self->t, 0, _("One-Sample Test"));
1006 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
1007 tab_vline(self->t, TAL_2, 1, 0, vsize);
1009 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
1010 _("Test Value = %f"),cmd->n_testval);
1012 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
1015 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1016 _("%d%% Confidence Interval of the Difference"),
1017 (int)round(cmd->criteria*100.0));
1019 tab_vline(self->t,TAL_0,6,1,1);
1020 tab_hline(self->t,TAL_1,5,6,2);
1021 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1022 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1023 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1024 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1025 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1026 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1031 /* Populate the one sample trbox */
1033 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1039 for (i=0; i < cmd->n_variables; ++i)
1047 struct t_test_proc *ttp;
1048 ttp= &cmd->v_variables[i]->p.t_t;
1051 tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
1053 t = (ttp->mean - cmd->n_testval ) * sqrt(ttp->n) / ttp->std_dev ;
1055 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1057 /* degrees of freedom */
1060 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1062 cdft(&which, &p, &q, &t, &df, &status, &bound);
1066 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1070 /* Multiply by 2 to get 2-tailed significance */
1071 tab_float (trb->t, 3, i+3, TAB_RIGHT, q*2.0, 8,3);
1073 tab_float (trb->t, 4, i+3, TAB_RIGHT, ttp->mean_diff, 8,3);
1076 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1078 which=2; /* Calc T from p,q and df */
1079 cdft(&which, &p, &q, &t, &df, &status, &bound);
1082 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1085 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1086 ttp->mean_diff - t * ttp->se_mean, 8,4);
1088 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1089 ttp->mean_diff + t * ttp->se_mean, 8,4);
1093 /* Base initializer for the generalized trbox */
1095 trbox_base_init(struct trbox *self, int data_rows, int cols)
1097 const int rows = 3 + data_rows;
1099 self->finalize = trbox_base_finalize;
1100 self->t = tab_create (cols, rows, 0);
1101 tab_headers (self->t,0,0,3,0);
1102 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1103 tab_hline(self->t, TAL_2,0,cols-1,3);
1104 tab_dim (self->t, tab_natural_dimensions);
1108 /* Base finalizer for the trbox */
1110 trbox_base_finalize(struct trbox *trb)
1116 /* Create , populate and submit the Paired Samples Correlation box */
1118 pscbox(struct cmd_t_test *cmd)
1120 const int rows=1+n_pairs;
1124 struct tab_table *table;
1126 table = tab_create (cols,rows,0);
1128 tab_columns (table, SOM_COL_DOWN, 1);
1129 tab_headers (table,0,0,1,0);
1130 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1131 tab_hline(table, TAL_2, 0, cols - 1, 1);
1132 tab_vline(table, TAL_2, 2, 0, rows - 1);
1133 tab_dim(table, tab_natural_dimensions);
1134 tab_title(table, 0, _("Paired Samples Correlations"));
1136 /* column headings */
1137 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1138 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1139 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1142 for (i=0; i < n_pairs; ++i)
1150 double df = pairs[i].v[0]->p.t_t.n -2;
1152 double correlation_t =
1153 pairs[i].correlation * sqrt(df) /
1154 sqrt(1 - sqr(pairs[i].correlation));
1158 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1161 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1162 _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
1166 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1167 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].v[0]->p.t_t.n , 4, 0);
1170 cdft(&which, &p, &q, &correlation_t, &df, &status, &bound);
1174 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1178 tab_float(table, 4, i+1, TAB_RIGHT, q*2.0, 8, 3);
1190 /* Calculation Implementation */
1192 /* Per case calculations common to all variants of the T test */
1194 common_calc (struct ccase *c)
1198 double weight = dict_get_case_weight(default_dict,c);
1200 for(i=0; i< cmd.n_variables ; ++i)
1202 struct t_test_proc *ttp;
1203 struct variable *v = cmd.v_variables[i];
1204 union value *val = &c->data[v->fv];
1206 ttp= &cmd.v_variables[i]->p.t_t;
1208 if (val->f != SYSMIS)
1211 ttp->sum+=weight * val->f;
1212 ttp->ssq+=weight * val->f * val->f;
1218 /* Pre calculations common to all variants of the T test */
1220 common_precalc (void)
1224 for(i=0; i< cmd.n_variables ; ++i)
1226 struct t_test_proc *ttp;
1227 ttp= &cmd.v_variables[i]->p.t_t;
1236 /* Post calculations common to all variants of the T test */
1238 common_postcalc (void)
1242 for(i=0; i< cmd.n_variables ; ++i)
1244 struct t_test_proc *ttp;
1245 ttp= &cmd.v_variables[i]->p.t_t;
1247 ttp->mean=ttp->sum / ttp->n;
1248 ttp->s_std_dev= sqrt(
1249 ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean )
1254 ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean )
1257 ttp->se_mean = ttp->std_dev / sqrt(ttp->n);
1258 ttp->mean_diff= ttp->sum_diff / ttp->n;
1262 /* Per case calculations for one sample t test */
1264 one_sample_calc (struct ccase *c)
1268 double weight = dict_get_case_weight(default_dict,c);
1270 for(i=0; i< cmd.n_variables ; ++i)
1272 struct t_test_proc *ttp;
1273 struct variable *v = cmd.v_variables[i];
1274 union value *val = &c->data[v->fv];
1276 ttp= &cmd.v_variables[i]->p.t_t;
1278 if (val->f != SYSMIS)
1279 ttp->sum_diff += weight * (val->f - cmd.n_testval);
1285 /* Pre calculations for one sample t test */
1287 one_sample_precalc (void)
1291 for(i=0; i< cmd.n_variables ; ++i)
1293 struct t_test_proc *ttp;
1294 ttp= &cmd.v_variables[i]->p.t_t;
1300 /* Post calculations for one sample t test */
1302 one_sample_postcalc (void)
1306 for(i=0; i< cmd.n_variables ; ++i)
1308 struct t_test_proc *ttp;
1309 ttp= &cmd.v_variables[i]->p.t_t;
1312 ttp->mean_diff = ttp->sum_diff / ttp->n ;
1319 compare_var_name (const void *a_, const void *b_, void *v_ unused)
1321 const struct variable *a = a_;
1322 const struct variable *b = b_;
1324 return strcmp(a->name,b->name);
1328 hash_var_name (const void *a_, void *v_ unused)
1330 const struct variable *a = a_;
1332 return hsh_hash_bytes (a->name, strlen(a->name));
1337 paired_precalc (void)
1341 for(i=0; i < n_pairs ; ++i )
1343 pairs[i].correlation=0;
1344 pairs[i].sum_of_diffs=0;
1345 pairs[i].ssq_diffs=0;
1351 paired_calc (struct ccase *c)
1355 for(i=0; i < n_pairs ; ++i )
1357 struct variable *v0 = pairs[i].v[0];
1358 struct variable *v1 = pairs[i].v[1];
1360 union value *val0 = &c->data[v0->fv];
1361 union value *val1 = &c->data[v1->fv];
1363 pairs[i].correlation += ( val0->f - pairs[i].v[0]->p.t_t.mean )
1365 ( val1->f - pairs[i].v[1]->p.t_t.mean );
1367 pairs[i].sum_of_diffs += val0->f - val1->f ;
1368 pairs[i].ssq_diffs += sqr(val0->f - val1->f);
1377 paired_postcalc (void)
1381 for(i=0; i < n_pairs ; ++i )
1383 const double n = pairs[i].v[0]->p.t_t.n ;
1385 pairs[i].correlation /= pairs[i].v[0]->p.t_t.std_dev *
1386 pairs[i].v[1]->p.t_t.std_dev ;
1387 pairs[i].correlation /= pairs[i].v[0]->p.t_t.n -1;
1390 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1393 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1394 ( pairs[i].ssq_diffs / n )
1396 sqr(pairs[i].mean_diff )