File: persistent.tex

package info (click to toggle)
gretl 2016d-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 48,620 kB
  • ctags: 22,779
  • sloc: ansic: 345,830; sh: 4,648; makefile: 2,712; xml: 570; perl: 364
file content (667 lines) | stat: -rw-r--r-- 21,869 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
\chapter{Named lists and strings}
\label{chap:persistent}


\section{Named lists}
\label{named-lists}

Many gretl commands take one or more lists of series as
arguments.  To make this easier to handle in the context of command
scripts, and in particular within user-defined functions, gretl
offers the possibility of \textit{named lists}.  

\subsection{Creating and modifying named lists}

A named list is created using the keyword \texttt{list}, followed by
the name of the list, an equals sign, and an expression that forms a
list.  The most basic sort of expression that works in this context
is a space-separated list of variables, given either by name or
by ID number.  For example,
%
\begin{code}
list xlist = 1 2 3 4
list reglist = income price 
\end{code}

Note that the variables in question must be of the series type.

Two abbreviations are available in defining lists:
\begin{itemize}
\item You can use the wildcard character, ``\texttt{*}'', to create a
  list of variables by name. For example, \texttt{dum*} can be used to
  indicate all variables whose names begin with \texttt{dum}.
\item You can use two dots to indicate a range of variables. For
  example \texttt{income..price} indicates the set of variables whose
  ID numbers are greater than or equal to that of \texttt{income}
  and less than or equal to that of \texttt{price}.
\end{itemize}

In addition there are two special forms:
\begin{itemize}
\item If you use the keyword \texttt{null} on the right-hand side,
  you get an empty list.
\item If you use the keyword \texttt{dataset} on the right, you get
  a list containing all the series in the current dataset (except
  the pre-defined \texttt{const}).
\end{itemize}

The name of the list must start with a letter, and must be composed
entirely of letters, numbers or the underscore character.  The maximum
length of the name is 31 characters; list names cannot contain
spaces.  

Once a named list has been created, it will be ``remembered'' for the
duration of the gretl session (unless you delete it), and can be
used in the context of any gretl command where a list of
variables is expected.  One simple example is the specification of a
list of regressors:
%
\begin{code}
list xlist = x1 x2 x3 x4
ols y 0 xlist
\end{code}

To get rid of a list, you use the following syntax:
\begin{code}
  list xlist delete
\end{code}
Be careful: \texttt{delete xlist} will delete the series contained
in the list, so it implies data loss (which may not be what you want).
On the other hand, \texttt{list xlist delete} will simply ``undefine''
the \texttt{xlist} identifier; the series themselves will not be
affected.

Similarly, to print the names of the members of a list you have to
invert the usual print command, as in
\begin{code}
  list xlist print
\end{code}
If you just say \texttt{print xlist} the list will be expanded and
the values of all the member series will be printed.

Lists can be modified in various ways.  To \textit{redefine} an existing
list altogether, use the same syntax as for creating a list.  For
example
%
\begin{code}
list xlist = 1 2 3
xlist = 4 5 6
\end{code}

After the second assignment, \texttt{xlist} contains just variables 4,
5 and 6.

To \textit{append} or \textit{prepend} variables to an existing list,
we can make use of the fact that a named list stands in for a
``longhand'' list.  For example, we can do
%
\begin{code}
list xlist = xlist 5 6 7
xlist = 9 10 xlist 11 12
\end{code}
%
Another option for appending a term (or a list) to an existing list is
to use \texttt{+=}, as in
%
\begin{code}
xlist += cpi
\end{code}
%
To drop a variable from a list, use \texttt{-=}:
%
\begin{code}
xlist -= cpi
\end{code}
%

In most contexts where lists are used in gretl, it is expected
that they do not contain any duplicated elements.  If you form a new
list by simple concatenation, as in \texttt{list L3 = L1 L2}
(where \texttt{L1} and \texttt{L2} are existing lists), it's possible
that the result may contain duplicates.  To guard against this you can
form a new list as the union of two existing ones:
%
\begin{code}
list L3 = L1 || L2
\end{code}
%
The result is a list that contains all the members of \texttt{L1},
plus any members of \texttt{L2} that are not already in \texttt{L1}.

In the same vein, you can construct a new list as the intersection of
two existing ones:
%
\begin{code}
list L3 = L1 && L2
\end{code}
%
Here \texttt{L3} contains all the elements that are present in both
\texttt{L1} and \texttt{L2}.

You can also subtract one list from another:
%
\begin{code}
list L3 = L1 - L2
\end{code}
%
The result contains all the elements of \texttt{L1} that are not 
present in \texttt{L2}.


\subsection{Lists and matrices}

Another way of forming a list is by assignment from a matrix.  The
matrix in question must be interpretable as a vector containing ID
numbers of data series.  It may be either a row or a column
vector, and each of its elements must have an integer part that is
no greater than the number of variables in the data set.  For example:
%
\begin{code}
matrix m = {1,2,3,4}
list L = m
\end{code}
%
The above is OK provided the data set contains at least 4 variables.

\subsection{Querying a list}

You can determine the number of variables or elements in a list
using the function \texttt{nelem()}.
%
\begin{code}
list xlist = 1 2 3
nl = nelem(xlist)
\end{code}

The (scalar) variable \texttt{nl} will be assigned a value of 3 since
\texttt{xlist} contains 3 members.

You can determine whether a given series is a member of a specified
list using the function \texttt{inlist()}, as in
%
\begin{code}
scalar k = inlist(L, y)
\end{code}
%
where \texttt{L} is a list and \texttt{y} a series. The series may
be specified by name or ID number. The return value is the (1-based)
position of the series in the list, or zero if the series is not
present in the list. 

\subsection{Generating lists of transformed variables}
\label{sec:transform-lists}

Given a named list of series, you are able to generate lists of
transformations of these series using the functions \texttt{log},
\texttt{lags}, \texttt{diff}, \texttt{ldiff}, \texttt{sdiff} or
\texttt{dummify}.  For example
%
\begin{code}
list xlist = x1 x2 x3
list lxlist = log(xlist)
list difflist = diff(xlist)
\end{code}

When generating a list of \textit{lags} in this way, you specify the
maximum lag order inside the parentheses, before the list name and
separated by a comma.  For example
%
\begin{code}
list xlist = x1 x2 x3
list laglist = lags(2, xlist)
\end{code}
%
or
%
\begin{code}
scalar order = 4
list laglist = lags(order, xlist)
\end{code}

These commands will populate \texttt{laglist} with the specified
number of lags of the variables in \texttt{xlist}.  You can give the
name of a single series in place of a list as the second argument to
\texttt{lags}: this is equivalent to giving a list with just one
member.

The \texttt{dummify} function creates a set of dummy variables coding
for all but one of the distinct values taken on by the original
variable, which should be discrete.  (The smallest value is taken as
the omitted catgory.)  Like lags, this function returns a list even if
the input is a single series.

Another useful operation you can perform with lists is creating
\emph{interaction} variables. Suppose you have a discrete variable
$x_i$, taking values from $1$ to $n$ and a variable $z_i$, which could
be continuous or discrete. In many cases, you want to ``split'' $z_i$
into a set of $n$ variables via the rule
\[
z^{(j)}_i =\left\{ 
    \begin{array}{ll}
      z_i & \mathrm{when} \quad x_i = j \\
      0 & \mathrm{otherwise;}
    \end{array}
    \right. 
\] 
in practice, you create dummies for the $x_i$ variable first and then
you multiply them all by $z_i$; these are commonly called the
\emph{interactions} between $x_i$ and $z_i$. In gretl you can do 
\begin{code}
  list H = D ^ Z
\end{code}
where \texttt{D} is a list of discrete series (or a single discrete
series), \texttt{Z} is a list (or a single series)\footnote{Warning:
  this construct does \emph{not} work if neither \texttt{D} nor
  \texttt{Z} are of the the list type.}; all the interactions will be
created and listed together under the name \texttt{H}.

An example is provided in script \ref{ex:interact_list}

\begin{script}[ht]
  \caption{Usage of interaction lists}
  \label{ex:interact_list}
Input:
\begin{scodebit}
open mroz87.gdt --quiet

# the coding below makes it so that
# KIDS = 0 -> no kids
# KIDS = 1 -> young kids only
# KIDS = 2 -> young or older kids

series KIDS = (KL6 > 0) + ((KL6 > 0) || (K618 > 0))

list D = CIT KIDS # interaction discrete variables 
list X = WE WA    # variables to "split"
list INTER = D ^ X

smpl 1 6

print D X INTER -o
\end{scodebit}
Output (selected portions):
\begin{scodebit}
           CIT         KIDS           WE           WA     WE_CIT_0

1            0            2           12           32           12
2            1            1           12           30            0
3            0            2           12           35           12
4            0            1           12           34           12
5            1            2           14           31            0
6            1            0           12           54            0

      WE_CIT_1     WA_CIT_0     WA_CIT_1    WE_KIDS_0    WE_KIDS_1

1            0           32            0            0            0
2           12            0           30            0           12
3            0           35            0            0            0
4            0           34            0            0           12
5           14            0           31            0            0
6           12            0           54           12            0

     WE_KIDS_2    WA_KIDS_0    WA_KIDS_1    WA_KIDS_2

1           12            0            0           32
2            0            0           30            0
3           12            0            0           35
4            0            0           34            0
5           14            0            0           31
6            0           54            0            0

\end{scodebit}
\end{script}

\subsection{Generating series from lists}

There are various ways of retrieving or generating individual series
from a named list. The most basic method is indexing into the
list. For example,
%
\begin{code}
series x3 = Xlist[3]
\end{code}
%
will retrieve the third element of the list \texttt{Xlist} under the
name \texttt{x3} (or will generate an error if \texttt{Xlist} has less
then three members).

In addition gretl offers several functions that apply to a list and
return a series.  In most cases, these functions also apply to single
series and behave as natural extensions when applied to lists, but
this is not always the case.

For recognizing and handling missing values, gretl offers
several functions (see the \GCR{} for details). In this context, it is
worth remarking that the \texttt{ok()} function can be used with a
list argument.  For example,
%
\begin{code}
list xlist = x1 x2 x3
series xok = ok(xlist)
\end{code}
%
After these commands, the series \texttt{xok} will have value 1 for
observations where none of \texttt{x1}, \texttt{x2}, or
\texttt{x3} has a missing value, and value 0 for any observations
where this condition is not met.

The functions \texttt{max}, \texttt{min}, \texttt{mean}, \texttt{sd},
\texttt{sum} and \texttt{var} behave ``horizontally'' rather than
``vertically'' when their argument is a list. For instance, the
following commands
\begin{code}
  list Xlist = x1 x2 x3
  series m = mean(Xlist)
\end{code}
produce a series \texttt{m} whose $i$-th element is the average of
$x_{1,i}, x_{2,i}$ and $x_{3,i}$; missing values, if any, are implicitly discarded.

\begin{table}
  \centering
  \begin{tabular}{lllllllll}
\hline
	& YpcFR & YpcGE	& YpcIT	& NFR		& NGE		& NIT        \\ 
\hline
\\ [-8pt]
1997	& 114.9 & 124.6	& 119.3	& 59830.635	& 82034.771	& 56890.372  \\ 
1998	& 115.3 & 122.7	& 120.0	& 60046.709	& 82047.195	& 56906.744  \\ 
1999	& 115.0	& 122.4	& 117.8	& 60348.255	& 82100.243	& 56916.317  \\ 
2000	& 115.6 & 118.8	& 117.2	& 60750.876	& 82211.508	& 56942.108  \\ 
2001	& 116.0	& 116.9	& 118.1	& 61181.560	& 82349.925	& 56977.217  \\ 
2002	& 116.3 & 115.5	& 112.2	& 61615.562	& 82488.495	& 57157.406  \\ 
2003	& 112.1 & 116.9	& 111.0	& 62041.798	& 82534.176	& 57604.658  \\ 
2004	& 110.3 & 116.6	& 106.9	& 62444.707	& 82516.260	& 58175.310  \\ 
2005	& 112.4 & 115.1	& 105.1	& 62818.185	& 82469.422	& 58607.043  \\ 
2006	& 111.9 & 114.2	& 103.3	& 63195.457	& 82376.451	& 58941.499  \\
\hline
  \end{tabular}
  \caption{GDP per capita and population in 3 European countries (Source: Eurostat)}
  \label{tab:EuroData}
\end{table}
In addition, gretl provides three functions for weighted
operations: \texttt{wmean}, \texttt{wsd} and \texttt{wvar}. Consider
as an illustration Table \ref{tab:EuroData}: the first three columns are GDP
per capita for France, Germany and Italy; columns 4 to 6 contain the
population for each country. If we want to compute an aggregate
indicator of per capita GDP, all we have to do is
\begin{code}
list Ypc = YpcFR YpcGE YpcIT
list N = NFR NGE NIT
y = wmean(Ypc, N)
\end{code}
so for example
\[
y_{1996} = \frac{114.9 \times 59830.635 + 124.6 \times 82034.771 +
  119.3 \times 56890.372} {59830.635 + 82034.771 + 56890.372} =
120.163
\]
See the \GCR\ for more details.

\section{Named strings}
\label{sec:named-strings}

For some purposes it may be useful to save a string (that is, a
sequence of characters) as a named variable that can be reused.

Some examples of the definition of a string variable are shown
below.
%
\begin{code}
string s1 = "some stuff I want to save"
string s2 = getenv("HOME")
string s3 = s1 + 11
\end{code}
%
The first field after the type-name \texttt{string} is the name under
which the string should be saved, then comes an equals sign, then
comes a specification of the string to be saved. This can be the
keyword \texttt{null}, to produce an empty string, or may take any of
the following forms:

\begin{itemize}
\item a string literal (enclosed in double quotes); or
\item the name of an existing string variable; or
\item a function that returns a string (see below); or
\item any of the above followed by \texttt{+} and an integer offset.
\end{itemize}

The role of the integer offset is to use a substring of the preceding
element, starting at the given character offset.  An empty string is
returned if the offset is greater than the length of the string in
question.

To add to the end of an existing string you can use the operator
\verb|~=|, as in
%
\begin{code}
string s1 = "some stuff I want to "
string s1 ~= "save"
\end{code}
or you can use the \verb|~| operator to join two or more strings, as
in
\begin{code}
string s1 = "sweet"
string s2 = "Home, " ~ s1 ~ " home."
\end{code}

Note that when you define a string variable using a string literal, no
characters are treated as ``special'' (other than the double quotes
that delimit the string).  Specifically, the backslash is not used as
an escape character.  So, for example,
%
\begin{code}
string s = "\"
\end{code}
%
is a valid assignment, producing a string that contains a single
backslash character.  If you wish to use backslash-escapes to denote
newlines, tabs, embedded double-quotes and so on, use \texttt{sprintf}
instead.

The \texttt{sprintf} command can also be used to define a string
variable. This command works in exactly the same way as the
\texttt{printf} command except that the ``format'' string must be
preceded by an identifier: either the name of an existing string
variable or a new name to which the string should be assigned.
For example,
%
\begin{code}
scalar x = 8
sprintf foo "var%d", x
\end{code}

\subsection{String variables and string substitution}

String variables can be used in two ways in scripting: the name of the
variable can be typed ``as is'', or it may be preceded by the ``at''
sign, \verb|@|. In the first variant the named string is treated as a
variable in its own right, while the second calls for ``string
substitution''. The context determines which of these variants is
appropriate. 

In the following contexts the names of string variables should be
given in plain form (without the ``at'' sign):

\begin{itemize}
\item When such a variable appears among the arguments to the
  commands \texttt{printf} or \texttt{sprintf}.
\item When such a variable is given as the argument to a function.
\item On the right-hand side of a \texttt{string} assignment.
\end{itemize}

Here is an illustration of the use of a named string argument with
\texttt{printf}:
%
\begin{code}
? string vstr = "variance"
Generated string vstr
? printf "vstr: %12s\n", vstr
vstr:     variance
\end{code}

String substitution can be used in contexts where a string variable is
not acceptable as such. If gretl encounters the symbol \verb|@|
followed directly by the name of a string variable, this notation is
treated as a ``macro'': the value of the variable is sustituted
literally into the command line before the regular parsing of the
command is carried out.

One common use of string substitution is when you want to construct
and use the name of a series programatically. For example, suppose you
want to create 10 random normal series named \texttt{norm1} to
\texttt{norm10}. This can be accomplished as follows.
%
\begin{code}
string sname = null
loop i=1..10
  sprintf sname "norm%d", i
  series @sname = normal()
endloop
\end{code}
%
Note that plain \texttt{sname} could not be used in the second line
within the loop: the effect would be to attempt to overwrite the
string variable named \texttt{sname} with a series of the same
name. What we want is for the current \textit{value} of
\texttt{sname} to be dumped directly into the command that defines a
series, and the ``\verb|@|'' notation achieves that.

Another typical use of string substitution is when you want the
options used with a particular command to vary depending on
some condition. For example,
%
\begin{code}
function void use_optstr (series y, list xlist, int verbose)
   string optstr = verbose ? "" : "--simple-print"
   ols y xlist @optstr 
end function

open data4-1
list X = const sqft
use_optstr(price, X, 1)
use_optstr(price, X, 0)
\end{code} 

When printing the value of a string variable using the \texttt{print}
command, the plain variable name should generally be used, as in
%
\begin{code}
string s = "Just testing"
print s
\end{code}
%
The following variant is equivalent, though clumsy.
%
\begin{code}
string s = "Just testing"
print "@s"
\end{code}
%
But note that this next variant does something quite different.
%
\begin{code}
string s = "Just testing"
print @s
\end{code}
%
After string substitution, the print command reads
%
\begin{code}
print Just testing
\end{code}
%
which attempts to print the values of two variables, \texttt{Just} and
\texttt{testing}.

\subsection{Built-in strings}

Apart from any strings that the user may define, some string variables
are defined by gretl itself.  These may be useful for people
writing functions that include shell commands.  The built-in strings
are as shown in Table~\ref{tab:pred-strings}.

\begin{table}[htbp]
\centering
\begin{tabular}{ll}
  \texttt{gretldir} & the gretl installation directory \\
  \texttt{workdir} & user's current gretl working directory \\
  \texttt{dotdir} & the directory gretl uses for temporary files \\
  \texttt{gnuplot} & path to, or name of, the \app{gnuplot} executable \\
  \texttt{tramo}& path to, or name of, the \app{tramo} executable \\
  \texttt{x12a} & path to, or name of, the \app{x-12-arima} executable \\
  \texttt{tramodir} & \app{tramo} data directory \\
  \texttt{x12adir} & \app{x-12-arima} data directory \\
\end{tabular}
\caption{Built-in string variables}
\label{tab:pred-strings}
\end{table}

To access these as ordinary string variables, prepend a dollar sign
(as in \verb|$dotdir|); to use them in string-substitution mode,
prepend the at-sign (\verb|@dotdir|).

\subsection{Reading strings from the environment}

In addition, it is possible to read into gretl's named strings,
values that are defined in the external environment.  To do this you
use the function \texttt{getenv}, which takes the name of an environment
variable as its argument.  For example:
%
\begin{code}
? string user = getenv("USER")
Generated string user
? string home = getenv("HOME")
Generated string home
? printf "%s's home directory is %s\n", user, home
cottrell's home directory is /home/cottrell
\end{code}
%
To check whether you got a non-empty value from a given call to
\texttt{getenv}, you can use the function \texttt{strlen}, which
retrieves the length of the string, as in
%
\begin{code}
? string temp = getenv("TEMP")
Generated string temp
? scalar x = strlen(temp)
Generated scalar x = 0
\end{code}

\subsection{Capturing strings via the shell}

If shell commands are enabled in gretl, you can capture the
output from such commands using the syntax 

\texttt{string} \textsl{stringname} = \texttt{\$(}\textsl{shellcommand}\texttt{)}

That is, you enclose a shell command in parentheses, preceded by
a dollar sign.

\subsection{Reading from a file into a string}

You can read the content of a file into a string variable using
the syntax

\texttt{string} \textsl{stringname} = \texttt{readfile(}\textsl{filename}\texttt{)}

The \textsl{filename} field may be given as a string variable.  For
example
%
\begin{code}
? sprintf fname "%s/QNC.rts", $x12adir
Generated string fname
? string foo = readfile(fname)
Generated string foo
\end{code}

\subsection{More string functions}

Gretl offers several functions for creating or manipulating
strings. You can find these listed and explained in the
\textit{Function Reference} under the category \textsf{Strings}.
    
%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "gretl-guide"
%%% End: