Skip to content

Commit

Permalink
a little more on dataset addobs, for panel data in particular
Browse files Browse the repository at this point in the history
  • Loading branch information
AllinCottrell committed Jan 23, 2025
1 parent 4a5895f commit 924ffe5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/src/dataset.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; i<dset->v; i++) {
Expand All @@ -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; t<dset->n; 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 */
}
}

Expand Down Expand Up @@ -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);
}
}
Expand Down
28 changes: 28 additions & 0 deletions lib/src/gretl_panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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; t<tmax && ret; t++) {
if (na(x[t]) || x[t] < 0 || x[t] != floor(x[t])) {
ret = 0;
} else if (t % dset->pd == 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 */

Expand Down
2 changes: 2 additions & 0 deletions lib/src/gretl_panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 924ffe5

Please sign in to comment.