Programming: Lynx Reader for R/Rmetrics

The Rmetrics package fImport has now a function "read.lynx()" which simplifies the download of economic and financial market data from the Internet and the conversion of the data records into time series objects.

Lynx Reader for Economic and Financial Market Data Download in R/Rmetrics
In the following we present how to install and use the Lynx Text Browser under Windows to read from R and easily to convert economic and financial market data into a convenient time series format. Lynx was originally designed for Unix. But versions are also available for almost all Microsoft Windows releases, Linux and Mac OS X. You can access the home page of the browser via the link http://lynx.isc.org . There you can find the current development sources, the main help page, the current User Guide.

Cygwin Win32 Installation
Our preferred way to use the Lynx Text Browser is to call the function lynx.exe from a cygwin installation. To learn how to install cygwin under Windows we refer to http://www.cygwin.com . Cygwin is a command-line interface which allows to use the Lynx Text Browser within the Windows operating context. When you have installed cygwin, please do not forget to add the location of the lynx.exe binary to the search path of Windows.

The Minimalist Win32 Installation
A Win32 standalone installation could be an alternative option if you like to avoid the tour on the cygwin installation. You can find many pointers on the Internet to lynx binaries but none to our experience is working properly together with R. Thus we have prepared a zip file with the necessary files from the cygwin distribution which you can download from the Rmetrics R-Forge repository.

Then unzip the downloaded file and copy the cygwin folder to your "Computer". i.e. place it under "C:\cygwin". Please do not forget to add the location of the lynx.exe binary, i.e. "C:\cygwin\bin", to the search path of your windows environment. That’s all.

Example: CFNAI Indicator Download
As an example we present how to download the data for the Chicago Fed National Activity Index (CFNAI) which is a monthly index designed to gauge overall economic activity and related inflationary pressure. From the table on their web site

http://www.chicagofed.org/webpages/research/data/cfnai/current_data.cfm

we extract the following 5 indicators
(i) Production and Income P&I,
(ii) Employment, Unemployment and Hours EU&H,
(iii) Personal Consumption and Housing C&H,
(iv) Sales, Orders, and Inventories SO&I, and
(v) the CFNAI index itself CFNAI.

The following R code snippet does the work for us

> # 1 Load Library from CRAN:
> library(fImport)
> 
> # 2 Read and Clean Data:
> URL = "http://www.chicagofed.org/webpages/research/data/cfnai/current_data.cfm"
> X = read.lynx(URL)
> X = X[grep("   ....:.. ", X)]
> X = gsub("  ", " ", X)
> 
> # 3 Split the downloaded data and add column and row Names:
> Data = dataSplit(X, split = " ", col = 4:8)
> rownames(Data) = paste(substr(X, 3, 6), substr(X, 8, 9), "01", sep = "-")
> colnames(Data) = c("P&I", "EU&H", "C&H", "SO&I", "CFNAI")
> 
> # 4 Revert the time ordering and convert the data to a timeSeries object:
> CFNAI = rev(as.timeSeries(Data))
> time(CFNAI)<- timeLastDayInMonth(time(CFNAI))
> tail(CFNAI)
GMT
            P&I  EU&H   C&H  SO&I CFNAI
2009-10-31 0.01 -0.50 -0.46  0.06 -0.90
2009-11-30 0.30  0.11 -0.41  0.16  0.15
2009-12-31 0.11 -0.23 -0.47  0.16 -0.45
2010-01-31 0.44  0.05 -0.45 -0.06 -0.05
2010-02-28 0.04 -0.13 -0.41  0.06 -0.44
2010-03-31 0.18  0.12 -0.42  0.06 -0.07

Reference
Financial Market Data for R/Rmetrics
, Diethelm W\ürtz, Andrew Ellis, Yohan Chalabi, Rmetrics eBooks 2010, Finance Online Publishing, Zurich