Ways to Use R in gretl

The standard method of working with R is by writing scripts, or by typing commands at the R prompt, much in the same way as one would write gretl scripts or work with the gretl console. This section is a gentle introduction to using R in general with a few tips on using it with gretl. As you will see, there are several ways in which to use R in gretl.

D. 1.1 Using the foreign command

In section 10.3.4 a foreign statement was used to actually execute R routines from within gretl and to pass results to gretl for further processing. A foreign block has the basic structure:

————————- Basic foreign block for R

1 foreign language=R —send-data —quiet

2 [ R code to create a matrix called ‘Rmatrix’ ]

3 gretl. export(Rmatrix)

4 end foreign

5

5 matrix m = mread("@dotdir/Rmatrix. mat")

The foreign command uses the language=R to open R and to ready it for further computing outside of gretl. The —send-data option sends the current gretl data set to R. The —quiet option prevents the output from R from being echoed in the gretl output. The block is closed and R exited with the end foreign command. What appears in between are statements coded in R. The last statement, gretl. export(Rmatrix), is used to export a matrix computation that I have called ‘Rmatrix’ to gretl. R attaches a .mat suffix to Rmatrix automatically. The matrix is written to the gretl working directory on your harddrive. To read the matrix and ready it for further processing, use the mread command (matrix read). The mread("@dotdir/Rmatrix. mat") tells gretl to look in the working directory (@dotdir)for Rmatrix. mat.

This achieves the same effect as submitting the enclosed R commands via the GUI in the noninteractive mode (see section 30.3 of the Gretl Users Guide). In other words, it allows you to use R commands from within gretl. Of course, you have to have installed R separately, but this greatly expands what can be done using gretl.

D.1.2 Opening an R session

To illustrate, open the cola. gdt data in gretl.

open "C:Program Files (x86)gretldatapoecola. gdt"

Now, select Tools>start GNU R from the pull-down menu. The current gretl data set, in this case cola. gdt, will be transported into R’s required format. You’ll see the R console which is shown in Figure D.1. The message in R tells you that the data are loaded into an R data frame called gretldata. You can now use R with the data loaded from gretl. Gretl’s data import features are very good and it makes an excellent front-end for getting data into R.

D.1.3 R Script from gretl

Opening an R window and keying in commands is a convenient method when the job is small. In some cases, however, it would be preferable to have R execute a script prepared in advance. One way to do this is via the source() command in R. Alternatively, gretl offers the facility to edit an R script and run it, having the current dataset pre-loaded automatically. This feature can be accessed via the File, Script Files menu entry. By selecting User file, one can load a pre-existing R script; if you want to create a new script instead, select the New script, R script menu entry. Figure D.2

In either case, you are presented with a window very similar to the editor window used for ordinary gretl scripts, as in Figure D.2.

Whis is taken almost directly from the gretl Users Guide, chapter 30

There are two main differences. First, you get syntax highlighting for Rs syntax instead of gretl’s. Second, clicking on the Execute button (the gears icon), launches an instance of R in which your commands are executed. Before R is actually run, you are asked if you want to run R interactively or not in this dialog box:

An interactive run opens an R instance similar to the one seen in the previous section: your data will be pre-loaded (if the pre-load data box is checked) and your commands will be executed. Once this is done, you will find yourself in R and at the R prompt. From here you can enter more R commands.

A non-interactive run, on the other hand, will execute your script, collect the output from R and present it to you in an output window; R will be run in the background. This was the approach taken in the canonical correlation analysis from chapter 10, since we did not have further use for R and the results were being passed back to gretl.

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>