Created
June 20, 2025 13:12
-
-
Save devLukaDev/682052160bb103829415efb7ac40fccb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| from scipy.stats import ttest_ind | |
| import numpy as np | |
| from scipy.stats import chi2_contingency | |
| # Replace 'your_file.csv' with the path to your CSV file | |
| df = pd.read_csv('results.csv') | |
| initial = [15] # Interaction order (SELECT WHICH ONE WAS FIRST) | |
| tilt_vs_swipe = [42, 43] # How likely would you like to use to use a system using tilt vs swipe | |
| overall = [41] # Overall preference for tilt vs swipe - 1 = tilt, 2 = swipe, 3 = no pref // Which interaction did you prefer overall? | |
| swipe_confidence = [20] | |
| swipe_informed = [21] | |
| tilt_confidence = [31] | |
| tilt_informed = [32] | |
| pstart = 2 | |
| pend = 17 | |
| initial_group_series = df.iloc[pstart:pend, initial] | |
| # Indices where initial_group == 1 (swipe first) and == 2 (tilt first) | |
| swipe_first_idx = initial_group_series[initial_group_series['Q32'] == '1'].index | |
| tilt_first_idx = initial_group_series[initial_group_series['Q32'] == '2'].index | |
| # Separate data based on initial_group | |
| swipe_first_data = df.loc[swipe_first_idx] | |
| tilt_first_data = df.loc[tilt_first_idx] | |
| def ttest(column): | |
| # Get the column name for tilt confidence | |
| tilt_conf_col = df.columns[column[0]] | |
| # Set the first row as the header with question texts | |
| print("T-test for question", tilt_conf_col , ":",df[tilt_conf_col][0]) | |
| # Convert to numeric, coerce errors to NaN | |
| swipe_first_tilt_conf = pd.to_numeric(swipe_first_data[tilt_conf_col], errors='coerce') | |
| tilt_first_tilt_conf = pd.to_numeric(tilt_first_data[tilt_conf_col], errors='coerce') | |
| # Drop NaN values for the t-test | |
| swipe_first_tilt_conf_clean = swipe_first_tilt_conf.dropna() | |
| tilt_first_tilt_conf_clean = tilt_first_tilt_conf.dropna() | |
| # Calculate means | |
| mean_swipe_first = swipe_first_tilt_conf_clean.mean() | |
| mean_tilt_first = tilt_first_tilt_conf_clean.mean() | |
| # Perform independent t-test | |
| t_stat, p_val = ttest_ind(swipe_first_tilt_conf_clean, tilt_first_tilt_conf_clean, equal_var=True) | |
| print(f"Mean (Started with Swiping): {mean_swipe_first:.2f}") | |
| print(f"Mean (Started with Tilting): {mean_tilt_first:.2f}") | |
| print(f"T-test: t={t_stat:.3f}, p={p_val:.3f}") | |
| print("-" * 40) | |
| ttest(tilt_confidence) | |
| ttest(swipe_confidence) | |
| ttest(tilt_informed) | |
| ttest(swipe_informed) | |
| # Chi-Square test: Is Initial_Group associated with Overall_Preference? | |
| # Get the column names | |
| initial_group_col = df.columns[initial[0]] | |
| overall_pref_col = df.columns[overall[0]] | |
| print(f"\nChi-Square Test for {initial_group_col} vs {overall_pref_col}") | |
| # Only use valid rows (within pstart:pend) | |
| subset = df.iloc[pstart:pend] | |
| # Build contingency table | |
| contingency_table = pd.crosstab(subset[initial_group_col], subset[overall_pref_col]) | |
| # Run chi-square test | |
| chi2, p, dof, expected = chi2_contingency(contingency_table) | |
| print("Contingency Table:") | |
| print(contingency_table) | |
| print(f"\nChi-square test: x²={chi2:.3f}, p={p:.3f}, dof={dof}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment