Skip to content

Instantly share code, notes, and snippets.

@urschrei
Created January 31, 2014 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save urschrei/8730975 to your computer and use it in GitHub Desktop.
Save urschrei/8730975 to your computer and use it in GitHub Desktop.
Calculate and plot Statsmodels OLS and WLS confidence intervals
from statsmodels.stats.outliers_influence import summary_table
from statsmodels.sandbox.regression.predstd import wls_prediction_std
# carry out yr fit
# ols cinv
st, data, ss2 = summary_table(ols_fit, alpha=0.05)
fittedvalues = data[:,2]
predict_mean_se = data[:,3]
predict_mean_ci_low, predict_mean_ci_upp = data[:,4:6].T
predict_ci_low, predict_ci_upp = data[:,6:8].T
# wls cinv
prstd, iv_l, iv_u = wls_prediction_std(wls_fit)
# plot OLS
cil, = plt.plot(x,predict_ci_low, 'r--', lw=1, alpha=0.5)
ciu, = plt.plot(x,predict_ci_upp, 'r--', lw=1, alpha=0.5)
mcil, = plt.plot(x,predict_mean_ci_low, 'b--', lw=1, alpha=0.5)
mciu, = plt.plot(x,predict_mean_ci_upp, 'b--', lw=1, alpha=0.5)
# plot WLS
wciu, = ax.plot(x, iv_u, 'r--', lw=1, alpha=0.5)
wcil, = ax.plot(x, iv_l, 'r--', lw=1, alpha=0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment