## rpy-list

 Re: [Rpy] Maximum likelihooh estimation in rpy2 From: B. Santos - 2011-07-12 15:12:25 ```Thank you very much for your reply. I finally managed to get some time to get back to this. I tried to parse and evaluate the expression from R the problem is I cannot do this in multiple lines. Is there anyway to do this? Because of that I went for the second option but now I get an error. My code is: from rpy2.robjects.packages import importr import rpy2.rinterface as rinterface rinterface.initr() IntSexpVector = rinterface.IntSexpVector log = rinterface.globalenv.get("log") lfactorial = rinterface.globalenv.get("lfactorial") lgamma = rinterface.globalenv.get("lgamma") stats = importr('stats') sum = rinterface.globalenv.get("sum") def rp(rp,k): print rp r = rinterface.FloatSexpVector(rp[0]) p = rinterface.FloatSexpVector(rp[1]) return sum(lgamma(k+r))-sum(lfactorial(k))-len(k)*lgamma(r)+len(k)*r*log(1-p)+sum(k)*log(p) cost_fr = [rinterface.rternalize(rp)] start_params = rinterface.FloatSexpVector([-1.2, 1]) res = stats.optim(start_params, cost_fr) and I get rpy2.rinterface.RRuntimeError: Error in function (par) : could not find function "fn". What am I doing wrong? 2011/6/16 Laurent Gautier : > On 2011-06-16 17:20, B. Santos wrote: >> >> Hello everyone, >> >> I am trying to use rpy2 to estimate the parameters of a specified >> function. But I am failing miserable to understand how I can do this >> using rpy2. I have the following R code: >> alphabeta<-function(alphabeta,x,dicerAcc) { alpha<-alphabeta[1] >> beta<-alphabeta[2] if (any(alphabeta<0)) return(NA) >> sum((alpha*log(beta) + lgamma(alpha + x) + x * log(dicerAcc) - >> lgamma(alpha) - (alpha + x) * log(beta+dicerAcc) - >> lfactorial(x))[dicerAcc >>> >>> noiseT]) ab<- optim(c(1,100), alphabeta, control=list(fnscale=-1), x = >> >> x, dicerAcc = dicerAcc)\$par >> >> Is there anyway to do this. I would imagine the easy way would be to >> import >> the function from R but I imagine that means I would have to create a >> package just for the function which is less than ideal. >> > > You can just parse and evaluate a string as R code as an anonymous R > function. > http://rpy.sourceforge.net/rpy2/doc-2.2/html/rinterface.html#parsing-and-evaluating-r-code > and then use base.eval(). > > You can also consider writing your cost function in Python: > http://rpy.sourceforge.net/rpy2/doc-2.2/html/rinterface.html#calling-python-functions-from-r > > > HTH, > > > L. >> >> Looking forward to your suggestion. Thank you very much in advance, >> Bruno Santos >> >> >> >> ------------------------------------------------------------------------------ >> EditLive Enterprise is the world's most technically advanced content >> authoring tool. Experience the power of Track Changes, Inline Image >> Editing and ensure content is compliant with Accessibility Checking. >> http://p.sf.net/sfu/ephox-dev2dev >> _______________________________________________ rpy-list mailing list >> rpy-list@... >> https://lists.sourceforge.net/lists/listinfo/rpy-list > > ```