Getting started with R

The R programming enviroment

R is free under the terms of GNU General Public License and it is available from the R Project website, for all common operating systems, including Linux, MacOS and Windows. To download it follow the CRAN link and select the mirror site nearest to you.
An appropriate  IDE for R is R Studio, which can be obtained for free under the same GNU free software license (there is also a link to R Studio from the CRAN). By the way, CRAN stands for Comprehensive R Archive Network.
The best place to start learning   R is at the   home page of the R Project. Once there, go to Manuals and download  An Introduction to R; afterwards,  go to Books and browse through the large list of R related literature. Each package comes also with an interactive Help detailing the usage of each function, together with examples.

Installing R packages and obtaining financial data.

After downloading and installing R,  tune it up with  the  contributed packages for doing serious financial time series analysis and econometrics  listed below. Except for RMetrics, to install a  package  XX  from the R console enter the command:


and once installed, to load it into your R workspace, type:


For example, to install and load the package quantmod type:


RMetrics is a suite of packages for financial analytics. It contains all the package prefixed with f (e.g. fArma, fGarch, etc.) and others; to install all these packages within the RMetrics set, type:


Then load each package with the library( ) command (e.g.,  library(fArma)). For more details and tutorials on RMetrics go to

Installing packages from R Studio is easier:  just click on the Packages window, then on  Install Packages and in the pop-up window type the code name of the package. Make sure to select Install all dependencies.

The packages for financial applications used in the R Labs are the following: fArma, fGarch, fOptions, fExoticOptions, RMetrics, e1071, kernlab, nnet, caret, Metrics, PerformanceAnalytics, quantmod, TTR, tseries, xts and AER.

Financial data can be freely  obtained from various sources. For example:  Yahoo finance, Google finance, MSN Moneycentral, and the Federal Reserve Bank of St. Louis  - FRED. One can obtain the data manually by directly accessing these sites, or access the server through various functions built in some of the R packages (e.g. getSymbols from quantmod ).

Example:  obtaining the data for Apple Inc. stock from Yahoo, saving it to a working directory, whose path is in a variable string wdir, and later retrieving the data with readRDS:

wdir = "<path-to-working-directory>"
library(quantmod); library(xts)
saveRDS(AAPL, file="Apple.rds")
Apple = readRDS("Apple.rds")

The getSymbols retrieves and converts financial data in xts format. This is an ``extensible time-series object", and among other amenities it allows to handle the indices of the time series in Date format. We need to load library(xts) to handle   xts objects.

Based on our own experience,  it seems  that the only way to save into disk all the information contained in an xts object is with saveRDS() which saves files in  binary format. This in fact makes the data files  portable across all platforms. To reload the data from your working directory into the R working environment use readRDS().

Sometimes we also get files from other sources in .csv format. In that case it is save in disk with save command, and read from disk with read.csv() or read.zoo().