Using R's arima function in python with rpy2 - r

I am using rpy2 to call R functions in python3.4 and I'm struggling with calling the arima function.
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
import pandas as pd
from rpy2.robjects import pandas2ri
ts=robjects.r('ts')
forecast=importr('forecast')
pandas2ri.activate()
traindf=pd.read_csv('MARUTI.NS.csv',index_col=0)
traindf.index=traindf.index.to_datetime()
rdata=ts(traindf.Close,frequency=1)
fit=forecast.arima(rdata,c=(1,0,0)) # error occurs here
forecast_output=forecast.forecast(fit,h=4,level=(95.0))
print(forecast_output)
Error:
AttributeError: 'InstalledSTPackage' object has no attribute 'arima'.

This error meant that there was no ARIMA function in forecast. You can find it in an R environment using ?ARIMA
The forecast object is forecast.auto_arima. Alternatively, you could import stats and run the arima.
stats = importr("stats")
fit=stats.arima(rdata,c=(1,0,0)) \

Related

Cannot import specific function in Jupyter notebook, however, some function in the same "~.py" can be imported

I have utils.py and there are two functions: runrealcmd, mol_with_atom_index
If I try to import the two function with the following code:
from utils import mol_with_atom_index, runrealcmd
It fails to import runrealcmd. The error message is like below:
ImportError: cannot import name 'runrealcmd' from 'utils'
However, if I try to import only the mol_with_atom_index with the following code:
from utils import mol_with_atom_index
It successes. The function of mol_with_atom_index can be imported in Jupyter notebook.
However, the function of runrealcmd cannot be imported in Jupyter notebook although both of the two functions are in the same utils.py file.
ImportError: cannot import name 'runrealcmd' from 'utils'
utils.py
from subprocess import Popen, PIPE, STDOUT
from IPython.core.magic import register_line_magic
#register_line_magic
def runrealcmd(command):
# Display instantly: https://stackoverflow.com/questions/52545512/realtime-output-from-a-shell-command-in-jupyter-notebook
process = Popen(command, stdout=PIPE, shell=True, stderr=STDOUT, bufsize=1, close_fds=True)
for line in iter(process.stdout.readline, b''):
print(line.rstrip().decode('utf-8'))
process.stdout.close()
process.wait()
def mol_with_atom_index(mol):
for atom in mol.GetAtoms():
atom.SetAtomMapNum(atom.GetIdx())
return mol
(Jupyter notebook) If you want to import the same-named function (previously imported with the same name) from the other path, you should restart the kernel first.

How to use Python block in R markdown?

I am trying to use a Python block in my R markdown document. Below is what I have been trying:
"""{r setup, include=FALSE}
require(reticulate)
use_python("/Users/hyunjindominiquecho/opt/anaconda3", required = T)
"""
"""{python}
# import the necessary python packages
import numpy as np
import pandas as pd
import scipy.stats as st
from rpy2 import robjects
import math
from scipy.optimize import newton
import torch
from pandas import dataframe
from statistics import mean
"""
but when I try to do this, R studio shows the following error:
Error in system2(command = python, args = paste0("\"", config_script, :
error in running command
How can I resolve this issue? Thank you,
What if you did:
```{python, engine.path = '/Users/hyunjindominiquecho/opt/anaconda3'}
import sys
print(sys.version)
```
What happens when you only run the first chunk? Do you still get the same error?

NameError: name 'np' is not defined

I try to import the following functions:
import numpy as np
def load_labels(path):
y = np.load(path)
return y
def print_sentence():
print("hi")
from a Jupyter notebook, with name "save_load" into another Jupyter notebook with the following code:
!pip install import-ipynb
import import_ipynb
import save_load
from save_load import load_labels, print_sentence
The function print_sentence works fine in the notebook, but with the function load_labels I receive the following error:
NameError: name 'np' is not defined
What could be the reason for this error? I've imported numpy as np in both notebooks.
In "save_load" instead of import numpy as np try import numpy, it worked for me.
You can try this:
import numpy as np
or
from numpy import *
I had the same problem when I was fixing my codes on VScode. Try saving the file. Then run it again.

Rpy2 - calling functions with a dot in their name

I am trying to call the R function memory.limit, with the argument 'size', using rpy2. I followed the instructions given here: Accessing functions with a dot in their name (eg. "as.vector") using rpy2, but cannot get it to work.
This is what I have tried:
from rpy2.robjects.packages import importr
from rpy2 import robjects
utils = importr('utils')
memory_limit = robjects.r("memory.limit")
utils.memory_limit(size=50000)
and also
from rpy2 import robjects
memory_limit = robjects.r("memory.limit")
memory_limit(size=50000)
In both cases I get the following error:
RRuntimeError: Error in (function (size = NA) : incorrect argument
Can you tell me what I am doing wrong?
I guess by now you have solved this, but I would do like this:
import rpy2.robjects as robjects
memory_limit = robjects.r('utils::memory.limit')
memory_limit(size=50000)

importerror trying to convert pandas dataframe to R data frame

I am attempting to convert some pandas dataframes to R objects for use in rpy2.
I am getting an error when i try to import the conversion the module. from rpy2.robjects import pandas2ri yields ImportError: cannot import name 're_type' as follows:
/opt/conda/lib/python3.6/site-packages/pandas/core/dtypes/inference.py in <module>()
6 from collections import Iterable
7 from numbers import Number
----> 8 from pandas.compat import (PY2, string_types, text_type,
9 string_and_binary_types, re_type)
10 from pandas._libs import lib
ImportError: cannot import name 're_type'
I haven't really seen any discussion of this error elsewhere.
in terms of dependencies, i am using pandas 0.23.4, rpy2 2.9.4, R 3.4, and it is running on the docker jupyter container datascience-notebook
Really hoping someone could help me here!

Resources