Last active
September 17, 2023 16:28
-
-
Save WillKoehrsen/fa59f7f28aefa09bc80138d3de8d6052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
steady_vars.remove(query_var) | |
# 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) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment