Thanks Laurent! That's exactly what I was looking for.

2011/6/3 Laurent Gautier <lgaut...@gmail.com>

>  On 2011-06-03 00:56, Eduardo Bañados Torres wrote:
>
> Hi Everyone,
>
>  I am currently struggling with rpy2, I hope someone could help me.
>
>  I am trying to do a maximum likelihood using the mle() task in the stats4
> R library,
> as part of a python script. mle() receives a R function to calculate the
> negative log-likelihood,
> my problem is that so far I have could not figure out how to give a numpy
> vector to the function,
>  or make the vector understandable for the R environment. Is there a way to
> do this?
>
>
> mle is evaluating the function minuslog in its parent environment (and no
> XX can be found that there, as the error message you obtain in return
> indicates it).
> One way to overcome this is to create a symbol 'XX' in the enclosing
> environment of the function:
>
> fx.closureenv()['XX'] = rpy2.robjects.numpy2ri.numpy2ri(XX)
>
>
>  With the following example will be clearer.
> This is a short version, trying to use mle() in a normal distribution.
> "I need to make the XX vector understandable for rfunction":
>
>  ======================================================================
> import rpy2.robjects as robjects
>  from rpy2.robjects.packages import importr
> import rpy2.robjects.numpy2ri
> import numpy as np
> r = robjects.r
>
>  stats4 = importr("stats4")
>
>  #Normal vector to test the script, which I need to give to the R function
> ll
> XX = np.random.normal(loc=10.0, scale=2.0, size=100)
> N = len(XX)
>
>  #negative logarithmic likelihood function of a Gaussian distribution
> #This function should receive the XX numpy vector someway
> rfunction = '''ll <- function(mean,sigma){
>         n <- %i
>         x <- XX   ##This is the critical point##
>         -(-0.5*n*log(2*pi)-n*log(sigma)-0.5*sum(((x-mean)/sigma)^2))
>         }''' % (N)
>
>  fx=r(rfunction)
>
>  starterd = {'mean':10,'sigma':1}
> starter= robjects.DataFrame(starterd)
>
> fit = stats4.mle(minuslog=fx,start=starter)
> summary = R.summary(fit)
> print summary
> ==========================================================================
>
>  I hope someone can help me with this or point me out a better solution
>
>  By the way, I am using:
> rpy2 v2.1.9
> R v2.11.0
> python v2.6
>
>  Thank you!
>
> --
> Eduardo Bañados Torres
>
>
>
>
> ------------------------------------------------------------------------------
> Simplify data backup and recovery for your virtual environment with vRanger.
> Installation's a snap, and flexible recovery options mean your data is safe,
> secure and there when you need it. Discover what all the cheering's about.
> Get your free trial download today. http://p.sf.net/sfu/quest-dev2dev2
>
>
> _______________________________________________
> rpy-list mailing 
> listrpy-list@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/rpy-list
>
>
>


-- 
Eduardo Bañados Torres
Licenciado en Astronomía
Pontificia Universidad Católica de Chile
Santiago, Chile
------------------------------------------------------------------------------
Simplify data backup and recovery for your virtual environment with vRanger.
Installation's a snap, and flexible recovery options mean your data is safe,
secure and there when you need it. Discover what all the cheering's about.
Get your free trial download today. 
http://p.sf.net/sfu/quest-dev2dev2 
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to