使用PyMC3重复测量的简单线性回归

时间:2015-11-25 05:11:36

标签: python linear-regression bayesian pymc3

我试图重现John Kruschke的书中的例子"做贝叶斯数据分析" (第2版)。这个例子来自第16章关于重复测量的简单线性回归。在这种情况下,我想我只是挂断了指定似然函数。我从John Kruschke's site下载了数据并用它制作了一个csv。这是我在书中遵循他的R代码的代码:

import pandas as pd
import pymc3 as pm
import numpy as np

data = pd.read_csv('Feldmandata.csv')
data = data[np.isfinite(data['retention'])]
data = data[data['group'] == 1]

data['log10_retention'] = np.log10(data['retention'])
nrow, ncol = data.shape[0], data.shape[1]

data['subjID'] = data['subjID']-1
subjID = data['subjID']
nsubj = len(np.unique(subjID))

y = data['time'].astype('float')
x = data['log10_retention'].astype('float')

std_y = (y - np.mean(y)) / np.std(y)
std_x = (x - np.mean(x)) / np.std(x)

sample = 1000
burnin = 500

with pm.Model() as model:
    # Priors
    m = pm.Gamma('m', 1, 0.1)
    d = pm.Gamma('d', 1, 0.1)
    G_r = pm.Deterministic('G_r', m / d**2)
    G_s = pm.Deterministic('G_s', m**2 / d**2)

    t_1G = pm.Gamma('t_1G', 0.1, 0.1)
    mu_1G = pm.Normal('mu_1G', 0, 0.1)
    t_0G = pm.Gamma('t_0G', 0.1, 0.1)
    mu_0G = pm.Gamma('mu_0G', 0, 0.01)

    b0 = pm.Normal('b0', mu = mu_0G, tau = t_0G, shape = nsubj)
    b1 = pm.Normal('b1', mu = mu_1G, tau = t_1G, shape = nsubj)
    tau = pm.Gamma('tau', G_s, G_r, shape = nsubj)

    # Likelihood - I think I just need help here.
    y_like = pm.Normal('y_like', mu = b0[subjID] + b1[subjID]*std_x, 
        tau = tau[subjID], observed = std_y)

    # Inference
    step = pm.Metropolis()
    trace = pm.sample(sample, step = step)

非常感谢任何帮助。

0 个答案:

没有答案