fd168c435057c401f0bf36410b285d3fdbfe5440
[pspp] / src / ui / gui / psppire-dialog-action-regression.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2010, 2011, 2012  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17
18 #include <config.h>
19
20 #include "psppire-dialog-action-regression.h"
21 #include "psppire-value-entry.h"
22
23 #include "dialog-common.h"
24 #include "helper.h"
25 #include <ui/syntax-gen.h>
26 #include "psppire-var-view.h"
27
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"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38
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
46 enum
47   {
48 #define RG(NAME, LABEL, TOOLTIP) RG_##NAME,
49     REGRESSION_STATS
50 #undef RG
51     N_REGRESSION_STATS
52   };
53
54 enum
55   {
56 #define RG(NAME, LABEL, TOOLTIP) B_RG_##NAME = 1u << RG_##NAME,
57     REGRESSION_STATS
58 #undef RG
59     B_RG_STATS_ALL = (1u << N_REGRESSION_STATS) - 1,
60     B_RG_STATS_DEFAULT = B_RG_ANOVA | B_RG_COEFF | B_RG_R
61   };
62
63 static const struct checkbox_entry_item stats[] =
64   {
65 #define RG(NAME, LABEL, TOOLTIP) {#NAME, LABEL, TOOLTIP},
66     REGRESSION_STATS
67 #undef RG
68   };
69
70 static void
71 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class);
72
73 G_DEFINE_TYPE (PsppireDialogActionRegression, psppire_dialog_action_regression, PSPPIRE_TYPE_DIALOG_ACTION);
74
75 static gboolean
76 dialog_state_valid (gpointer data)
77 {
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));
81
82   GtkTreeIter notused;
83
84   return (gtk_tree_model_get_iter_first (dep_vars, &notused)
85     && gtk_tree_model_get_iter_first (indep_vars, &notused));
86 }
87
88 static void
89 refresh (PsppireDialogAction *rd_)
90 {
91   PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (rd_);
92
93   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dep_vars));
94   gtk_list_store_clear (GTK_LIST_STORE (liststore));
95
96   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
97   gtk_list_store_clear (GTK_LIST_STORE (liststore));
98 }
99
100 static void
101 on_statistics_clicked (PsppireDialogActionRegression *rd)
102 {
103   int ret;
104   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
105
106   /* Take a backup copy of the existing model */
107   GtkListStore *backup_model = clone_list_store (GTK_LIST_STORE (model));
108
109   ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->stat_dialog));
110
111   if ( ret != PSPPIRE_RESPONSE_CONTINUE )
112     {
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));
115     }
116   g_object_unref (backup_model);
117 }
118
119 static void
120 on_save_clicked (PsppireDialogActionRegression *rd)
121 {
122   int ret;
123   if (rd->pred)
124     {
125       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->pred_button), TRUE);
126     }
127   if (rd->resid)
128     {
129       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->resid_button), TRUE);
130     }
131
132   ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->save_dialog));
133
134   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
135     {
136       rd->pred = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->pred_button)) == TRUE)
137         ? TRUE : FALSE;
138       rd->resid = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->resid_button)) == TRUE)
139         ? TRUE : FALSE;
140     }
141 }
142
143
144 static void
145 psppire_dialog_action_regression_activate (GtkAction *a)
146 {
147   PsppireDialogActionRegression *act = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
148   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
149
150   GHashTable *thing = psppire_dialog_action_get_pointer (pda);
151   GtkBuilder *xml = g_hash_table_lookup (thing, a);
152   if (!xml)
153     {
154       xml = builder_new ("regression.ui");
155       g_hash_table_insert (thing, a, xml);
156     }
157
158   GtkWidget *stat_button = get_widget_assert (xml, "stat-button");
159   GtkWidget *save_button = get_widget_assert (xml, "save-button");
160
161   pda->dialog = get_widget_assert   (xml, "regression-dialog");
162   pda->source = get_widget_assert   (xml, "dict-view");
163
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");
171
172   psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->stat_view),
173                                   B_RG_STATS_DEFAULT,
174                                   N_REGRESSION_STATS,
175                                   stats);
176
177   psppire_dialog_action_set_refresh (pda, refresh);
178
179   psppire_dialog_action_set_valid_predicate (pda,
180                                         dialog_state_valid);
181
182   g_signal_connect_swapped (stat_button, "clicked",
183                             G_CALLBACK (on_statistics_clicked),  act);
184
185   g_signal_connect_swapped (save_button, "clicked",
186                             G_CALLBACK (on_save_clicked),  act);
187
188
189   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_regression_parent_class)->activate)
190     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_regression_parent_class)->activate (pda);
191 }
192
193
194
195 static char *
196 generate_syntax (PsppireDialogAction *a)
197 {
198   PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
199   gchar *text = NULL;
200
201   gint i;
202   int n;
203   guint selected;
204   GtkTreeIter iter;
205   gboolean ok;
206
207   GString *string = g_string_new ("REGRESSION");
208
209   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
210
211   g_string_append (string, "\n\t/VARIABLES=");
212   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
213   g_string_append (string, "\n\t/DEPENDENT=");
214   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->dep_vars), 0, string);
215   g_string_append (string, "\n\t/METHOD=ENTER");
216   
217   selected = 0;
218   for (i = 0, ok = gtk_tree_model_get_iter_first (model, &iter); ok; 
219        i++, ok = gtk_tree_model_iter_next (model, &iter))
220     {
221       gboolean toggled;
222       gtk_tree_model_get (model, &iter,
223                           CHECKBOX_COLUMN_SELECTED, &toggled, -1); 
224       if (toggled) 
225         selected |= 1u << i; 
226       else 
227         selected &= ~(1u << i);
228     }
229
230   if (selected)
231     {
232       g_string_append (string, "\n\t/STATISTICS=");
233       n = 0;
234       for (i = 0; i < N_REGRESSION_STATS; i++)
235         if (selected & (1u << i))
236           {
237             if (n++)
238               g_string_append (string, " ");
239             g_string_append (string, stats[i].name);
240           }
241     }
242
243   if (rd->pred || rd->resid)
244     {
245       g_string_append (string, "\n\t/SAVE=");
246       if (rd->pred)
247         g_string_append (string, " PRED");
248       if (rd->resid)
249         g_string_append (string, " RESID");
250     }
251
252   g_string_append (string, ".\n");
253
254   text = string->str;
255
256   g_string_free (string, FALSE);
257
258   return text;
259 }
260
261 static void
262 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class)
263 {
264   psppire_dialog_action_set_activation (class, psppire_dialog_action_regression_activate);
265
266   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
267 }
268
269
270 static void
271 psppire_dialog_action_regression_init (PsppireDialogActionRegression *act)
272 {
273 }
274