From 924ffe508027894ba8b71d4696794f992efb1b02 Mon Sep 17 00:00:00 2001 From: Allin Cottrell Date: Thu, 23 Jan 2025 10:57:23 -0500 Subject: [PATCH] a little more on dataset addobs, for panel data in particular --- lib/src/dataset.c | 25 +++++++++++++++++++------ lib/src/gretl_panel.c | 28 ++++++++++++++++++++++++++++ lib/src/gretl_panel.h | 2 ++ 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/lib/src/dataset.c b/lib/src/dataset.c index dd236bd06..6c37b7a1f 100644 --- a/lib/src/dataset.c +++ b/lib/src/dataset.c @@ -985,6 +985,7 @@ static void maybe_extend_dummies (DATASET *dset, int oldn) static void maybe_extend_indices (DATASET *dset, int oldn) { int minval, incr; + int udone = 0; int i, t; for (i=1; iv; i++) { @@ -996,8 +997,16 @@ static void maybe_extend_indices (DATASET *dset, int oldn) dset->Z[i][t] = dset->Z[i][t-1] + incr; } } + } else if (!udone && is_panel_unit_var(dset, i, oldn)) { + for (t=oldn; tn; t++) { + if (t % dset->pd == 0) { + dset->Z[i][t] = dset->Z[i][t-1] + 1; + } else { + dset->Z[i][t] = dset->Z[i][t-1]; + } + } + udone = 1; } - /* FIXME unit index */ } } @@ -3348,23 +3357,27 @@ static int add_obs (DATASET *dset, int n, gretlopt opt, PRN *prn) if (opt & OPT_T) { /* extending the time dimension */ err = panel_dataset_extend_time(dset, n, opt | OPT_A); - if (!err) { + if (!err && gretl_messages_on()) { pprintf(prn, _("Panel time extended by %d observations"), n); pputc(prn, '\n'); } } else { err = real_dataset_add_observations(dset, n * dset->pd, OPT_A); if (!err) { - pprintf(prn, _("Dataset extended by %d units"), n); - pputc(prn, '\n'); + if (gretl_messages_on()) { + pprintf(prn, _("Dataset extended by %d units"), n); + pputc(prn, '\n'); + } extend_function_sample_range(n * dset->pd); } } } else { err = real_dataset_add_observations(dset, n, OPT_A); if (!err) { - pprintf(prn, _("Dataset extended by %d observations"), n); - pputc(prn, '\n'); + if (gretl_messages_on()) { + pprintf(prn, _("Dataset extended by %d observations"), n); + pputc(prn, '\n'); + } extend_function_sample_range(n); } } diff --git a/lib/src/gretl_panel.c b/lib/src/gretl_panel.c index 6caa1301c..a82689920 100644 --- a/lib/src/gretl_panel.c +++ b/lib/src/gretl_panel.c @@ -7335,6 +7335,12 @@ int plausible_panel_time_var (const DATASET *dset) return ret; } +/* Tests whether the series with index number @v codes for the + time dimension of a panel dataset. If so, returns 1 and + records the minimum value and (constant) increment of the + index; otherwise returns 0. +*/ + int is_panel_time_var (const DATASET *dset, int v, int tmax, int *minval, int *incr) @@ -7371,7 +7377,29 @@ int is_panel_time_var (const DATASET *dset, int v, return ret; } +int is_panel_unit_var (const DATASET *dset, int v, int tmax) +{ + const double *x = dset->Z[v]; + int t, ret = 1; + + if (x[0] != 1) { + return 0; + } + + for (t=1; tpd == 0) { + if (x[t] != x[t-1] + 1) { + ret = 0; + } + } else if (x[t] != x[t-1]) { + ret = 0; + } + } + return ret; +} /* FIXME: this does not yet handle the dropping of instruments */ diff --git a/lib/src/gretl_panel.h b/lib/src/gretl_panel.h index 4a9a44e42..10f60f1b8 100644 --- a/lib/src/gretl_panel.h +++ b/lib/src/gretl_panel.h @@ -73,6 +73,8 @@ int is_panel_time_var (const DATASET *dset, int v, int tnax, int *minval, int *incr); +int is_panel_unit_var (const DATASET *dset, int v, int tmax); + int panel_isconst (int t1, int t2, int pd, const double *x, int bygroup);