1 /* PSPP - One way ANOVA. -*-c-*-
3 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 Author: John Darrington 2004
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 #include <gsl/gsl_cdf.h>
37 #include "value-labels.h"
47 +missing=miss:!analysis/listwise,
48 incl:include/!exclude;
49 contrast= double list;
50 statistics[st_]=descriptives,homogeneity.
57 static struct cmd_oneway cmd;
59 /* The independent variable */
60 static struct variable *indep_var;
62 /* A hash of the values of the independent variable */
63 struct hsh_table *ind_vals;
65 /* Number of factors (groups) */
68 /* Number of dependent variables */
71 /* The dependent variables */
72 static struct variable **vars;
78 /* Function to use for testing for missing values */
79 static is_missing_func value_is_missing;
82 static void calculate(const struct casefile *cf, void *_mode);
85 /* Routines to show the output tables */
86 static void show_anova_table(void);
87 static void show_descriptives(void);
88 static void show_homogeneity(void);
89 static void show_contrast_coeffs(void);
90 static void show_contrast_tests(void);
99 if ( !parse_oneway(&cmd) )
102 /* If /MISSING=INCLUDE is set, then user missing values are ignored */
103 if (cmd.incl == ONEWAY_INCLUDE )
104 value_is_missing = is_system_missing;
106 value_is_missing = is_missing;
108 multipass_procedure_with_splits (calculate, &cmd);
110 /* Check the sanity of the given contrast values */
111 for (i = 0 ; i < cmd.sbc_contrast ; ++i )
116 if ( subc_list_double_count(&cmd.dl_contrast[i]) != n_groups )
118 msg(SE, _("Number of contrast coefficients must equal the number of groups"));
122 for (j=0; j < n_groups ; ++j )
123 sum += subc_list_double_at(&cmd.dl_contrast[i],j);
126 msg(SW,_("Coefficients for contrast %d do not total zero"),i + 1);
130 /* Show the statistics tables */
131 if ( cmd.sbc_statistics )
133 for (i = 0 ; i < ONEWAY_ST_count ; ++i )
135 if ( ! cmd.a_statistics[i] ) continue;
138 case ONEWAY_ST_DESCRIPTIVES:
141 case ONEWAY_ST_HOMOGENEITY:
151 if (cmd.sbc_contrast)
153 show_contrast_coeffs();
154 show_contrast_tests();
157 hsh_destroy(ind_vals);
166 /* Parser for the variables sub command */
168 oneway_custom_variables(struct cmd_oneway *cmd UNUSED)
173 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
178 if (!parse_variables (default_dict, &vars, &n_vars,
180 | PV_NUMERIC | PV_NO_SCRATCH) )
188 if ( ! lex_match(T_BY))
192 indep_var = parse_variable();
196 msg(SE,_("`%s' is not a variable name"),tokid);
205 /* Show the ANOVA table */
207 show_anova_table(void)
211 int n_rows = n_vars * 3 + 1;
216 t = tab_create (n_cols,n_rows,0);
217 tab_headers (t, 2, 0, 1, 0);
218 tab_dim (t, tab_natural_dimensions);
225 n_cols - 1, n_rows - 1);
227 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
228 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
229 tab_vline (t, TAL_0, 1, 0, 0);
231 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
232 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
233 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
234 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
235 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
238 for ( i=0 ; i < n_vars ; ++i )
240 char *s = (vars[i]->label) ? vars[i]->label : vars[i]->name;
242 tab_text (t, 0, i * 3 + 1, TAB_LEFT | TAT_TITLE, s);
243 tab_text (t, 1, i * 3 + 1, TAB_LEFT | TAT_TITLE, _("Between Groups"));
244 tab_text (t, 1, i * 3 + 2, TAB_LEFT | TAT_TITLE, _("Within Groups"));
245 tab_text (t, 1, i * 3 + 3, TAB_LEFT | TAT_TITLE, _("Total"));
248 tab_hline(t, TAL_1, 0, n_cols - 1 , i * 3 + 1);
252 tab_title (t, 0, "ANOVA");
260 calculate(const struct casefile *cf, void *cmd_)
262 struct casereader *r;
265 struct cmd_t_test *cmd = (struct cmd_t_test *) cmd_;
268 ind_vals = hsh_create(4, (hsh_compare_func *) compare_values,
269 (hsh_hash_func *) hash_value,
270 0, (void *) indep_var->width );
272 for(r = casefile_get_reader (cf);
273 casereader_read (r, &c) ;
277 const union value *val = case_data (&c, indep_var->fv);
279 hsh_insert(ind_vals, (void *) val);
282 if (! value_is_missing(val,v) )
285 gs->sum+=weight * val->f;
286 gs->ssq+=weight * val->f * val->f;
291 casereader_destroy (r);
294 n_groups = hsh_count(ind_vals);
300 /* Show the descriptives table */
302 show_descriptives(void)
306 int n_rows = n_vars * (n_groups + 1 )+ 2;
311 t = tab_create (n_cols,n_rows,0);
312 tab_headers (t, 2, 0, 2, 0);
313 tab_dim (t, tab_natural_dimensions);
316 /* Put a frame around the entire box, and vertical lines inside */
321 n_cols - 1, n_rows - 1);
323 /* Underline headers */
324 tab_hline (t, TAL_2, 0, n_cols - 1, 2 );
325 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
327 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("N"));
328 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Mean"));
329 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
330 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
333 tab_vline(t, TAL_0, 7, 0, 0);
334 tab_hline(t, TAL_1, 6, 7, 1);
335 tab_joint_text (t, 6, 0, 7, 0, TAB_CENTER | TAT_TITLE, _("95% Confidence Interval for Mean"));
337 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
338 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
340 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Minimum"));
341 tab_text (t, 9, 1, TAB_CENTER | TAT_TITLE, _("Maximum"));
344 tab_title (t, 0, "Descriptives");
347 for ( v=0 ; v < n_vars ; ++v )
349 struct hsh_iterator g;
350 union value *group_value;
352 char *s = (vars[v]->label) ? vars[v]->label : vars[v]->name;
354 tab_text (t, 0, v * ( n_groups + 1 ) + 2, TAB_LEFT | TAT_TITLE, s);
356 tab_hline(t, TAL_1, 0, n_cols - 1 , v * (n_groups + 1) + 2);
359 for (group_value = hsh_first (ind_vals,&g);
361 group_value = hsh_next(ind_vals,&g))
365 lab = val_labs_find(indep_var->val_labs,*group_value);
368 tab_text (t, 1, v * (n_groups + 1)+ count + 2,
369 TAB_LEFT | TAT_TITLE ,lab);
371 tab_text (t, 1, v * (n_groups + 1) + count + 2,
372 TAB_LEFT | TAT_TITLE | TAT_PRINTF, "%g", group_value->f);
377 tab_text (t, 1, v * (n_groups + 1)+ count + 2,
378 TAB_LEFT | TAT_TITLE ,_("Total"));
390 /* Show the homogeneity table */
392 show_homogeneity(void)
396 int n_rows = n_vars + 1;
401 t = tab_create (n_cols,n_rows,0);
402 tab_headers (t, 1, 0, 1, 0);
403 tab_dim (t, tab_natural_dimensions);
405 /* Put a frame around the entire box, and vertical lines inside */
410 n_cols - 1, n_rows - 1);
413 tab_hline(t, TAL_2, 0, n_cols - 1, 1);
414 tab_vline(t, TAL_2, 1, 0, n_rows - 1);
417 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Levene Statistic"));
418 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("df1"));
419 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df2"));
420 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
423 tab_title (t, 0, _("Test of Homogeneity of Variances"));
425 for ( v=0 ; v < n_vars ; ++v )
427 char *s = (vars[v]->label) ? vars[v]->label : vars[v]->name;
429 tab_text (t, 0, v + 1, TAB_LEFT | TAT_TITLE, s);
438 /* Show the contrast coefficients table */
440 show_contrast_coeffs(void)
443 int n_cols = 2 + n_groups;
444 int n_rows = 2 + cmd.sbc_contrast;
445 struct hsh_iterator g;
446 union value *group_value;
453 t = tab_create (n_cols,n_rows,0);
454 tab_headers (t, 2, 0, 2, 0);
455 tab_dim (t, tab_natural_dimensions);
457 /* Put a frame around the entire box, and vertical lines inside */
462 n_cols - 1, n_rows - 1);
478 tab_hline(t, TAL_1, 2, n_cols - 1, 1);
481 tab_hline(t, TAL_2, 0, n_cols - 1, 2);
482 tab_vline(t, TAL_2, 2, 0, n_rows - 1);
485 tab_title (t, 0, _("Contrast Coefficients"));
487 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Contrast"));
489 s = (indep_var->label) ? indep_var->label : indep_var->name;
491 tab_joint_text (t, 2, 0, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, s);
493 for (group_value = hsh_first (ind_vals,&g);
495 group_value = hsh_next(ind_vals,&g))
500 lab = val_labs_find(indep_var->val_labs,*group_value);
503 tab_text (t, count + 2, 1,
504 TAB_CENTER | TAT_TITLE ,lab);
506 tab_text (t, count + 2, 1,
507 TAB_CENTER | TAT_TITLE | TAT_PRINTF, "%g", group_value->f);
509 for (i = 0 ; i < cmd.sbc_contrast ; ++i )
511 tab_text(t, 1, i + 2, TAB_CENTER | TAT_PRINTF, "%d", i + 1);
512 tab_text(t, count + 2, i + 2, TAB_RIGHT | TAT_PRINTF, "%g",
513 subc_list_double_at(&cmd.dl_contrast[i],count)
526 /* Show the results of the contrast tests */
528 show_contrast_tests(void)
532 int n_rows = 1 + n_vars * 2 * cmd.sbc_contrast;
536 t = tab_create (n_cols,n_rows,0);
537 tab_headers (t, 3, 0, 1, 0);
538 tab_dim (t, tab_natural_dimensions);
540 /* Put a frame around the entire box, and vertical lines inside */
545 n_cols - 1, n_rows - 1);
554 tab_hline(t, TAL_2, 0, n_cols - 1, 1);
555 tab_vline(t, TAL_2, 3, 0, n_rows - 1);
558 tab_title (t, 0, _("Contrast Tests"));
560 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Contrast"));
561 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Value of Contrast"));
562 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
563 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
564 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("df"));
565 tab_text (t, 7, 0, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
567 for ( v = 0 ; v < n_vars ; ++v )
570 int lines_per_variable = 2 * cmd.sbc_contrast;
572 tab_text (t, 0, (v * lines_per_variable) + 1, TAB_LEFT | TAT_TITLE,
573 vars[v]->label?vars[v]->label:vars[v]->name);
575 for ( i = 0 ; i < cmd.sbc_contrast ; ++i )
577 tab_text (t, 1, (v * lines_per_variable) + i*2 + 1,
578 TAB_LEFT | TAT_TITLE,
579 _("Assume equal variances"));
581 tab_text (t, 1, (v * lines_per_variable) + i*2 + 2,
582 TAB_LEFT | TAT_TITLE,
583 _("Does not assume equal"));
586 tab_text (t, 2, (v * lines_per_variable) + i*2 + 1,
587 TAB_CENTER | TAT_TITLE | TAT_PRINTF, "%d",i+1);
589 tab_text (t, 2, (v * lines_per_variable) + i*2 + 2,
590 TAB_CENTER | TAT_TITLE | TAT_PRINTF, "%d",i+1);
595 tab_hline(t, TAL_1, 0, n_cols - 1, (v * lines_per_variable) + 1);