EViews COM Automation Client Support (MATLAB and R)

EViews offers COM Automation client support for select external application servers. Support is currently provided for two applications: MATLAB and R.

The client support includes a set of EViews functions for exporting EViews data to the external application, running commands and programs in the application, and importing data back into EViews. These functions provide easy access to the powerful programming languages of MATLAB and R to create programs and routines that perform tasks not currently implemented in EViews. The interface also offers access to the large library of statistical routines already written in the MATLAB and R languages.

There are nine EViews commands that control the use of external applications: xclose, xget, xlog, xopen, xput, xon, xoff, xpackage, and xrun.

xopen and xclose are used to open and close a connection to the external application (MATLAB or R). xput and xget are used to send data to and from the external application. xrun is used to send a command to the external application, and, finally, xlog lets you show or hide an external application log window within EViews.

Using MATLAB®

To open a connection to MATLAB, simply use the xopen(type=m) command. EViews will then attempt to launch MATLAB on your computer. Note, your computer must have access to MATLAB, either through a local installation, or through a network. EViews has been tested with MATLAB release R2008a, although other versions may work as well.

Once a connection to MATLAB has been made, xput may be used to pass data from EViews over to MATLAB. All numerical data is passed to MATLAB as a matrix. String data (in the form of an alpha series or svector) will be passed to a MATLAB char if each string contains the same number of characters. Otherwise the string data will be passed as a cell array. Series and group objects are always filtered by the current sample unless you specify an explicit sample in the xput command.

Note that object names in EViews are not case sensitive. Unless otherwise specified, EViews objects that are passed into MATLAB using xput will have the same name as the EViews objects that are being pushed, with the case determined by the case established for the COM connection (see xopen).

xrun can be used to issue a single line command to MATLAB. You may, for example, use xrun to invert a matrix or run a program in MATLAB. If you wish to run multiple commands, each command must be entered with a separate xrun command. Commands should be surrounded in quotes.

xget can be used to fetch data from MATLAB into EViews. The “type=” option lets you specify the type of object that will be created in EViews. If no option is specified, MATLAB matrices will be brought in as EViews matrix objects, while chars and cell arrays will be brought in as svectors. If you use “type=series” or “type=alpha” to specify that the data is brought in as a series or an alpha series, EViews will create a series of workfile length, and either truncate the data, or pad with NAs, if the incoming matrix does not have the same number of rows as there are workfile observations.

The follow program offers a simple example using MATLAB to perform a least-squares regression (more complicated operations may be performed by using xrun to run a MATLAB program):

'create a workfile

wfcreate u 100

'create some data

series y=nrnd

series x1=nrnd

series x2=nrnd

'group regressor data into a group

group xs c x1 x2

'open a connection to Matlab with lower-case default output names

xopen(type=m, case=lower)

'put regressors and dependent variable into Matlab

xput xs

xput y

'run a command to perform least squares as a matrix operation

xrun "beta = inv(xs'*xs)*xs'*y"

'retrieve betas back into EViews

xget beta

'perform same least squares estimation in EViews

equation e1.ls y xs

show e1

show beta

'close Matlab connection

xclose

The program first creates a new workfile, and then creates some series objects. The series called Y is the dependent variable, and the series X1 and X2 are the regressors (along with a constant). xopen is used to open a connection to MATLAB, and then xput is used to pass the dependent variable and the regressors over to MATLAB. Note that the names of the matrices and vectors will all be lower-cased in MATLAB since the connection was opened with the “case=lower” option.

xrun is used to create a vector in MATLAB, called “beta”, equal to the least squares coefficient, using the matrix algebra formula for LSQ. beta is brought back into EViews with the xget command, and then we estimate the same equation inside EViews and show the two results for comparison. Finally the connection to MATLAB is closed.

Using R

R is a GNU-project software environment for statistical computing and graphics. R is free software (under the terms of the GNU General Public License) that is readily available for download from the Official R Project website: http://www.r-project.org/ and other mirror sites.

To use the EViews external interface to R, you must have R installed on your Windows computer. Once installed, you may use the commands listed above to communicate with R from within EVIews.

EViews 10 supports R integration directly, eliminating the need for any third party software. Install both EViews 10 and R on the same computer and use normal EViews commands, such as XOPEN and XRUN.

Installing R

EViews was developed and tested with R version 3.2.3. You must have version 3.2.3 or newer installed on the machine running EViews.

If you do not currently have R installed, simply go to the following site and download the latest version:

Verifying EViews R Connector

Verify that the new EViews R Connector Interface components are properly registered by running the EViews command REGCOMPONENTS:

In the Register Components dialog, make sure that the EViews R Connector Interface objects have been properly registered. If not, click the Yes (All) button to re-register these components on your local machine, then re-run REGCOMPONENTS to verify the registration. Note: Registration of these components will require local administrator rights. If you do not have these admin rights, please contact your IT department for support.

Next, go to the External Program Interface dialog under the Options menu/General Options/External Program Interface:

In COM ProgID group, make sure that EViewsRConn.VariantRConn is selected for R.

Next, if Home Path is blank, click the […] button and navigate to your local R installation. Select the version-specific R folder (e.g., “R-3.2.3”) under the parent R folder as your home directory.

Optionally, you can also select your Save Series as preference when pushing EViews series objects into R. If you select “ts (time series)”, series objects that have a compatible R ts frequency will be pushed as R ts objects. Otherwise, an R data.frame structure will be created instead.

Now we're ready to begin using R.

Opening a Connection to R

All external program interface methods must begin with a call to XOPEN:

XOPEN(type=r)

This will begin a new session of R and, by default, an associated RConn Output log window should appear. This log window will show any R commands we run and any output generated by R. You can also type R commands interactively into the RConn log window.

Now that we have an open connection to R, we can use the other 'X' methods to perform some actions.

Sending EViews Data to R

Now that we have an open connection to R, we can send EViews object(s) to R using the XPUT command:

XPUT(rtype=data.frame, name=vars) y x1 x2

Basic EViews object types such as series, alphas, matrices, vectors, and scalars can be pushed successfully to R. Depending on the EViews object type, an appropriate R object will be created when the data is sent. As our example shows, XPUT also supports grouping multiple series objects into a single R time series or R data.frame object.

Running an R Command

Now that we have some data in R, we can run R specific commands by using the XRUN command:

XRUN z<-glm(y~x1+x2, family=Gamma(link=log), data=vars)

You can also type this command directly into the bottom of the RConn log window (without XRUN):

XON and XOFF Commands

To run multiple R commands without having to specify XRUN on each line of an EViews program, you can use the XON and XOFF commands (introduced in EViews 10) to control external programming mode. Once you call XON, all program lines after that will be sent to R directly. Use XOFF (or XCLOSE) to turn this mode off.

XON 'Turn on external programming mode

z<-glm(y~x1+x2, family=Gamma(link=log), data=vars) summary(z)

XOFF 'Turn off external programming mode

Output Display

Note that our connector does not always automatically capture all of your R output. Consequently, you may find that using XRUN to issue a command that displays output in R may return only a subset of the usual output to your log window. In the most extreme case, you may see a simple "OK" message displayed in your log window. To instruct R to show all of the output, you should use enclose your command inside an explicit print statement in R. Thus, to display the contents of a matrix X, you must issue the command

XRUN print(X)

instead of the more natural

XRUN X

Retrieving R Data into EViews

To retrieve data back into EViews, we will use the XGET command:

XGET(name=beta, type=vector) z$coef

Closing the R Connection

Once we've completed our R operations, we can close our connection with a simple call to XCLOSE:

XCLOSE

Ability to Save EViews Workfile as RDATA Workspace File

EViews also has native RDATA workspace file compatibility.

This means you can save an EViews workfile (one page at a time) directly to a new RDATA workspace file:

wfsave(type=rdata) c:\files\tq.rdata

Each simple object (series, alpha, matrix, vector, and scalar) on the current workfile page will be converted to an appropriate R data structure and saved into the new RDATA file.

You can also open a pre-existing RDATA file as an EViews workfile, though because this operation can only open all R objects into a single workfile page, all the objects in the RDATA file must be the same frequency for this to work properly.

In cases where there are R objects with different frequencies, it would be better to treat the RDATA file as an EViews Database instead.

Open RDATA Workspace File as an EViews Database

In EViews 10, you can now open an RDATA workspace file as an EViews Database:

DBOPEN(type=rdata) c:\files\data.rdata

This will open the RDATA file in its own EViews database window. You can view all the R objects found in this database and individually FETCH the objects you want into your workfile:

Note: R data structures that could have multiple columns/elements (such as data.frame, list, and mts objects) are listed once per column/element with the appropriate R name.

As an EViews Database, you can use both FETCH and STORE to read and write data to the RDATA file and even link EViews series objects back to their source R structure to allow for simple data refreshes.

R Related Links

The Official R Project website: http://www.r-project.org/