1 /*
2  *  gretl -- Gnu Regression, Econometrics and Time-series Library
3  *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 /* Trivial sample client program for libgretl */
21 
22 #include <gretl/libgretl.h>
23 
noalloc(void)24 void noalloc (void)
25 {
26     printf("Couldn't allocate memory.\n");
27     exit(EXIT_FAILURE);
28 }
29 
main(void)30 int main (void)
31 {
32 
33     DATASET *dataset;       /* pointer to dataset struct */
34     int *list;              /* list of regressors etc. */
35     MODEL *model;           /* pointer to model struct */
36     PRN *prn;               /* pointer to struct for printing */
37     int model_count = 0;    /* keep a tally of models estimated */
38     int i;                  /* index variable */
39 
40     /* the first data series to load */
41     double z1[] = {
42 	199.9, 228, 235, 285, 239, 293, 285,
43 	365, 295, 290, 385, 505, 425, 415
44     };
45     /* and the second series */
46     double z2[] = {
47 	1065, 1254, 1300, 1577, 1600, 1750, 1800,
48 	1870, 1935, 1948, 2254, 2600, 2800, 3000
49     };
50 
51     /* basic initialization of library */
52     libgretl_init();
53 
54     prn = gretl_print_new(GRETL_PRINT_STDOUT, NULL); /* simple printing */
55 
56     /* allocate the dataset struct: the parameters are the number of
57        variables (here 3, allowing for the constant in position 0),
58        the number of observations on each variable (here 14), and
59        a 0/1 flag indicating whether we want to supply "case marker"
60        strings for the observations (here we don't).
61     */
62     dataset = create_new_dataset(3, 14, 0);
63     if (dataset == NULL) noalloc();
64 
65     /* copy in the names of the variables (starting at [1]
66        because [0] refers to the constant) */
67     strcpy(dataset->varname[1], "price");
68     strcpy(dataset->varname[2], "sqft");
69 
70     /* Fill in the data array, starting at variable 1. Note that
71        this array may be a superset of the data actually used in
72        the regression equation. Note that dataset->n records the
73        number of observations.
74     */
75 
76     for (i=0; i<dataset->n; i++) {
77 	dset_set_data(dataset, 1, i, z1[i]);
78 	dset_set_data(dataset, 2, i, z2[i]);
79     }
80 
81     /* Set up the "list", which is fed to the regression function.
82        The first element of list represents the length of the list
83        vector itself, counting from zero.  The second entry is the ID
84        number of the dependent variable (i.e. its place in the data
85        set Z) counting from one (zero being reserved for the
86        constant).  The third entry (and there can be more) is the ID
87        number of the first independent variable.
88     */
89 
90     list = gretl_list_new(3); /* number of terms will be 3 */
91     list[1] = 1;   /* the dependent variable is the one with ID# 1 */
92     list[2] = 0;   /* we include a constant (ID# 0) */
93     list[3] = 2;   /* the independent variable has ID# 2 */
94 
95     /* Now we call the lsq function from libgretl to get least squares
96        estimates and associated statistics. */
97     model = gretl_model_new();
98     if (model == NULL) noalloc();
99     *model = lsq(list,     /* regressand and regressors */
100 		 dataset,  /* the dataset */
101 		 OLS,      /* use Ordinary Least Squares */
102 		 OPT_NONE  /* no special options */
103 		 );
104 
105     /* Handle case where lsq bombed */
106     if (model->errcode) {
107         printf("model->errcode: %d\n", model->errcode);
108         printf("error message: %s\n", gretl_errmsg_get());
109         return 1;
110     }
111 
112     /* Otherwise give this model an ID number for reference */
113     model->ID = ++model_count;
114 
115     /* and print the regression results */
116     printmodel(model, dataset, OPT_NONE, prn);
117 
118     /* memory management check -- try explicitly freeing all allocated
119        memory */
120     gretl_model_free(model);
121     free(list);
122     destroy_dataset(dataset);
123     gretl_print_destroy(prn);
124 
125     libgretl_cleanup();
126 
127     return 0;
128 }
129 
130