My R Environment
I updated my R installation and libraries recently, and I decided to take a moment to explain how I configure R and install libraries to aid me in quant.fin exploration.
But first, disclaimers: I am not a professionally-trained quant, simply a hobbyist "dinking around". You have been warned. I'm running Ubuntu 11.10, and these instructions will be geared toward my environment. They will probably also work on Debian, although I have not tested them there. Package names and installation methodology for other Linux distros (Redhat/Centos/etc.) and Mac OS will differ, although R build methodology and R library installation should not. I will provide a script to accomplish the installation of my preferred libraries, mentioned toward the end of the post. Please do not run it blindly; if you want to use the script, read the post and the script before running.
So, let's get down to brass tacks. In order to get a suitable R environment set up, the first thing I need to do is, of course, install R. I like to run the latest stable R release, and the best way I've found to do this is to build it myself, negating any problems with my distro's official packages lagging behind the R releases. Since I'm on Ubuntu, I do have the option of installing the R project's Ubuntu packages, but this can cause interesting problems, particularly when updating core R libraries.
To build R from source, I must first prep my Linux environment with the necessary tools. I run two commands, the first to install a suitable build environment (with the X.Org headers, so that R can link with the libraries necessary for graphical output), and the second to pull in packages specifically necessary to build R (the FORTRAN compiler, math libraries, TeX Live, etc.):
$ sudo apt-get install build-essential xorg-dev
$ sudo apt-get build-dep r-base
I also run a third command, but it's not absolutely necessary—this installs a bunch of extra TeX Live fonts, one of which R likes to use when building PDF manuals and vignettes for packages:
$ sudo apt-get install texlive-fonts-extra
Once equipped to build R, I need the R source code. CRAN indicates the latest R version, which at the time of this writing is 2.14.1. The same page also provides a link to the source tarball, which I download:
$ mkdir -p ~/dev && cd ~/dev
$ wget http://cran.r-project.org/src/base/R-2/R-2.14.1.tar.gz
$ tar zxf R-2.14.1.tar.gz
$ cd R-2.14.1
and then configure, compile, and install, specifying an installation directory inside of my home directory:
$ ./configure --prefix=~/R-2.14.1 && make && make install
Then I grab another cup of coffee. If everything goes according to plan, I soon have a brand new R installation!
Now R is built and installed, but not in my path. I add the following line to the end of my ~/.profile (to add the R binaries to my path for any future login sessions):
export PATH=~/R-2.14.1/bin:$PATH
and source the file (to bring the path to the R binaries into this session):
$ source ~/.profile
Now my R installation should be working:
$ R --version
R version 2.14.1 (2011-12-22)
(further output truncated)
It's a good moment to update any core R libraries that might be out of date, so I run:
$ Rscript -e 'update.packages(repos=getCRANmirrors(local=T)$URL[70], ask=F)'
and watch as a few packages are updated.
Now it's time to install the add-ons I like to use for quant work. The list is by no means comprehensive, simply things I've found indispensible. They are (descriptions sometimes shamelessly copied from respective webpages):
- xts: The eXtensible Time Series. The preferred way of storing and manipulating finance-related time series in R.
- quantmod: A rapid prototyping environment, where quant traders can quickly and cleanly explore and build trading models.
- blotter/quantstrat: Specify, build, and back-test quantitative financial trading and portfolio strategies.
- FactorAnalytics: Functions for performing factor analysis on financial time series.
- PerformanceAnalytics: Econometric tools for performance and risk analysis.
- PortfolioAnalytics: Portfolio analysis, including numeric methods for optimization of portfolios.
Because these packages are moving targets and are updated frequently, I don't like to rely on the CRAN packages. Instead, I build them from their Subversion source trees, located on R-Forge. This is a somewhat tedious process, so I've automated it using a shell script that can be found here.
After running the script, I have a robust environment for quant.fin research! Next step: do work!




Comments [0]