我在使用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()
请注意,系数估计是正确的(0.5和0.2),但比例尺(21.995)与我设置的比例尺(2)相差甚远。
谁能指出我到底是哪里理解错了? 谢谢!
正如Josef在评论中提到的,statsmodels使用了一种不同的参数化。