Beta 二项式和 Beta 分布的 alpha 和 beta 估计 [英] alpha and beta estimates for beta binomial and beta distributions

查看:88
本文介绍了Beta 二项式和 Beta 分布的 alpha 和 beta 估计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的数据拟合为 beta 二项式分布并估计 alpha 和 beta 形状参数.对于此分布,先验值取自 beta 分布.Python 没有适用于 beta-binomial 的 fit 函数,但它适用于 beta.python beta 拟合和 R beta 二项式拟合接近但系统性关闭.

I am trying to fit my data to a beta-binomial distribution and estimate the alpha and beta shape parameters. For this distribution, the prior is taken from a beta distribution. Python does not have a fit function for beta-binomial but it does for beta. The python beta fitting and R beta binomial fitting is close but systematically off.

R:

library("VGAM")
x = c(222,909,918,814,970,346,746,419,610,737,201,865,573,188,450,229,629,708,250,508)
y = c(2,18,45,11,41,38,22,7,40,24,34,21,49,35,31,44,20,28,39,17)
fit=vglm(cbind(y, x) ~ 1, betabinomialff, trace = TRUE)
Coef(fit)
   shape1    shape2 
  1.736093 26.870768

蟒蛇:

import scipy.stats
import numpy as np
x = np.array([222,909,918,814,970,346,746,419,610,737,201,865,573,188,450,229,629,708,250,508], dtype=float)
y = np.array([2,18,45,11,41,38,22,7,40,24,34,21,49,35,31,44,20,28,39,17])
scipy.stats.beta.fit((y)/(x+y), floc=0, fscale=1)
    (1.5806623978910086, 24.031893492546242, 0, 1)

我已经这样做了很多次,似乎python系统地比R结果低一点.我想知道这是我的输入错误还是计算方式的不同?

I have done this many times and it seems like python is systematically a little bit lower than the R results. I was wondering if this is an input error on my part or just a difference in the way they are calculated?

推荐答案

您的问题是拟合 Beta 二项式模型与拟合值等于比率的 Beta 模型不同.我将在这里用 bbmle 包进行说明,它适用于与 VGAM 类似的模型(但我更熟悉).

Your problem is that fitting a beta-binomial model is just not the same as fitting a Beta model with the values equal to the ratios. I'm going to illustrate here with the bbmle package, which will fit similar models to VGAM (but with which I'm more familiar).

预赛:

library("VGAM")  ## for dbetabinom.ab
x <- c(222,909,918,814,970,346,746,419,610,737,
       201,865,573,188,450,229,629,708,250,508)
y <- c(2,18,45,11,41,38,22,7,40,24,34,21,49,35,31,44,20,28,39,17)

library("bbmle")

拟合 beta-二项式模型:

Fit beta-binomial model:

mle2(y~dbetabinom.ab(size=x+y,shape1,shape2),
     data=data.frame(x,y),
     start=list(shape1=2,shape2=30))
## Coefficients:
##    shape1    shape2 
##  1.736046 26.871526 

这与您引用的 VGAM 结果或多或少完全一致.

This agrees more or less perfectly with the VGAM result you quote.

现在使用相同的框架来拟合 Beta 模型:

Now use the same framework to fit a Beta model instead:

mle2(y/(x+y) ~ dbeta(shape1,shape2),
     data=data.frame(x,y),
     start=list(shape1=2,shape2=30))
## Coefficients:
##    shape1    shape2 
## 1.582021 24.060570 

这符合您的 Python 测试版结果.(我敢肯定,如果您使用 VGAM 来适应 Beta,您也会得到相同的答案.)

This fits your Python, beta-fit result. (I'm sure if you used VGAM to fit the Beta you'd get the same answer too.)

这篇关于Beta 二项式和 Beta 分布的 alpha 和 beta 估计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆