rpy2

aligning distinct non-facet plots in ggplot2 using Rpy2 in Python

末鹿安然 提交于 2019-11-30 12:33:27
I am combining two distinct plots into a grid layout with grid as suggested by @lgautier in rpy2 using python. The top plot is a density and and the bottom a bar graph: iris = r('iris') import pandas # define layout lt = grid.layout(2, 1) vp = grid.viewport(layout = lt) vp.push() # first plot vp_p = grid.viewport(**{'layout.pos.row': 1, 'layout.pos.col':1}) p1 = ggplot2.ggplot(iris) + \ ggplot2.geom_density(aes_string(x="Sepal.Width", colour="Species")) + \ ggplot2.facet_wrap(Formula("~ Species")) p1.plot(vp = vp_p) # second plot mean_df = pandas.DataFrame({"Species": ["setosa", "virginica",

Difference in Python statsmodels OLS and R's lm

不问归期 提交于 2019-11-30 12:14:54
问题 I'm not sure why I'm getting slightly different results for a simple OLS, depending on whether I go through panda's experimental rpy interface to do the regression in R or whether I use statsmodels in Python. import pandas from rpy2.robjects import r from functools import partial loadcsv = partial(pandas.DataFrame.from_csv, index_col="seqn", parse_dates=False) demoq = loadcsv("csv/DEMO.csv") rxq = loadcsv("csv/quest/RXQ_RX.csv") num_rx = {} for seqn, num in rxq.rxd295.iteritems(): try: val =

Converting an RPy2 ListVector to a Python dictionary

喜你入骨 提交于 2019-11-30 11:31:55
The natural Python equivalent to a named list in R is a dict, but RPy2 gives you a ListVector object. import rpy2.robjects as robjects a = robjects.r('list(foo="barbat", fizz=123)') At this point, a is a ListVector object. <ListVector - Python:0x108f92a28 / R:0x7febcba86ff0> [StrVector, FloatVector] foo: <class 'rpy2.robjects.vectors.StrVector'> <StrVector - Python:0x108f92638 / R:0x7febce0ae0d8> [str] fizz: <class 'rpy2.robjects.vectors.FloatVector'> <FloatVector - Python:0x10ac38fc8 / R:0x7febce0ae108> [123.000000] What I'd like to have is something I can treat like a normal Python

IPython notebook and rmagic/rpy2: cannot find module ri2py (OSX 10.8.5, python 2.7, R 3.1)

徘徊边缘 提交于 2019-11-30 09:46:57
I'm trying to use the rmagic extension for the IPython notebook, using Python 2.7.6 via Enthought Canopy. When I try the following example: import numpy as np import pylab X = np.array([0,1,2,3,4]) Y = np.array([3,5,4,6,7]) pylab.scatter(X, Y) %Rpush X Y %R lm(Y~X)$coef I get an error: AttributeError Traceback (most recent call last) <ipython-input-7-96dff2c70ba0> in <module>() 1 get_ipython().magic(u'Rpush X Y') ----> 2 get_ipython().magic(u'R lm(Y~X)$coef') … /Users/hrob/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/extensions/rmagic.pyc in eval(self, line) 212 res

Accessing functions with a dot in their name (eg. “as.vector”) using rpy2

南笙酒味 提交于 2019-11-30 09:25:18
问题 I am trying to access the "as.vector" R function from within Python, using rpy2. Let's say, for the sake of simplicity, that I want to do something as simple as this using rpy2 (R code): x <- as.vector(c(1, 2, 3)) Since "as.vector" contains a dot in its name, it is not directly available as a member of rpy2.robjects.r According to the documentation, rpy2 replaces dots by underscores for named function parameters, but it doesn't seem to work for the function name itself. I tried eg. "as_vector

Error when loading rpy2 with anaconda

天大地大妈咪最大 提交于 2019-11-30 08:48:45
I am trying to load rpy2 inside a project where I am working with anaconda and I am getting a surprising error for which I cannot find a solution. My python version is 3.4 , my anaconda version is 3.17.0 - I am using a Mac (OSX Yosemite version 10.10.4) R version 3.2.2 (2015-08-14) -- "Fire Safety" Platform: x86_64-apple-darwin11.4.2 (64-bit) try: from rpy2 import robjects except ImportError as e: print(e) I am getting this error message dlopen(/Users/user1/anaconda/lib/python3.4/site-packages/rpy2/rinterface/_rinterface.so, 2): Library not loaded: @rpath/R/lib/libR.dylib Referenced from:

trouble installing rpy2 on win7 (R 2.12, Python 2.5)

空扰寡人 提交于 2019-11-30 05:56:11
问题 I'm brand new to Python (and programming in general) I come from a finance background, so please bear with me. I just started using Python (Enthought's Pylab/Scipy/Numpy) and R for statistical analysis. I'm trying to install rpy2 into Python to integrate R, but I get the error: Tried to guess R's HOME but no R command in the PATH. I'm not sure what this means. The path to my R.exe is "C:\Program Files\R\R-2.12.1\bin" if that's useful. Any help would be much appreciated! Here's setup.py's code

Difference in Python statsmodels OLS and R's lm

浪尽此生 提交于 2019-11-30 01:43:00
I'm not sure why I'm getting slightly different results for a simple OLS, depending on whether I go through panda's experimental rpy interface to do the regression in R or whether I use statsmodels in Python. import pandas from rpy2.robjects import r from functools import partial loadcsv = partial(pandas.DataFrame.from_csv, index_col="seqn", parse_dates=False) demoq = loadcsv("csv/DEMO.csv") rxq = loadcsv("csv/quest/RXQ_RX.csv") num_rx = {} for seqn, num in rxq.rxd295.iteritems(): try: val = int(num) except ValueError: val = 0 num_rx[seqn] = val series = pandas.Series(num_rx, name="num_rx")

How to create an empty R vector to add new items

拈花ヽ惹草 提交于 2019-11-29 19:13:49
I want to use R in Python, as provided by the module Rpy2. I notice that R has very convenient [] operations by which you can extract the specific columns or lines. How could I achieve such a function by Python scripts? My idea is to create an R vector and add those wanted elements into this vector so that the final vector is the same as that in R. I created a seq() , but it seems that it has an initial digit 1, so the final result would always start with the digit 1, which is not what I want. So, is there a better way to do this? vec <- vector() See also vector help ?vector I pre-allocate a

Unable to install R package in python (Jupyter notebook)?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 12:59:48
I am trying to install R package on python 3x in the jupyter notebook. I understand that I have to pip install rpy2 and it has been successful This works fine when I call a built-in function in R such as ccf or other easy issues. # Call function from R import os os.environ['R_USER'] = 'D:\Anaconda3\Lib\site-packages\rpy2' import rpy2.robjects as robjects from rpy2.robjects import pandas2ri pandas2ri.activate() However, if I want to install a package such as DirichletReg or vars , it is not that easy especially that there might be more packages that are required to be downloaded. I indeed