Rgretl-package: Interface to _gretlcli_

Description Details Author(s) References

Description

An interface to GNU gretl: running gretl scripts from R, estimating econometric models with backward passing of model results, opening gretl data files ‘.gdt’. gretl can be downloaded from http://gretl.sourceforge.net. This package could make life on introductory/intermediate econometrics courses much easier: gretl provides full battery of the required regression diagnostics, including White's heteroskedasticity test, restricted OLS estimation, advanced weak instrument test after iv estimation, very convenient dealing with lagged variables in econometric models, standard case treatment in unit root tests, vector autoregressions, and vector error correction models. Datasets for 8 popular econometrics textbooks can be installed into gretl from its server. All datasets can be easily imported using this package.

Details

Package: Rgretl
Type: Package
Version: 0.2.2
Date: 2018-03-17
Depends: R (>= 3.4.0)
SystemRequirements: gretl (>= 2017c), gretlcli

Index: This package was not yet installed at build time.

Getting started

For the package to work properly gretl should be installed. The installation instruction is in README file.

The first thing to run is

run_grcli("help help")

Yes! Run, run, help, help! Imagine, a hungry tiger is after you and there will be nothing for humdrum cramming. The output will show how to use gretl help system from a script or console, hence using run_gretlcli. In contrast with R gretl has commands and functions. A command syntax is similar to that of shell commands. Run

run_grcli("help") run_grcli("help functions")

to obtain lists of commands and functions. Let's turn to a slighly longer script:

myfirstscript <- 'nulldata 50 eval 2 + 2 set seed 13 x1 = normal() x2 = normal() y = x1 + x2 + normal() y = y > 0 printf "\\nThis is my first gretl model:\\n" logit y 0 x1 x2 varlist --accessors' run_grcli(myfirstscript)

It is convenient to render a part of the output:

This is my first gretl model: Model 1: Logit, using observations 1-50 Dependent variable: y Standard errors based on Hessian coefficient std. error z slope ------------------------------------------------------ const 0.398073 0.360291 1.105 x1 1.15444 0.523459 2.205 0.280427 x2 1.50154 0.521053 2.882 0.364742

...

model-related $ess (scalar: 4.92415) $T (scalar: 50)

Since myfirstscript is an R character vector rather than a genuine gretl script file it uses '\\n' instead of '\n' (as it should be in the native script) to indicate a new line in formatted printing. The single quote (') is used only inside strings in gretl. Hence if a user does not apply sohpisticated string manipulation, there exists one by one correspondence. Otherwise, some additional experimenting with '\"' may be needed. Instead of turning this subsection into a kind of textbook it should be sufficient to give a couple of hints. To understand the script above and its output one can proceed by running run_grcli("help nulldata"), run_grcli("help eval"), run_grcli("help normal --func"), etc. The output of varlist --accessors is an analogue of output of names(obj) and/or names(summary(obj)) where obj is an estimated model object, e.g. an object of class "glm". In gretl parlance dollar-prefixed objects are called accessors. Getting help on them follows the same pattern: run_grcli("help $ess").

A user can broadcast named variables backward to R:

logitres <- run_grcli(myfirstscript, output = list(series = c("y","$uhat"), matr = c("$coeff","$vcv","$rsq","$pd","$windows"))) names(logitres) logitres$'$rsq' class(logitres$y) logitres$'$coeff' logitres$'$vcv'

Just reproduce and look at the output. Note that both scalars and matrices included into matr component of output. It worth mentioning that accessors listed by varlist --accessors are present in the memory during gretl session, so they needn't to be included into text argument of run_grcli to be broadcasted to R. By contrast, in the example above the set of admissible 'non-dollar' series names includes only x1, x2, and y, since there are no other 'non-dollar' series names inside myfirstscript. If a data file is open, its series also can be broadcasted without mentioning them in the script:

logmoney <- run_grcli("open denmark.gdt -q", output = list(series = "LRM")) head(logmoney$LRM).

We end the subsection by reproducing a simple trick to estimate the 'calling' time:

system.time(run_grcli('set stopwatch\n eval $stopwatch'))

Using gretl sample data files and sample scripts

Sample data files

If gretl is installed and is visible from inside R all gretl sample files are ready to be explored. Additional information is supplied by Rgretl::datasets_info and Rgretl::description functions.

Sample scripts

Three data files in Rgretl (gretl.rdata, greene.rdata, and ramanathan.rdata) contain character string data only. Its contents is formed by adopted versions of gretl-supplied hansl language sample scripts. In the first place, these scripts are easily searcheable and available from gretl GUI but not from gretlcli. Secon, since run_grcli deals with R character strings, and not with the original hansl scripts some minor changes are needed to compensate enclosing quotes. As a matter of fact, all changes appears to be inserting additional backslashes in several cases: (\) was substituted for (\\) to break long lines in code, (\n) – for (\\n) in formatted printing (prinf) command, (') was substituted for (\') in comments and for matrix transposition when single quotes were used to enclose character string input to Rgretl::run_grcli, etc. All scripts can be easily run via run_grcli and all have been tested in the batch mode having produced a neat nearly 1MB-sized text log file. See also help entries for these data files for the further information.

Reading and saving data files

Reading

The open_gdt function can open data files of the following formats: ‘*.gdt, *.gdtb’ (gretl data files), ‘*.csv’, ‘*.txt’, ‘*.gnumeric’ (Gnumeric), ‘*.ods’ (Open Document), ‘*.xls, *.xlsx’ (Excel), ‘*.dta’ (Stata), ‘*.wf1’ (Eviews), ‘*.sav’ (SPSS), ‘*.xpt’ (SAS xport), ‘*.dat’ (JMulTi). For example, one can run

denmark <- open_gdt("denmark")

# .gdt extension can be omitted in case (i) below

capm4 <- open_gdt("http://www.principlesofeconometrics.com/poe4/data/eviews/capm4.wf1")

We need not indicating path to a file in two cases:

(i) for gretl sample data files. The list of available files is at the bottom of README file. Additional sample files can be downloaded and installed via gretl GUI. README file contains installation instruction and the list of data files.

(ii) for files in gretl working directory (as extracted from gretl or by running get_grwd).

The output is a data.frame object. Curretntly, time-series structure is preserved (output data frame columns will be ts objects with the right attributes) only for yearly, quorterly, and monthly data. Otherwise, an additional column will be present for a user to recode it into time series structure by means of R. Panel data structure is easily recovered if time and unit indicators are present. When there are no visible time/unit identifiers a user can run

my.frame <- open_gdt("a_file.gdt")

time_n_id <- run_grcli('genr time',data = "a_file.gdt",output = list(series = c("time","$unit")))

my.frame$time <- time_n_id$time

my.frame$id <- time_n_id$'$unit'

Now my.frame is ready. A user can load her favourite R package for estimation of panel data models.

Another useful cheat sheet: running

run_grcli('help $datatype\n eval $datatype', data = "denmark")

will give

$datatype Output: scalar Returns an integer value representing the sort of dataset that is currently loaded: 0 = no data; 1 = cross-sectional (undated) data; 2 = time-series data; 3 = panel data. 2

Saving

To this end the package have two functions: save_gtd for saving data frames and/or (multiple) time-series objects as gretl data files (this should be useful mostly for gretl-oriented users); for a wider target group the most interisting feature is possibility to export data to Stata format; tsave_bin saves lists with time series components as gretl binary data bases‘*.bin’; the latter is supplied by a helper, merge_data for easy constructing lists with the appropriate structure to export, also this function supplies all series with unique names as required by gretl for saving its binary data bases. The best way to render panel data structure is providing R data frames with explicit unit and period identifiers before exporting.

Plotting

Simplified plotting

To this end gretl has textplot command which outputs simple plots in console. To see a working example execute

data(ramanathan)

cat(paste0(ramanathan$PS3.1,"\n"))

run_grcli(ramanathan$PS3.1)

True plots

gretl has several plotting commands: boxplot, gnuplot, graphpg, hfplot, plot, qqplot, rmplot, scatters. All commands above have '--output=filename' option, e.g. '--output=myfile.pdf', or '--output=display'. Execute run_grcli("help gnuplot") to see the details on '--output=filename'. "Saving" options work as intended in Rgretl. At the other hand, '--output=display' option may output nothing on some systems if being called from Rgretl. To compensate this sad fact Rgretl has a special option for gretl plotting commands: --output=#R. With this option plots will be rendered on R standard graphic windows. A user can copy-paste and execute the code below to see how it works:

a_script <- 'nulldata 93 setobs 4 1995:1 --time-series set seed 13 y = normal() RandomWalk = cum(y) x = normal() gnuplot RandomWalk --time-series --with-lines --output=#R qqplot y x --output=#R ' run_grcli(a_script)

Note that '--output=#R' option is not intended to work with gretl graphpg command. Also, vector graphics is not retained currently. To insert publishing-quality plots in a tect editor use one of the "saving" options.

Estimation of econometric models

It is a wide practice inside R to have several alternatives to estimating certain models. From the autor's subjective point of view the most valuable for elementary/intermediate econometrics courses features include post-estimation menu (execute run_grcli("help modtest") and references therein), heteroscedasticity toolbox (execute run_grcli("help hsk")). gretl provides a large toolbox for very easy working with dynamic regressions. Also gretl provides full case treatment for unit root testing and cointegration analisys. Written on c commands for MLE estimation of models for limited and discrete dependent variable are very quick and reliable.

The grmod function in this version by default outputs a sizable list of model output similar to that of lm, glm, etc. Still, not all estimation commands are processed by grmod. In such cases run_grcli provides an alternative; thought this way requires much more typing from a user. Some useful extensions are considered in README file.

Author(s)

Oleh Komashko

Maintainer: Oleh Komashko <oleg_komashko@ukr.net>

References

Cottrell, A., and Lucchetti, R. (2018) "Gretl User's Guide," http://ricardo.ecn.wfu.edu/pub//gretl/manual/en/gretl-guide.pdf

Cottrell, A., and Lucchetti, R. (2018) "Gretl Command Reference", http://ricardo.ecn.wfu.edu/pub/gretl/manual/PDF/gretl-guide.pdf

Cottrell, A., and Lucchetti, R. (2018) "A Hansl Primer", http://ricardo.ecn.wfu.edu/pub/gretl/manual/PDF/hansl-primer.pdf


Rgretl documentation built on May 2, 2019, 3:46 p.m.