[Python] Running a non-parametric stats analysis using a python jupyterhub script, but i have...

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am looking into comparing VCL as an outcome response in 3 different incubation times (1h, 2h, 3h) and 4 treatment conditions (LT, LC, HTF, MRS).

    The boxplots show a clear difference between LC and other treatments at all time points, but I am having issues running the stats script to compare LC and other treatment conditions at specific timings. See code below

    import pandas as pd
    from scipy.stats import mannwhitneyu
    from itertools import combinations
    from statsmodels.stats.multitest import multipletests

    # Clean column names if needed
    df1.columns = df1.columns.str.strip().str.replace(' ', '_')

    # Clean string values
    df1['Treatment'] = df1['Treatment'].str.strip()
    df1['Incubation_time'] = df1['Incubation_time'].astype(str).str.strip()

    # Step 1: Exclude 0H incubation
    df_no0H = df1[df1['Incubation_time'] != '0H']

    # Step 2: Subset to only 1H time
    df_1H = df_no0H[df_no0H['Incubation_time'] == '1H']

    # Step 3: Prepare VCL data for 4 treatment groups
    treatments = ['LT', 'LC', 'HTF', 'MRS']
    group_data = {
    t: df_1H[df_1H['Treatment'] == t]['VCL'].dropna().values
    for t in treatments
    }

    # Step 4: Mann-Whitney U test for all pairs
    results = []
    pairs = list(combinations(treatments, 2))

    for a, b in pairs:
    if len(group_data[a]) >= 3 and len(group_data) >= 3:
    stat, p = mannwhitneyu(group_data[a], group_data, alternative='two-sided')
    results.append({'Comparison': f'{a} vs {b}', 'U': stat, 'p-value': p})
    else:
    results.append({'Comparison': f'{a} vs {b}', 'U': None, 'p-value': None})

    # Step 5: Bonferroni correction
    pvals = [r['p-value'] for r in results if r['p-value'] is not None]
    _, corrected, _, _ = multipletests(pvals, method='bonferroni')

    # Add corrected p-values back into results
    i = 0
    for r in results:
    if r['p-value'] is not None:
    r['p-value (Bonferroni)'] = corrected
    i += 1
    else:
    r['p-value (Bonferroni)'] = None

    # Step 6: Convert to DataFrame for display
    results_df = pd.DataFrame(results)
    print(results_df)

    Continue reading...

Compartilhe esta Página