Chapter 3. Modes of working
or start up the command-line program gretlcli and consult its help, or consult the Gretl Command
Ifyou run the script when part ofit is highlighted, gretl will only run that portion. Moreover, if you
want to run just the current line, you can do so by pressing Ctrl-Enter.
Clicking the right mouse button in the script editor window produces a pop-up menu. This gives
you the option of executing either the line on which the cursor is located, or the selected region of
the script if there’s a selection in place. If the script is editable, this menu also gives the option of
adding or removing comment markers from the start of the line or lines.
The gretl package includes over 70 “practice” scripts. Most of these relate toRamanathan (2002),
but they may also be used as a free-standingintroduction to scriptingin gretl and to various points
ofeconometrictheory. You can explore the practice ﬁles under“File,Script ﬁles, Practice ﬁle” There
you will ﬁnd a listing of the ﬁles along with a brief description of the points they illustrate and the
data they employ. Open any ﬁle and run it to see the output. Note that long commands in a script
can be broken over two or more lines, using backslash as a continuation character.
You can, if you wish, use the GUI controls and the scripting approach in tandem, exploiting each
method where it oﬀers greater convenience. Here are two suggestions.
Open a data ﬁle in the GUI. Explore the data—generate graphs,run regressions, perform tests.
Then open the Command log, edit out any redundant commands, and save it under a speciﬁc
name. Run the script to generate a single ﬁle containing a concise record of your work.
Start by establishing a new script ﬁle. Type in any commands that may be required to set
up transformations of the data (see the genr command in the Gretl Command Reference).
Typically this sort of thing can be accomplished more eﬃciently via commands assembled
with forethought rather than point-and-click. Then save and run the script: the GUI data
window will be updated accordingly. Now you can carry out further exploration of the data
via the GUI. To revisit the data at a later point, open and rerun the “preparatory” script ﬁrst.
Scripts and data ﬁles
One common way ofdoing econometric research with gretl is asfollows: compose a script; execute
the script; inspect the output; modify the script; run it again—with the last three steps repeated as
many times as necessary. In this context, note that when you open a data ﬁle this clears out most
of gretl’s internal state. It’s therefore probably a good idea to have your script start with an open
command: the data ﬁle will be re-opened each time, and you can be conﬁdent you’re getting“fresh”
One further point should be noted. When you go to open a newdata ﬁle via the graphical interface,
you are always prompted: opening a new data ﬁle will lose any unsaved work, do you really want
to do this? When you execute a script that opens a data ﬁle, however, you are not prompted. The
assumption is that in this case you’re not going to lose any work, because the work is embodied
in the script itself (and it would be annoying to be prompted at each iteration of the work cycle
This means you should be careful if you’ve done work using the graphical interface and then decide
to run a script: the current data ﬁle will be replaced without any questions asked, and it’s your
responsibility to save any changes to your data ﬁrst.
1Thisfeatureisnotuniquetogretl;othereconometricpackagesoﬀer thesamefacility. . However,experienceshows
thatwhile thiscan be remarkably useful, itcan alsolead towriting dinosaurscripts thatare never meantto be executed
all at once, butrather used as a chaotic repository to cherry-pick snippets from. Since gretl allows you to have several
script windows open atthe same time, you may wantto keepyour scripts tidyand reasonably small.
Chapter 3. Modes of working
3.2 Saving script objects
When you estimate a model using point-and-click, the model results are displayed in a separate
window, oﬀering menus which let you perform tests, draw graphs, save data from the model, and
so on. Ordinarily, when you estimate a model using a script you just get a non-interactive printout
of the results. You can, however, arrange for models estimated in a script to be “captured”, so that
you can examine them interactively when the script is ﬁnished. Here is an example of the syntax
for achieving this eﬀect:
Model1 <- ols Ct 0 Yt
That is, you type a name forthe model to be saved under, then a back-pointing“assignment arrow”,
then the model command. The assignment arrow is composed of the less-than sign followed by a
dash; it must be separated by spaces from both the preceding name and the following command.
The name for a saved object may include spaces, but in that case it must be wrapped in double
"Model 1" <- ols Ct 0 Yt
Models saved in this way will appear as icons in the gretl icon view window (see Section3.4) after
the script is executed. In addition, you can arrange to have a named model displayed (in its own
window) automatically as follows:
Again, if the name contains spaces it must be quoted:
The same facility can be used for graphs. For example the following will create a plot of Ct against
Yt, save it under the name “CrossPlot” (it will appear under this name in the icon view window),
and have it displayed:
CrossPlot <- gnuplot Ct Yt
You can also save the output from selected commands as named pieces of text (again, these will
appear in the session icon window, from where you can open them later). For example this com-
mand sends the output from an augmented Dickey–Fuller test to a “text object” named ADF1 and
displays it in a window:
ADF1 <- adf 2 x1
Objectssaved in this way (whether models, graphs or pieces of text output) can be destroyed using
the command .free appended to the name of the object, as in ADF1.free.
3.3 The gretl console
Afurther option is available for your computing convenience. Under gretl’s “Tools” menu you will
ﬁnd the item “Gretl console” (there is also an “open gretl console” button on the toolbar in the
main window). This opens up a window in which you can type commands and execute them one
by one (by pressing the Enter key) interactively. This is essentially the same as gretlcli’s mode of
operation, except that the GUIis updated based on commands executed from the console, enabling
you to work back and forth as you wish.
Chapter 3. Modes of working
In the console, you have “command history”; that is, you can use the up and down arrow keys to
navigate the list of command you have entered to date. You can retrieve, edit and then re-enter a
In console mode, you can create, display and free objects (models, graphs or text) aa described
above for script mode.
3.4 The Session concept
Gretl oﬀers the idea of a “session” as a way of keeping track of your work and revisiting it later.
The basic idea is to provide an iconic space containing various objects pertaining to your current
working session (see Figure3.2). You can add objects (represented by icons) to this space as you
go along. If you save the session, these added objects should be available again if you re-open the
Figure 3.2: Icon view: one model and onegraphhave been addedto the default icons
If you start gretl and open a data set, then select “Icon view” from the View menu, you should see
the basic default set of icons: these give you quick access to information on the data set (if any),
correlation matrix (“Correlations”) and descriptive summary statistics (“Summary”). All of these
are activated by double-clicking the relevant icon. The “Data set” icon is a little more complex:
double-clicking opens up the data in the built-in spreadsheet, but you can also right-click on the
icon for a menu of other actions.
To add a model to the Icon view, ﬁrst estimate it using the Model menu. Then pull down the File
menu in the model window and select “Save to session as icon...” or “Save as icon and close”.
Simply hitting the S key over the model window is a shortcut to the latter action.
To add a graph, ﬁrst create it (under the View menu, “Graph speciﬁed vars”, or via one of gretl’s
other graph-generating commands). Click on the graph window to bring up the graph menu, and
select “Save to session as icon”.
Once a model or graph isadded its icon will appear in the Icon viewwindow. Double-clicking on the
icon redisplays the object, while right-clicking brings up a menu which lets you display or delete
the object. This popup menu also gives you the option of editing graphs.
The model table
In econometric research it is common to estimate several models with a common dependent
variable—the models diﬀering in respect of which independent variables are included, or per-
haps in respect of the estimator used. In this situation it is convenient to present the regression
results in the form of a table, where each column contains the results (coeﬃcient estimates and
standard errors) for a given model, and each row contains the estimates for a given variable across
themodels. Note that some estimation methods are not compatible with the straightforwardmodel
Chapter 3. Modes of working
table format, therefore gretl will not let those models be added to the model table. These methods
include non-linear least squares (nls), generic maximum-likelihood estimators (mle), generic GMM
(gmm), dynamic panel models (dpanel or its predecessor arbond), interval regressions (intreg),
bivariate probit models (biprobit), AR(I)MA models (arima or arma), and(G)ARCH models (garch
In the Icon view window gretl provides a meansof constructingsuch a table (and copyingit in plain
Xor Rich Text Format). The procedure is outlinedbelow. (The model table can also be built
non-interactively, in script mode—see the entry for modeltab in the Gretl Command Reference.)
1. Estimate a model which you wish to include in the table, and in the model display window,
under the File menu, select “Save to session as icon” or “Save as icon and close”.
2. Repeat step 1 for the other models to be included in the table (up to a total of six models).
3. When you are done estimating the models, open the icon view of your gretl session, by se-
lecting “Icon view” under the Viewmenu in the main gretl window, or by clicking the “session
icon view” icon on the gretl toolbar.
4. In the Icon view, there is an icon labeled “Model table”. Decide which model you wish to
appear in the left-most column of the model table and add it to the table, either by dragging
its icon onto the Model table icon, or by right-clicking on the model icon and selecting “Add
to model table” from the pop-up menu.
5. Repeat step4 for theothermodelsyou wish toincludein the table. The secondmodel selected
will appear in the second column from the left, and so on.
6. When you are ﬁnished composing the model table, display it by double-clicking on its icon.
Under the Edit menu in the window which appears, you have the option of copying the table
to the clipboard in various formats.
7. If the ordering of the models in the table is not what you wanted, right-click on the model
table icon and select “Clear table”. Then go back to step 4 above and try again.
Asimple instance of gretl’s model table is shown in Figure3.3.
Figure 3.3: Exampleof model table
Chapter 3. Modes of working
The graph page
The “graph page” icon in the session window oﬀers a means of putting together several graphs
for printing on a single page. This facility will work only if you have the LAT
installed, and are able to generate and view either PDF or PostScript output. The output format
is controlled by your choice of program for compiling T
X ﬁles, which can be found under the
“Programs” tab in the Preferences dialog box(under the “Tools” menu in the main window). Usually
this should be pdﬂatex for PDF output or latex for PostScript. In the latter case you must have a
working set-up for handling PostScript, which will usually include dvips, ghostscript and a viewer
such as gv, ggv or kghostview.
In the Icon view window, you can drag up to eight graphs onto the graph page icon. When you
double-click on the icon (or right-click and select “Display”), a page containing the selected graphs
(in PDF or EPS format) will be composed andopened in your viewer. From there you should be able
to print the page.
To clear the graph page, right-click on its icon and select “Clear”.
As with the model table, it is also possible to manipulate the graph page via commands in script or
console mode—see the entry for the graphpg command in the Gretl Command Reference.
Saving and re-opening sessions
Ifyou create models or graphsthat you think you may wish to re-examine later, then before quitting
gretl select “Session ﬁles, Save session” from the File menu and give a name under which to save
the session. To re-open the session later, either
Start gretl then re-open the session ﬁle by going to the “File, Session ﬁles, Open session”, or
From the command line, type gretl -r sessionﬁle,wheresessionﬁle isthe name under which
the session was saved, or
Drag the icon representing a session ﬁle onto gretl.
4.1 Data ﬁle formats
Gretl has its own native format for data ﬁles. Most users will probably not want to read or write
such ﬁles outside of gretl itself, but occasionally this may be useful and details on the ﬁle formats
are given in AppendixA. The program can also import data from a variety of other formats. In
the GUI program this can be done via the “File, Open Data, User ﬁle” menu—note the drop-down
list of acceptable ﬁle types. In script mode, simply use the open command. The supported import
formats are as follows.
Plain text ﬁles (comma-separatedor “CSV” being the most common type). For details on what
gretl expects of such ﬁles, see Section4.3.
Spreadsheets: MS Excel,Gnumeric and Open Document (ODS). The requirementsfor such ﬁles
are given in Section4.3.
Stata data ﬁles (.dta).
SPSS data ﬁles (.sav).
SAS “xport” ﬁles (.xpt).
Eviews workﬁles (.wf1).
JMulTi data ﬁles.
When you import data from a plain text format, gretl opens a “diagnostic” window, reporting on its
progress in reading the data. If you encounter a problem with ill-formatted data, the messages in
this window should give you a handle on ﬁxing the problem.
Note that gretl has a facility for writing out data in the native formats of GNUR, Octave, JMulTi and
PcGive (see AppendixE). In the GUI client this option is found under the “File, Export data” menu;
in the command-line client use the store command with the appropriate option ﬂag.
For working with large amounts of data gretl is supplied with a database-handling routine. A
database, as opposed to a data ﬁle, is not read directly into the program’s workspace. A database
can contain series of mixed frequencies and sample ranges. You open the database and select
series to import into the working dataset. You can then save those series in a native format data
ﬁle if you wish. Databases can be accessed via the menu item “File, Databases”.
For details on the format of gretl databases, see AppendixA.
Chapter 4. Data ﬁles
Online access to databases
Several gretl databases are available from Wake Forest University. Your computer must be con-
nected to the internet for this option to work. Please see the description of the “data” command
under the Help menu.
Visit the gretldatapagefor details and updates onavailable data.
Foreign database formats
Thanks to Thomas Doan of Estima, who made available the speciﬁcation of the database format
used by RATS 4 (Regression Analysis ofTime Series), gretl can handle such databases—or at least,
asubset of same, namely time-series databases containing monthly and quarterly series.
Gretl can also import data from PcGive databases. These take the form of a pair of ﬁles, one
containing the actual data (with suﬃx .bn7) and one containing supplementary information (.in7).
In addition, gretl oﬀers ODBC connectivity. Be warned: this feature is meant for somewhat ad-
vanced users; there is currently no graphical interface. Interested readers will ﬁnd more info in
4.3 Creating a dataset from scratch
There are several ways of doing this:
1. Find, or create using a text editor, a plain text data ﬁle and open it via “Import”.
2. Use your favorite spreadsheet to establish the data ﬁle, save it in comma-separated format if
necessary (this may not be necessary ifthe spreadsheet format is MS Excel, Gnumeric or Open
Document), then use one of the “Import” options.
3. Use gretl’s built-in spreadsheet.
4. Select data series from a suitable database.
5. Use your favorite text editor or other software tools to a create data ﬁle in gretl format inde-
Here are a few comments and details on these methods.
Common points on imported data
Options (1) and (2) involve using gretl’s “import” mechanism. For the program to read such data
successfully, certain general conditions must be satisﬁed:
The ﬁrst row must contain valid variable names. A valid variable name is of 31 characters
maximum; starts with a letter; and contains nothing but letters, numbers and the underscore
character, _. (Longer variable names will be truncated to 31 characters.) Qualiﬁcations to the
above: First, in the case ofan plain text import, if the ﬁle contains no row with variable names
the program will automatically add names, v1, v2 and so on. Second, by “the ﬁrst row” is
meant the ﬁrst relevant row. In the case of plain text imports, blank rows and rows beginning
with a hash mark, #, are ignored. In the case of Excel, Gnumeric and ODS imports, you are
presented with a dialog box where you can select an oﬀset into the spreadsheet, so that gretl
will ignore a speciﬁed number ofrows and/or columns.
Data values: these should constitute a rectangular block, with one variable per column (and
one observation per row). The number of variables (data columns) must match the number
of variable names given. See also section4.6. Numeric data are expected, but in the case of
Documents you may be interested
Documents you may be interested