Hello,

I'm trying to use a Python function as input for the mle2 task from the
bbmle R library (same thing with mle task from the stats4 library, both
meant to perform maximum likelihood analysis),  by using the new feature
introduced in rpy2.2 (
http://rpy.sourceforge.net/rpy2/doc-2.2/html/rinterface.html#calling-python-functions-from-r).


The problem is that mle2 receives 'start' which is a Named list for the
initial parameters of the (log)likelihood function (unlike the optim task
used as example in the rpy2 documentation), these names have to match with
the parameter names of the likelihood function. It seems that if I define
this function in Python, R cannot identify the function parameter names
because I'm getting the following error:

*RRuntimeError: Error in function (minuslogl, start = formals(minuslogl),
method = "BFGS",  :
  some named arguments in 'start' are not arguments to the supplied
log-likelihood
*
I'm not sure if this is a real bug/problem or if I'm doing something wrong,
in that case please point it out, I would greatly appreciate it...

Here I attach an almost self-contained example where I get the error (if you
need the input files to run it, please do not hesitate in contact me)

Thanks,

===========================Example code=============================
'''
rpy2 v2.2.1
python v2.6.5
R v2.13.1
scipy v0.9  important to use NearestNDInterpolator, which is needed in my
code
'''
import numpy as np
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
from rpy2.robjects.numpy2ri import numpy2ri
robjects.conversion.py2ri = numpy2ri
r = robjects.r
import rpy2.rinterface as rinterface

namefile1="LFNBselsim2.dat"
#lL=1d vector with numbers between 42 and 44 drawn from a X distribution
lL = np.loadtxt(namefile1,skiprows=1,usecols=(2,),unpack=True) #1d vector
L = 10**lL
N = len(lL)

#Initial parameters
lLstar_0=42.6;a_0=-1.36;low_lLstar=42.0;up_lLstar=44.0;
low_a=-2.0;up_a=1.5

#creating named lists
starterd = {'lL_star':lLstar_0,'alpha':a_0,'T':N}
starter= robjects.DataFrame(starterd)
lower2d = {'lL_star':low_lLstar,'alpha':low_a,'T':0}
lower2r= robjects.DataFrame(lower2d)
upper2d = {'lL_star':up_lLstar,'alpha':up_a,'T':70000}
upper2r= robjects.DataFrame(upper2d)

namefile2 = "he0940_center_fLe_tab.npy"
#4-vector. Three first three columns are the coordinates and the fourth the
#value. I will interpolate this values with their nearest value.
data = np.load(namefile2)
Lstar_int = data.T[0] # x
alpha_int = data.T[1] #y
Lest_int = data.T[2]  #z
fLe_int = data.T[3]   #f(x,y,z)
#Load a N dim Interpolator from scipy (scipy v0.9+ required)
from scipy.interpolate import NearestNDInterpolator
#Create array for NearestNDInterpolator
puntos = np.array((Lstar_int,alpha_int,Lest_int)).T
print "Now doing NearestNDInterpolator"
interp2 = NearestNDInterpolator(puntos,fLe_int)

#Function which is going to be called from R
def functionp(lL_star,alpha,T=N):
    '''Return the -loglikelihood function required.
        The variable names are the same of the ones in the named
        lists created previously'''
    import math
    loglike=0.0
    Le=self.L
    for i in range(len(Le)):
        loglike += math.log(interp2(lL_star,alpha,Le[i]))
    return -(-T+N*log(T)+loglike)

# wrap the function function so it can be exposed to R (hopefully)
functionr = rinterface.rternalize(functionp)

bbmle = importr("bbmle") # To use the mle2() task
print "="*80
print " "*5+"Estimating Parameters via ML using mle2 method"+" "*5
print "="*80
#Code fails in next line
mle =
bbmle.mle2(minuslogl=functionr,method="L-BFGS-B",start=starter,lower=lower2r,upper=upper2r)

summary = r.summary(mle)
logLik = r.logLik(mle)
vcov = r.vcov(mle)
print "Creating summary:"
print summary
print "Maximized log-likelihood value:"
print logLik
print "Generating variance-covariance matrix:"
print vcov

-- 
Eduardo Bañados
------------------------------------------------------------------------------
Got Input?   Slashdot Needs You.
Take our quick survey online.  Come on, we don't ask for help often.
Plus, you'll get a chance to win $100 to spend on ThinkGeek.
http://p.sf.net/sfu/slashdot-survey
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to