<jupyter_start><jupyter_text>Regression and Other Stories: Elections Economy – model checking<jupyter_code>import arviz as az
from bambi import Model
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
data = "https://raw.githubusercontent.com/avehtari/ROS-Examples/master/ElectionsEconomy/data/hibbs.dat"
hibbs = pd.read_csv(data, delimiter=" ")
hibbs<jupyter_output><empty_output><jupyter_text>Step 1: Creating the pretend world<jupyter_code>a = 46.2
b = 3.1
sigma = 3.8
x = hibbs["growth"]
n = x.shape
y = a + b*x + stats.norm(0, sigma).rvs(n)
fake=- pd.DataFrame(dict(x=x, y=y))
model = Model(fake)
results = model.fit('y ~ x', samples=1000, chains=4)
func_dict = {"Median": np.median,
            "MAD_SD":stats.median_abs_deviation,
             }
coefs = az.summary(results, stat_funcs=func_dict, extend=False)
coefs
b_hat, b_se = coefs.loc["x[0]"]<jupyter_output><empty_output><jupyter_text>Step 4: Embedding the simulation in a loop<jupyter_code>n_fake = 100
cover_68 = np.empty(n_fake)
cover_95 = np.empty(n_fake)

for i in range(n_fake):
    y = a + b*x + stats.norm(0, sigma).rvs(n)
    fake = pd.DataFrame(dict(x=x, y=y))
    model = Model(fake)
    
    # Less chains so this will run faster
    results = model.fit('y ~ x', samples=1500, chains=2)
    cover_68[i] = np.abs(b-b_hat) < b_se
    cover_95[i] = np.abs(b-b_hat) < 2*b_se
print(f"68% coverage: {cover_68.mean()}")
f"95% coverage: {cover_95.mean()}"<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14505347593582887, 'fraction_numerical': 0.02807486631016043, 'mean_word_length': 4.757692307692308, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '549328', 'license': 'MIT', 'n_tokens_mistral': 632, 'n_tokens_neox': 596, 'n_words': 129, 'path': 'ROS/ElectionsEconomy/hibbs_coverage.ipynb', 'repo_name': 'farhanreynaldo/Bambi_resources', 'size': '1496'}