2

Python プログラミング言語は、財務データ分析アプリケーションの開発に大いに役立ちました。あるいは、データ分析用の R もあります。これには、専用の財務データ分析パッケージがあります (例: quantmod )

現在、これらの両方の言語 (つまり、Python と R) の間のインターフェースとなる rpy2があります。Python の機能を quantmod パッケージで使用して、いくつかの財務データ分析アプリケーションのプロトタイプを作成したいと考えています。

ここまでで、rpy2(python パッケージ) を使用して quantmod 関数を呼び出す Python プログラミング言語のクイック スターター コード例をインターネットで検索するのに数時間費やしました。これまでのところ、適切な資料を見つけることに成功していません... rpy2 と quantmod のドキュメントは別として。

したがって、質問は次のとおりです=>

  1. rpy2を使用してpythonとquantmodを始めるのに適したリソースを知っている人はいますか?
  2. あるいは、rpy2 を使用して quantmod 関数を呼び出す pythonic コードの簡単な例を投稿できますか?

これは、rpy2 と quantmod を使用してプロトタイプを実装する際の私の試みです。

from rpy2.robjects.packages import importr

sta = {"skeleton.TA": "skeleton_dot_TA", "skeleton_TA": "skeleton_uscore_TA"}
quantmod = importr('quantmod', robject_translations = sta)

IBM = quantmod.getSymbols("IBM")

上記のコード (quantmodplot.py) の問題は、次のように "RuntimeError" が生成されることです。

 As of 0.4-0, ‘getSymbols’ uses env=parent.frame() and
 auto.assign=TRUE by default.

 This  behavior  will be  phased out in 0.5-0  when the call  will
 default to use auto.assign=FALSE. getOption("getSymbols.env") and 
 getOptions("getSymbols.auto.assign") are now checked for alternate defaults

 This message is shown once per session and may be disabled by setting 
 options("getSymbols.warning4.0"=FALSE). See ?getSymbol for more details
Error in as.character(sc[[1]]) : 
  cannot coerce type 'closure' to vector of type 'character'
Traceback (most recent call last):
  File "quantmodplot.py", line 6, in <module>
    IBM = quantmod.getSymbols("IBM")
  File "/usr/local/lib/python2.7/dist-packages/rpy2-2.3.6-py2.7-linux-i686.egg/rpy2/robjects/functions.py", line 86, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/rpy2-2.3.6-py2.7-linux-i686.egg/rpy2/robjects/functions.py", line 35, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in as.character(sc[[1]]) : 
  cannot coerce type 'closure' to vector of type 'character'

あなたの助けは大歓迎です...

4

2 に答える 2

1

今日、私は次のアプローチもテストしました...つまりrpy2.robjects、Python内でRスクリプトを呼び出す順序で明示的に呼び出します。このようなアプローチの例はIn [5]: In [6]:、以下の python スクリプトの行です。興味深いことに、このアプローチは機能しているようです。

In [1]: from rpy2.robjects import r
In [2]: from rpy2.robjects.packages import importr
In [3]: sta = {"skeleton.TA": "skeleton_dot_TA", "skeleton_TA": "skeleton_uscore_TA"}
In [4]: quantmod = importr('quantmod', robject_translations = sta)
In [5]: IBM = r("getSymbols('IBM', src='google', from='2013-01-01')")
 As of 0.4-0, ‘getSymbols’ uses env=parent.frame() and
 auto.assign=TRUE by default.

 This  behavior  will be  phased out in 0.5-0  when the call  will
 default to use auto.assign=FALSE. getOption("getSymbols.env") and 
 getOptions("getSymbols.auto.assign") are now checked for alternate defaults

 This message is shown once per session and may be disabled by setting 
 options("getSymbols.warning4.0"=FALSE). See ?getSymbol for more details

In [6]: r("candleChart(IBM, TA=NULL)")
Out[6]: <RS4 - Python:0xae3a14c / R:0xaae455c>

ただし、そのような python スクリプトを実行すると =>quantmod.getSymbols("IBM")が生成されRRuntimeErrorます。
したがって、主要な問題はまだ解決されていません。

さて、私の開発マシンに関する環境の詳細は次のとおりです。

OS: Ubuntu Linux 12.04
Python version: '2.7.3'
Python package: rpy2-2.3.6
R version: 3.0.0 (2013-04-03)
R package: quantmod_0.4-0
于 2013-05-18T01:35:26.123 に答える