Rsafd
Rsafd is an R package that provides statistical analysis of financial data.
Rsafd and Jupyter Notebook
macOS
Paste each of the following commands into the Terminal app. You can find the Terminal app inside the Utilities folder found within the Applications folder, or search for “Terminal”.
- Install the Apple Developer Command Line Tools.
xcode-select --install
- Download Miniconda.
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-`uname -m`.sh -o ~/Downloads/miniconda.sh
- Install Miniconda.
chmod +x ~/Downloads/miniconda.sh ; ~/Downloads/miniconda.sh -bu
- Initialize Miniconda.
~/miniconda3/bin/conda init bash ; ~/miniconda3/bin/conda init zsh
- Close and re-open the Terminal.
- Configure the conda command to prioritize the Apple channel first, the Conda Forge channel second.
conda config --add channels conda-forge --add channels apple
- Create a conda environment named Renv.
conda create -y -n Renv tensorflow keras jupyter r-irkernel r-hmisc
This step requires conda to resolve a complex set of dependencies such that each requested software component can install. This short list should take tens of minutes, not hours. If you’re having trouble, try the Alternative Environment Creation Method and then return to complete the remaining steps.
- Activate the Renv environment.
conda activate Renv
- Correct for a bug in the environment installation.
[ -f $CONDA_PREFIX/lib/gcc/arm64-apple-darwin20.0.0/11.3.0 ] && ln -s $CONDA_PREFIX/lib/gcc/arm64-apple-darwin20.0.0/11.3.0 $CONDA_PREFIX/lib/gcc/arm64-apple-darwin20.0.0/11.0.1
- Install the necessary R packages.
PKG_CPPFLAGS="-DHAVE_WORKING_LOG1P" Rscript -e "install.packages(c('timeDate', 'quadprog', 'quantreg', 'plot3D', 'robustbase', 'scatterplot3d', 'splines', 'tseries', 'glasso', 'qgraph', 'reticulate', 'keras', 'rgl', 'glmnet'), repos='https://cran.rstudio.com')"
- Download the Rsafd R package.
curl -L https://carmona.princeton.edu/orf505/Rsafd.zip -o ~/Downloads/Rsafd.zip
- Decompress the Rsafd R package.
unzip -o ~/Downloads/Rsafd.zip -d ~/Downloads
- Install the Rsafd R package.
Rscript -e "install.packages('~/Downloads/Rsafd', repos = NULL, type='source')"
- Close Terminal.
Each time you wish to work with a Jupyter Notebook for the class, open the Terminal app and then run the following commands.
- Activate the Renv conda environment.
conda activate Renv
If you have a previously-installed version of Anaconda, you may need to use the following to activate the environment.
conda activate $HOME/miniconda3/envs/Renv
- Open Jupyter.
jupyter notebook
Google Colab
Paste each of the following commands into a new cell of a Google Colab notebook that is set to use the R runtime.
The runtime is set under the Runtime menu by clicking on Change Runtime Type.
- Install the necessary R packages.
install.packages(c('timeDate', 'quadprog', 'quantreg', 'plot3D', 'robustbase', 'scatterplot3d', 'splines', 'tseries', 'glasso', 'qgraph', 'reticulate', 'keras', 'rgl', 'glmnet'), repos='https://cran.rstudio.com')
- Download the Rsafd package.
rsafd_url <- 'https://carmona.princeton.edu/orf505/Rsafd.zip' download.file(rsafd_url, destfile = 'Rsafd.zip')
- Decompress the Rsafd package.
unzip('Rsafd.zip')
- Install the Rsafd package.
install.packages('Rsafd', repos = NULL, type = 'source')
Windows
We will use the Windows Subsystem for Linux and the Ubuntu Linux app as the foundation of our development environment. This will have the additional benefit of our environment working like other Linux systems, from cloud-provided servers to high performance computing environments.
Prepare the Linux Subsystem
- Search for and open the “Turn Windows Features On and Off” control panel on your computer.
- Check the box labeled “Virtual Machine Platform”, click the Ok button and then restart.
- Install Windows Subsystem for Linux (WSL) via the Microsoft Store.
- Install Ubuntu via the Microsoft Store.
- Open the Ubuntu app to complete the Linux installation process. Provide a username and password when prompted. These can optionally match those you already use to log in to your computer.
Install and Configure the Development Environment
Each of the remaining commands can be pasted into the Ubuntu app window.
- Download Miniconda.
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-`uname -m`.sh -o ~/miniconda.sh
- Install Miniconda.
chmod +x ~/miniconda.sh ; ~/miniconda.sh -bu
- Initialize Miniconda, then close and re-open the Ubuntu app.
~/miniconda3/bin/conda init bash ; ~/miniconda3/bin/conda init zsh
- Configure the conda command to prioritize the Conda Forge channel first, then the default.
conda config --add channels conda-forge
- Create a conda environment named
Renv
.conda create -y -n Renv unzip jupyter==1.0.0 tensorflow==2.12.1 r-irkernel==1.3.2 keras==2.12.0 r-hmisc
This step requires conda to resolve a complex set of dependencies such that each requested software component can install. This short list should take tens of minutes, not hours. If you’re having trouble, try the Alternative Environment Creation Method and then return to complete the remaining steps.
- Activate the Renv environment.
conda activate Renv
- Install the necessary R packages.
Rscript -e "install.packages(c('timeDate', 'quadprog', 'quantreg', 'plot3D', 'robustbase', 'scatterplot3d', 'splines', 'tseries', 'glasso', 'qgraph', 'reticulate', 'keras', 'rgl'), repos='https://cran.rstudio.com')"
- Download and extract the Rsafd R package.
curl -L https://carmona.princeton.edu/orf505/Rsafd.zip -o Rsafd.zip ; unzip -o ~/Rsafd.zip -d ~
- Install the Rsafd R package.
Rscript -e "install.packages('~/Rsafd', repos = NULL, type='source')"
- Close the Ubuntu app.
Each time you wish to work with a Jupyter Notebook for the class, open the Ubuntu app and then run the following commands.
- Activate the Renv conda environment.
conda activate Renv
- Start Jupyter.
jupyter notebook
- Visit the Jupyter web interface.
Note that for files and folders to be accessible to Jupyter, you will need to copy them to the Linux disk that appears along the left side of the File Explorer window.
Alternative Environment Creation Method
If the creation of the environment is taking 30 minutes or more on your computer or is otherwise incomplete, download the file corresponding to your architecture, then use the command conda create --name Renv --file <file_name>
to create the environment, substituting the file’s name and location for <file_name>
.
This method skips dependency resolution by providing conda a predefined list of software versions.