On 4/20/10 3:10 AM, Christian Marquardt wrote:
> Hi,
>
> I'm very new to RPy, so I apologize in advance if the question is stupid...
>
> In the following (python 2.5.2, R 2.10.1, numpy 1.4.0, rpy 2.1.0rc), the call
> to
> R.lm works fine, but a similar call to locfit (from the locfit package at
> CRAN)
> throws an error message:
>
> ----<snip, snip>----------------------------------------
>
> import numpy as np
>
> import rpy2.robjects as ro
> import rpy2.robjects.numpy2ri
>
> from rpy2.robjects.packages import importr
>
> R = ro.r
>
> x = np.arange(-10., 10., 0.1)
> y = 1. + 2*x + np.random.normal(1., 0.5, len(x))
> df = ro.DataFrame({'x': x, 'y': y})
>
> fit = R.lm("y ~ x", data = df) # works fine...
>
> importr("locfit")
> fit = R.locfit("y ~ x", data = df)
> locfit 1.5-6 2010-01-20
> Error in x$terms : $ operator is invalid for atomic vectors
> Traceback (most recent call last):
> File "locfit_1.py", line 17, in<module>
> fit = R.locfit("y ~ x", data = df)
> File "/opt/apps/lib/python2.5/site-packages/rpy2/robjects/functions.py",
> line 81, in __call__
> return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
> File "/opt/apps/lib/python2.5/site-packages/rpy2/robjects/functions.py",
> line 35, in __call__
> res = super(Function, self).__call__(*new_args, **new_kwargs)
> rpy2.rinterface.RRuntimeError: Error in x$terms : $ operator is
> invalid for atomic vectors
>
> -----</snip>-------------------------------------------
>
> On the other hand, this
>
> ro.globalenv["df"] = df
>
> fit = R("lm(y ~ x, data = df)")
>
> importr("locfit")
> fit = R("locfit(y ~ x, data = df)")
>
> works apparently fine for both lm and locfit. The signature of locfit in R is
>
> locfit(formula, data=sys.frame(sys.parent()), weights=1, cens=0,
> base=0,
> subset, geth=FALSE, ..., lfproc=locfit.raw)
>
> Am I doing something wrong, or is this behavior (when calling locfit
> from python)
> a feature I just dont understand?
locfit = importr("locfit")
would be recommended (so you are sure of where "locfit" comes from)
locfit is advertised to accept a /formula/.
I'd suspect that the error is because loc does not implicitly turn
strings into formulae (as lm does). Try if the following helps:
fit = locfit.locfit(ro.Formula("y ~ x"), data = df)
HTH,
L.
> Thanks,
>
> Christian.
>
> ------------------------------------------------------------------------------
> Download Intel® Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> _______________________________________________
> rpy-list mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/rpy-list
------------------------------------------------------------------------------
_______________________________________________
rpy-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rpy-list