From 204a1ee35aebcc2cf955017070c1a3638cdaee22 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Fri, 25 Dec 2009 19:18:13 +0100 Subject: [PATCH] FACTOR: Added "Scree Plots" --- src/language/stats/factor.c | 36 ++++++++++-- src/output/automake.mk | 2 + src/output/charts/scree.c | 111 ++++++++++++++++++++++++++++++++++++ src/output/charts/scree.h | 31 ++++++++++ 4 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 src/output/charts/scree.c create mode 100644 src/output/charts/scree.h diff --git a/src/language/stats/factor.c b/src/language/stats/factor.c index b035fc8d..2dbf3b69 100644 --- a/src/language/stats/factor.c +++ b/src/language/stats/factor.c @@ -46,6 +46,8 @@ #include +#include +#include #include "gettext.h" #define _(msgid) gettext (msgid) @@ -70,6 +72,12 @@ enum extraction_method EXTRACTION_PAF, }; +enum plot_opts + { + PLOT_SCREE = 0x0001, + PLOT_ROTATION = 0x0002 + }; + enum print_opts { PRINT_UNIVARIATE = 0x0001, @@ -100,6 +108,7 @@ struct cmd_factor enum mv_class exclude; enum print_opts print; enum extraction_method extraction; + enum plot_opts plot; /* Extraction Criteria */ int n_factors; @@ -526,6 +535,7 @@ cmd_factor (struct lexer *lexer, struct dataset *ds) factor.econverge = 0.001; factor.blank = 0; factor.sort = false; + factor.plot = 0; factor.wv = dict_get_weight (dict); @@ -546,7 +556,6 @@ cmd_factor (struct lexer *lexer, struct dataset *ds) { lex_match (lexer, '/'); -#if FACTOR_FULLY_IMPLEMENTED if (lex_match_id (lexer, "PLOT")) { lex_match (lexer, '='); @@ -554,10 +563,13 @@ cmd_factor (struct lexer *lexer, struct dataset *ds) { if (lex_match_id (lexer, "EIGEN")) { + factor.plot |= PLOT_SCREE; } +#if FACTOR_FULLY_IMPLEMENTED else if (lex_match_id (lexer, "ROTATION")) { } +#endif else { lex_error (lexer, NULL); @@ -565,9 +577,7 @@ cmd_factor (struct lexer *lexer, struct dataset *ds) } } } - else -#endif - if (lex_match_id (lexer, "METHOD")) + else if (lex_match_id (lexer, "METHOD")) { lex_match (lexer, '='); while (lex_token (lexer) != '.' && lex_token (lexer) != '/') @@ -911,6 +921,22 @@ communality (struct idata *idata, int n, int n_factors) } +static void +show_scree (const struct cmd_factor *f, struct idata *idata) +{ + struct scree *s; + const char *label ; + + if ( !(f->plot & PLOT_SCREE) ) + return; + + + label = f->extraction == EXTRACTION_PC ? _("Component Number") : _("Factor Number"); + + s = scree_create (idata->eval, label); + + chart_submit (scree_get_chart (s)); +} static void show_communalities (const struct cmd_factor * factor, @@ -1517,6 +1543,8 @@ do_factor (const struct cmd_factor *factor, struct casereader *r) factor_matrix_workspace_free (fmw); + show_scree (factor, idata); + show_factor_matrix (factor, idata, factor_matrix); gsl_vector_free (initial_communalities); diff --git a/src/output/automake.mk b/src/output/automake.mk index 7e233750..b89dc05c 100644 --- a/src/output/automake.mk +++ b/src/output/automake.mk @@ -22,6 +22,8 @@ src_output_liboutput_la_SOURCES = \ src/output/charts/plot-hist.h \ src/output/charts/roc-chart.c \ src/output/charts/roc-chart.h \ + src/output/charts/scree.c \ + src/output/charts/scree.h \ src/output/html.c \ src/output/htmlP.h \ src/output/journal.c \ diff --git a/src/output/charts/scree.c b/src/output/charts/scree.c new file mode 100644 index 00000000..44f7c7a6 --- /dev/null +++ b/src/output/charts/scree.c @@ -0,0 +1,111 @@ +/* PSPP - a program for statistical analysis. + Copyright (C) 2009 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 + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + 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 . */ + +#include + +#include + +#include +#include +#include + +#include + +#include "xalloc.h" + +#include "gettext.h" +#define _(msgid) gettext (msgid) + +struct scree + { + struct chart chart; + const gsl_vector *eval; + const char *xlabel; + }; + +static const struct chart_class scree_class; + +struct scree * +scree_create (const gsl_vector *eigenvalues, const char *xlabel) +{ + struct scree *rc = xmalloc (sizeof *rc); + chart_init (&rc->chart, &scree_class); + rc->eval = eigenvalues; + rc->xlabel = xlabel; + return rc; +} + + +struct chart * +scree_get_chart (struct scree *rc) +{ + return &rc->chart; +} + +static void +scree_draw (const struct chart *chart, cairo_t *cr, + struct chart_geometry *geom) +{ + const struct scree *rc = UP_CAST (chart, struct scree, chart); + size_t i; + double min, max; + + chart_write_title (cr, geom, _("Scree Plot")); + chart_write_xlabel (cr, geom, rc->xlabel); + chart_write_ylabel (cr, geom, _("Eigenvalue")); + + gsl_vector_minmax (rc->eval, &min, &max); + + if ( fabs (max) > fabs (min)) + max = fabs (max); + else + max = fabs (min); + + chart_write_yscale (cr, geom, 0, max, max); + chart_write_xscale (cr, geom, 0, rc->eval->size + 1, rc->eval->size + 1); + + chart_vector_start (cr, geom, ""); + for (i = 0 ; i < rc->eval->size; ++i) + { + const double x = 1 + i; + const double y = gsl_vector_get (rc->eval, i); + chart_vector (cr, geom, x, y); + } + chart_vector_end (cr, geom); + + for (i = 0 ; i < rc->eval->size; ++i) + { + const double x = 1 + i; + const double y = gsl_vector_get (rc->eval, i); + chart_datum (cr, geom, 0, x, y); + } +} + +static void +scree_destroy (struct chart *chart) +{ + struct scree *rc = UP_CAST (chart, struct scree, chart); + + free (rc); +} + +static const struct chart_class scree_class = + { + scree_draw, + scree_destroy + }; + + diff --git a/src/output/charts/scree.h b/src/output/charts/scree.h new file mode 100644 index 00000000..93244998 --- /dev/null +++ b/src/output/charts/scree.h @@ -0,0 +1,31 @@ +/* PSPP - a program for statistical analysis. + Copyright (C) 2009 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 + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + 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 . */ + +#ifndef OUTPUT_CHARTS_SCREE_H +#define OUTPUT_CHARTS_SCREE_H 1 + +#include + +struct scree; +struct chart; + +/* Create a "Scree Plot" of EIGENVALUES with LABEL on the X Axis */ +struct scree *scree_create (const gsl_vector *eigenvalues, const char *label); + +/* Return the chart underlying SCREE */ +struct chart *scree_get_chart (struct scree *scree); + +#endif -- 2.30.2