【问题标题】:R fitdistr for Beta distribution: which starting parameters?R fitdistr 用于 Beta 分布:哪些起始参数?
【发布时间】:2012-07-17 16:22:54
【问题描述】:

我需要将我的数据放入 Beta 分布并检索 alpha 参数。我正在尝试从 python (rpy2) 使用 R,我的代码如下所示:

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")

但我收到此错误:

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

我似乎找不到任何关于 R 的详细示例的好的文档,所以我只找到了this

start 一个命名列表,给出要优化的参数以及初始值。这对于一些命名的可以省略 分布(见详情)。 ... 附加参数,无论是 densfun 或优化。特别是,它可用于指定边界 通过下部或上部或两者。如果 densfun 的论点(或密度 对应于字符串规范的函数)是 包括它们将被固定。

我真的不知道:

  • 将什么作为起始参数以及这将如何影响我的估计
  • 在 Python 中使用什么语法,因为 start=list(shape1=0.5, shape2=0.5) 无法解决问题

有什么提示吗?

【问题讨论】:

  • 至少,您需要阅读Rfitdistrthe beta distribution 手册页。它们一起揭示了您正在尝试执行的 R 命令类似于 fitdistr(x, "beta", start=list(shape1=1/2, shape2=1/2))
  • 谢谢。一个大问题是fitdistr(x, "beta", start=list(shape1=1/2, shape2=1/2)) 不会被 Python 的解释器接受。 Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: list() takes at most 1 argument (2 given)
  • 我在python中也找不到任何带有start参数的fitdistr示例。
  • rpy2 的文档建议您可能希望使用 dict 对象将列表作为参数传递。如果这不起作用,请创建一个VECSXP object。无论如何,因为这纯粹是一个编程接口问题,所以你会得到更好的服务——我会为你迁移这个问题。
  • 参见第 12 页。我的第一条评论中第二个链接中的 2 个:shape1 是 alpha,shape2 是 beta。

标签: r estimation python beta


【解决方案1】:

好的,经过一番挖掘,我找到了解决方案:

from rpy2.robjects import DataFrame
starter= DataFrame({'shape1':0.5,'shape2':0.5})
x = MASS.fitdistr(myValues, "beta", start=starter))

【讨论】:

    猜你喜欢
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 2021-02-24
    相关资源
    最近更新 更多