{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Calling R Functions in IPython\n", "\n", "# Installation Instructions on Windows\n", "%rmagic can be difficult to install/setup. These are the steps that worked for me. Please feel free to\n", "suggest additions, subtractions and edits that make the process easier. I advise you take the steps below in order if you can. \n", "There can be some discrepencies when the environment variables do not exist when *rpy2* is installed.\n", "\n", "# 1. Install R\n", "[http://www.r-project.org/](The R Project for Statistical Computing)\n", "\n", "# 2. Set Environment Variables\n", "* Add the path to R.dll to the environment variable PATH (C:\\Program Files\\R\\R-3.0.3\\bin\\i386)\n", "* Add an environment variable R_HOME (C:\\Program Files\\R\\R-3.0.3 in my case)\n", "* Add an environment variable R_USER (simply my username in Windows)\n", "\n", "# Python Modules\n", "You can use pip or easy_install for many of these packages, however on Windows, I suggest you visit the Unofficial \n", "Windows Binaries for Python Extension Packages created by Christoph Gohlke at UC Irvine: \n", "[http://www.lfd.uci.edu/~gohlke/pythonlibs/](http://www.lfd.uci.edu/~gohlke/pythonlibs/)\n", "* pyreadline\n", "* pyzmq\n", "* tornado\n", "* jinja2\n", "* ipython\n", "* rpy2\n", "* Python for Windows Extensions [http://sourceforge.net/projects/pywin32/](http://sourceforge.net/projects/pywin32/)\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import Modules" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as NUM\n", "import pylab as PYLAB\n", "import arcpy as ARCPY\n", "import numpy as NUM\n", "import SSDataObject as SSDO\n", "import scipy as SCIPY\n", "import pandas as PANDAS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initialize Data Object, Select Fields and Obtain Data\n", "\n", "### Use Case - Using Regression Trees to Analyze 2008 Presidential Vote in California Counties" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "inputFC = r'../data/CA_Polygons.shp'\n", "ssdo = SSDO.SSDataObject(inputFC)\n", "ssdo.obtainData(ssdo.oidName, ['PCR2008', 'POPDEN08', 'PERCNOHS', 'MAJORO'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Make Use of PANDAS Data Frame" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ids = [ssdo.order2Master[i] for i in range(ssdo.numObs)]\n", "convertDictDF = {}\n", "for fieldName in ssdo.fields.keys():\n", " convertDictDF[fieldName] = ssdo.fields[fieldName].data\n", "df = PANDAS.DataFrame(convertDictDF, index = ids)\n", "print(df[0:5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Push PANDAS Data Frame to R Data Frame - Use the -i flag" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load_ext rpy2.ipython\n", "#%reload_ext rpy2.ipython\n", "\n", "%R -i df " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analyze in R" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%R library(rms)\n", "%R logit = lrm(MAJORO ~ PCR2008 + POPDEN08 + PERCNOHS, data = df, x = TRUE, y = TRUE)\n", "%R z_scores = logit$coefficients / sqrt(diag(logit$var))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pull Results Back to Python - Use the -o flag" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%R -o logit_coef logit_coef = logit$coefficients\n", "%R -o p_values p_values = pnorm(abs(z_scores), lower.tail = FALSE) * 2.0\n", "\n", "print(\"Coefficients\")\n", "py_coef = NUM.array(logit_coef)\n", "print(py_coef)\n", "\n", "print(\"p_values\")\n", "py_pvalues = NUM.array(p_values)\n", "print(py_pvalues)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.4" }, "widgets": { "state": {}, "version": "1.1.2" } }, "nbformat": 4, "nbformat_minor": 0 }