df['percent'] = preprocessing.scale(df['percent'])
df['agreeableness'] = preprocessing.scale(df['agreeableness'])
df['assertiveness'] = preprocessing.scale(df['assertiveness'])
df['conscientiousness'] = preprocessing.scale(df['conscientiousness'])
df['extraversion'] = preprocessing.scale(df['extraversion'])
def linear_setup(df, ind_cols, dep_col):
# model our intercept and error term as above
b0 = Normal('b0', 0, 0.0001)
err = Uniform('err', 0, 500)
# initialize a NumPy array to hold our betas
# and our observed x values
b = np.empty(len(ind_cols), dtype=object)
x = np.empty(len(ind_cols), dtype=object)
# loop through b, and make our ith beta
# a normal random variable, as in the univariate case
for i in range(len(b)):
b[i] = Normal('b' + str(i + 1), 0, 0.0001)
# loop through x, and inform our model about the observed
# x values that correspond to the ith position
for i, col in enumerate(ind_cols):
x[i] = Normal('x' + str(i + 1), 0, 1, value=np.array(df[col]), observed=True)
# as above, but use .dot() for 2D array (i.e., matrix) multiplication
@pymc.deterministic
def y_pred(b0=b0, b=b, x=x):
return b0 + b.dot(x)
# finally, "model" our observed y values as above
y = Normal('y', y_pred, err, value=np.array(df[dep_col]), observed=True)
return Model([b0, Container(b), err, Container(x), y, y_pred])
M = linear_setup(df, ['agreeableness', 'assertiveness', 'conscientiousness', 'extraversion'], 'percent')
MC = MCMC(M)
MC.sample(50000, 20000)