I'm trying to implement the R package TSdist from python jupyter notebook.
import rpy2.robjects.numpy2ri
from rpy2.robjects.packages import importr
rpy2.robjects.numpy2ri.activate()
R = rpy2.robjects.r
## load in package
TSdist = importr('TSdist')
## t,c are two series
dist = TSdist.ERPDistance(t.values,c.values,g=0,sigma =30)
## dist is a R Boolean vector with one value
dist[0]
This gives me an NA and I got a warning:
/usr/lib64/python3.4/site-packages/rpy2/rinterface/init.py:186: RRuntimeWarning: Error : The series must be univariate vectors
warnings.warn(x, RRuntimeWarning)
Any ideas of how to properly implement it? Or how to measure time series similarity with python packages using discrete Fourier transforms (DFT), Auto-regressive coefficient, Edit distance on real sequence(EDR). Methods mentioned in this paper.
package comes with a Python engine you can use in R Markdown. Reticulate allows you to run chunks of Python code, print Python output, access Python objects, and so on. Easy, right? You can import any Python library and write any Python code you want, and then access the variables and functions declared with R.
Likely the reason is the two series objects being passed into the method. Assuming series mean pandas series, calling values
returns a numpy array. And per the docs, the ERPDistance
expects numeric vectors, not arrays.
print(type(pd.Series(np.random.randn(5))))
# <class 'pandas.core.series.Series'>
print(type(pd.Series(np.random.randn(5)).values))
# <class 'numpy.ndarray'>
Consider simply casting series as numeric vectors with base R or use rpy2's FloatVector
:
from rpy2.robjects.packages import importr
R = rpy2.robjects.r
## load in package
base = importr('base')
TSdist = importr('TSdist')
new_t = base.as_numeric(t.tolist())
print(type(new_t))
# <class 'rpy2.robjects.vectors.FloatVector'>
new_c = rpy2.robjects.FloatVector(c.tolist())
print(type(new_c))
# <class 'rpy2.robjects.vectors.FloatVector'>
## new_t, new_c are now numeric vectors
dist = TSdist.ERPDistance(new_t, new_c, g=0, sigma =30)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With