1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2010, 2011, 2012 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "psppire-dialog-action-regression.h"
21 #include "psppire-value-entry.h"
23 #include "dialog-common.h"
25 #include <ui/syntax-gen.h>
26 #include "psppire-var-view.h"
28 #include "psppire-dialog.h"
29 #include "builder-wrapper.h"
30 #include "psppire-checkbox-treeview.h"
31 #include "psppire-dict.h"
32 #include "libpspp/str.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
39 #define REGRESSION_STATS \
40 RG (COEFF, N_("Coeff"), N_("Show the regression coefficients")) \
41 RG (CI, N_("Conf. Interval"), N_("Show the confidence interval for the regression coefficients")) \
42 RG (R, N_("R"), N_("Show the correlation between observed and predicted values")) \
43 RG (ANOVA, N_("Anova"), N_("Show the analysis of variance table")) \
44 RG (BCOV, N_("Bcov"), N_("Show the variance coefficient matrix")) \
45 RG (TOL, N_("Tol"), N_("Show the variance inflation factor and its reciprocal"))
49 #define RG(NAME, LABEL, TOOLTIP) RG_##NAME,
57 #define RG(NAME, LABEL, TOOLTIP) B_RG_##NAME = 1u << RG_##NAME,
60 B_RG_STATS_ALL = (1u << N_REGRESSION_STATS) - 1,
61 B_RG_STATS_DEFAULT = B_RG_ANOVA | B_RG_COEFF | B_RG_R
64 static const struct checkbox_entry_item stats[] =
66 #define RG(NAME, LABEL, TOOLTIP) {#NAME, LABEL, TOOLTIP},
72 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class);
74 G_DEFINE_TYPE (PsppireDialogActionRegression, psppire_dialog_action_regression, PSPPIRE_TYPE_DIALOG_ACTION);
77 dialog_state_valid (gpointer data)
79 PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (data);
80 GtkTreeModel *dep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dep_vars));
81 GtkTreeModel *indep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
85 return (gtk_tree_model_get_iter_first (dep_vars, ¬used)
86 && gtk_tree_model_get_iter_first (indep_vars, ¬used));
90 refresh (PsppireDialogAction *rd_)
92 PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (rd_);
94 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dep_vars));
95 gtk_list_store_clear (GTK_LIST_STORE (liststore));
97 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
98 gtk_list_store_clear (GTK_LIST_STORE (liststore));
102 on_statistics_clicked (PsppireDialogActionRegression *rd)
105 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
107 /* Take a backup copy of the existing model */
108 GtkListStore *backup_model = clone_list_store (GTK_LIST_STORE (model));
110 ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->stat_dialog));
112 if (ret != PSPPIRE_RESPONSE_CONTINUE)
114 /* If the user chose to abandon his changes, then replace the model, from the backup */
115 gtk_tree_view_set_model (GTK_TREE_VIEW (rd->stat_view) , GTK_TREE_MODEL (backup_model));
117 g_object_unref (backup_model);
121 on_save_clicked (PsppireDialogActionRegression *rd)
126 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->pred_button), TRUE);
130 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->resid_button), TRUE);
133 ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->save_dialog));
135 if (ret == PSPPIRE_RESPONSE_CONTINUE)
137 rd->pred = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->pred_button)) == TRUE)
139 rd->resid = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->resid_button)) == TRUE)
146 psppire_dialog_action_regression_activate (PsppireDialogAction *a, GVariant *param)
148 PsppireDialogActionRegression *act = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
149 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
151 GtkBuilder *xml = builder_new ("regression.ui");
153 GtkWidget *stat_button = get_widget_assert (xml, "stat-button");
154 GtkWidget *save_button = get_widget_assert (xml, "save-button");
156 pda->dialog = get_widget_assert (xml, "regression-dialog");
157 pda->source = get_widget_assert (xml, "dict-view");
159 act->dep_vars = get_widget_assert (xml, "dep-view");
160 act->indep_vars = get_widget_assert (xml, "indep-view");
161 act->stat_view = get_widget_assert (xml, "stat-view");
162 act->stat_dialog = get_widget_assert (xml, "statistics-dialog");
163 act->save_dialog = get_widget_assert (xml, "save-dialog");
164 act->pred_button = get_widget_assert (xml, "pred-button");
165 act->resid_button = get_widget_assert (xml, "resid-button");
167 psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->stat_view),
172 psppire_dialog_action_set_refresh (pda, refresh);
174 psppire_dialog_action_set_valid_predicate (pda,
177 g_signal_connect_swapped (stat_button, "clicked",
178 G_CALLBACK (on_statistics_clicked), act);
180 g_signal_connect_swapped (save_button, "clicked",
181 G_CALLBACK (on_save_clicked), act);
189 generate_syntax (const PsppireDialogAction *a)
191 PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
200 GString *string = g_string_new ("REGRESSION");
202 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
204 g_string_append (string, "\n\t/VARIABLES=");
205 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
206 g_string_append (string, "\n\t/DEPENDENT=");
207 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->dep_vars), 0, string);
208 g_string_append (string, "\n\t/METHOD=ENTER");
211 for (i = 0, ok = gtk_tree_model_get_iter_first (model, &iter); ok;
212 i++, ok = gtk_tree_model_iter_next (model, &iter))
215 gtk_tree_model_get (model, &iter,
216 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
220 selected &= ~(1u << i);
225 g_string_append (string, "\n\t/STATISTICS=");
227 for (i = 0; i < N_REGRESSION_STATS; i++)
228 if (selected & (1u << i))
231 g_string_append (string, " ");
232 g_string_append (string, stats[i].name);
236 if (rd->pred || rd->resid)
238 g_string_append (string, "\n\t/SAVE=");
240 g_string_append (string, " PRED");
242 g_string_append (string, " RESID");
245 g_string_append (string, ".\n");
249 g_string_free (string, FALSE);
255 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class)
257 PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_regression_activate;
259 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
264 psppire_dialog_action_regression_init (PsppireDialogActionRegression *act)