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. */
75 struct variable *v[2];
78 static struct pair *pairs;
81 static int parse_value (union value * v, int type) ;
84 /* Structures and Functions for the Statistics Summary Box */
86 typedef void populate_ssbox_func(struct ssbox *ssb,
87 struct cmd_t_test *cmd);
88 typedef void finalize_ssbox_func(struct ssbox *ssb);
94 populate_ssbox_func *populate;
95 finalize_ssbox_func *finalize;
100 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
102 /* Populate a ssbox according to cmd */
103 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
105 /* Submit and destroy a ssbox */
106 void ssbox_finalize(struct ssbox *ssb);
108 /* A function to create, populate and submit the Paired Samples Correlation
110 void pscbox(struct cmd_t_test *cmd);
113 /* Structures and Functions for the Test Results Box */
116 typedef void populate_trbox_func(struct trbox *trb,
117 struct cmd_t_test *cmd);
118 typedef void finalize_trbox_func(struct trbox *trb);
122 populate_trbox_func *populate;
123 finalize_trbox_func *finalize;
127 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
129 /* Populate a ssbox according to cmd */
130 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
132 /* Submit and destroy a ssbox */
133 void trbox_finalize(struct trbox *trb);
135 /* Which mode was T-TEST invoked */
143 static int common_calc (struct ccase *);
144 static void common_precalc (void);
145 static void common_postcalc (void);
147 static int one_sample_calc (struct ccase *);
148 static void one_sample_precalc (void);
149 static void one_sample_postcalc (void);
151 static int paired_calc (struct ccase *);
152 static void paired_precalc (void);
153 static void paired_postcalc (void);
155 static int compare_var_name (const void *a_, const void *b_, void *v_ unused);
156 static unsigned hash_var_name (const void *a_, void *v_ unused);
164 struct ssbox stat_summary_box;
165 struct trbox test_results_box;
167 if (!lex_force_match_id ("T"))
171 lex_match_id ("TEST");
173 if ( !parse_t_test(&cmd) )
176 if (! cmd.sbc_criteria)
179 if ( cmd.sbc_testval + cmd.sbc_groups + cmd.sbc_pairs != 1 )
182 _("Exactly one of TESTVAL, GROUPS or PAIRS subcommands is required")
189 else if (cmd.sbc_groups)
194 if ( mode == T_PAIRED)
196 if (cmd.sbc_variables)
198 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
203 /* Iterate through the pairs and put each variable that is a
204 member of a pair into cmd.v_variables */
207 struct hsh_iterator hi;
208 struct hsh_table *hash;
211 hash=hsh_create(n_pairs,compare_var_name,hash_var_name,0,0);
213 for (i=0; i < n_pairs; ++i)
215 hsh_insert(hash,pairs[i].v[0]);
216 hsh_insert(hash,pairs[i].v[1]);
219 assert(cmd.n_variables == 0);
220 cmd.n_variables = hsh_count(hash);
222 cmd.v_variables = xrealloc(cmd.v_variables,
223 sizeof(struct variable) * cmd.n_variables);
224 /* Iterate through the hash */
225 for (i=0,v = (struct variable *) hsh_first(hash,&hi);
227 v=hsh_next(hash,&hi) )
228 cmd.v_variables[i++]=v;
235 procedure(common_precalc,common_calc,common_postcalc);
240 procedure(one_sample_precalc,one_sample_calc,one_sample_postcalc);
243 procedure(paired_precalc,paired_calc,paired_postcalc);
248 t_test_pool = pool_create ();
250 ssbox_create(&stat_summary_box,&cmd,mode);
251 ssbox_populate(&stat_summary_box,&cmd);
252 ssbox_finalize(&stat_summary_box);
254 if ( mode == T_PAIRED)
259 trbox_create(&test_results_box,&cmd,mode);
260 trbox_populate(&test_results_box,&cmd);
261 trbox_finalize(&test_results_box);
263 pool_destroy (t_test_pool);
271 tts_custom_groups (struct cmd_t_test *cmd unused)
275 if (token != T_ALL &&
276 (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
279 msg(SE,_("`%s' is not a variable name"),tokid);
283 groups = parse_variable ();
286 lex_error ("expecting variable name in GROUPS subcommand");
290 if (groups->type == T_STRING && groups->width > MAX_SHORT_STRING)
292 msg (SE, _("Long string variable %s is not valid here."),
297 if (!lex_match ('('))
299 if (groups->type == NUMERIC)
302 groups_values[0].f = 1;
303 groups_values[1].f = 2;
308 msg (SE, _("When applying GROUPS to a string variable, at "
309 "least one value must be specified."));
314 if (!parse_value (&groups_values[0],groups->type))
322 if (!parse_value (&groups_values[1],groups->type))
326 if (!lex_force_match (')'))
336 tts_custom_pairs (struct cmd_t_test *cmd unused)
338 struct variable **vars;
342 int n_after_WITH = -1;
343 int paired ; /* Was the PAIRED keyword given ? */
347 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
350 msg(SE,_("`%s' is not a variable name"),tokid);
355 if (!parse_variables (default_dict, &vars, &n_vars,
356 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
364 if (lex_match (T_WITH))
366 n_before_WITH = n_vars;
367 if (!parse_variables (default_dict, &vars, &n_vars,
368 PV_DUPLICATE | PV_APPEND
369 | PV_NUMERIC | PV_NO_SCRATCH))
374 n_after_WITH = n_vars - n_before_WITH;
377 paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
379 /* Determine the number of pairs needed */
382 if (n_before_WITH != n_after_WITH)
385 msg (SE, _("PAIRED was specified but the number of variables "
386 "preceding WITH (%d) did not match the number "
388 n_before_WITH, n_after_WITH );
391 n_pairs=n_before_WITH;
393 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
395 n_pairs=n_before_WITH * n_after_WITH ;
397 else /* Neither WITH nor PAIRED keyword given */
402 msg (SE, _("At least two variables must be specified "
407 /* how many ways can you pick 2 from n_vars ? */
408 n_pairs = n_vars * (n_vars -1 ) /2 ;
411 /* Allocate storage for the pairs */
412 pairs = xrealloc(pairs,sizeof(struct pair) *n_pairs);
414 /* Populate the pairs with the appropriate variables */
419 assert(n_pairs == n_vars/2);
420 for (i = 0; i < n_pairs ; ++i)
422 pairs[i].v[0] = vars[i];
423 pairs[i].v[1] = vars[i+n_pairs];
426 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
431 for(i=0 ; i < n_before_WITH ; ++i )
433 for(j=0 ; j < n_after_WITH ; ++j)
435 pairs[p].v[0] = vars[i];
436 pairs[p].v[1] = vars[j+n_before_WITH];
441 else /* Neither WITH nor PAIRED given */
446 for(i=0 ; i < n_vars ; ++i )
448 for(j=i+1 ; j < n_vars ; ++j)
450 pairs[p].v[0] = vars[i];
451 pairs[p].v[1] = vars[j];
460 /* Parses the current token (numeric or string, depending on type)
461 value v and returns success. */
463 parse_value (union value * v, int type )
467 if (!lex_force_num ())
473 if (!lex_force_string ())
475 strncpy (v->s, ds_value (&tokstr), ds_length (&tokstr));
484 /* Implementation of the SSBOX object */
486 void ssbox_base_init(struct ssbox *this, int cols,int rows);
488 void ssbox_base_finalize(struct ssbox *ssb);
490 void ssbox_one_sample_init(struct ssbox *this,
491 struct cmd_t_test *cmd );
493 void ssbox_independent_samples_init(struct ssbox *this,
494 struct cmd_t_test *cmd);
496 void ssbox_paired_init(struct ssbox *this,
497 struct cmd_t_test *cmd);
499 /* Factory to create an ssbox */
501 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
506 ssbox_one_sample_init(ssb,cmd);
509 ssbox_independent_samples_init(ssb,cmd);
512 ssbox_paired_init(ssb,cmd);
520 /* Despatcher for the populate method */
522 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
524 ssb->populate(ssb,cmd);
528 /* Despatcher for finalize */
530 ssbox_finalize(struct ssbox *ssb)
536 /* Submit the box and clear up */
538 ssbox_base_finalize(struct ssbox *ssb)
543 /* Initialize a ssbox struct */
545 ssbox_base_init(struct ssbox *this, int cols,int rows)
547 this->finalize = ssbox_base_finalize;
548 this->t = tab_create (cols, rows, 0);
550 tab_columns (this->t, SOM_COL_DOWN, 1);
551 tab_headers (this->t,0,0,1,0);
552 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
553 tab_hline(this->t, TAL_2,0,cols-1,1);
554 tab_dim (this->t, tab_natural_dimensions);
557 void ssbox_one_sample_populate(struct ssbox *ssb,
558 struct cmd_t_test *cmd);
560 /* Initialize the one_sample ssbox */
562 ssbox_one_sample_init(struct ssbox *this,
563 struct cmd_t_test *cmd )
566 const int vsize=cmd->n_variables+1;
568 this->populate = ssbox_one_sample_populate;
570 ssbox_base_init(this, hsize,vsize);
571 tab_title (this->t, 0, _("One-Sample Statistics"));
572 tab_vline(this->t, TAL_2, 1,0,vsize);
573 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
574 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
575 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
576 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
579 void ssbox_independent_samples_populate(struct ssbox *ssb,
580 struct cmd_t_test *cmd);
582 /* Initialize the independent samples ssbox */
584 ssbox_independent_samples_init(struct ssbox *this,
585 struct cmd_t_test *cmd)
588 int vsize = cmd->n_variables*2 +1;
590 this->populate = ssbox_independent_samples_populate;
592 ssbox_base_init(this, hsize,vsize);
593 tab_title (this->t, 0, _("Group Statistics"));
594 tab_vline(this->t,0,1,0,vsize);
595 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, groups->name);
596 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
597 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
598 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
599 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
603 /* Populate the ssbox for independent samples */
605 ssbox_independent_samples_populate(struct ssbox *ssb,
606 struct cmd_t_test *cmd)
613 if ( groups->type == NUMERIC )
615 val_lab1 = val_labs_find( groups->val_labs,groups_values[0]);
616 val_lab2 = val_labs_find( groups->val_labs,groups_values[1]);
620 val_lab1 = groups_values[0].s;
621 val_lab2 = groups_values[1].s;
626 for (i=0; i < cmd->n_variables; ++i)
628 tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
631 tab_text (ssb->t, 1, i*2+1, TAB_LEFT, val_lab1);
633 tab_float(ssb->t, 1 ,i*2+1, TAB_LEFT, groups_values[0].f, 2,0);
636 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT, val_lab2);
638 tab_float(ssb->t, 1 ,i*2+1+1, TAB_LEFT, groups_values[1].f,2,0);
643 void ssbox_paired_populate(struct ssbox *ssb,
644 struct cmd_t_test *cmd);
646 /* Initialize the paired values ssbox */
648 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd unused)
652 int vsize = n_pairs*2+1;
654 this->populate = ssbox_paired_populate;
656 ssbox_base_init(this, hsize,vsize);
657 tab_title (this->t, 0, _("Paired Sample Statistics"));
658 tab_vline(this->t,TAL_0,1,0,vsize-1);
659 tab_vline(this->t,TAL_2,2,0,vsize-1);
660 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
661 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
662 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
663 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
667 /* Populate the ssbox for paired values */
669 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd unused)
675 for (i=0; i < n_pairs; ++i)
679 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
681 for (j=0 ; j < 2 ; ++j)
683 struct t_test_proc *ttp;
685 ttp=&pairs[i].v[j]->p.t_t;
689 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT, pairs[i].v[j]->name);
692 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, ttp->mean, 8, 2);
693 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, ttp->n, 2, 0);
694 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, ttp->std_dev, 8, 3);
695 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, ttp->se_mean, 8, 3);
703 /* Populate the one sample ssbox */
705 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
711 for (i=0; i < cmd->n_variables; ++i)
713 struct t_test_proc *ttp;
714 ttp= &cmd->v_variables[i]->p.t_t;
716 tab_text (ssb->t, 0, i+1, TAB_LEFT, cmd->v_variables[i]->name);
717 tab_float (ssb->t,1, i+1, TAB_RIGHT, ttp->n, 2, 0);
718 tab_float (ssb->t,2, i+1, TAB_RIGHT, ttp->mean, 8, 2);
719 tab_float (ssb->t,3, i+1, TAB_RIGHT, ttp->std_dev, 8, 2);
720 tab_float (ssb->t,4, i+1, TAB_RIGHT, ttp->se_mean, 8, 3);
727 /* Implementation of the Test Results box struct */
729 void trbox_base_init(struct trbox *self,int n_vars, int cols);
730 void trbox_base_finalize(struct trbox *trb);
732 void trbox_independent_samples_init(struct trbox *trb,
733 struct cmd_t_test *cmd );
735 void trbox_independent_samples_populate(struct trbox *trb,
736 struct cmd_t_test *cmd);
738 void trbox_one_sample_init(struct trbox *self,
739 struct cmd_t_test *cmd );
741 void trbox_one_sample_populate(struct trbox *trb,
742 struct cmd_t_test *cmd);
744 void trbox_paired_init(struct trbox *self,
745 struct cmd_t_test *cmd );
747 void trbox_paired_populate(struct trbox *trb,
748 struct cmd_t_test *cmd);
752 /* Create a trbox according to mode*/
754 trbox_create(struct trbox *trb,
755 struct cmd_t_test *cmd, int mode)
760 trbox_one_sample_init(trb,cmd);
763 trbox_independent_samples_init(trb,cmd);
766 trbox_paired_init(trb,cmd);
773 /* Populate a trbox according to cmd */
775 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
777 trb->populate(trb,cmd);
780 /* Submit and destroy a trbox */
782 trbox_finalize(struct trbox *trb)
787 /* Initialize the independent samples trbox */
789 trbox_independent_samples_init(struct trbox *self,
790 struct cmd_t_test *cmd unused)
793 const int vsize=cmd->n_variables*2+3;
796 self->populate = trbox_independent_samples_populate;
798 trbox_base_init(self,cmd->n_variables*2,hsize);
799 tab_title(self->t,0,_("Independent Samples Test"));
800 tab_hline(self->t,TAL_1,2,hsize-1,1);
801 tab_vline(self->t,TAL_2,2,0,vsize-1);
802 tab_vline(self->t,TAL_1,4,0,vsize-1);
803 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
804 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
805 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
806 tab_joint_text(self->t, 2, 0, 3, 0,
807 TAB_CENTER,_("Levine's Test for Equality of Variances"));
808 tab_joint_text(self->t, 4,0,hsize-1,0,
809 TAB_CENTER,_("t-test for Equality of Means"));
811 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
812 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
813 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
814 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
815 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
816 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
817 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
818 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
819 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
821 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
822 _("%d%% Confidence Interval of the Difference"),
823 (int)round(cmd->criteria*100.0));
827 /* Populate the independent samples trbox */
829 trbox_independent_samples_populate(struct trbox *self,
830 struct cmd_t_test *cmd )
835 for (i=0; i < cmd->n_variables; ++i)
837 tab_text (self->t, 0, i*2+3, TAB_LEFT, cmd->v_variables[i]->name);
839 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
841 tab_text (self->t, 1, i*2+3+1,
842 TAB_LEFT, _("Equal variances not assumed"));
846 /* Initialize the paired samples trbox */
848 trbox_paired_init(struct trbox *self,
849 struct cmd_t_test *cmd unused)
853 const int vsize=n_pairs*2+3;
855 self->populate = trbox_paired_populate;
857 trbox_base_init(self,n_pairs*2,hsize);
858 tab_title (self->t, 0, _("Paired Samples Test"));
859 tab_hline(self->t,TAL_1,2,6,1);
860 tab_vline(self->t,TAL_2,2,0,vsize);
861 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
862 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
863 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
864 tab_hline(self->t,TAL_1,5,6, 2);
865 tab_vline(self->t,TAL_0,6,0,1);
867 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
868 _("%d%% Confidence Interval of the Difference"),
869 (int)round(cmd->criteria*100.0));
871 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
872 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
873 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
874 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
875 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
876 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
877 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
878 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
881 /* Populate the paired samples trbox */
883 trbox_paired_populate(struct trbox *trb,
884 struct cmd_t_test *cmd unused)
888 for (i=0; i < n_pairs; ++i)
890 tab_text (trb->t, 0, i*2+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
891 tab_text (trb->t, 1, i*2+3, TAB_LEFT, pairs[i].v[0]->name);
892 tab_text (trb->t, 1, i*2+4, TAB_LEFT, pairs[i].v[1]->name);
897 /* Initialize the one sample trbox */
899 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
902 const int vsize=cmd->n_variables+3;
904 self->populate = trbox_one_sample_populate;
906 trbox_base_init(self, cmd->n_variables,hsize);
907 tab_title (self->t, 0, _("One-Sample Test"));
908 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
909 tab_vline(self->t, TAL_2, 1, 0, vsize);
911 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
912 _("Test Value = %f"),cmd->n_testval);
914 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
917 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
918 _("%d%% Confidence Interval of the Difference"),
919 (int)round(cmd->criteria*100.0));
921 tab_vline(self->t,TAL_0,6,1,1);
922 tab_hline(self->t,TAL_1,5,6,2);
923 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
924 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
925 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
926 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
927 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
928 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
933 /* Populate the one sample trbox */
935 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
941 for (i=0; i < cmd->n_variables; ++i)
949 struct t_test_proc *ttp;
950 ttp= &cmd->v_variables[i]->p.t_t;
953 tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
955 t = (ttp->mean - cmd->n_testval ) * sqrt(ttp->n) / ttp->std_dev ;
957 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
959 /* degrees of freedom */
962 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
964 cdft(&which, &p, &q, &t, &df, &status, &bound);
968 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
972 /* Multiply by 2 to get 2-tailed significance */
973 tab_float (trb->t, 3, i+3, TAB_RIGHT, q*2.0, 8,3);
975 tab_float (trb->t, 4, i+3, TAB_RIGHT, ttp->mean_diff, 8,3);
978 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
980 which=2; /* Calc T from p,q and df */
981 cdft(&which, &p, &q, &t, &df, &status, &bound);
984 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
987 tab_float (trb->t, 5, i+3, TAB_RIGHT,
988 ttp->mean_diff - t * ttp->se_mean, 8,4);
990 tab_float (trb->t, 6, i+3, TAB_RIGHT,
991 ttp->mean_diff + t * ttp->se_mean, 8,4);
995 /* Base initializer for the generalized trbox */
997 trbox_base_init(struct trbox *self, int data_rows, int cols)
999 const int rows = 3 + data_rows;
1001 self->finalize = trbox_base_finalize;
1002 self->t = tab_create (cols, rows, 0);
1003 tab_headers (self->t,0,0,3,0);
1004 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1005 tab_hline(self->t, TAL_2,0,cols-1,3);
1006 tab_dim (self->t, tab_natural_dimensions);
1010 /* Base finalizer for the trbox */
1012 trbox_base_finalize(struct trbox *trb)
1018 /* Create , populate and submit the Paired Samples Correlation box */
1020 pscbox(struct cmd_t_test *cmd)
1022 const int rows=1+n_pairs;
1026 struct tab_table *table;
1028 table = tab_create (cols,rows,0);
1030 tab_columns (table, SOM_COL_DOWN, 1);
1031 tab_headers (table,0,0,1,0);
1032 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1033 tab_hline(table, TAL_2, 0, cols - 1, 1);
1034 tab_vline(table, TAL_2, 2, 0, rows - 1);
1035 tab_dim(table, tab_natural_dimensions);
1036 tab_title(table, 0, _("Paired Samples Correlations"));
1038 /* column headings */
1039 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1040 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1041 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1044 for (i=0; i < n_pairs; ++i)
1052 const double df = pairs[i].v[0]->p.t_t.n -2;
1054 double correlation_t =
1055 pairs[i].correlation * sqrt(df) /
1056 sqrt(1 - sqr(pairs[i].correlation));
1060 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1063 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1064 _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
1068 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1069 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].v[0]->p.t_t.n , 4, 0);
1072 cdft(&which, &p, &q, &correlation_t, &df, &status, &bound);
1076 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1080 tab_float(table, 4, i+1, TAB_RIGHT, q*2.0, 8, 3);
1091 /* Calculation Implementation */
1093 /* Per case calculations common to all variants of the T test */
1095 common_calc (struct ccase *c)
1099 double weight = dict_get_case_weight(default_dict,c);
1101 for(i=0; i< cmd.n_variables ; ++i)
1103 struct t_test_proc *ttp;
1104 struct variable *v = cmd.v_variables[i];
1105 union value *val = &c->data[v->fv];
1107 ttp= &cmd.v_variables[i]->p.t_t;
1109 if (val->f != SYSMIS)
1112 ttp->sum+=weight * val->f;
1113 ttp->ssq+=weight * val->f * val->f;
1119 /* Pre calculations common to all variants of the T test */
1121 common_precalc (void)
1125 for(i=0; i< cmd.n_variables ; ++i)
1127 struct t_test_proc *ttp;
1128 ttp= &cmd.v_variables[i]->p.t_t;
1137 /* Post calculations common to all variants of the T test */
1139 common_postcalc (void)
1143 for(i=0; i< cmd.n_variables ; ++i)
1145 struct t_test_proc *ttp;
1146 ttp= &cmd.v_variables[i]->p.t_t;
1148 ttp->mean=ttp->sum / ttp->n;
1151 ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean )
1154 ttp->se_mean = ttp->std_dev / sqrt(ttp->n);
1155 ttp->mean_diff= ttp->sum_diff / ttp->n;
1159 /* Per case calculations for one sample t test */
1161 one_sample_calc (struct ccase *c)
1165 double weight = dict_get_case_weight(default_dict,c);
1167 for(i=0; i< cmd.n_variables ; ++i)
1169 struct t_test_proc *ttp;
1170 struct variable *v = cmd.v_variables[i];
1171 union value *val = &c->data[v->fv];
1173 ttp= &cmd.v_variables[i]->p.t_t;
1175 if (val->f != SYSMIS)
1176 ttp->sum_diff += weight * (val->f - cmd.n_testval);
1182 /* Pre calculations for one sample t test */
1184 one_sample_precalc (void)
1188 for(i=0; i< cmd.n_variables ; ++i)
1190 struct t_test_proc *ttp;
1191 ttp= &cmd.v_variables[i]->p.t_t;
1197 /* Post calculations for one sample t test */
1199 one_sample_postcalc (void)
1203 for(i=0; i< cmd.n_variables ; ++i)
1205 struct t_test_proc *ttp;
1206 ttp= &cmd.v_variables[i]->p.t_t;
1209 ttp->mean_diff = ttp->sum_diff / ttp->n ;
1216 compare_var_name (const void *a_, const void *b_, void *v_ unused)
1218 const struct variable *a = a_;
1219 const struct variable *b = b_;
1221 return strcmp(a->name,b->name);
1225 hash_var_name (const void *a_, void *v_ unused)
1227 const struct variable *a = a_;
1229 return hsh_hash_bytes (a->name, strlen(a->name));
1234 paired_precalc (void)
1237 for(i=0; i < n_pairs ; ++i )
1238 pairs[i].correlation=0;
1242 paired_calc (struct ccase *c)
1246 for(i=0; i < n_pairs ; ++i )
1248 struct variable *v0 = pairs[i].v[0];
1249 struct variable *v1 = pairs[i].v[1];
1251 union value *val0 = &c->data[v0->fv];
1252 union value *val1 = &c->data[v1->fv];
1254 pairs[i].correlation += ( val0->f - pairs[i].v[0]->p.t_t.mean )
1256 ( val1->f - pairs[i].v[1]->p.t_t.mean );
1264 paired_postcalc (void)
1268 for(i=0; i < n_pairs ; ++i )
1271 pairs[i].correlation /= pairs[i].v[0]->p.t_t.std_dev *
1272 pairs[i].v[1]->p.t_t.std_dev ;
1274 pairs[i].correlation /= pairs[i].v[0]->p.t_t.n -1;