Rsafd
Rsafd is an R package that provides statistical analysis of financial data.
Contents
Google Colab
Multiple steps require switching the runtime between Python and R; switch the runtime for a step from the Change Runtime Type option on the Runtime menu.
- Switch to a Python runtime, then execute the following code to connect your Google Drive.
from google.colab import drive drive.mount('/content/drive')
- Switch to an R runtime, then execute the following code to install the Rsafd dependencies.
drive_lib <- "/content/drive/MyDrive/Rlibs" dir.create(drive_lib, showWarnings = FALSE, recursive = TRUE) .libPaths(c(drive_lib, .libPaths())) install.packages(c('timeDate', 'quadprog', 'quantreg', 'plot3D', 'robustbase', 'scatterplot3d', 'splines', 'tseries', 'glasso', 'qgraph', 'reticulate', 'keras', 'rgl', 'glmnet'), repos='https://cran.rstudio.com', lib = drive_lib)
- Download the Rsafd R package, decompress, and install by copying the Rsafd folder to the Rlibs folder in your Google Drive.
Each time you wish to work with a Colab Notebook and run library(Rsafd)
, run the following commands first.
- Switch to a Python runtime and reconnect your Google Drive.
from google.colab import drive drive.mount('/content/drive')
- Switch to an R runtime.
drive_lib <- "/content/drive/MyDrive/Rlibs" dir.create(drive_lib, showWarnings = FALSE, recursive = TRUE) .libPaths(c(drive_lib, .libPaths()))
Local Install
Local installation utilizes a conda environment.
macOS
All commands are executed from Terminal and assume an existing installation of conda. Install Miniconda if you do not have this command available.
- Install the Apple Developer Command Line Tools.
xcode-select --install
- Create the
Renv
environment.conda create -n Renv --file <(curl -s https://orfe.io/renv-spec-macm.txt)
- Activate the environment and install the necessary R packages.
conda activate Renv 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, decompress, and install the Rsafd R package.
curl -sL $(curl -sL https://api.github.com/repos/PrincetonUniversity/Rsafd/releases/latest | jq -r '.assets[] | select(.name == "Rsafd.zip") | .browser_download_url') -o ~/Downloads/Rsafd.zip unzip -o ~/Downloads/Rsafd.zip -d ~/Downloads/Rsafd cp -r ~/Downloads/Rsafd ${CONDA_PREFIX}/lib/R/library
- Close Terminal.
Each time you wish to work with a Jupyter Notebook and run library(Rsafd)
, open the Terminal app and then run the following commands.
- Activate the Renv conda environment.
conda activate Renv
- Open Jupyter.
jupyter notebook
Windows
All commands require the Windows Subsystem for Linux (WSL) and the Ubuntu Linux app. The steps that follow assume WSL is not already installed and the WSL installation does not contain a conda installation.
Prepare WSL
- 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 jq unzip jupyter==1.0.0 tensorflow==2.12.1 r-irkernel==1.3.2 keras==2.12.0 r-hmisc
- 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 -sL $(curl -sL https://api.github.com/repos/PrincetonUniversity/Rsafd/releases/latest | jq -r '.assets[] | select(.name == "Rsafd.zip") | .browser_download_url') -o ~/Rsafd.zip ; unzip -o ~/Rsafd.zip -d ~/Rsafd
- Install the Rsafd R package.
cp -r ~/Rsafd ${CONDA_PREFIX}/lib/R/library
- Close the Ubuntu app.
Each time you wish to work with a Jupyter Notebook and load the library withlibrary(Rsafd)
, open the Ubuntu app and then run the following commands first.
- Activate the Renv conda environment.
conda activate Renv
- Start Jupyter.
jupyter notebook
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.
Docker Container
If you have an installation of Docker Desktop, a pre-built Docker image is available that includes Python, R, Rsafd, dependencies, and Jupyter.
Open Docker Desktop and run the command appropriate for your platform in the Docker Desktop Terminal window.
If you receive a “Dead Kernel” message when attempting to load large datasets, you may need to adjust Docker Desktop’s memory allocation. See the Troubleshooting section for more details.
macOS
docker run -p 8888:8888 -e JUPYTER_LINK_ONLY=1 -v "$HOME":/workspace/notebooks ghcr.io/princetonuniversity/rsafd-docker:latest
Windows (PowerShell)
docker run -p 8888:8888 -e JUPYTER_LINK_ONLY=1 -v "${env:USERPROFILE}:/workspace/notebooks" ghcr.io/princetonuniversity/rsafd-docker:latest
Linux
docker run -p 8888:8888 -e JUPYTER_LINK_ONLY=1 --user $(id -u):$(id -g) -v "$HOME":/workspace/notebooks -e HOME=/workspace/notebooks --workdir /workspace/notebooks ghcr.io/princetonuniversity/rsafd-docker:latest
Open the printed URL in your browser to begin working with notebooks; see the README for further details.