From 30f6d8606d41c9ee1738b2f92541d836df1f13c0 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 28 Jan 2022 19:10:26 -0800 Subject: [PATCH] More tests for totals and subtotals, with fixes. --- src/language/stats/ctables.c | 16 ++- tests/language/stats/ctables.at | 202 ++++++++++++++++++++------------ 2 files changed, 138 insertions(+), 80 deletions(-) diff --git a/src/language/stats/ctables.c b/src/language/stats/ctables.c index c982f19cf6..652534ec77 100644 --- a/src/language/stats/ctables.c +++ b/src/language/stats/ctables.c @@ -186,6 +186,7 @@ struct ctables_cell struct hmap_node node; /* The domains that contain this cell. */ + bool contributes_to_domains; struct ctables_domain *domains[N_CTDTS]; bool hide; @@ -2366,6 +2367,7 @@ ctables_cell_insert__ (struct ctables_table *t, const struct ccase *c, cell = xmalloc (sizeof *cell); cell->hide = false; cell->sv = sv; + cell->contributes_to_domains = true; for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++) { const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]]; @@ -2375,14 +2377,19 @@ ctables_cell_insert__ (struct ctables_table *t, const struct ccase *c, : NULL); for (size_t i = 0; i < nest->n; i++) { + const struct ctables_category *cat = cats[a][i]; + if (i != nest->scale_idx) { - const struct ctables_category *subtotal = cats[a][i]->subtotal; + const struct ctables_category *subtotal = cat->subtotal; if (subtotal && subtotal->type == CCT_HSUBTOTAL) cell->hide = true; + + if (cat->type == CCT_TOTAL || cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL) + cell->contributes_to_domains = false; } - cell->axes[a].cvs[i].category = cats[a][i]; + cell->axes[a].cvs[i].category = cat; value_clone (&cell->axes[a].cvs[i].value, case_data (c, nest->vars[i]), var_get_width (nest->vars[i])); } @@ -2411,8 +2418,9 @@ ctables_cell_add__ (struct ctables_table *t, const struct ccase *c, for (size_t i = 0; i < specs->n; i++) ctables_summary_add (&cell->summaries[i], &specs->specs[i], specs->var, case_data (c, specs->var), weight); - for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++) - cell->domains[dt]->valid += weight; + if (cell->contributes_to_domains) + for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++) + cell->domains[dt]->valid += weight; } static void diff --git a/tests/language/stats/ctables.at b/tests/language/stats/ctables.at index 511ea02d18..43413ac21f 100644 --- a/tests/language/stats/ctables.at +++ b/tests/language/stats/ctables.at @@ -155,80 +155,98 @@ AT_SETUP([CTABLES simple nesting]) AT_CHECK([ln $top_srcdir/examples/nhtsa.sav . || cp $top_srcdir/examples/nhtsa.sav .]) AT_DATA([ctables.sps], [[GET 'nhtsa.sav'. -CTABLES /TABLE (qn105ba + qn105bb + qn105bc + qn105bd) > qns3a [TABLEPCT PCT8.0] - /CATEGORIES VARIABLES=qns3a TOTAL=NO. +CTABLES /TABLE (qn105ba + qn105bb + qn105bc + qn105bd) > qns3a [COUNT, TABLEPCT PCT8.0] + /CATEGORIES VARIABLES=qns3a TOTAL=YES. CTABLES /TABLE qns3a > (qn105ba + qn105bb + qn105bc + qn105bd) [TABLEPCT PCT8.0] - /CATEGORIES VARIABLES=qns3a TOTAL=NO + /CATEGORIES VARIABLES=qns3a TOTAL=YES /CLABELS ROW=OPPOSITE. ]]) -dnl XXX With TOTAL=YES above, the totals get included in the denominator so that -dnl the percentages are about half of the correct values. AT_CHECK([pspp ctables.sps -O box=unicode -O width=80], [0], [dnl Custom Tables -╭───────────────────────────────────────────────────────────────────────┬──────╮ -│ │ Table│ -│ │ % │ -├───────────────────────────────────────────────────────────────────────┼──────┤ -│105b. How likely is it that drivers who Almost S3a. Male │ 4%│ -│have had too much to drink to drive safely certain GENDER: Female│ 6%│ -│will A. Get stopped by the police? ╶───────────────────────────┼──────┤ -│ Very likely S3a. Male │ 10%│ -│ GENDER: Female│ 12%│ -│ ╶───────────────────────────┼──────┤ -│ Somewhat S3a. Male │ 17%│ -│ likely GENDER: Female│ 23%│ -│ ╶───────────────────────────┼──────┤ -│ Somewhat S3a. Male │ 9%│ -│ unlikely GENDER: Female│ 10%│ -│ ╶───────────────────────────┼──────┤ -│ Very S3a. Male │ 5%│ -│ unlikely GENDER: Female│ 4%│ -├───────────────────────────────────────────────────────────────────────┼──────┤ -│105b. How likely is it that drivers who Almost S3a. Male │ 6%│ -│have had too much to drink to drive safely certain GENDER: Female│ 10%│ -│will B. Have an accident? ╶───────────────────────────┼──────┤ -│ Very likely S3a. Male │ 16%│ -│ GENDER: Female│ 25%│ -│ ╶───────────────────────────┼──────┤ -│ Somewhat S3a. Male │ 17%│ -│ likely GENDER: Female│ 18%│ -│ ╶───────────────────────────┼──────┤ -│ Somewhat S3a. Male │ 4%│ -│ unlikely GENDER: Female│ 2%│ -│ ╶───────────────────────────┼──────┤ -│ Very S3a. Male │ 1%│ -│ unlikely GENDER: Female│ 1%│ -├───────────────────────────────────────────────────────────────────────┼──────┤ -│105b. How likely is it that drivers who Almost S3a. Male │ 8%│ -│have had too much to drink to drive safely certain GENDER: Female│ 9%│ -│will C. Be convicted for drunk driving? ╶───────────────────────────┼──────┤ -│ Very likely S3a. Male │ 14%│ -│ GENDER: Female│ 15%│ -│ ╶───────────────────────────┼──────┤ -│ Somewhat S3a. Male │ 12%│ -│ likely GENDER: Female│ 18%│ -│ ╶───────────────────────────┼──────┤ -│ Somewhat S3a. Male │ 7%│ -│ unlikely GENDER: Female│ 8%│ -│ ╶───────────────────────────┼──────┤ -│ Very S3a. Male │ 4%│ -│ unlikely GENDER: Female│ 5%│ -├───────────────────────────────────────────────────────────────────────┼──────┤ -│105b. How likely is it that drivers who Almost S3a. Male │ 7%│ -│have had too much to drink to drive safely certain GENDER: Female│ 9%│ -│will D. Be arrested for drunk driving? ╶───────────────────────────┼──────┤ -│ Very likely S3a. Male │ 12%│ -│ GENDER: Female│ 15%│ -│ ╶───────────────────────────┼──────┤ -│ Somewhat S3a. Male │ 14%│ -│ likely GENDER: Female│ 19%│ -│ ╶───────────────────────────┼──────┤ -│ Somewhat S3a. Male │ 8%│ -│ unlikely GENDER: Female│ 8%│ -│ ╶───────────────────────────┼──────┤ -│ Very S3a. Male │ 4%│ -│ unlikely GENDER: Female│ 4%│ -╰───────────────────────────────────────────────────────────────────────┴──────╯ +╭─────────────────────────────────────────────────────────────────┬─────┬──────╮ +│ │ │ Table│ +│ │Count│ % │ +├─────────────────────────────────────────────────────────────────┼─────┼──────┤ +│105b. How likely is it that drivers Almost S3a. Male │ 297│ 4%│ +│who have had too much to drink to certain GENDER: Female│ 403│ 6%│ +│drive safely will A. Get stopped by Total │ 700│ 10%│ +│the police? ╶──────────────────────────┼─────┼──────┤ +│ Very S3a. Male │ 660│ 10%│ +│ likely GENDER: Female│ 842│ 12%│ +│ Total │ 1502│ 22%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Somewhat S3a. Male │ 1174│ 17%│ +│ likely GENDER: Female│ 1589│ 23%│ +│ Total │ 2763│ 40%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Somewhat S3a. Male │ 640│ 9%│ +│ unlikely GENDER: Female│ 667│ 10%│ +│ Total │ 1307│ 19%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Very S3a. Male │ 311│ 5%│ +│ unlikely GENDER: Female│ 298│ 4%│ +│ Total │ 609│ 9%│ +├─────────────────────────────────────────────────────────────────┼─────┼──────┤ +│105b. How likely is it that drivers Almost S3a. Male │ 429│ 6%│ +│who have had too much to drink to certain GENDER: Female│ 671│ 10%│ +│drive safely will B. Have an accident? Total │ 1100│ 16%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Very S3a. Male │ 1104│ 16%│ +│ likely GENDER: Female│ 1715│ 25%│ +│ Total │ 2819│ 41%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Somewhat S3a. Male │ 1203│ 17%│ +│ likely GENDER: Female│ 1214│ 18%│ +│ Total │ 2417│ 35%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Somewhat S3a. Male │ 262│ 4%│ +│ unlikely GENDER: Female│ 168│ 2%│ +│ Total │ 430│ 6%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Very S3a. Male │ 81│ 1%│ +│ unlikely GENDER: Female│ 59│ 1%│ +│ Total │ 140│ 2%│ +├─────────────────────────────────────────────────────────────────┼─────┼──────┤ +│105b. How likely is it that drivers Almost S3a. Male │ 539│ 8%│ +│who have had too much to drink to certain GENDER: Female│ 610│ 9%│ +│drive safely will C. Be convicted for Total │ 1149│ 17%│ +│drunk driving? ╶──────────────────────────┼─────┼──────┤ +│ Very S3a. Male │ 988│ 14%│ +│ likely GENDER: Female│ 1049│ 15%│ +│ Total │ 2037│ 30%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Somewhat S3a. Male │ 822│ 12%│ +│ likely GENDER: Female│ 1210│ 18%│ +│ Total │ 2032│ 30%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Somewhat S3a. Male │ 446│ 7%│ +│ unlikely GENDER: Female│ 548│ 8%│ +│ Total │ 994│ 15%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Very S3a. Male │ 268│ 4%│ +│ unlikely GENDER: Female│ 354│ 5%│ +│ Total │ 622│ 9%│ +├─────────────────────────────────────────────────────────────────┼─────┼──────┤ +│105b. How likely is it that drivers Almost S3a. Male │ 498│ 7%│ +│who have had too much to drink to certain GENDER: Female│ 603│ 9%│ +│drive safely will D. Be arrested for Total │ 1101│ 16%│ +│drunk driving? ╶──────────────────────────┼─────┼──────┤ +│ Very S3a. Male │ 805│ 12%│ +│ likely GENDER: Female│ 1029│ 15%│ +│ Total │ 1834│ 27%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Somewhat S3a. Male │ 975│ 14%│ +│ likely GENDER: Female│ 1332│ 19%│ +│ Total │ 2307│ 34%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Somewhat S3a. Male │ 535│ 8%│ +│ unlikely GENDER: Female│ 560│ 8%│ +│ Total │ 1095│ 16%│ +│ ╶──────────────────────────┼─────┼──────┤ +│ Very S3a. Male │ 270│ 4%│ +│ unlikely GENDER: Female│ 279│ 4%│ +│ Total │ 549│ 8%│ +╰─────────────────────────────────────────────────────────────────┴─────┴──────╯ Custom Tables ╭─────────────────────────────────┬────────┬──────┬─────────┬─────────┬────────╮ @@ -253,6 +271,14 @@ AT_CHECK([pspp ctables.sps -O box=unicode -O width=80], [0], [dnl │ drive safely will │ │ │ │ │ │ │ A. Get stopped by │ │ │ │ │ │ │ the police? │ │ │ │ │ │ +│ ╶─────────────────────────┼────────┼──────┼─────────┼─────────┼────────┤ +│ Total 105b. How likely │ 10%│ 22%│ 40%│ 19%│ 9%│ +│ is it that drivers│ │ │ │ │ │ +│ who have had too │ │ │ │ │ │ +│ much to drink to │ │ │ │ │ │ +│ drive safely will │ │ │ │ │ │ +│ A. Get stopped by │ │ │ │ │ │ +│ the police? │ │ │ │ │ │ ├─────────────────────────────────┼────────┼──────┼─────────┼─────────┼────────┤ │S3a. Male 105b. How likely │ 6%│ 16%│ 17%│ 4%│ 1%│ │GENDER: is it that drivers│ │ │ │ │ │ @@ -269,6 +295,14 @@ AT_CHECK([pspp ctables.sps -O box=unicode -O width=80], [0], [dnl │ drive safely will │ │ │ │ │ │ │ B. Have an │ │ │ │ │ │ │ accident? │ │ │ │ │ │ +│ ╶─────────────────────────┼────────┼──────┼─────────┼─────────┼────────┤ +│ Total 105b. How likely │ 16%│ 41%│ 35%│ 6%│ 2%│ +│ is it that drivers│ │ │ │ │ │ +│ who have had too │ │ │ │ │ │ +│ much to drink to │ │ │ │ │ │ +│ drive safely will │ │ │ │ │ │ +│ B. Have an │ │ │ │ │ │ +│ accident? │ │ │ │ │ │ ├─────────────────────────────────┼────────┼──────┼─────────┼─────────┼────────┤ │S3a. Male 105b. How likely │ 8%│ 14%│ 12%│ 7%│ 4%│ │GENDER: is it that drivers│ │ │ │ │ │ @@ -285,6 +319,14 @@ AT_CHECK([pspp ctables.sps -O box=unicode -O width=80], [0], [dnl │ drive safely will │ │ │ │ │ │ │ C. Be convicted │ │ │ │ │ │ │ for drunk driving?│ │ │ │ │ │ +│ ╶─────────────────────────┼────────┼──────┼─────────┼─────────┼────────┤ +│ Total 105b. How likely │ 17%│ 30%│ 30%│ 15%│ 9%│ +│ is it that drivers│ │ │ │ │ │ +│ who have had too │ │ │ │ │ │ +│ much to drink to │ │ │ │ │ │ +│ drive safely will │ │ │ │ │ │ +│ C. Be convicted │ │ │ │ │ │ +│ for drunk driving?│ │ │ │ │ │ ├─────────────────────────────────┼────────┼──────┼─────────┼─────────┼────────┤ │S3a. Male 105b. How likely │ 7%│ 12%│ 14%│ 8%│ 4%│ │GENDER: is it that drivers│ │ │ │ │ │ @@ -301,6 +343,14 @@ AT_CHECK([pspp ctables.sps -O box=unicode -O width=80], [0], [dnl │ drive safely will │ │ │ │ │ │ │ D. Be arrested for│ │ │ │ │ │ │ drunk driving? │ │ │ │ │ │ +│ ╶─────────────────────────┼────────┼──────┼─────────┼─────────┼────────┤ +│ Total 105b. How likely │ 16%│ 27%│ 34%│ 16%│ 8%│ +│ is it that drivers│ │ │ │ │ │ +│ who have had too │ │ │ │ │ │ +│ much to drink to │ │ │ │ │ │ +│ drive safely will │ │ │ │ │ │ +│ D. Be arrested for│ │ │ │ │ │ +│ drunk driving? │ │ │ │ │ │ ╰─────────────────────────────────┴────────┴──────┴─────────┴─────────┴────────╯ ]) AT_CLEANUP @@ -574,17 +624,17 @@ AT_CHECK([pspp ctables.sps -O box=unicode -O width=120], [0], [dnl │ ├─────────┼────────┼─────────┼────────┼─────────┼─────────┤ │ │ Column %│Column %│ Column %│Column %│ Column %│ Column %│ ├────────────────────────────────────────────────────────────┼─────────┼────────┼─────────┼────────┼─────────┼─────────┤ -│105b. How likely is it that drivers who have had Almost │ 4.8%│ 4.1%│ 6.2%│ 4.9%│ 10.0%│ 11.9%│ +│105b. How likely is it that drivers who have had Almost │ 9.5%│ 8.2%│ 12.4%│ 9.9%│ 20.0%│ 23.8%│ │too much to drink to drive safely will A. Get certain │ │ │ │ │ │ │ -│stopped by the police? Very likely│ 12.5%│ 9.2%│ 12.0%│ 13.3%│ 12.7%│ 16.7%│ -│ Somewhat │ 19.2%│ 20.9%│ 19.3%│ 18.8%│ 18.2%│ 11.9%│ +│stopped by the police? Very likely│ 24.9%│ 18.5%│ 24.0%│ 26.6%│ 25.5%│ 33.3%│ +│ Somewhat │ 38.3%│ 41.9%│ 38.6%│ 37.5%│ 36.4%│ 23.8%│ │ likely │ │ │ │ │ │ │ -│ Subtotal │ 36.4%│ 34.3%│ 37.5%│ 37.0%│ 40.9%│ 40.5%│ -│ Somewhat │ 9.0%│ 10.8%│ 8.4%│ 8.3%│ 5.5%│ 4.8%│ +│ Subtotal │ 72.8%│ 68.6%│ 75.0%│ 74.0%│ 81.8%│ 81.0%│ +│ Somewhat │ 18.1%│ 21.7%│ 16.8%│ 16.7%│ 10.9%│ 9.5%│ │ unlikely │ │ │ │ │ │ │ -│ Very │ 4.6%│ 4.9%│ 4.1%│ 4.7%│ 3.6%│ 4.8%│ +│ Very │ 9.2%│ 9.7%│ 8.2%│ 9.4%│ 7.3%│ 9.5%│ │ unlikely │ │ │ │ │ │ │ -│ Subtotal │ 13.6%│ 15.7%│ 12.5%│ 13.0%│ 9.1%│ 9.5%│ +│ Subtotal │ 27.2%│ 31.4%│ 25.0%│ 26.0%│ 18.2%│ 19.0%│ ╰────────────────────────────────────────────────────────────┴─────────┴────────┴─────────┴────────┴─────────┴─────────╯ Custom Tables -- 2.30.2