183301d02f01d006212bd7376c4f9663e483f422
[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 (PsppireDialogAction *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_hash_table (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 }
190
191
192
193 static char *
194 generate_syntax (const PsppireDialogAction *a)
195 {
196   PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
197   gchar *text = NULL;
198
199   gint i;
200   int n;
201   guint selected;
202   GtkTreeIter iter;
203   gboolean ok;
204
205   GString *string = g_string_new ("REGRESSION");
206
207   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
208
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");
214   
215   selected = 0;
216   for (i = 0, ok = gtk_tree_model_get_iter_first (model, &iter); ok; 
217        i++, ok = gtk_tree_model_iter_next (model, &iter))
218     {
219       gboolean toggled;
220       gtk_tree_model_get (model, &iter,
221                           CHECKBOX_COLUMN_SELECTED, &toggled, -1); 
222       if (toggled) 
223         selected |= 1u << i; 
224       else 
225         selected &= ~(1u << i);
226     }
227
228   if (selected)
229     {
230       g_string_append (string, "\n\t/STATISTICS=");
231       n = 0;
232       for (i = 0; i < N_REGRESSION_STATS; i++)
233         if (selected & (1u << i))
234           {
235             if (n++)
236               g_string_append (string, " ");
237             g_string_append (string, stats[i].name);
238           }
239     }
240
241   if (rd->pred || rd->resid)
242     {
243       g_string_append (string, "\n\t/SAVE=");
244       if (rd->pred)
245         g_string_append (string, " PRED");
246       if (rd->resid)
247         g_string_append (string, " RESID");
248     }
249
250   g_string_append (string, ".\n");
251
252   text = string->str;
253
254   g_string_free (string, FALSE);
255
256   return text;
257 }
258
259 static void
260 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class)
261 {
262   psppire_dialog_action_set_activation (class, psppire_dialog_action_regression_activate);
263
264   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
265 }
266
267
268 static void
269 psppire_dialog_action_regression_init (PsppireDialogActionRegression *act)
270 {
271 }
272