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

Upload Phase1_run_cfa.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. Phase1_run_cfa.py +42 -0
Phase1_run_cfa.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from semopy import Model, Optimizer
3
+
4
+ # Load dataset
5
+ df = pd.read_csv("phase1_clean.csv")
6
+
7
+ # Extract all column names
8
+ cols = df.columns.tolist()
9
+
10
+ # Identify operator prefixes (everything before the underscore)
11
+ operators = sorted(list(set([c.split("_")[0] for c in cols])))
12
+
13
+ # Build CFA model dynamically
14
+ model_lines = []
15
+ for op in operators:
16
+ items = sorted([c for c in cols if c.startswith(op + "_")])
17
+ if len(items) > 0:
18
+ line = f"{op.capitalize()} =~ " + " + ".join(items)
19
+ model_lines.append(line)
20
+
21
+ model_desc = "\n".join(model_lines)
22
+
23
+ # Build model
24
+ model = Model(model_desc)
25
+
26
+ # IMPORTANT FOR OLD SEMOPY: load dataset BEFORE optimizer
27
+ model.load_dataset(df)
28
+
29
+ # Fit model
30
+ opt = Optimizer(model)
31
+ opt.optimize()
32
+
33
+ # Extract loadings from parameter table (old semopy)
34
+ params = model.parameters
35
+ loadings = params[params["op"] == "~"]
36
+ loadings.to_csv("cfa_loadings.csv", index=False)
37
+
38
+ # Fit indices
39
+ stats = model.calc_stats()
40
+ pd.DataFrame([stats]).to_csv("cfa_fit_indices.csv", index=False)
41
+
42
+ print("CFA completed successfully.")