1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
/*
* gretl -- Gnu Regression, Econometrics and Time-series Library
* Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "libgretl.h"
#include "version.h"
#include "gretl_matrix.h"
#include <gtk/gtk.h>
#if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 18)
# include "gtk_compat.h"
#endif
#define PCA_DEBUG 0
struct flag_info {
GtkWidget *dialog;
gint *flag;
};
enum pca_flags {
PCA_SAVE_NONE,
PCA_SAVE_MAIN,
PCA_SAVE_ALL
};
static gboolean destroy_pca_dialog (GtkWidget *w, struct flag_info *finfo)
{
free(finfo);
gtk_main_quit();
return FALSE;
}
static gboolean set_pca_flag (GtkWidget *w, struct flag_info *finfo)
{
gint opt = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w), "opt"));
*(finfo->flag) = opt;
return FALSE;
}
static gboolean cancel_set_flag (GtkWidget *w, struct flag_info *finfo)
{
*(finfo->flag) = PCA_SAVE_NONE;
gtk_widget_destroy(finfo->dialog);
return FALSE;
}
static gboolean pca_dialog_finalize (GtkWidget *w, struct flag_info *finfo)
{
gtk_widget_destroy(finfo->dialog);
return FALSE;
}
static gretlopt pca_flag_dialog (void)
{
struct flag_info *finfo;
GtkWidget *dialog, *tmp, *button, *hbox;
GtkWidget *vbox, *internal_vbox;
GSList *group;
gint flag = PCA_SAVE_MAIN;
finfo = malloc(sizeof *finfo);
if (finfo == NULL) return 0;
dialog = gtk_dialog_new();
finfo->dialog = dialog;
finfo->flag = &flag;
vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
gtk_window_set_title(GTK_WINDOW(dialog), _("gretl: save data"));
gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
gtk_box_set_spacing(GTK_BOX(vbox), 5);
gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
g_signal_connect(G_OBJECT(dialog), "destroy",
G_CALLBACK(destroy_pca_dialog), finfo);
internal_vbox = gtk_vbox_new(FALSE, 5);
hbox = gtk_hbox_new(FALSE, 5);
tmp = gtk_label_new (_("Variables to save:"));
gtk_box_pack_start(GTK_BOX(hbox), tmp, TRUE, TRUE, 5);
gtk_box_pack_start(GTK_BOX(internal_vbox), hbox, TRUE, TRUE, 5);
/* Only those with eigenvalues > 1.0 */
button = gtk_radio_button_new_with_label(NULL,
_("Components with eigenvalues > mean"));
gtk_box_pack_start(GTK_BOX(internal_vbox), button, TRUE, TRUE, 0);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(set_pca_flag), finfo);
g_object_set_data(G_OBJECT(button), "opt", GINT_TO_POINTER(PCA_SAVE_MAIN));
/* All components */
group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (button));
button = gtk_radio_button_new_with_label(group, _("All components"));
gtk_box_pack_start (GTK_BOX(internal_vbox), button, TRUE, TRUE, 0);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), FALSE);
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(set_pca_flag), finfo);
g_object_set_data(G_OBJECT(button), "opt", GINT_TO_POINTER(PCA_SAVE_ALL));
hbox = gtk_hbox_new(FALSE, 5);
gtk_box_pack_start(GTK_BOX(hbox), internal_vbox, TRUE, TRUE, 5);
gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5);
hbox = gtk_dialog_get_action_area(GTK_DIALOG(dialog));
gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
gtk_box_set_spacing(GTK_BOX(hbox), 10);
/* Cancel button */
tmp = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
gtk_container_add(GTK_CONTAINER(hbox), tmp);
g_signal_connect(G_OBJECT(tmp), "clicked",
G_CALLBACK(cancel_set_flag), finfo);
/* "OK" button */
tmp = gtk_button_new_from_stock(GTK_STOCK_OK);
gtk_container_add(GTK_CONTAINER(hbox), tmp);
g_signal_connect(G_OBJECT(tmp), "clicked",
G_CALLBACK(pca_dialog_finalize), finfo);
gtk_widget_set_can_default(tmp, TRUE);
gtk_widget_grab_default(tmp);
gtk_widget_show_all(dialog);
gtk_main();
if (flag == PCA_SAVE_MAIN) {
return OPT_O;
} else if (flag == PCA_SAVE_ALL) {
return OPT_A;
} else {
return OPT_NONE;
}
}
#define PCA_COLS 7
static void pca_print (VMatrix *cmat, gretl_matrix *E,
gretl_matrix *C, PRN *prn)
{
double cum, esum;
char pcname[8];
int nl, namelen = 8;
int n = cmat->dim;
int done, todo;
int i, j;
pprintf(prn, "%s\n", _("Principal Components Analysis"));
pprintf(prn, "n = %d", cmat->n);
if (cmat->missing > 0) {
pprintf(prn, " (dropped %d incomplete observations)",
cmat->missing);
}
pputs(prn, "\n\n");
if (cmat->ci == CORR) {
pprintf(prn, "%s\n\n", _("Eigenanalysis of the Correlation Matrix"));
} else {
pprintf(prn, "%s\n\n", _("Eigenanalysis of the Covariance Matrix"));
}
pputs(prn, _("Component Eigenvalue Proportion Cumulative\n"));
if (cmat->ci == CORR) {
esum = n;
} else {
esum = 0.0;
for (i=0; i<n; i++) {
esum += E->val[i];
}
}
cum = 0.0;
for (i=0; i<n; i++) {
cum += E->val[i] / esum;
pprintf(prn, "%5d%13.4f%13.4f%13.4f\n", i + 1,
E->val[i], E->val[i] / esum, cum);
nl = strlen(cmat->names[i]);
if (nl > namelen) {
namelen = nl;
}
}
pputc(prn, '\n');
pprintf(prn, "%s\n\n", _("Eigenvectors (component loadings)"));
done = 0;
todo = n;
while (todo > 0) {
int ncols = todo > PCA_COLS ? PCA_COLS : todo;
pprintf(prn, "%-*s", namelen + 1, " ");
for (j=0; j<ncols; j++) {
sprintf(pcname, "PC%d", done + j + 1);
pprintf(prn, "%9s", pcname);
}
pputc(prn, '\n');
for (i=0; i<n; i++) {
pprintf(prn, "%-*s", namelen + 1, cmat->names[i]);
for (j=0; j<ncols; j++) {
pprintf(prn, "%9.3f", gretl_matrix_get(C, i, done + j));
}
pputc(prn, '\n');
}
pputc(prn, '\n');
todo -= ncols;
done += ncols;
}
}
static int standardize (double *sy, const double *y,
VMatrix *v, int i)
{
double sd;
int t;
if (v->xbar == NULL || v->ssx == NULL) {
/* "can't happen" */
return E_DATA;
}
sd = sqrt(v->ssx[i] / (v->n - 1));
for (t=0; t<v->n; t++) {
sy[t] = (y[t] - v->xbar[i]) / sd;
}
return 0;
}
/* Add components to the dataset, either "major" ones (eigenvalues
greater than 1.0), or a specified number (if @nsave > 0), or
all of them.
*/
static int pca_save_components (VMatrix *cmat,
gretl_matrix *E,
gretl_matrix *C,
DATASET *dset,
int nsave,
gretlopt opt)
{
int save_all = (opt & OPT_A);
double **sX = NULL;
int m = 0, v = dset->v;
int k = cmat->dim;
int i, j, t, vi;
int err = 0;
if (save_all) {
m = k;
} else if (nsave > 0) {
m = nsave > k ? k : nsave;
} else {
for (i=0; E->val[i] > 1.0; i++) {
m++;
}
}
/* Note that cmat->t1 and cmat->t2 may differ from
dset->t1 and dset->t2 if the PCA sample was
restricted by the presence of NAs. Here it's
the cmat values that should be used.
*/
err = dataset_add_series(dset, m);
if (!err) {
/* construct standardized versions of all variables */
double *xi;
sX = doubles_array_new(k, cmat->n);
if (sX == NULL) {
err = E_ALLOC;
} else {
for (i=0; i<k && !err; i++) {
vi = cmat->list[i+1];
xi = dset->Z[vi] + cmat->t1;
err = standardize(sX[i], xi, cmat, i);
}
}
}
if (!err) {
gchar *label;
double x, load;
int s;
for (i=0; i<m; i++) {
vi = v + i;
sprintf(dset->varname[vi], "PC%d", i+1);
make_varname_unique(dset->varname[vi], vi, dset);
label = g_strdup_printf(_("Component with eigenvalue = %.4f"),
E->val[i]);
series_set_label(dset, vi, label);
g_free(label);
s = 0;
for (t=0; t<dset->n; t++) {
if (t < cmat->t1 || t > cmat->t2) {
dset->Z[vi][t] = NADBL;
continue;
}
dset->Z[vi][t] = 0.0;
for (j=0; j<k; j++) {
x = sX[j][s];
if (na(x)) {
dset->Z[vi][t] = NADBL;
break;
} else {
load = gretl_matrix_get(C, j, i);
dset->Z[vi][t] += load * x;
}
}
s++;
}
}
}
doubles_array_free(sX, k);
return err;
}
/* The incoming options here:
CLI: the option may be OPT_O (save the first XX components) or
OPT_A (save all the components). OPT_Q suppresses printing of
the results.
GUI: no option (simply display the results) or OPT_D. The latter
means that we should not display the results, but should put up a
dialog box allowing the user to decide what to save.
Note that depending on the original option supplied to the pca
command, the incoming matrix may be either a correlation matrix
(the default) or a covariance matrix. This prior option is not
included in the @opt passed here; rather it's encoded in the ci
member of the VMatrix struct.
*/
int pca_from_cmatrix (VMatrix *cmat, DATASET *dset,
gretlopt opt, PRN *prn)
{
gretl_matrix *C;
gretl_matrix *evals = NULL;
gretlopt saveopt = opt;
int k = cmat->dim;
int nsave = 0;
int i, j, idx;
double x;
int err = 0;
if (opt & OPT_D) {
saveopt = pca_flag_dialog();
if (saveopt == OPT_NONE) {
/* canceled */
return 0;
}
} else if (opt & OPT_O) {
nsave = get_optval_int(PCA, OPT_O, &err);
if (err) {
return err;
}
}
C = gretl_matrix_alloc(k, k);
if (C == NULL) {
return E_ALLOC;
}
for (i=0; i<k; i++) {
for (j=0; j<k; j++) {
idx = ijton(i, j, k);
x = cmat->vec[idx];
gretl_matrix_set(C, i, j, x);
}
}
#if PCA_DEBUG
gretl_matrix_print(C, "original C, in pca");
#endif
evals = gretl_symm_matrix_eigenvals_descending(C, 1, &err);
#if PCA_DEBUG
gretl_matrix_print(C, "revised C (eigenvecs)");
gretl_matrix_print(evals, "eigenvalues");
#endif
if (!err && !(opt & OPT_Q) && prn != NULL) {
pca_print(cmat, evals, C, prn);
}
if (!err && saveopt) {
err = pca_save_components(cmat, evals, C, dset, nsave,
saveopt);
}
gretl_matrix_free(evals);
gretl_matrix_free(C);
return err;
}
|