使用 statsmodels 计算 Gamma GLM 的尺度/离散度 [英] Calculating scale/dispersion of Gamma GLM using statsmodels

查看:33
本文介绍了使用 statsmodels 计算 Gamma GLM 的尺度/离散度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用 statsmodels 的 GLM 函数获取模拟数据的离散参数.

I'm having trouble obtaining the dispersion parameter of simulated data using statsmodels' GLM function.

import statsmodels.api as sm
import matplotlib.pyplot as plt 
import scipy.stats as stats 
import numpy as np

np.random.seed(1)

# Generate data
x=np.random.uniform(0, 100,50000)
x2 = sm.add_constant(x)
a = 0.5
b = 0.2
y_true = 1/(a+(b*x))
# Add error 
scale = 2 # the scale parameter I'm trying to obtain
shape = y_true/scale # given that, for Gamma, mu = scale*shape
y = np.random.gamma(shape=shape, scale=scale)

# Run model
model = sm.GLM(y, x2, family=sm.families.Gamma()).fit() 

model.summary()

这是上面的总结:

请注意,系数估计值是正确的(0.5 和 0.2),但比例 (21.995) 与我设置的比例 (2) 相差甚远.

Note that the coefficient estimates are correct (0.5 and 0.2), but the scale (21.995) is way off the scale I set (2).

有人能指出我误解/做错了什么吗?谢谢!

Can someone point out what it is I'm misunderstanding/doing wrong? Thanks!

推荐答案

正如 Josef 在评论中指出的那样,statsmodels 使用了不同类型的参数化.

As Josef noted in the comments, statsmodels uses a different kind of parameterization.

这篇关于使用 statsmodels 计算 Gamma GLM 的尺度/离散度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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