JamelBulgaria commited on
Commit
134becc
·
verified ·
1 Parent(s): bc91aa4

Upload Phase1_analysis.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. Phase1_analysis.py +103 -0
Phase1_analysis.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import pingouin as pg
4
+ from semopy import Model
5
+ from semopy import calc_stats
6
+
7
+ # ---------------------------------------------------------
8
+ # 1. LOAD DATA
9
+ # ---------------------------------------------------------
10
+ DATA_PATH = "phase1_clean.csv"
11
+ print("Loading data from:", DATA_PATH)
12
+
13
+ df = pd.read_csv(DATA_PATH)
14
+
15
+ # ---------------------------------------------------------
16
+ # 2. DEFINE OPERATORS
17
+ # ---------------------------------------------------------
18
+ operators = [
19
+ "accepting", "achieving", "appreciating", "arranging",
20
+ "boosting", "calculating", "constricting", "deciding",
21
+ "expanding", "sensing"
22
+ ]
23
+
24
+ # ---------------------------------------------------------
25
+ # 3. RECONSTRUCT ITEMS IN THE ORDER THEY APPEAR IN THE CSV
26
+ # ---------------------------------------------------------
27
+ ordered_items = {}
28
+
29
+ for op in operators:
30
+ cols = [c for c in df.columns if c.startswith(op + "_")]
31
+ # Sort by the numeric suffix
32
+ cols_sorted = sorted(cols, key=lambda x: int(x.split("_")[1]))
33
+ ordered_items[op] = cols_sorted
34
+
35
+ # ---------------------------------------------------------
36
+ # 4. COMPUTE CRONBACH ALPHAS
37
+ # ---------------------------------------------------------
38
+ print("\n=== CRONBACH ALPHAS ===")
39
+ alpha_results = {}
40
+
41
+ for op, cols in ordered_items.items():
42
+ data = df[cols]
43
+ alpha = pg.cronbach_alpha(data)[0]
44
+ alpha_results[op] = alpha
45
+ print(f"{op:12s} α = {alpha:.3f}")
46
+
47
+ pd.DataFrame(alpha_results.items(), columns=["operator", "alpha"]).to_csv(
48
+ "operator_reliability.csv", index=False
49
+ )
50
+ print("\nSaved operator_reliability.csv")
51
+
52
+ # ---------------------------------------------------------
53
+ # 5. BUILD CFA MODEL STRING
54
+ # ---------------------------------------------------------
55
+ model_lines = []
56
+ for op, cols in ordered_items.items():
57
+ line = f"{op} =~ " + " + ".join(cols)
58
+ model_lines.append(line)
59
+
60
+ model_string = "\n".join(model_lines)
61
+
62
+ print("\n=== CFA MODEL STRING ===")
63
+ print(model_string)
64
+
65
+ # ---------------------------------------------------------
66
+ # 6. FIT CFA MODEL
67
+ # ---------------------------------------------------------
68
+ print("\n=== FITTING CFA MODEL ===")
69
+
70
+ # Drop rows with missing values
71
+ all_items = [c for cols in ordered_items.values() for c in cols]
72
+ cfa_data = df[all_items].dropna()
73
+
74
+ model = Model(model_string)
75
+ model.fit(cfa_data)
76
+
77
+ # ---------------------------------------------------------
78
+ # 7. FIT INDICES
79
+ # ---------------------------------------------------------
80
+ print("\n=== CFA FIT INDICES ===")
81
+ stats = calc_stats(model)
82
+ fit_df = pd.DataFrame(stats.items(), columns=["index", "value"])
83
+ print(fit_df)
84
+
85
+ fit_df.to_csv("cfa_fit_indices.csv", index=False)
86
+ print("Saved cfa_fit_indices.csv")
87
+
88
+ # ---------------------------------------------------------
89
+ # 8. STANDARDIZED LOADINGS (modern semopy API)
90
+ # ---------------------------------------------------------
91
+ print("\n=== STANDARDIZED LOADINGS ===")
92
+
93
+ params = model.inspect(std_est=True)
94
+
95
+ # Filter for loadings: op == "=~"
96
+ loadings = params[params["op"] == "~"]
97
+
98
+ print(loadings)
99
+
100
+ loadings.to_csv("cfa_loadings.csv", index=False)
101
+ print("Saved cfa_loadings.csv")
102
+
103
+ print("\nDone.")