On Thu, 22 Oct 2020, ri...@triton.net wrote:

Hello Allin

Below you will find a complete compilable cpp program that demonstrates that the corrgram() function does not plot(at least on my machine) the ACF and PACF when called through the c/c++ api. I use the dataset from /share/gretl/data/ramanathan/data9-7.gdt . The only library that this program is linking to is gretl-1.0.

Thanks, Riley. I'm attaching a modified version which displays the
plot.

The main point is that when you call libgretl independently the paths to third-party programs such as gnuplot are not registered, so it's necessary to call gretl_set_path_by_name() to tell the library where to find gnuplot.

I've also modified the file to illustrate (a) two methods of generating the plot, and (b) how to use the residual from a model as the series to work on. Hopefully these points should be reasonably self-explanatory but feel free to come back with questions.

Allin
#include <gretl/libgretl.h>

int arma_estimate (DATASET *dset, PRN *prn, int *resid_id)
{
    MODEL *model;
    int *list;
    int err;

    model = gretl_model_new();
    list = gretl_list_new(5);

    list[1] = 1;        /* AR order */
    list[2] = 0;        /* order of integration */
    list[3] = 1;        /* MA order */
    list[4] = LISTSEP;  /* separator */
    list[5] = 1;        /* position of dependent variable in dataset */

    *model = arma(list, NULL, dset, OPT_NONE, prn);
    err = model->errcode;

    if (err) {
        errmsg(err, prn);
    } else {
        printmodel(model, dset, OPT_NONE, prn);
    }

    if (!err) {
        /* save arma residual series? */
        int v;

        dataset_add_allocated_series(dset, model->uhat);
        v = dset->v - 1 ; /* ID number of last series */
        strcpy(dset->varname[v], "residual");
        *resid_id = v;
        model->uhat = NULL; /* don't double free! */
    }

    gretl_model_free(model);
    free(list);

    return err;
}

int CorrgramGretl (int varno, int order, int nparam, DATASET *dset,
                   gretlopt opt, PRN *prn)
{
    int err;

    err = corrgram(varno, order, nparam, dset, opt, prn);
    if (err) {
        errmsg(err, prn);
    }

    return err;
}

int main (void)
{
    int use_matrix = 1;   /* or 0 */
    int use_residual = 1; /* or 0 */
    int vnum;             /* series ID for correlogram */
    gretlopt opt = OPT_NONE;
    DATASET *dset;
    PRN *prn;
    int err;

    libgretl_init();

    /* tell libgretl where to find gnuplot */
    gretl_set_path_by_name("gnuplot", "/usr/bin/gnuplot");

    dset = datainfo_new();
    prn = gretl_print_new(GRETL_PRINT_STDOUT, NULL);

    err = gretl_read_native_data("data9-7.gdt", dset);

    if (!err && use_residual) {
        opt = OPT_R;
        err = arma_estimate(dset, prn, &vnum);
    } else {
        vnum = 2; /* or whatever */
    }

    if (err) {
        errmsg(err, prn);
        exit(EXIT_FAILURE);
    }   

    int order = 7; /* or whatever */

    if (use_matrix) {
        /* call correlogram_plot() directly */
        gretl_matrix *cmat;

        cmat = acf_matrix(dset->Z[vnum], order, dset, dset->n, &err);

        if (!err) {
            /* take pointers into the two @cmat columns */
            const double *acf = cmat->val;
            const double *pacf = acf + cmat->rows;
            /* set the plus/minus range */
            double pm = 1.96 / sqrt(dset->n);

            correlogram_plot(dset->varname[vnum], acf, pacf, NULL,
                             order, pm, opt);
        }
        gretl_matrix_free(cmat);
    } else {
        /* emulate "command"-style call */
        opt |= OPT_U; /* add --plot=<whatever> */
        /* supply the parameter to --plot */
        set_optval_string(CORRGM, OPT_U, "display");
        err = CorrgramGretl(vnum, order, 1, dset, opt, prn);
    }

    destroy_dataset(dset);
    gretl_print_destroy(prn);

    libgretl_cleanup();

    return 0;
}
_______________________________________________
Gretl-users mailing list -- gretl-users@gretlml.univpm.it
To unsubscribe send an email to gretl-users-le...@gretlml.univpm.it
Website: 
https://gretlml.univpm.it/postorius/lists/gretl-users.gretlml.univpm.it/

Reply via email to