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"))
48 #define RG(NAME, LABEL, TOOLTIP) RG_##NAME,
56 #define RG(NAME, LABEL, TOOLTIP) B_RG_##NAME = 1u << RG_##NAME,
59 B_RG_STATS_ALL = (1u << N_REGRESSION_STATS) - 1,
60 B_RG_STATS_DEFAULT = B_RG_ANOVA | B_RG_COEFF | B_RG_R
63 static const struct checkbox_entry_item stats[] =
65 #define RG(NAME, LABEL, TOOLTIP) {#NAME, LABEL, TOOLTIP},
71 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class);
73 G_DEFINE_TYPE (PsppireDialogActionRegression, psppire_dialog_action_regression, PSPPIRE_TYPE_DIALOG_ACTION);
76 dialog_state_valid (gpointer data)
78 PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (data);
79 GtkTreeModel *dep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dep_vars));
80 GtkTreeModel *indep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
84 return (gtk_tree_model_get_iter_first (dep_vars, ¬used)
85 && gtk_tree_model_get_iter_first (indep_vars, ¬used));
89 refresh (PsppireDialogAction *rd_)
91 PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (rd_);
93 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dep_vars));
94 gtk_list_store_clear (GTK_LIST_STORE (liststore));
96 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
97 gtk_list_store_clear (GTK_LIST_STORE (liststore));
101 on_statistics_clicked (PsppireDialogActionRegression *rd)
104 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
106 /* Take a backup copy of the existing model */
107 GtkListStore *backup_model = clone_list_store (GTK_LIST_STORE (model));
109 ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->stat_dialog));
111 if ( ret != PSPPIRE_RESPONSE_CONTINUE )
113 /* If the user chose to abandon his changes, then replace the model, from the backup */
114 gtk_tree_view_set_model (GTK_TREE_VIEW (rd->stat_view) , GTK_TREE_MODEL (backup_model));
116 g_object_unref (backup_model);
120 on_save_clicked (PsppireDialogActionRegression *rd)
125 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->pred_button), TRUE);
129 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->resid_button), TRUE);
132 ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->save_dialog));
134 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
136 rd->pred = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->pred_button)) == TRUE)
138 rd->resid = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->resid_button)) == TRUE)
145 psppire_dialog_action_regression_activate (PsppireDialogAction *a)
147 PsppireDialogActionRegression *act = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
148 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
150 GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
151 GtkBuilder *xml = g_hash_table_lookup (thing, a);
154 xml = builder_new ("regression.ui");
155 g_hash_table_insert (thing, a, xml);
158 GtkWidget *stat_button = get_widget_assert (xml, "stat-button");
159 GtkWidget *save_button = get_widget_assert (xml, "save-button");
161 pda->dialog = get_widget_assert (xml, "regression-dialog");
162 pda->source = get_widget_assert (xml, "dict-view");
164 act->dep_vars = get_widget_assert (xml, "dep-view");
165 act->indep_vars = get_widget_assert (xml, "indep-view");
166 act->stat_view = get_widget_assert (xml, "stat-view");
167 act->stat_dialog = get_widget_assert (xml, "statistics-dialog");
168 act->save_dialog = get_widget_assert (xml, "save-dialog");
169 act->pred_button = get_widget_assert (xml, "pred-button");
170 act->resid_button = get_widget_assert (xml, "resid-button");
172 psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->stat_view),
177 psppire_dialog_action_set_refresh (pda, refresh);
179 psppire_dialog_action_set_valid_predicate (pda,
182 g_signal_connect_swapped (stat_button, "clicked",
183 G_CALLBACK (on_statistics_clicked), act);
185 g_signal_connect_swapped (save_button, "clicked",
186 G_CALLBACK (on_save_clicked), act);
194 generate_syntax (const PsppireDialogAction *a)
196 PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
205 GString *string = g_string_new ("REGRESSION");
207 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
209 g_string_append (string, "\n\t/VARIABLES=");
210 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
211 g_string_append (string, "\n\t/DEPENDENT=");
212 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->dep_vars), 0, string);
213 g_string_append (string, "\n\t/METHOD=ENTER");
216 for (i = 0, ok = gtk_tree_model_get_iter_first (model, &iter); ok;
217 i++, ok = gtk_tree_model_iter_next (model, &iter))
220 gtk_tree_model_get (model, &iter,
221 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
225 selected &= ~(1u << i);
230 g_string_append (string, "\n\t/STATISTICS=");
232 for (i = 0; i < N_REGRESSION_STATS; i++)
233 if (selected & (1u << i))
236 g_string_append (string, " ");
237 g_string_append (string, stats[i].name);
241 if (rd->pred || rd->resid)
243 g_string_append (string, "\n\t/SAVE=");
245 g_string_append (string, " PRED");
247 g_string_append (string, " RESID");
250 g_string_append (string, ".\n");
254 g_string_free (string, FALSE);
260 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class)
262 psppire_dialog_action_set_activation (class, psppire_dialog_action_regression_activate);
264 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
269 psppire_dialog_action_regression_init (PsppireDialogActionRegression *act)