I am attempting to use the R "NADA" package using the rpy2 interface in Python. The end goal is to perform survival analysis on left-censored environmental data. Things seem to be interacting correctly between Python and R for other functions, and I am able to perform a test function in R, but I get an error when attempting the same through rpy2.
This is my code in Python. It is entirely fictitious data.
from rpy2.robjects import FloatVector, BoolVector, FactorVector
from rpy2.robjects.packages import importr
nada = importr('NADA')
obs = FloatVector([1.0,2.0,3.0,5.0,56.0,1.0,4.0])
nds = BoolVector([False, True, True, True, True, False, True])
groups = FactorVector([1,0,1,0,1,1,0])
nada.cendiff(obs, nds, groups)
This is the error message I receive:
Traceback (most recent call last):
File "C:/Users/XXXXXXX/rpy2_test.py", line 9, in <module>
nada.cendiff(obs, nds, groups)
File "C:\Program Files\Python35\lib\site-packages\rpy2\robjects\functions.py", line 178, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\rpy2\robjects\functions.py", line 106, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in terms.formula(tmp, simplify = TRUE) :
invalid model formula in ExtractVars
This code works fine in the R terminal:
library("NADA")
cendiff(c(1.0,2.0,3.0,5.0,56.0,1.0,4.0), c(FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE), factor(c(1,0,1,0,1,1,0)))
I tried adding some print lines at the rpy2 error lines listed, and suspect there may be an issue with rpy2 removing the levels from the factor vector when sending them to the function. However, I'm in new territory and that may just be a red herring.
If anyone can lend some insight or offer an alternative, I would appreciate it. I have a lot of data processing coded in Python and going all R isn't a good option, but R has more analysis options so I was hoping rpy2 would do the trick.