1

I wish to compute transfer entropy in a JupyterLab notebook by calling R's RTransferEntropy package using rpy2 but am encountering problems doing so.

I'm using Anaconda with Python 3.7 and have RStudio installed as one of my environments. Within RStudio I've successfully implemented an example found in the RTransferEntropy package:

 install.packages('RTransferEntropy')
 library(RTransferEntropy)
 set.seed(12345)
 n <- 2500
 x <- rep(0, n + 1)
 y <- rep(0, n + 1)

 for (i in 2:(n + 1)) {
    x[i] <- 0.2 * x[i - 1] + rnorm(1, 0, 2)
   y[i] <- x[i - 1] + rnorm(1, 0, 2)}

 x <- x[-1]
 y <- y[-1]

 library(future)
 plan(multiprocess)
 set.seed(12345)
 shannon_te <- transfer_entropy(x, y)

 shannon_te 

Everything works find and I'm able to obtain the correct results shown in the documentation.

Now I wish to use rpy2. Here's what I enter in my Jupyter notebook:

 from rpy2.robjects.packages import importr
 base = importr('base')
 utils = importr('utils')
 te=rpackages.importr('RTransferEntropy')

I've tried importing just the first two R packages, and have done so with no difficulty. However, when trying to import RTransferEntropy, I receive a lengthy error message, the last few lines being as follows:

 RRuntimeError: Error in loadNamespace(name) : 
 there is no package called ‘RTransferEntropy’
 Calls: <Anonymous> ... tryCatch -> tryCatchList -> tryCatchOne -> 
 <Anonymous>

I'm not sure what's causing the error. Is it perhaps because RTransferEntropy is not in the correct directory?

Also, I do realize there exist transfer entropy computation methods for direct use in Python, e.g. NPEET and JIDT. However, I've been unsuccessful in getting them up and running.

fishbacp
  • 1,123
  • 3
  • 14
  • 29
  • See this `reticulate` (counterpart of `rpy2`) [answer](https://stackoverflow.com/a/55600468/1422451). You may have multiple Python or R versions or multiple R library paths. So `RTransferEntropy` may be installed in a different library path that `rpy2` cannot find but RStudio can find. – Parfait Jun 20 '19 at 16:59
  • 1
    Remember `rpy2` uses the [R_USER and R_HOME](https://stackoverflow.com/q/12698877/1422451) environment variables. So whatever R version is installed in this path, check its default library for `RTransferEntropy` package. – Parfait Jun 20 '19 at 17:03
  • I see what you are both saying. Here's what worked. In R I typed .libPaths() I received /Users/myname/anaconda3/envs/RStudio/lib/R/library and then specified the path inside importr: In Python: from rpy2.robjects.packages import importr importr('RTransferEntropy',lib_loc="/Users/myname/anaconda3/envs/RStudio/lib/R/library"), – fishbacp Jun 20 '19 at 17:13

1 Answers1

1

Thank you. With your insights I found this worked:

Inside R: .libPaths()

The resulting pathname (there was only one) was used in the Jupyter notebook to correctly locate the package:

from rpy2.robjects.packages import importr

importr('RTransferEntropy', lib_loc="pathname")
Parfait
  • 104,375
  • 17
  • 94
  • 125
fishbacp
  • 1,123
  • 3
  • 14
  • 29