Skip to content

Instantly share code, notes, and snippets.

@csetzkorn
Created August 29, 2018 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csetzkorn/84cfb993328904e07c4fd0908552d38c to your computer and use it in GitHub Desktop.
Save csetzkorn/84cfb993328904e07c4fd0908552d38c to your computer and use it in GitHub Desktop.
get CI of r-squared
rsquared_boot, coefs_boot, sims = [], [], 1000
reg_fit = sm.OLS(df['y'], df.iloc[:,1:]).fit()
# Run 1K iterations
for i in range(sims):
# First create a bootstrap sample with replacement with n=df.shape[0]
bootstrap = df.sample(n=df.shape[0], replace=True)
# Fit the regression and append the r square to rsquared_boot
rsquared_boot.append(sm.OLS(bootstrap['y'],bootstrap.iloc[:,1:]).fit().rsquared)
# Calculate 95% CI on rsquared_boot
print("R Squared 95% CI = {}".format(np.percentile(rsquared_boot, [2.5, 97.5])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment