<jupyter_start><jupyter_text>Create data<jupyter_code>import pandas as pd
import numpy as np
from scipy.stats import f_oneway
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
from statsmodels.stats.multicomp import MultiComparison
from statsmodels.graphics.gofplots import qqplot
import warnings
from IPython.display import display, Math, Latex, Markdown

warnings.filterwarnings("ignore")
cotton_weight_percent = [
    15,
    15,
    15,
    15,
    15,
    20,
    20,
    20,
    20,
    20,
    25,
    25,
    25,
    25,
    25,
    30,
    30,
    30,
    30,
    30,
    35,
    35,
    35,
    35,
    35,
]
observations = [
    7,
    7,
    15,
    11,
    9,
    12,
    16,
    12,
    18,
    18,
    14,
    19,
    19,
    18,
    18,
    19,
    25,
    22,
    19,
    23,
    7,
    10,
    11,
    15,
    11,
]
df = pd.DataFrame(
    {"observations": observations, "cotton_weight_percent": cotton_weight_percent}
)
df<jupyter_output><empty_output><jupyter_text>One-way ANOVA<jupyter_code>model = ols("observations ~ C(cotton_weight_percent)", df).fit()
model.summary()
res = anova_lm(model, typ=1)
def model_evaluation(
    model,
    independent_name: str = "cotton",
    dependent_name: str = "tensile strength",
    alpha=0.5,
):
    p_value = model.f_pvalue
    display(
        Markdown(
            f"""
**Null hypothesis**: All means are equal.<br>
**Alternative hypothesis**: Not all mean are equal<br>
**Significance level**: α = {alpha}

The F-statistic of the model is {round(model.fvalue, 6)}. The p-value of the model is {round(p_value, 6)}."""
        )
    )
    if p_value > alpha:
        display(
            Markdown(
                f"""Since the p-value is greater than the significance level of {alpha}, the differences between the means are not statistically significant."""
            )
        )
    else:
        display(
            Markdown(
                f"""Since the p-value is less than the significance level of {alpha}, there is enough evidence to claim that the differences between some of the means are statistically significant."""
            )
        )
model_evaluation(model)<jupyter_output><empty_output><jupyter_text>Compare Each Pair of Means Using Tukey's HSD<jupyter_code>comparison = MultiComparison(df["observations"], df["cotton_weight_percent"])
comparison_results = comparison.tukeyhsd()
comparison_results.summary()
fig_15 = comparison_results.plot_simultaneous(comparison_name=15)
fig_20 = comparison_results.plot_simultaneous(comparison_name=20)
fig_25 = comparison_results.plot_simultaneous(comparison_name=25)
fig_30 = comparison_results.plot_simultaneous(comparison_name=30)
fig_35 = comparison_results.plot_simultaneous(comparison_name=35)<jupyter_output><empty_output><jupyter_text>Check model assumptions<jupyter_code>residuals = model.resid
plot = qqplot(residuals, line="s")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1018107277075504, 'fraction_numerical': 0.041339255210112745, 'mean_word_length': 3.2496371552975325, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '150348', 'license': 'MIT', 'n_tokens_mistral': 1058, 'n_tokens_neox': 966, 'n_words': 240, 'path': 'Mathematics/Statistics/Statistics and Probability Python Notebooks/General Stats Notebooks/ANOVA_examples.ipynb', 'repo_name': 'okara83/Becoming-a-Data-Scientist', 'size': '2927'}