문제

I need to fit my data into a Beta distribution and retrieve the alpha parameter. I'm trying to use R from python (rpy2) and my code looks like:

from rpy2 import *
from rpy2.robjects.packages import importr
MASS = importr('MASS') #myVector is a Numpy array with values between 0 and 1
MASS.fitdistr(myVector,"beta")

But I get this error:

Error in function (x, densfun, start, ...)  : 
  'start' must be a named list
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 82, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 34, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in function (x, densfun, start, ...)  : 
  'start' must be a named list

I can't seem to find any good documentation for R with detailed examples, so I only found this:

start A named list giving the parameters to be optimized with initial values. This can be omitted for some of the named distributions (see Details). ... Additional parameters, either for densfun or for optim. In particular, it can be used to specify bounds via lower or upper or both. If arguments of densfun (or the density function corresponding to a character-string specification) are included they will be held fixed.

I really have no clue as to:

  • what to put as a starting parameters and how that will affect my estimation
  • what syntax to use in Python, since start=list(shape1=0.5, shape2=0.5) won't do the trick

Any hint?

도움이 되었습니까?

해결책

Ok, after a little bit more of digging, I found a solution:

from rpy2.robjects import DataFrame
starter= DataFrame({'shape1':0.5,'shape2':0.5})
x = MASS.fitdistr(myValues, "beta", start=starter))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top