1

这可能是一个愚蠢的问题,但我搜索了 pyMC3 文档和论坛,似乎找不到答案。我正在尝试从我知道先验不应该有截距的数据集创建线性回归模型。目前我的实现如下所示:

formula = 'Y ~ ' + ' + '.join(['X1', 'X2'])

# Define data to be used in the model
X = df[['X1', 'X2']]
Y = df['Y']

# Context for the model
with pm.Model() as model:
    # set distribution for priors
    priors = {'X1':     pm.Wald.dist(mu=0.01),
              'X2':     pm.Wald.dist(mu=0.01) }
    
    family = pm.glm.families.Normal()
    
    # Creating the model requires a formula and data
    pm.GLM.from_formula(formula, data = X, family=family, priors = priors)
    
    # Perform Markov Chain Monte Carlo sampling
    trace = pm.sample(draws=4000, cores = 2, tune = 1000)

正如我所说,我知道我不应该拦截,但我似乎无法找到一种方法来告诉 GLM.from_formula() 不要寻找一个。大家有解决办法吗?提前致谢!

4

1 回答 1

0

我实际上很困惑它确实以拦截方式运行,因为代码中的默认值GLM.from_formula是传递intercept=False给构造函数。也许是因为patsy解析器默认添加拦截?

无论哪种方式,都可以通过 patsy 公式明确地包含或排除截距,即分别使用10。也就是说,你想要:

formula = 'Y ~ 0 + ' + ' + '.join(['X1', 'X2'])
于 2021-02-10T21:04:21.147 回答