使用statsmodels计算Gamma GLM的scaledispersion。

问题描述 投票:0回答:1

我在使用statsmodels的GLM函数获取模拟数据的离散度参数时遇到了麻烦。

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

这是上面的总结。glm output

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

谁能指出我到底是哪里理解错了? 谢谢!

python statistics statsmodels
1个回答
0
投票

正如Josef在评论中提到的,statsmodels使用了一种不同的参数化。

© www.soinside.com 2019 - 2024. All rights reserved.