Logistical Regression Dialog: Automatically mark appropriate variables as categorical
[pspp] / src / ui / gui / psppire-dialog-action-logistic.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-logistic.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-dict.h"
31 #include "libpspp/str.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35 #define N_(msgid) msgid
36
37
38 static void
39 psppire_dialog_action_logistic_class_init (PsppireDialogActionLogisticClass *class);
40
41 G_DEFINE_TYPE (PsppireDialogActionLogistic, psppire_dialog_action_logistic, PSPPIRE_TYPE_DIALOG_ACTION);
42
43 static gboolean
44 dialog_state_valid (gpointer data)
45 {
46   PsppireDialogActionLogistic *rd = PSPPIRE_DIALOG_ACTION_LOGISTIC (data);
47
48   const gchar *text = gtk_entry_get_text (GTK_ENTRY (rd->dep_var));
49
50   GtkTreeModel *indep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
51
52   GtkTreeIter notused;
53
54   return 0 != strcmp ("", text) &&
55     gtk_tree_model_get_iter_first (indep_vars, &notused);
56 }
57
58 static void
59 refresh (PsppireDialogAction *rd_)
60 {
61   PsppireDialogActionLogistic *rd = PSPPIRE_DIALOG_ACTION_LOGISTIC (rd_);
62
63   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
64   gtk_list_store_clear (GTK_LIST_STORE (liststore));
65
66   gtk_entry_set_text (GTK_ENTRY (rd->dep_var), "");
67 }
68
69
70 static void
71 on_opts_clicked (PsppireDialogActionLogistic *act)
72 {
73   int ret;
74
75   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(act->conf_checkbox), act->conf);
76   gtk_spin_button_set_value (GTK_SPIN_BUTTON (act->conf_entry), act->conf_level);
77   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(act->const_checkbox), act->constant);
78
79   gtk_spin_button_set_value (GTK_SPIN_BUTTON (act->cut_point_entry), act->cut_point);
80   gtk_spin_button_set_value (GTK_SPIN_BUTTON (act->iterations_entry), act->max_iterations);
81
82   
83   ret = psppire_dialog_run (PSPPIRE_DIALOG (act->opts_dialog));
84
85   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
86     {
87       act->conf = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(act->conf_checkbox));
88       act->conf_level = gtk_spin_button_get_value (GTK_SPIN_BUTTON (act->conf_entry));
89       
90       act->constant = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(act->const_checkbox));
91
92       act->cut_point = gtk_spin_button_get_value (GTK_SPIN_BUTTON (act->cut_point_entry));
93       act->max_iterations = gtk_spin_button_get_value (GTK_SPIN_BUTTON (act->iterations_entry));
94     }
95 }
96
97
98 static void
99 psppire_dialog_action_logistic_activate (PsppireDialogAction *a)
100 {
101   PsppireDialogActionLogistic *act = PSPPIRE_DIALOG_ACTION_LOGISTIC (a);
102   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
103   GtkWidget *opts_button;
104
105   GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
106   GtkBuilder *xml = g_hash_table_lookup (thing, a);
107   if (!xml)
108     {
109       xml = builder_new ("logistic.ui");
110       g_hash_table_insert (thing, a, xml);
111     }
112
113   pda->dialog = get_widget_assert   (xml, "logistic-dialog");
114   pda->source = get_widget_assert   (xml, "dict-view");
115   act->cut_point = 0.5;
116   act->max_iterations = 20;
117   act->constant = true;
118   act->conf = false;
119   act->conf_level = 95;
120
121   act->dep_var  = get_widget_assert   (xml, "dependent-entry");
122   act->indep_vars  = get_widget_assert   (xml, "indep-view");
123   act->opts_dialog = get_widget_assert (xml, "options-dialog");
124   act->conf_checkbox = get_widget_assert (xml, "checkbutton2");
125   act->conf_entry = get_widget_assert (xml, "spinbutton1");
126   act->const_checkbox = get_widget_assert (xml, "checkbutton1");
127
128   act->iterations_entry = get_widget_assert (xml, "spinbutton3");
129   act->cut_point_entry = get_widget_assert (xml, "spinbutton2");
130
131   opts_button = get_widget_assert (xml, "options-button");
132
133   g_signal_connect_swapped (opts_button, "clicked",
134                             G_CALLBACK (on_opts_clicked),  act);
135
136   g_signal_connect (act->conf_checkbox, "toggled",
137                     G_CALLBACK (set_sensitivity_from_toggle),  
138                     act->conf_entry);
139
140   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(act->conf_checkbox), TRUE);
141   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(act->conf_checkbox), FALSE);
142
143   psppire_dialog_action_set_refresh (pda, refresh);
144
145   psppire_dialog_action_set_valid_predicate (pda,
146                                         dialog_state_valid);
147
148   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_logistic_parent_class)->activate)
149     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_logistic_parent_class)->activate (pda);
150 }
151
152
153
154 static char *
155 generate_syntax (PsppireDialogAction *a)
156 {
157   PsppireDialogActionLogistic *rd = PSPPIRE_DIALOG_ACTION_LOGISTIC (a);
158   gchar *text = NULL;
159
160   const gchar *dep = gtk_entry_get_text (GTK_ENTRY (rd->dep_var));
161
162   GString *strx = g_string_new ("LOGISTIC REGRESSION ");
163
164   g_string_append (strx, dep);
165
166   g_string_append (strx, " WITH");
167
168   GSList *vars = psppire_var_view_list_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0);
169   GSList *node = vars;
170
171   GString *var_names = g_string_new ("");
172   while (node)
173     {
174       g_string_prepend (var_names, var_get_name (node->data));
175       g_string_prepend (var_names, " ");
176       node = node->next;
177     }
178
179   g_string_append (strx, var_names->str);
180   g_string_free (var_names, TRUE);
181
182
183   GString *categoricals = g_string_new ("");
184   for (node = vars; node; node = node->next)
185     {
186       const struct variable *v = node->data;
187       enum measure m = var_get_measure (v);
188
189       if (m == MEASURE_NOMINAL || m == MEASURE_ORDINAL || var_is_alpha (v))
190         {
191           g_string_prepend (categoricals, var_get_name (v));
192           g_string_prepend (categoricals, " ");
193         }
194     }
195   if (0 != strcmp (categoricals->str, ""))
196     g_string_prepend (categoricals, "\n\t/CATEGORICAL =");
197
198   g_string_append (strx, categoricals->str);
199   g_string_free (categoricals, TRUE);
200   g_slist_free (vars);
201   
202   g_string_append (strx, "\n\t/CRITERIA =");
203
204   g_string_append_printf (strx, " CUT(%g)", rd->cut_point);
205
206   g_string_append_printf (strx, " ITERATE(%d)", rd->max_iterations);
207
208   if (rd->conf)
209     {
210       g_string_append_printf (strx, "\n\t/PRINT = CI(%g)", rd->conf_level);
211     }
212
213   if (rd->constant) 
214     g_string_append (strx, "\n\t/NOORIGIN");
215   else
216     g_string_append (strx, "\n\t/ORIGIN");
217
218   g_string_append (strx, ".\n");
219
220   text = strx->str;
221
222   g_string_free (strx, FALSE);
223
224   return text;
225 }
226
227 static void
228 psppire_dialog_action_logistic_class_init (PsppireDialogActionLogisticClass *class)
229 {
230   psppire_dialog_action_set_activation (class, psppire_dialog_action_logistic_activate);
231
232   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
233 }
234
235
236 static void
237 psppire_dialog_action_logistic_init (PsppireDialogActionLogistic *act)
238 {
239 }
240