X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=inline;f=src%2Flanguage%2Fstats%2Fmedian.c;h=9c35c15a456aa54820a3faafaa847e5bbbcd3136;hb=42d90dac4bc5ee06ef0dd3cd62b84378f16e7ea9;hp=2db57ab76f025df50ee42797074981e2e09cfa7e;hpb=1babb2ecc20dcae51f6cc639d39cce197d31dbdc;p=pspp
diff --git a/src/language/stats/median.c b/src/language/stats/median.c
index 2db57ab76f..9c35c15a45 100644
--- a/src/language/stats/median.c
+++ b/src/language/stats/median.c
@@ -12,7 +12,7 @@
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program. If not, see .
+ along with this program. If not, see .
*/
#include
@@ -21,7 +21,7 @@
#include
#include "data/format.h"
-#include "libpspp/cast.h"
+
#include "data/variable.h"
#include "data/case.h"
@@ -30,22 +30,21 @@
#include "data/casereader.h"
#include "data/casewriter.h"
#include "data/subcase.h"
+#include "data/value.h"
-
-#include "data/casereader.h"
#include "math/percentiles.h"
-
#include "math/sort.h"
+#include "libpspp/cast.h"
#include "libpspp/hmap.h"
#include "libpspp/array.h"
#include "libpspp/str.h"
-#include "data/value.h"
#include "libpspp/misc.h"
-#include "output/tab.h"
+#include "output/pivot-table.h"
#include "gettext.h"
+#define N_(msgid) msgid
#define _(msgid) gettext (msgid)
@@ -63,12 +62,12 @@ struct results
struct val_node **sorted_array;
double n;
double median;
- double chisq;
+ double chisq;
};
-static int
+static int
val_node_cmp_3way (const void *a_, const void *b_, const void *aux)
{
const struct variable *indep_var = aux;
@@ -78,15 +77,15 @@ val_node_cmp_3way (const void *a_, const void *b_, const void *aux)
return value_compare_3way (&(*a)->val, &(*b)->val, var_get_width (indep_var));
}
-static void
+static void
show_frequencies (const struct n_sample_test *nst, const struct results *results, int n_vals, const struct dictionary *);
-static void
+static void
show_test_statistics (const struct n_sample_test *nst, const struct results *results, int, const struct dictionary *);
static struct val_node *
-find_value (const struct hmap *map, const union value *val,
+find_value (const struct hmap *map, const union value *val,
const struct variable *var)
{
struct val_node *foo = NULL;
@@ -119,7 +118,7 @@ median_execute (const struct dataset *ds,
const bool n_sample_test = (value_compare_3way (&nst->val2, &nst->val1,
var_get_width (nst->indep_var)) > 0);
- struct results *results = xcalloc (nst->n_vars, sizeof (*results));
+ struct results *results = XCALLOC (nst->n_vars, struct results);
int n_vals = 0;
for (v = 0; v < nst->n_vars; ++v)
{
@@ -146,7 +145,7 @@ median_execute (const struct dataset *ds,
var_get_width (nst->indep_var), 0));
}
- if ( median == SYSMIS)
+ if (median == SYSMIS)
{
struct percentile *ptl;
struct order_stats *os;
@@ -158,9 +157,9 @@ median_execute (const struct dataset *ds,
rr = casereader_clone (r);
writer = sort_create_writer (&sc, casereader_get_proto (rr));
- for (; (c = casereader_read (rr)) != NULL; )
+ for (; (c = casereader_read (rr)) != NULL;)
{
- if ( var_is_value_missing (var, case_data (c, var), exclude))
+ if (var_is_value_missing (var, case_data (c, var), exclude))
{
case_unref (c);
continue;
@@ -176,7 +175,7 @@ median_execute (const struct dataset *ds,
ptl = percentile_create (0.5, cc);
os = &ptl->parent;
-
+
order_stats_accumulate (&os, 1,
rr,
wvar,
@@ -188,7 +187,7 @@ median_execute (const struct dataset *ds,
}
results[v].median = median;
-
+
for (; (c = casereader_read (r)) != NULL; case_unref (c))
{
@@ -197,9 +196,8 @@ median_execute (const struct dataset *ds,
const union value *val = case_data (c, var);
const union value *indep_val = case_data (c, nst->indep_var);
- if ( var_is_value_missing (var, case_data (c, var), exclude))
+ if (var_is_value_missing (var, case_data (c, var), exclude))
{
- case_unref (c);
continue;
}
@@ -211,22 +209,21 @@ median_execute (const struct dataset *ds,
value_compare_3way (indep_val, &nst->val1, width) < 0
||
value_compare_3way (indep_val, &nst->val2, width) > 0
- )
+ )
{
- case_unref (c);
continue;
}
}
vn = find_value (&map, indep_val, nst->indep_var);
- if ( vn == NULL)
+ if (vn == NULL)
{
- if ( n_sample_test == true)
+ if (n_sample_test == true)
{
int width = var_get_width (nst->indep_var);
vn = xzalloc (sizeof *vn);
value_clone (&vn->val, indep_val, width);
-
+
hmap_insert (&map, &vn->node, value_hash (indep_val, width, 0));
}
else
@@ -273,7 +270,7 @@ median_execute (const struct dataset *ds,
n_vals = x;
hmap_destroy (&map);
- sort (results[v].sorted_array, x, sizeof (*results[v].sorted_array),
+ sort (results[v].sorted_array, x, sizeof (*results[v].sorted_array),
val_node_cmp_3way, nst->indep_var);
}
@@ -302,160 +299,88 @@ median_execute (const struct dataset *ds,
-static void
+static void
show_frequencies (const struct n_sample_test *nst, const struct results *results, int n_vals, const struct dictionary *dict)
{
- const struct variable *weight = dict_get_weight (dict);
- const struct fmt_spec *wfmt = weight ? var_get_print_format (weight) : &F_8_0;
-
- int i;
- int v;
-
- const int row_headers = 2;
- const int column_headers = 2;
- const int nc = row_headers + n_vals;
- const int nr = column_headers + nst->n_vars * 2;
-
- struct tab_table *table = tab_create (nc, nr);
-
- tab_headers (table, row_headers, 0, column_headers, 0);
-
- tab_title (table, _("Frequencies"));
-
- /* Box around the table and vertical lines inside*/
- tab_box (table, TAL_2, TAL_2, -1, TAL_1,
- 0, 0, tab_nc (table) - 1, tab_nr (table) - 1 );
-
- tab_hline (table, TAL_2, 0, tab_nc (table) -1, column_headers);
- tab_vline (table, TAL_2, row_headers, 0, tab_nr (table) - 1);
-
- tab_joint_text (table,
- row_headers, 0, row_headers + n_vals - 1, 0,
- TAT_TITLE | TAB_CENTER, var_to_string (nst->indep_var));
-
-
- tab_hline (table, TAL_1, row_headers, tab_nc (table) - 1, 1);
-
-
- for (i = 0; i < n_vals; ++i)
- {
- const struct results *rs = results + 0;
- struct string label;
- ds_init_empty (&label);
-
- var_append_value_name (nst->indep_var, &rs->sorted_array[i]->val,
- &label);
-
- tab_text (table, row_headers + i, 1,
- TAT_TITLE | TAB_LEFT, ds_cstr (&label));
-
- ds_destroy (&label);
- }
-
- for (v = 0; v < nst->n_vars; ++v)
+ struct pivot_table *table = pivot_table_create (N_("Frequencies"));
+ pivot_table_set_weight_var (table, dict_get_weight (dict));
+
+ struct pivot_dimension *indep = pivot_dimension_create__ (
+ table, PIVOT_AXIS_COLUMN, pivot_value_new_variable (nst->indep_var));
+ indep->root->show_label = true;
+ for (int i = 0; i < n_vals; ++i)
+ pivot_category_create_leaf_rc (
+ indep->root, pivot_value_new_var_value (
+ nst->indep_var, &results->sorted_array[i]->val), PIVOT_RC_COUNT);
+ pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Statistics"),
+ N_("> Median"), N_("⤠Median"));
+
+ struct pivot_dimension *dep = pivot_dimension_create (
+ table, PIVOT_AXIS_ROW, N_("Dependent Variables"));
+
+ for (int v = 0; v < nst->n_vars; ++v)
{
const struct results *rs = &results[v];
- tab_text (table, 0, column_headers + v * 2,
- TAT_TITLE | TAB_LEFT, var_to_string (rs->var) );
- tab_text (table, 1, column_headers + v * 2,
- TAT_TITLE | TAB_LEFT, _("> Median") );
+ int dep_idx = pivot_category_create_leaf (
+ dep->root, pivot_value_new_variable (rs->var));
- tab_text (table, 1, column_headers + v * 2 + 1,
- TAT_TITLE | TAB_LEFT, _("⤠Median") );
-
- if ( v > 0)
- tab_hline (table, TAL_1, 0, tab_nc (table) - 1, column_headers + v * 2);
- }
-
- for (v = 0; v < nst->n_vars; ++v)
- {
- int i;
- const struct results *rs = &results[v];
-
- for (i = 0; i < n_vals; ++i)
+ for (int indep_idx = 0; indep_idx < n_vals; indep_idx++)
{
- const struct val_node *vn = rs->sorted_array[i];
- tab_double (table, row_headers + i, column_headers + v * 2,
- 0, vn->gt, wfmt);
-
- tab_double (table, row_headers + i, column_headers + v * 2 + 1,
- 0, vn->le, wfmt);
+ const struct val_node *vn = rs->sorted_array[indep_idx];
+ pivot_table_put3 (table, indep_idx, 0, dep_idx,
+ pivot_value_new_number (vn->gt));
+ pivot_table_put3 (table, indep_idx, 1, dep_idx,
+ pivot_value_new_number (vn->le));
}
}
- tab_submit (table);
+ pivot_table_submit (table);
}
-
-static void
+static void
show_test_statistics (const struct n_sample_test *nst,
const struct results *results,
int n_vals,
const struct dictionary *dict)
{
- const struct variable *weight = dict_get_weight (dict);
- const struct fmt_spec *wfmt = weight ? var_get_print_format (weight) : &F_8_0;
-
- int v;
-
- const int row_headers = 1;
- const int column_headers = 1;
- const int nc = row_headers + 5;
- const int nr = column_headers + nst->n_vars;
-
- struct tab_table *table = tab_create (nc, nr);
-
- tab_headers (table, row_headers, 0, column_headers, 0);
+ struct pivot_table *table = pivot_table_create (N_("Test Statistics"));
+ pivot_table_set_weight_var (table, dict_get_weight (dict));
- tab_title (table, _("Test Statistics"));
+ pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
+ N_("N"), PIVOT_RC_COUNT,
+ N_("Median"),
+ N_("Chi-Square"), PIVOT_RC_OTHER,
+ N_("df"), PIVOT_RC_COUNT,
+ N_("Asymp. Sig."), PIVOT_RC_SIGNIFICANCE);
+ struct pivot_dimension *variables = pivot_dimension_create (
+ table, PIVOT_AXIS_ROW, N_("Variables"));
- tab_box (table, TAL_2, TAL_2, -1, TAL_1,
- 0, 0, tab_nc (table) - 1, tab_nr (table) - 1 );
-
- tab_hline (table, TAL_2, 0, tab_nc (table) -1, column_headers);
- tab_vline (table, TAL_2, row_headers, 0, tab_nr (table) - 1);
-
- tab_text (table, row_headers + 0, 0,
- TAT_TITLE | TAB_CENTER, _("N"));
-
- tab_text (table, row_headers + 1, 0,
- TAT_TITLE | TAB_CENTER, _("Median"));
-
- tab_text (table, row_headers + 2, 0,
- TAT_TITLE | TAB_CENTER, _("Chi-Square"));
-
- tab_text (table, row_headers + 3, 0,
- TAT_TITLE | TAB_CENTER, _("df"));
-
- tab_text (table, row_headers + 4, 0,
- TAT_TITLE | TAB_CENTER, _("Asymp. Sig."));
-
-
- for (v = 0; v < nst->n_vars; ++v)
+ for (int v = 0; v < nst->n_vars; ++v)
{
double df = n_vals - 1;
const struct results *rs = &results[v];
- tab_text (table, 0, column_headers + v,
- TAT_TITLE | TAB_LEFT, var_to_string (rs->var));
-
-
- tab_double (table, row_headers + 0, column_headers + v,
- 0, rs->n, wfmt);
-
- tab_double (table, row_headers + 1, column_headers + v,
- 0, rs->median, NULL);
- tab_double (table, row_headers + 2, column_headers + v,
- 0, rs->chisq, NULL);
-
- tab_double (table, row_headers + 3, column_headers + v,
- 0, df, wfmt);
-
- tab_double (table, row_headers + 4, column_headers + v,
- 0, gsl_cdf_chisq_Q (rs->chisq, df), NULL);
+ int var_idx = pivot_category_create_leaf (
+ variables->root, pivot_value_new_variable (rs->var));
+
+ double entries[] = {
+ rs->n,
+ rs->median,
+ rs->chisq,
+ df,
+ gsl_cdf_chisq_Q (rs->chisq, df),
+ };
+ for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
+ {
+ struct pivot_value *value
+ = pivot_value_new_number (entries[i]);
+ if (i == 1)
+ value->numeric.format = *var_get_print_format (rs->var);
+ pivot_table_put2 (table, i, var_idx, value);
+ }
}
-
- tab_submit (table);
+
+ pivot_table_submit (table);
}