Skip to content

Instantly share code, notes, and snippets.

Last active Apr 20, 2018
What would you like to do?
# Examines the effect of changing a single variable
# Takes in the name of the variable, the trace, and the data
def model_effect(query_var, trace, X):
# Variables that do not change
steady_vars = list(X.columns)
# Linear Model that estimates a grade based on the value of the query variable
# and one sample from the trace
def lm(value, sample):
# Prediction is the estimate given a value of the query variable
prediction = sample['Intercept'] + sample[query_var] * value
# Each non-query variable is assumed to be at the median value
for var in steady_vars:
# Multiply the weight by the median value of the variable
prediction += sample[var] * X[var].median()
return prediction
figsize(6, 6)
# Find the minimum and maximum values for the range of the query var
var_min = X[query_var].min()
var_max = X[query_var].max()
# Plot the estimated grade versus the range of query variable
pm.plot_posterior_predictive_glm(trace, eval=np.linspace(var_min, var_max, 100),
lm=lm, samples=100, color='blue',
alpha = 0.4, lw = 2)
# Plot formatting
plt.xlabel('%s' % query_var, size = 16)
plt.ylabel('Grade', size = 16)
plt.title("Posterior of Grade vs %s" % query_var, size = 18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment