<jupyter_start><jupyter_text>t-Tests***  Packages***<jupyter_code>import numpy as np
import pandas as pd
import statsmodels.stats.weightstats as stat
import scipy.stats as ss
import matplotlib.pyplot as plt
import seaborn as sns

plt.style.use("fivethirtyeight")<jupyter_output><empty_output><jupyter_text>Fake data values***<jupyter_code>m_a, s_a, m_b, s_b = 1.0, 0.4, 2.0, 0.4
N = 40

a = np.random.normal(loc=m_a, scale=s_a, size=N)
b = np.random.normal(loc=m_b, scale=s_b, size=N)

df = pd.DataFrame({'Category': ['A'] * len(a) + ['B'] * len(b), 'Value': np.hstack([a,b])})

df<jupyter_output><empty_output><jupyter_text>Plot data***<jupyter_code>sns.catplot(x='Category', y='Value', jitter=False, data=df);<jupyter_output><empty_output><jupyter_text>T-test***<jupyter_code># The scipy.stats version.
t_ss, p_ss = ss.ttest_ind(a, b)
print(f"P_scipy: {p_ss:0.2f}")
# test statistic, pvalue of the t-test, degrees of freedom used in the t-test
t, p, d = stat.ttest_ind(a, b)
print(f"P: {p:0.2f}")<jupyter_output>P: 0.00<jupyter_text>Visualisation***<jupyter_code>x = np.linspace(-2.0, 4.0, 1000)

y_a = ss.norm.pdf(x, m_a, s_a)
y_b = ss.norm.pdf(x, m_b, s_b)

fig, ax = plt.subplots(figsize=(10,6))

ax.plot(x, y_a)
ax.plot(x, y_b)

plt.show()
x_t = np.linspace(-4.0, 4.0, 1000)
t = ss.t.pdf(x_t, d)
tf = pd.DataFrame({'x': x_t, 't': t})

tcrit = abs(ss.t.ppf(0.025, d))
one = tf[tf['x'] >= tcrit]
two = tf[tf['x'] <= -tcrit]

fig, ax = plt.subplots(figsize=(10,6))

ax.plot(x_t, t)
ax.fill_between(one['x'], one['t'], 0, facecolor="red")
ax.fill_between(two['x'], two['t'], 0, facecolor="red")

plt.show()<jupyter_output><empty_output><jupyter_text>Type I errors - False Positives***<jupyter_code>trials = 10000
N = 100
m_a, m_b, s = 2.0, 2.0, 0.3
rejects = 0

for i in range(trials):
    a = np.random.normal(loc=m_a, scale=s, size=N)
    b = np.random.normal(loc=m_b, scale=s, size=N)
    if ss.ttest_ind(a, b)[1] <= 0.05:
        rejects = rejects + 1

typei = 100.0 * (rejects / trials)
print(f"{typei:0.2f}%")<jupyter_output>5.33%<jupyter_text>Type II errors - False Negatives***<jupyter_code>trials = 10000
N = 100
m_a, m_b, s = 2.0, 2.1, 0.3
dont = 0

for i in range(trials):
    a = np.random.normal(loc=m_a, scale=s, size=N)
    b = np.random.normal(loc=m_b, scale=s, size=N)
    if ss.ttest_ind(a, b)[1] > 0.05:
        dont = dont + 1

typeii = 100.0 * (dont / trials)
print(f"{typeii:0.2f}%")<jupyter_output>34.72%<jupyter_text>Paired samples*** *References for this section:*[<NAME>'s R datasets list](https://vincentarelbundock.github.io/Rdatasets/articles/data.html)[t-test: Comparing Group Means](https://uc-r.github.io/t_test)***<jupyter_code>dfsleep = pd.read_csv("https://vincentarelbundock.github.io/Rdatasets/csv/datasets/sleep.csv")
dfsleep
drugA = dfsleep[dfsleep["group"] == 1]
drugA = drugA.sort_values("ID")
drugA = drugA["extra"].to_numpy()
drugA
drugB = dfsleep[dfsleep["group"] == 2]
drugB = drugB.sort_values("ID")
drugB = drugB["extra"].to_numpy()
drugB
ss.ttest_rel(drugA, drugB)
ss.ttest_1samp(drugB - drugA, 0)
stat.DescrStatsW(drugB - drugA).ttest_mean(0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19532500800512329, 'fraction_numerical': 0.034902337495997436, 'mean_word_length': 5.336713995943205, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 3, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '361397', 'license': 'Unlicense', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1437, 'n_words': 256, 'path': 'ttest.ipynb', 'repo_name': 'sean-meade/jupyter-teaching-notebooks', 'size': '3137'}