X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fstats%2Freliability.c;h=e1c205f60f608c8667b51c140d1c2871dc405701;hb=b754fbf65fcd1b4fb466bbba6af71e6717df01e3;hp=144c31125b6f7fc4ca70c22b92bb79a7abfba284;hpb=f481fd69631024bcdc7dc2369bbc1592d7a43ac7;p=pspp diff --git a/src/language/stats/reliability.c b/src/language/stats/reliability.c index 144c31125b..e1c205f60f 100644 --- a/src/language/stats/reliability.c +++ b/src/language/stats/reliability.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc. + Copyright (C) 2009, 2010, 2011, 2013, 2015, 2016 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -31,7 +31,7 @@ #include "libpspp/misc.h" #include "libpspp/str.h" #include "math/moments.h" -#include "output/tab.h" +#include "output/pivot-table.h" #include "output/text-item.h" #include "gettext.h" @@ -113,15 +113,17 @@ reliability_destroy (struct reliability *rel) { int j; ds_destroy (&rel->scale_name); - for (j = 0; j < rel->n_sc ; ++j) - { - int x; - free (rel->sc[j].items); - moments1_destroy (rel->sc[j].total); - for (x = 0; x < rel->sc[j].n_items; ++x) - free (rel->sc[j].m[x]); - free (rel->sc[j].m); - } + if (rel->sc) + for (j = 0; j < rel->n_sc ; ++j) + { + int x; + free (rel->sc[j].items); + moments1_destroy (rel->sc[j].total); + if (rel->sc[j].m) + for (x = 0; x < rel->sc[j].n_items; ++x) + free (rel->sc[j].m[x]); + free (rel->sc[j].m); + } free (rel->sc); free (rel->variables); @@ -136,12 +138,14 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds) reliability.n_variables = 0; reliability.variables = NULL; reliability.model = MODEL_ALPHA; - reliability.exclude = MV_ANY; + reliability.exclude = MV_ANY; reliability.summary = 0; - + reliability.n_sc = 0; + reliability.sc = NULL; reliability.wv = dict_get_weight (dict); - reliability.total_start = 0; + ds_init_empty (&reliability.scale_name); + lex_match (lexer, T_SLASH); @@ -160,23 +164,23 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds) msg (MW, _("Reliability on a single variable is not useful.")); - { - int i; - struct cronbach *c; - /* Create a default Scale */ + { + int i; + struct cronbach *c; + /* Create a default Scale */ - reliability.n_sc = 1; - reliability.sc = xzalloc (sizeof (struct cronbach) * reliability.n_sc); + reliability.n_sc = 1; + reliability.sc = xzalloc (sizeof (struct cronbach) * reliability.n_sc); - ds_init_cstr (&reliability.scale_name, "ANY"); + ds_assign_cstr (&reliability.scale_name, "ANY"); - c = &reliability.sc[0]; - c->n_items = reliability.n_variables; - c->items = xzalloc (sizeof (struct variable*) * c->n_items); + c = &reliability.sc[0]; + c->n_items = reliability.n_variables; + c->items = xzalloc (sizeof (struct variable*) * c->n_items); - for (i = 0 ; i < c->n_items ; ++i) - c->items[i] = reliability.variables[i]; - } + for (i = 0 ; i < c->n_items ; ++i) + c->items[i] = reliability.variables[i]; + } @@ -190,7 +194,7 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds) if ( ! lex_force_match (lexer, T_LPAREN)) goto error; - if ( ! lex_force_string (lexer) ) + if ( ! lex_force_string (lexer) ) goto error; ds_assign_substring (&reliability.scale_name, lex_tokss (lexer)); @@ -225,12 +229,13 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds) reliability.model = MODEL_SPLIT; reliability.split_point = -1; - if ( lex_match (lexer, T_LPAREN)) + if ( lex_match (lexer, T_LPAREN) + && lex_force_num (lexer)) { - lex_force_num (lexer); reliability.split_point = lex_number (lexer); lex_get (lexer); - lex_force_match (lexer, T_RPAREN); + if (! lex_force_match (lexer, T_RPAREN)) + goto error; } } else @@ -270,6 +275,14 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds) } } } + else if (lex_match_id (lexer, "STATISTICS")) + { + lex_match (lexer, T_EQUALS); + msg (SW, _("The STATISTICS subcommand is not yet implemented. " + "No statistics will be produced.")); + while (lex_match (lexer, T_ID)) + continue; + } else { lex_error (lexer, NULL); @@ -341,7 +354,7 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds) } - if ( ! run_reliability (ds, &reliability)) + if ( ! run_reliability (ds, &reliability)) goto error; reliability_destroy (&reliability); @@ -371,6 +384,20 @@ run_reliability (struct dataset *ds, const struct reliability *reliability) struct casereader *group; struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict); + int si; + + for (si = 0 ; si < reliability->n_sc; ++si) + { + struct cronbach *s = &reliability->sc[si]; + int i; + + s->m = xzalloc (sizeof *s->m * s->n_items); + s->total = moments1_create (MOMENT_VARIANCE); + + for (i = 0 ; i < s->n_items ; ++i ) + s->m[i] = moments1_create (MOMENT_VARIANCE); + } + while (casegrouper_get_next_group (grouper, &group)) { @@ -434,11 +461,10 @@ do_reliability (struct casereader *input, struct dataset *ds, { struct cronbach *s = &rel->sc[si]; - s->m = xzalloc (sizeof (s->m) * s->n_items); - s->total = moments1_create (MOMENT_VARIANCE); + moments1_clear (s->total); for (i = 0 ; i < s->n_items ; ++i ) - s->m[i] = moments1_create (MOMENT_VARIANCE); + moments1_clear (s->m[i]); } input = casereader_create_filter_missing (input, @@ -496,7 +522,7 @@ do_reliability (struct casereader *input, struct dataset *ds, alpha (s->n_items, s->sum_of_variances, s->variance_of_sums); } - text_item_submit (text_item_create_format (TEXT_ITEM_PARAGRAPH, _("Scale: %s"), + text_item_submit (text_item_create_format (TEXT_ITEM_TITLE, _("Scale: %s"), ds_cstr (&rel->scale_name))); case_processing_summary (n_valid, n_missing, dataset_dict (ds)); @@ -510,351 +536,161 @@ static void case_processing_summary (casenumber n_valid, casenumber n_missing, const struct dictionary *dict) { - const struct variable *wv = dict_get_weight (dict); - const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0; - - casenumber total; - int n_cols = 4; - int n_rows = 4; - int heading_columns = 2; - int heading_rows = 1; - struct tab_table *tbl; - tbl = tab_create (n_cols, n_rows); - tab_headers (tbl, heading_columns, 0, heading_rows, 0); - - tab_title (tbl, _("Case Processing Summary")); - - /* Vertical lines for the data only */ - tab_box (tbl, - -1, -1, - -1, TAL_1, - heading_columns, 0, - n_cols - 1, n_rows - 1); - - /* Box around table */ - tab_box (tbl, - TAL_2, TAL_2, - -1, -1, - 0, 0, - n_cols - 1, n_rows - 1); - - - tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows); - - tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1); - - - tab_text (tbl, 0, heading_rows, TAB_LEFT | TAT_TITLE, - _("Cases")); - - tab_text (tbl, 1, heading_rows, TAB_LEFT | TAT_TITLE, - _("Valid")); - - tab_text (tbl, 1, heading_rows + 1, TAB_LEFT | TAT_TITLE, - _("Excluded")); - - tab_text (tbl, 1, heading_rows + 2, TAB_LEFT | TAT_TITLE, - _("Total")); + struct pivot_table *table = pivot_table_create ( + N_("Case Processing Summary")); + pivot_table_set_weight_var (table, dict_get_weight (dict)); - tab_text (tbl, heading_columns, 0, TAB_CENTER | TAT_TITLE, - _("N")); + pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"), + N_("N"), PIVOT_RC_COUNT, + N_("Percent"), PIVOT_RC_PERCENT); - tab_text (tbl, heading_columns + 1, 0, TAB_CENTER | TAT_TITLE, _("%")); + struct pivot_dimension *cases = pivot_dimension_create ( + table, PIVOT_AXIS_ROW, N_("Cases"), N_("Valid"), N_("Excluded"), + N_("Total")); + cases->root->show_label = true; - total = n_missing + n_valid; + casenumber total = n_missing + n_valid; - tab_double (tbl, 2, heading_rows, TAB_RIGHT, - n_valid, wfmt); - - - tab_double (tbl, 2, heading_rows + 1, TAB_RIGHT, - n_missing, wfmt); - - - tab_double (tbl, 2, heading_rows + 2, TAB_RIGHT, - total, wfmt); - - - tab_double (tbl, 3, heading_rows, TAB_RIGHT, - 100 * n_valid / (double) total, NULL); - - - tab_double (tbl, 3, heading_rows + 1, TAB_RIGHT, - 100 * n_missing / (double) total, NULL); - - - tab_double (tbl, 3, heading_rows + 2, TAB_RIGHT, - 100 * total / (double) total, NULL); + struct entry + { + int stat_idx; + int case_idx; + double x; + } + entries[] = { + { 0, 0, n_valid }, + { 0, 1, n_missing }, + { 0, 2, total }, + { 1, 0, 100.0 * n_valid / total }, + { 1, 1, 100.0 * n_missing / total }, + { 1, 2, 100.0 } + }; + for (size_t i = 0; i < sizeof entries / sizeof *entries; i++) + { + const struct entry *e = &entries[i]; + pivot_table_put2 (table, e->stat_idx, e->case_idx, + pivot_value_new_number (e->x)); + } - tab_submit (tbl); + pivot_table_submit (table); } - - static void reliability_summary_total (const struct reliability *rel) { - int i; - const int n_cols = 5; - const int heading_columns = 1; - const int heading_rows = 1; - const int n_rows = rel->sc[0].n_items + heading_rows ; - - struct tab_table *tbl = tab_create (n_cols, n_rows); - tab_headers (tbl, heading_columns, 0, heading_rows, 0); + struct pivot_table *table = pivot_table_create (N_("Item-Total Statistics")); - tab_title (tbl, _("Item-Total Statistics")); + pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"), + N_("Scale Mean if Item Deleted"), + N_("Scale Variance if Item Deleted"), + N_("Corrected Item-Total Correlation"), + N_("Cronbach's Alpha if Item Deleted")); - /* Vertical lines for the data only */ - tab_box (tbl, - -1, -1, - -1, TAL_1, - heading_columns, 0, - n_cols - 1, n_rows - 1); + struct pivot_dimension *variables = pivot_dimension_create ( + table, PIVOT_AXIS_ROW, N_("Variables")); - /* Box around table */ - tab_box (tbl, - TAL_2, TAL_2, - -1, -1, - 0, 0, - n_cols - 1, n_rows - 1); - - - tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows); - - tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1); - - tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE, - _("Scale Mean if Item Deleted")); - - tab_text (tbl, 2, 0, TAB_CENTER | TAT_TITLE, - _("Scale Variance if Item Deleted")); - - tab_text (tbl, 3, 0, TAB_CENTER | TAT_TITLE, - _("Corrected Item-Total Correlation")); - - tab_text (tbl, 4, 0, TAB_CENTER | TAT_TITLE, - _("Cronbach's Alpha if Item Deleted")); - - - for (i = 0 ; i < rel->sc[0].n_items; ++i) + for (size_t i = 0 ; i < rel->sc[0].n_items; ++i) { - double cov, item_to_total_r; - double mean, weight, var; - const struct cronbach *s = &rel->sc[rel->total_start + i]; - tab_text (tbl, 0, heading_rows + i, TAB_LEFT| TAT_TITLE, - var_to_string (rel->sc[0].items[i])); - - moments1_calculate (s->total, &weight, &mean, &var, 0, 0); - - tab_double (tbl, 1, heading_rows + i, TAB_RIGHT, - mean, NULL); - - tab_double (tbl, 2, heading_rows + i, TAB_RIGHT, - s->variance_of_sums, NULL); - - tab_double (tbl, 4, heading_rows + i, TAB_RIGHT, - s->alpha, NULL); - - - moments1_calculate (rel->sc[0].m[i], &weight, &mean, &var, 0,0); - cov = rel->sc[0].variance_of_sums + var - s->variance_of_sums; - cov /= 2.0; - - item_to_total_r = (cov - var) / (sqrt(var) * sqrt (s->variance_of_sums)); - - tab_double (tbl, 3, heading_rows + i, TAB_RIGHT, - item_to_total_r, NULL); + int var_idx = pivot_category_create_leaf ( + variables->root, pivot_value_new_variable (rel->sc[0].items[i])); + + double mean; + moments1_calculate (s->total, NULL, &mean, NULL, NULL, NULL); + + double var; + moments1_calculate (rel->sc[0].m[i], NULL, NULL, &var, NULL, NULL); + double cov + = (rel->sc[0].variance_of_sums + var - s->variance_of_sums) / 2.0; + + double entries[] = { + mean, + s->variance_of_sums, + (cov - var) / sqrt (var * s->variance_of_sums), + s->alpha, + }; + for (size_t i = 0; i < sizeof entries / sizeof *entries; i++) + pivot_table_put2 (table, i, var_idx, + pivot_value_new_number (entries[i])); } - - tab_submit (tbl); + pivot_table_submit (table); } -static void reliability_statistics_model_alpha (struct tab_table *tbl, - const struct reliability *rel); - -static void reliability_statistics_model_split (struct tab_table *tbl, - const struct reliability *rel); - - -struct reliability_output_table -{ - int n_cols; - int n_rows; - int heading_cols; - int heading_rows; - void (*populate) (struct tab_table *, const struct reliability *); -}; - - -static struct reliability_output_table rol[2] = - { - { 2, 2, 1, 1, reliability_statistics_model_alpha}, - { 4, 9, 3, 0, reliability_statistics_model_split} - }; - static void reliability_statistics (const struct reliability *rel) { - int n_cols = rol[rel->model].n_cols; - int n_rows = rol[rel->model].n_rows; - int heading_columns = rol[rel->model].heading_cols; - int heading_rows = rol[rel->model].heading_rows; - - struct tab_table *tbl = tab_create (n_cols, n_rows); - tab_headers (tbl, heading_columns, 0, heading_rows, 0); - - tab_title (tbl, _("Reliability Statistics")); - - /* Vertical lines for the data only */ - tab_box (tbl, - -1, -1, - -1, TAL_1, - heading_columns, 0, - n_cols - 1, n_rows - 1); - - /* Box around table */ - tab_box (tbl, - TAL_2, TAL_2, - -1, -1, - 0, 0, - n_cols - 1, n_rows - 1); - - - tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows); - - tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1); - - if ( rel->model == MODEL_ALPHA ) - reliability_statistics_model_alpha (tbl, rel); - else if (rel->model == MODEL_SPLIT ) - reliability_statistics_model_split (tbl, rel); - - tab_submit (tbl); -} - - -static void -reliability_statistics_model_alpha (struct tab_table *tbl, - const struct reliability *rel) -{ - const struct variable *wv = rel->wv; - const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0; - - const struct cronbach *s = &rel->sc[0]; - - tab_text (tbl, 0, 0, TAB_CENTER | TAT_TITLE, - _("Cronbach's Alpha")); - - tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE, - _("N of Items")); - - tab_double (tbl, 0, 1, TAB_RIGHT, s->alpha, NULL); - - tab_double (tbl, 1, 1, TAB_RIGHT, s->n_items, wfmt); -} - - -static void -reliability_statistics_model_split (struct tab_table *tbl, - const struct reliability *rel) -{ - const struct variable *wv = rel->wv; - const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0; - - tab_text (tbl, 0, 0, TAB_LEFT, - _("Cronbach's Alpha")); - - tab_text (tbl, 1, 0, TAB_LEFT, - _("Part 1")); + struct pivot_table *table = pivot_table_create ( + N_("Reliability Statistics")); + pivot_table_set_weight_var (table, rel->wv); - tab_text (tbl, 2, 0, TAB_LEFT, - _("Value")); - - tab_text (tbl, 2, 1, TAB_LEFT, - _("N of Items")); - - - - tab_text (tbl, 1, 2, TAB_LEFT, - _("Part 2")); - - tab_text (tbl, 2, 2, TAB_LEFT, - _("Value")); - - tab_text (tbl, 2, 3, TAB_LEFT, - _("N of Items")); - - - - tab_text (tbl, 1, 4, TAB_LEFT, - _("Total N of Items")); - - tab_text (tbl, 0, 5, TAB_LEFT, - _("Correlation Between Forms")); - - - tab_text (tbl, 0, 6, TAB_LEFT, - _("Spearman-Brown Coefficient")); - - tab_text (tbl, 1, 6, TAB_LEFT, - _("Equal Length")); - - tab_text (tbl, 1, 7, TAB_LEFT, - _("Unequal Length")); - - - tab_text (tbl, 0, 8, TAB_LEFT, - _("Guttman Split-Half Coefficient")); - - - - tab_double (tbl, 3, 0, TAB_RIGHT, rel->sc[1].alpha, NULL); - tab_double (tbl, 3, 2, TAB_RIGHT, rel->sc[2].alpha, NULL); - - tab_double (tbl, 3, 1, TAB_RIGHT, rel->sc[1].n_items, wfmt); - tab_double (tbl, 3, 3, TAB_RIGHT, rel->sc[2].n_items, wfmt); - - tab_double (tbl, 3, 4, TAB_RIGHT, - rel->sc[1].n_items + rel->sc[2].n_items, wfmt); - - { - /* R is the correlation between the two parts */ - double r = rel->sc[0].variance_of_sums - - rel->sc[1].variance_of_sums - - rel->sc[2].variance_of_sums ; - - /* Guttman Split Half Coefficient */ - double g = 2 * r / rel->sc[0].variance_of_sums; - - /* Unequal Length Spearman Brown Coefficient, and - intermediate value used in the computation thereof */ - double uly, tmp; - - r /= sqrt (rel->sc[1].variance_of_sums); - r /= sqrt (rel->sc[2].variance_of_sums); - r /= 2.0; - - tab_double (tbl, 3, 5, TAB_RIGHT, r, NULL); - - /* Equal length Spearman-Brown Coefficient */ - tab_double (tbl, 3, 6, TAB_RIGHT, 2 * r / (1.0 + r), NULL); - - tab_double (tbl, 3, 8, TAB_RIGHT, g, NULL); - - tmp = (1.0 - r*r) * rel->sc[1].n_items * rel->sc[2].n_items / - pow2 (rel->sc[0].n_items); - - uly = sqrt( pow4 (r) + 4 * pow2 (r) * tmp); - uly -= pow2 (r); - uly /= 2 * tmp; + if (rel->model == MODEL_ALPHA) + { + pivot_dimension_create (table, PIVOT_AXIS_COLUMN, + N_("Statistics"), + N_("Cronbach's Alpha"), PIVOT_RC_OTHER, + N_("N of Items"), PIVOT_RC_COUNT); + + const struct cronbach *s = &rel->sc[0]; + pivot_table_put1 (table, 0, pivot_value_new_number (s->alpha)); + pivot_table_put1 (table, 1, pivot_value_new_number (s->n_items)); + } + else + { + struct pivot_dimension *statistics = pivot_dimension_create ( + table, PIVOT_AXIS_ROW, N_("Statistics")); + struct pivot_category *alpha = pivot_category_create_group ( + statistics->root, N_("Cronbach's Alpha")); + pivot_category_create_group (alpha, N_("Part 1"), + N_("Value"), PIVOT_RC_OTHER, + N_("N of Items"), PIVOT_RC_COUNT); + pivot_category_create_group (alpha, N_("Part 2"), + N_("Value"), PIVOT_RC_OTHER, + N_("N of Items"), PIVOT_RC_COUNT); + pivot_category_create_leaves (alpha, + N_("Total N of Items"), PIVOT_RC_COUNT); + pivot_category_create_leaves (statistics->root, + N_("Correlation Between Forms"), + PIVOT_RC_OTHER); + pivot_category_create_group (statistics->root, + N_("Spearman-Brown Coefficient"), + N_("Equal Length"), PIVOT_RC_OTHER, + N_("Unequal Length"), PIVOT_RC_OTHER); + pivot_category_create_leaves (statistics->root, + N_("Guttman Split-Half Coefficient"), + PIVOT_RC_OTHER); + + /* R is the correlation between the two parts */ + double r0 = rel->sc[0].variance_of_sums - + rel->sc[1].variance_of_sums - + rel->sc[2].variance_of_sums ; + double r1 = (r0 / sqrt (rel->sc[1].variance_of_sums) + / sqrt (rel->sc[2].variance_of_sums) + / 2.0); + + /* Guttman Split Half Coefficient */ + double g = 2 * r0 / rel->sc[0].variance_of_sums; + + double tmp = (1.0 - r1*r1) * rel->sc[1].n_items * rel->sc[2].n_items / + pow2 (rel->sc[0].n_items); + + double entries[] = { + rel->sc[1].alpha, + rel->sc[1].n_items, + rel->sc[2].alpha, + rel->sc[2].n_items, + rel->sc[1].n_items + rel->sc[2].n_items, + r1, + 2 * r1 / (1.0 + r1), + (sqrt ( pow4 (r1) + 4 * pow2 (r1) * tmp) - pow2 (r1)) / (2 * tmp), + g, + }; + for (size_t i = 0; i < sizeof entries / sizeof *entries; i++) + pivot_table_put1 (table, i, pivot_value_new_number (entries[i])); + } - tab_double (tbl, 3, 7, TAB_RIGHT, uly, NULL); - } + pivot_table_submit (table); } -