Related scripts to the Master's Thesis "Exploring Sonification in Website Navigation on Smartphones"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
4.0 KiB

3 months ago
  1. import pandas as pd
  2. import statsmodels.api as sm
  3. from statsmodels.formula.api import ols
  4. import scipy.stats as stats
  5. from scipy.stats import f_oneway, friedmanchisquare, mannwhitneyu, ranksums
  6. import numpy as np
  7. import pingouin as pg
  8. # SUS
  9. df = pd.read_csv('QuestionnaireDataSUS.csv')
  10. df['TotalIMIScore'] = ((df['Q1'] - 1) + (5 - df['Q2']) + (df['Q3'] - 1) + (5 - df['Q4']) + (df['Q5'] - 1) + (5 - df['Q6']) + (df['Q7'] - 1) + (5 - df['Q8']) + (df['Q9'] - 1) + (5 - df['Q10'])) * 2.5
  11. # IMI
  12. #df = pd.read_csv('QuestionnaireDataIMI.csv')
  13. #df['TotalIMIScore'] = (df['Q1'] + df['Q2'] + (8 - df['Q3']) + (8 - df['Q4']) + df['Q5'] + df['Q6'] + df['Q7']) / 7.0
  14. grouped = df.groupby('WebpageID').agg(
  15. mean_IMIScore=('TotalIMIScore', 'mean'),
  16. std_IMIScore=('TotalIMIScore', 'std'),
  17. count=('TotalIMIScore', 'count')
  18. )
  19. grouped['variance_IMIScore'] = grouped['std_IMIScore'] ** 2
  20. anova_data = [group['TotalIMIScore'].values for name, group in df.groupby('WebpageID')]
  21. anova_result = f_oneway(*anova_data)
  22. print(f"ANOVA Result: F-statistic = {anova_result.statistic}, p-value = {anova_result.pvalue}")
  23. friedman_data = df.pivot(index='ParticipantID', columns='WebpageID', values='TotalIMIScore').dropna()
  24. spher, W, chisq, dof, pval = pg.sphericity(data=df, within='WebpageID', dv='TotalIMIScore', subject='ParticipantID')
  25. gg = pg.epsilon(data=df, within='WebpageID', dv='TotalIMIScore', subject='ParticipantID', correction='gg')
  26. print(gg)
  27. friedman_result = friedmanchisquare(*[friedman_data[col] for col in friedman_data])
  28. print(f"Friedman Test Result: Chi-square statistic = {friedman_result.statistic}, p-value = {friedman_result.pvalue}")
  29. model = ols('TotalIMIScore ~ C(WebpageID)', data=df).fit()
  30. anova_table = sm.stats.anova_lm(model, typ=2)
  31. print(anova_table)
  32. print(stats.shapiro(anova_data[0]))
  33. print(stats.shapiro(anova_data[1]))
  34. print(stats.shapiro(anova_data[2]))
  35. print(stats.shapiro(anova_data[3]))
  36. print(stats.shapiro(anova_data[4]))
  37. print(stats.shapiro(anova_data[5]))
  38. print(stats.levene(*anova_data))
  39. print(spher, round(W, 5), round(chisq, 3), dof, round(pval, 3))
  40. group_1 = df[df['WebpageID'] == 1]['TotalIMIScore']
  41. group_2 = df[df['WebpageID'] == 2]['TotalIMIScore']
  42. group_3 = df[df['WebpageID'] == 3]['TotalIMIScore']
  43. group_4 = df[df['WebpageID'] == 4]['TotalIMIScore']
  44. group_5 = df[df['WebpageID'] == 5]['TotalIMIScore']
  45. group_6 = df[df['WebpageID'] == 6]['TotalIMIScore']
  46. def mann_whitney_test(group_a, group_b):
  47. u_statistic, p_value = mannwhitneyu(group_a, group_b)
  48. n1 = len(group_a)
  49. n2 = len(group_b)
  50. mu_u = n1 * n2 / 2
  51. sigma_u = np.sqrt(n1 * n2 * (n1 + n2 + 1) / 12)
  52. z_value = (u_statistic - mu_u) / sigma_u
  53. effect_size_r = z_value / np.sqrt(n1 + n2)
  54. return u_statistic, p_value, z_value, effect_size_r
  55. def wilcoxon_rank_sum_test(group_a, group_b):
  56. rank_sum_statistic, p_value = ranksums(group_a, group_b)
  57. effect_size_r = rank_sum_statistic / np.sqrt(len(group_a) + len(group_b))
  58. return rank_sum_statistic, p_value, effect_size_r
  59. comparisons = [
  60. ("2 vs 5", group_2, group_5),
  61. ("2 vs 3", group_2, group_3),
  62. ("3 vs 5", group_3, group_5),
  63. ("1 vs 6", group_1, group_6),
  64. ("4 vs 6", group_4, group_6),
  65. ("1 vs 4", group_1, group_4),
  66. ]
  67. results = []
  68. res = pg.rm_anova(data=df, within='WebpageID', dv='TotalIMIScore', subject='ParticipantID', detailed=True)
  69. print(res)
  70. for label, group_a, group_b in comparisons:
  71. mw_u_statistic, mw_p_value, mw_z_value, mw_effect_size_r = mann_whitney_test(group_a, group_b)
  72. ws_rank_sum_statistic, ws_p_value, ws_effect_size_r = wilcoxon_rank_sum_test(group_a, group_b)
  73. results.append({
  74. 'Comparison': label,
  75. 'Mann-Whitney U Statistic': mw_u_statistic,
  76. 'Mann-Whitney p-value': mw_p_value,
  77. 'Mann-Whitney Z-value': mw_z_value,
  78. 'Mann-Whitney Effect Size (r)': mw_effect_size_r,
  79. 'Wilcoxon Rank-Sum Statistic': ws_rank_sum_statistic,
  80. 'Wilcoxon p-value': ws_p_value,
  81. 'Wilcoxon Effect Size (r)': ws_effect_size_r
  82. })
  83. results_df = pd.DataFrame(results)
  84. print(results_df)