通过Python在黄土函数中发出rpy2 / R问题?

时间:2013-03-21 20:48:04

标签: python r statistics pandas rpy2

我正在尝试通过Python中的Rpy2在此数据文件中调用R函数loesshttp://filebin.ca/azuz9Piv0z8/test.data

当我使用数据的子集(前1000个点)时,它工作,但当我尝试使用整个文件时,我收到错误。我的代码:

import pandas
from rpy2.robjects import r
import rpy2.robjects as robjects
data = pandas.read_table(os.path.expanduser("~/test2.data"), sep="\t").values
small_data = data[0:1000, :]
print "small data loess:"
a, b = robjects.FloatVector(list(small_data[:, 0])), \
       robjects.FloatVector(list(small_data[:, 1]))
df = robjects.DataFrame({"a": a, "b": b})
loess_fit = r.loess("b ~ a", data=df)
print loess_fit

print "large data loess:"
a, b = robjects.FloatVector(list(data[:, 0])), \
       robjects.FloatVector(list(data[:, 1]))
df = robjects.DataFrame({"a": a, "b": b})
loess_fit = r.loess("b ~ a", data=df)
print loess_fit

适合small_datadata无效。我收到错误:

Error in simpleLoess(y, x, w, span, degree, parametric, drop.square, normalize,  : 
  NA/NaN/Inf in foreign function call (arg 1)
    loess_fit = r.loess("b ~ a", data=df)
  File "/usr/local/lib/python2.7/dist-packages/rpy2-2.3.3-py2.7-linux-x86_64.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.3-py2.7-linux-x86_64.egg/rpy2/robjects/functions.py", line 35, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in simpleLoess(y, x, w, span, degree, parametric, drop.square, normalize,  : 
  NA/NaN/Inf in foreign function call (arg 1)

如何解决这个问题?我不确定R函数loess或Rpy2接口是否有问题?感谢。

2 个答案:

答案 0 :(得分:3)

问题是数据中的-Inf值:

DF <- read.table('http://filebin.ca/azuz9Piv0z8/test.data')
DF[!is.finite(DF[,1]) | !is.finite(DF[,2]),]
#        V1   V2
# 5952 -Inf -Inf

答案 1 :(得分:1)

为什么在使用statsmodels package in Python for lowess smoothing

时拨打R.

还有一个适用于lowess的Bio.Statistics软件包,但它似乎并不准确,我无法将其收敛为this lowess example