Gretl: an example
I have e-mailed you the example script file (gretl_example.inp) and the data file it uses (example_data.csv). You can also find these files on the course website: flash.lakeheadu.ca/~mshannon/ec3111.html). Download and save them from your e-mail or the website (if you do it from the website make sure your browser doesn't change the extension name of the file).
Example Data file: example_data.csv
Look first at the data file by opening it with Excel (if you open it instead as a text file -- this is what your browser will do if you open it on the website -- you will see that it is in comma-delimited format where variable values are separated by commas). Once open, you will see the names of six variables in the first row of the file: RECORD, SEX, AGE, WAGE, EDUCATION and UNION. Below this are 20 made up observations on these variables (each row contains the data on a particular person or observation). A codebook for this file might look something like the following:
Codebook for example_data.csv:
Name:Description:Code
RECORDPerson ID number in file
SEXSex of individual
Man 1
Woman 2
AGEAge in years (15-75)
WAGEHourly wage in $ (rounded)
EDUCATIONEducation level
Low1
Middle2
High3
UNIONUnionized job
No0
Yes1
Using the codebook you can see for example that the first person in the dataset is a man, age 22, paid $16/hour, with middle level education in a non-union job. We now want to write a Gretl program that will read this file and do some basic calculations using it.
Creating Script files:
A script or program file contains a set of gretl commands that will be executed when the program file is run. To create a script file in Gretl format: (i) open Gretl; (ii) click the “New script” icon at the bottom of the resulting page. You can now type your commands in Gretl’s editor. When done save the resulting file using the “save as” icon at the top of the gretl editor. Gretl script files have the file type extension “inp”. The file “gretl_example.inp” was created in this way.
Example script file:
Double click the file “gretl_example.inp” – Gretl will open the file in its editor. Have a look. The Gretl editor assigns different colors to different types of code. Lines colored blue are explanatory comments rather than commands (in the editor you can tell Gretl that you are writing a comment by starting the line with a #). The lines starting with text in reddish-brown are the Gretl commands while text shown in green are options associated with some command.
The first command you see in the file is:
outfile c:\Users\Mike\3111\gretl_example.log --write
this tells Gretl to create an output file called “gretl_example.log” located in a folder on my computer called c:\Users\Mike\3111\. All commands and resulting output from running the program will be written to this file and it will be in text format. This is useful as it gives you a record of your session. If you do not include an “outfile” command Gretl will display the output on screen in a “gretl script output” window – once the program has finished running you can save the onscreen output to a file by clicking the “save as” icon at the top of the output file window.
Scroll down! The next command you will see is:
open C:\Users\Mike\3111\example_data.csv
this opens the example data set“example_data.csv” which when I ran this program was located in my folder “C:\Users\Mike\3111\” (if you want to run this change the folder name to wherever on your computer that you have saved the datafile to). If you do not give the location Gretl will try and find it e.g. it by looking in the same location as the script file.
The next commands you see are all variations on the “summary” command. These create summary statistics for all, or a subset of variables, in the dataset. For example:
summary –simple gives mean, std. deviation, minimum and maximum value for each variable in the dataset. Have a look at the other versions of the
summary command in the example -- you may find them useful.
Other descriptive statistics commands include “freq” which reports frequencies e.g.
freq EDUCATION gives the numbers and shares of people with each of the three
levels of education reported in the dataset.
Also useful is the crosstab command “xtab”:
xtab SEX EDUCATION this will give the number of observations in the dataset with each possible SEX-EDUCATION combination.
The next commands you see create new variables from those read in with the open command. The Gretl command used to do this is called "genr" (generate):
genr wage_cents=WAGE*100 creates a variable called wage_cents which is WAGE
times 100. See other examples in the script file. Look in
the “Command Reference” under ‘genr’ for details on operators and functions.
Dummy variables equal 1 if the observation has some characteristic and 0 if not. Dummies can be created using the genr command. Notice that the variable UNION in the example is already a dummy. To create a dummy write genr followed by the name you want to give the dummy variable and on the other side of the equation state the logical condition that the observation must satisfy if the dummy is to equal 1. For such statements operators are: == for equals, <= less than or equal, >= greater than or equal, != not equal. If there are multiple conditions || means “or” and & means “and” (see Gretl command reference under command genr). For example:
genr male=(SEX==1) creates a dummy “male” which equals 1 for observations with
SEX equal to 1 and equal to 0 for other observations.
.
genrunion_man=(SEX==1) & (UNION==1) dummy "union_man" equals 1 for men
who are unionized.
genrhed_un=(UNION==1) || (EDUCATION==3) dummy hed_un equals 1 for those
who either are unionized or have high level education.
Next is an ordinary least squares regression (ols) command:
olslog_wageconst man UNION AGE age_squared
ols is the command, log_wage is the dependent variable, “const” specifies that you want the regression to have an intercept (constant) and the variables “man”, UNION, AGE and age_squared are the explanatory variables.
A couple of other useful commands dealing with creating subsets of the dataset are provided near the end of the script file.
“smpl” followed by a logical condition allows you to restrict the sample to observations
satisfying that condition. The following restricts the sample to men only:
smpl SEX==1 --restrict
smpl--full restores the full sample if there is a restriction in effect.
Running a script file:
Once you have written and saved a script file you can run it by clicking the "run" icon at the top of the editor. The file “gretl_example.log” (website) was obtained by running gretl_example.inp.
1