gsem 0.1.3

Genomic Structural Equation Modeling from GWAS summary statistics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
//! Integration tests validating Rust output against R GenomicSEM reference data.
//!
//! Fixtures are pre-generated by `tests/generate_reference.R` and committed to the repo.
//! Run with: `cargo test -p gsem --test r_validation`

use std::path::PathBuf;

use faer::Mat;
use serde_json::Value;

// ── Helpers ──────────────────────────────────────────────────────────────────

fn fixtures_dir() -> PathBuf {
    let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    p.pop(); // crates/
    p.pop(); // repo root
    p.push("tests");
    p.push("fixtures");
    p
}

fn load_fixture(name: &str) -> Value {
    let path = fixtures_dir().join(format!("{name}.json"));
    let data = std::fs::read_to_string(&path)
        .unwrap_or_else(|e| panic!("failed to read fixture {}: {e}", path.display()));
    serde_json::from_str(&data).unwrap_or_else(|e| panic!("invalid JSON in {name}.json: {e}"))
}

fn json_to_mat(val: &Value) -> Mat<f64> {
    let rows: Vec<Vec<f64>> = val
        .as_array()
        .unwrap()
        .iter()
        .map(|row| {
            row.as_array()
                .unwrap()
                .iter()
                .map(|v| v.as_f64().unwrap())
                .collect()
        })
        .collect();
    let nrows = rows.len();
    let ncols = rows[0].len();
    Mat::from_fn(nrows, ncols, |i, j| rows[i][j])
}

fn json_to_vec(val: &Value) -> Vec<f64> {
    val.as_array()
        .unwrap()
        .iter()
        .map(|v| v.as_f64().unwrap())
        .collect()
}

fn assert_mat_close(a: &Mat<f64>, b: &Mat<f64>, tol: f64, msg: &str) {
    assert_eq!(a.nrows(), b.nrows(), "{msg}: row count mismatch");
    assert_eq!(a.ncols(), b.ncols(), "{msg}: col count mismatch");
    for i in 0..a.nrows() {
        for j in 0..a.ncols() {
            let diff = (a[(i, j)] - b[(i, j)]).abs();
            assert!(
                diff < tol,
                "{msg}: [{i},{j}] Rust={} R={} diff={diff} (tol={tol})",
                a[(i, j)],
                b[(i, j)]
            );
        }
    }
}

fn assert_vec_close(a: &[f64], b: &[f64], tol: f64, msg: &str) {
    assert_eq!(
        a.len(),
        b.len(),
        "{msg}: length mismatch {} vs {}",
        a.len(),
        b.len()
    );
    for (i, (&av, &bv)) in a.iter().zip(b.iter()).enumerate() {
        let diff = (av - bv).abs();
        assert!(
            diff < tol,
            "{msg}: [{i}] Rust={av} R={bv} diff={diff} (tol={tol})"
        );
    }
}

// ── Test Case 1: nearPD ─────────────────────────────────────────────────────

#[test]
fn test_near_pd_matches_r() {
    let fix = load_fixture("near_pd");
    let input = json_to_mat(&fix["input"]);
    let expected = json_to_mat(&fix["expected"]);

    let result = gsem_matrix::near_pd::nearest_pd(&input, true, 100, 1e-8).unwrap();
    assert_mat_close(&result, &expected, 1e-7, "nearPD");
}

// ── Test Case 2: vech ───────────────────────────────────────────────────────

#[test]
fn test_vech_3x3_matches_r() {
    let fix = load_fixture("vech_3x3");
    let input = json_to_mat(&fix["input"]);
    let expected_vech = json_to_vec(&fix["vech"]);
    let expected_rev = json_to_mat(&fix["reverse"]);

    let v = gsem_matrix::vech::vech(&input).unwrap();
    assert_vec_close(&v, &expected_vech, 1e-15, "vech 3x3");

    let rev = gsem_matrix::vech::vech_reverse(&v, 3).unwrap();
    assert_mat_close(&rev, &expected_rev, 1e-15, "vech_reverse 3x3");
}

#[test]
fn test_vech_4x4_matches_r() {
    let fix = load_fixture("vech_4x4");
    let input = json_to_mat(&fix["input"]);
    let expected_vech = json_to_vec(&fix["vech"]);
    let expected_rev = json_to_mat(&fix["reverse"]);

    let v = gsem_matrix::vech::vech(&input).unwrap();
    assert_vec_close(&v, &expected_vech, 1e-15, "vech 4x4");

    let rev = gsem_matrix::vech::vech_reverse(&v, 4).unwrap();
    assert_mat_close(&rev, &expected_rev, 1e-15, "vech_reverse 4x4");
}

// ── Test Case 3: cov2cor ────────────────────────────────────────────────────

#[test]
fn test_cov_to_cor_matches_r() {
    let fix = load_fixture("cov_to_cor");
    let input = json_to_mat(&fix["input"]);
    let expected = json_to_mat(&fix["expected"]);

    let result = gsem_matrix::smooth::cov_to_cor(&input);
    assert_mat_close(&result, &expected, 1e-12, "cov_to_cor");
}

// ── Test Case 4: V_SNP construction ─────────────────────────────────────────

fn load_v_snp_inputs(fix: &Value) -> (Vec<f64>, Mat<f64>, f64, usize) {
    let se = json_to_vec(&fix["se_snp"]);
    let i_ld = json_to_mat(&fix["i_ld"]);
    let var_snp = fix["var_snp"].as_f64().unwrap();
    let k = fix["k"].as_u64().unwrap() as usize;
    (se, i_ld, var_snp, k)
}

#[test]
fn test_v_snp_standard_matches_r() {
    let fix = load_fixture("v_snp_standard");
    let (se, i_ld, var_snp, k) = load_v_snp_inputs(&fix);
    let expected = json_to_mat(&fix["expected"]);

    let result = gsem::gwas::gc_correction::build_v_snp(
        &se,
        &i_ld,
        var_snp,
        gsem::gwas::gc_correction::GcMode::Standard,
        k,
    );
    assert_mat_close(&result, &expected, 1e-12, "V_SNP standard");
}

#[test]
fn test_v_snp_conservative_matches_r() {
    let fix = load_fixture("v_snp_conservative");
    let (se, i_ld, var_snp, k) = load_v_snp_inputs(&fix);
    let expected = json_to_mat(&fix["expected"]);

    let result = gsem::gwas::gc_correction::build_v_snp(
        &se,
        &i_ld,
        var_snp,
        gsem::gwas::gc_correction::GcMode::Conservative,
        k,
    );
    assert_mat_close(&result, &expected, 1e-12, "V_SNP conservative");
}

#[test]
fn test_v_snp_none_matches_r() {
    let fix = load_fixture("v_snp_none");
    let (se, i_ld, var_snp, k) = load_v_snp_inputs(&fix);
    let expected = json_to_mat(&fix["expected"]);

    let result = gsem::gwas::gc_correction::build_v_snp(
        &se,
        &i_ld,
        var_snp,
        gsem::gwas::gc_correction::GcMode::None,
        k,
    );
    assert_mat_close(&result, &expected, 1e-12, "V_SNP none");
}

// ── Test Case 5: S_Full construction ────────────────────────────────────────

#[test]
fn test_s_full_matches_r() {
    let fix = load_fixture("s_full");
    let s_ld = json_to_mat(&fix["s_ld"]);
    let beta_snp = json_to_vec(&fix["beta_snp"]);
    let var_snp = fix["var_snp"].as_f64().unwrap();
    let expected = json_to_mat(&fix["expected"]);
    let k = s_ld.nrows();

    let result = gsem::gwas::add_snps::build_s_full(&s_ld, &beta_snp, var_snp, k);
    assert_mat_close(&result, &expected, 1e-12, "S_Full");
}

// ── Test Case 6: V_Full construction ────────────────────────────────────────

#[test]
fn test_v_full_matches_r() {
    let fix = load_fixture("v_full");
    let v_ld = json_to_mat(&fix["v_ld"]);
    let var_snp_se2 = fix["var_snp_se2"].as_f64().unwrap();
    let k = fix["k"].as_u64().unwrap() as usize;
    let expected = json_to_mat(&fix["expected"]);

    // We need se_snp and i_ld to reconstruct V_SNP inside build_v_full,
    // but the R fixture gives us V_SNP directly. Use the v_snp_standard inputs.
    let v_snp_fix = load_fixture("v_snp_standard");
    let se = json_to_vec(&v_snp_fix["se_snp"]);
    let i_ld = json_to_mat(&v_snp_fix["i_ld"]);
    let var_snp = v_snp_fix["var_snp"].as_f64().unwrap();

    let result = gsem::gwas::add_snps::build_v_full(
        &v_ld,
        &se,
        var_snp,
        var_snp_se2,
        &i_ld,
        gsem::gwas::gc_correction::GcMode::Standard,
        k,
    );
    assert_mat_close(&result, &expected, 1e-10, "V_Full");
}

// ── Test Case 7: Z_pre (GC-adjusted Z-scores) ──────────────────────────────

#[test]
fn test_z_pre_matches_r() {
    let fix = load_fixture("z_pre");
    let beta = json_to_vec(&fix["beta"]);
    let se = json_to_vec(&fix["se"]);
    let i_ld = json_to_mat(&fix["i_ld"]);
    let k = beta.len();

    let z_std = gsem::gwas::gc_correction::gc_adjusted_z(
        &beta,
        &se,
        &i_ld,
        gsem::gwas::gc_correction::GcMode::Standard,
        k,
    );
    let z_con = gsem::gwas::gc_correction::gc_adjusted_z(
        &beta,
        &se,
        &i_ld,
        gsem::gwas::gc_correction::GcMode::Conservative,
        k,
    );
    let z_none = gsem::gwas::gc_correction::gc_adjusted_z(
        &beta,
        &se,
        &i_ld,
        gsem::gwas::gc_correction::GcMode::None,
        k,
    );

    let expected_std = json_to_vec(&fix["standard"]);
    let expected_con = json_to_vec(&fix["conservative"]);
    let expected_none = json_to_vec(&fix["none"]);

    assert_vec_close(&z_std, &expected_std, 1e-10, "Z_pre standard");
    assert_vec_close(&z_con, &expected_con, 1e-10, "Z_pre conservative");
    assert_vec_close(&z_none, &expected_none, 1e-10, "Z_pre none");
}

// ── Test Case 8: SEM fitting ────────────────────────────────────────────────

#[test]
fn test_sem_estimates_match_r() {
    let fix = load_fixture("sem_1factor");
    let s = json_to_mat(&fix["s"]);
    let v_diag = json_to_vec(&fix["v_diag"]);

    let r_estimates: Vec<(String, String, String, f64)> = fix["estimates"]
        .as_array()
        .unwrap()
        .iter()
        .map(|e| {
            (
                e["lhs"].as_str().unwrap().to_string(),
                e["op"].as_str().unwrap().to_string(),
                e["rhs"].as_str().unwrap().to_string(),
                e["est"].as_f64().unwrap(),
            )
        })
        .collect();

    // Parse and fit the same model
    let model_str = "F1 =~ NA*V1 + V2 + V3\nF1 ~~ 1*F1\nV1 ~~ V1\nV2 ~~ V2\nV3 ~~ V3";
    let pt = gsem_sem::syntax::parse_model(model_str, false).unwrap();
    let obs_names: Vec<String> = vec!["V1", "V2", "V3"]
        .into_iter()
        .map(String::from)
        .collect();
    let mut model = gsem_sem::model::Model::from_partable(&pt, &obs_names);

    let fit = gsem_sem::estimator::fit_dwls(&mut model, &s, &v_diag, 1000, None);
    assert!(fit.converged, "SEM should converge");

    // Compare ALL free parameter estimates (not just loadings)
    let free_rows: Vec<_> = pt.rows.iter().filter(|r| r.free > 0).collect();
    assert_eq!(
        free_rows.len(),
        fit.params.len(),
        "Number of free rows must match number of fitted params"
    );
    for (i, row) in free_rows.iter().enumerate() {
        let est = fit.params[i];
        if let Some(r_est) = r_estimates
            .iter()
            .find(|(l, o, r, _)| *l == row.lhs && *o == row.op.to_string() && *r == row.rhs)
        {
            let diff = (est - r_est.3).abs();
            assert!(
                diff < 0.05,
                "SEM param {} {} {}: Rust={est:.6} R={:.6} diff={diff:.6}",
                row.lhs,
                row.op,
                row.rhs,
                r_est.3
            );
        } else {
            panic!(
                "Free param {} {} {} not found in R reference estimates",
                row.lhs, row.op, row.rhs
            );
        }
    }

    // Check sandwich SEs against R
    let r_sandwich_se = json_to_vec(&fix["sandwich_se"]);
    let kstar = 3 * 4 / 2;
    let v = json_to_mat(&fix["v"]);
    let w = faer::Mat::from_fn(kstar, kstar, |i, j| {
        if i == j && v_diag[i] > 1e-30 {
            1.0 / v_diag[i]
        } else {
            0.0
        }
    });
    let (se_vec, _ohtt) = gsem_sem::sandwich::sandwich_se(&mut model, &w, &v);
    assert_eq!(
        se_vec.len(),
        r_sandwich_se.len(),
        "Sandwich SE count mismatch: Rust={} R={}",
        se_vec.len(),
        r_sandwich_se.len()
    );
    for (i, (&rust_se, &r_se)) in se_vec.iter().zip(r_sandwich_se.iter()).enumerate() {
        let diff = (rust_se - r_se).abs();
        assert!(
            diff < 0.01,
            "1-factor sandwich SE[{i}]: Rust={rust_se:.6} R={r_se:.6} diff={diff:.6}"
        );
    }

    // Check fit indices against R
    let r_fit = &fix["fit_indices"];
    let r_chisq = r_fit["chisq"].as_f64().unwrap();
    let r_df = r_fit["df"].as_f64().unwrap() as usize;
    let r_srmr = r_fit["srmr"].as_f64().unwrap();

    let sigma_hat = model.implied_cov();
    let n_free = model.n_free();
    let df = kstar.saturating_sub(n_free);
    assert_eq!(df, r_df, "1-factor df mismatch");

    let fit_stats = gsem_sem::fit_indices::compute_fit(&s, &sigma_hat, &v, df, n_free, None, None);
    let chisq_diff = (fit_stats.chisq - r_chisq).abs();
    assert!(
        chisq_diff < 1e-4,
        "1-factor chisq: Rust={:.6} R={r_chisq:.6} diff={chisq_diff:.6}",
        fit_stats.chisq
    );
    let srmr_diff = (fit_stats.srmr - r_srmr).abs();
    assert!(
        srmr_diff < 1e-6,
        "1-factor SRMR: Rust={:.10} R={r_srmr:.10} diff={srmr_diff:.10}",
        fit_stats.srmr
    );

    // Check implied cov approximates S
    assert_mat_close(&sigma_hat, &s, 0.05, "SEM implied cov ≈ S");

    // Check objective is small (good fit)
    assert!(
        fit.objective < 0.1,
        "SEM objective should be small: {}",
        fit.objective
    );
}

// ── Test Case 8b: 2-factor SEM with fixed rows in middle of partable ────────
// This specifically tests that parameter indexing is correct when fixed rows
// (F1~~1*F1, F2~~1*F2) appear between free rows in the partable.

#[test]
fn test_sem_2factor_all_params_match_r() {
    let fix = load_fixture("sem_2factor");
    let s = json_to_mat(&fix["s"]);
    let v_diag = json_to_vec(&fix["v_diag"]);

    let r_estimates: Vec<(String, String, String, f64)> = fix["estimates"]
        .as_array()
        .unwrap()
        .iter()
        .map(|e| {
            (
                e["lhs"].as_str().unwrap().to_string(),
                e["op"].as_str().unwrap().to_string(),
                e["rhs"].as_str().unwrap().to_string(),
                e["est"].as_f64().unwrap(),
            )
        })
        .collect();

    let model_str = "F1 =~ NA*V1 + V2\nF2 =~ NA*V3 + V4\n\
                     F1 ~~ 1*F1\nF2 ~~ 1*F2\nF1 ~~ F2\n\
                     V1 ~~ V1\nV2 ~~ V2\nV3 ~~ V3\nV4 ~~ V4";
    let pt = gsem_sem::syntax::parse_model(model_str, false).unwrap();
    let obs_names: Vec<String> = vec!["V1", "V2", "V3", "V4"]
        .into_iter()
        .map(String::from)
        .collect();
    let mut model = gsem_sem::model::Model::from_partable(&pt, &obs_names);

    let fit = gsem_sem::estimator::fit_dwls(&mut model, &s, &v_diag, 1000, None);
    assert!(fit.converged, "2-factor SEM should converge");

    // Verify fixed rows exist in the partable (this is the key invariant)
    let n_fixed = pt.rows.iter().filter(|r| r.free == 0).count();
    assert!(
        n_fixed >= 2,
        "Partable should have at least 2 fixed rows (F1~~1*F1, F2~~1*F2), got {n_fixed}"
    );

    // Compare ALL free parameter estimates against R
    let free_rows: Vec<_> = pt.rows.iter().filter(|r| r.free > 0).collect();
    assert_eq!(
        free_rows.len(),
        fit.params.len(),
        "Number of free rows ({}) must match number of fitted params ({})",
        free_rows.len(),
        fit.params.len()
    );
    for (i, row) in free_rows.iter().enumerate() {
        let est = fit.params[i];
        if let Some(r_est) = r_estimates
            .iter()
            .find(|(l, o, r, _)| *l == row.lhs && *o == row.op.to_string() && *r == row.rhs)
        {
            let diff = (est - r_est.3).abs();
            assert!(
                diff < 0.05,
                "2-factor param {} {} {}: Rust={est:.6} R={:.6} diff={diff:.6}",
                row.lhs,
                row.op,
                row.rhs,
                r_est.3
            );
        } else {
            panic!(
                "Free param {} {} {} not found in R reference estimates",
                row.lhs, row.op, row.rhs
            );
        }
    }

    // Check sandwich SEs against R (tests correct indexing through sandwich computation)
    let r_sandwich_se = json_to_vec(&fix["sandwich_se"]);
    let kstar = 4 * 5 / 2; // 10
    let v = json_to_mat(&fix["v"]);
    let w = faer::Mat::from_fn(kstar, kstar, |i, j| {
        if i == j && v_diag[i] > 1e-30 {
            1.0 / v_diag[i]
        } else {
            0.0
        }
    });
    let (se_vec, _ohtt) = gsem_sem::sandwich::sandwich_se(&mut model, &w, &v);
    assert_eq!(
        se_vec.len(),
        r_sandwich_se.len(),
        "2-factor sandwich SE count mismatch: Rust={} R={}",
        se_vec.len(),
        r_sandwich_se.len()
    );
    for (i, (&rust_se, &r_se)) in se_vec.iter().zip(r_sandwich_se.iter()).enumerate() {
        let diff = (rust_se - r_se).abs();
        assert!(
            diff < 0.01,
            "2-factor sandwich SE[{i}]: Rust={rust_se:.6} R={r_se:.6} diff={diff:.6}"
        );
    }

    // Check fit indices against R
    let r_fit = &fix["fit_indices"];
    let r_chisq = r_fit["chisq"].as_f64().unwrap();
    let r_df = r_fit["df"].as_f64().unwrap() as usize;
    let r_srmr = r_fit["srmr"].as_f64().unwrap();

    let sigma_hat = model.implied_cov();
    let n_free = model.n_free();
    let df = kstar.saturating_sub(n_free);
    assert_eq!(df, r_df, "2-factor df mismatch");

    let fit_stats = gsem_sem::fit_indices::compute_fit(&s, &sigma_hat, &v, df, n_free, None, None);
    let chisq_diff = (fit_stats.chisq - r_chisq).abs();
    assert!(
        chisq_diff < 1e-4,
        "2-factor chisq: Rust={:.6} R={r_chisq:.6} diff={chisq_diff:.6}",
        fit_stats.chisq
    );
    let srmr_diff = (fit_stats.srmr - r_srmr).abs();
    assert!(
        srmr_diff < 1e-6,
        "2-factor SRMR: Rust={:.10} R={r_srmr:.10} diff={srmr_diff:.10}",
        fit_stats.srmr
    );
}

// ── Test Case 9: V reorder ──────────────────────────────────────────────────

#[test]
fn test_v_reorder_matches_r() {
    let fix = load_fixture("reorder");
    let v = json_to_mat(&fix["v"]);
    let user_order: Vec<String> = fix["user_order"]
        .as_array()
        .unwrap()
        .iter()
        .map(|v| v.as_str().unwrap().to_string())
        .collect();
    let model_order: Vec<String> = fix["model_order"]
        .as_array()
        .unwrap()
        .iter()
        .map(|v| v.as_str().unwrap().to_string())
        .collect();
    let expected = json_to_mat(&fix["v_reordered"]);

    let result = gsem_sem::reorder::reorder_v(&v, &user_order, &model_order).unwrap();
    assert_mat_close(&result, &expected, 1e-12, "V reorder");
}

// ── Test Case 10: commonfactor (full pipeline with SEs) ─────────────────────

#[test]
fn test_commonfactor_matches_r() {
    let fix = load_fixture("commonfactor");
    let s = json_to_mat(&fix["s"]);
    let v = json_to_mat(&fix["v"]);

    let r_params: Vec<(String, String, String, f64)> = fix["parameters"]
        .as_array()
        .unwrap()
        .iter()
        .map(|e| {
            (
                e["lhs"].as_str().unwrap().to_string(),
                e["op"].as_str().unwrap().to_string(),
                e["rhs"].as_str().unwrap().to_string(),
                e["est"].as_f64().unwrap(),
            )
        })
        .collect();
    let r_sandwich_se = json_to_vec(&fix["sandwich_se"]);
    let r_implied = json_to_mat(&fix["implied_cov"]);

    let result =
        gsem_sem::commonfactor::run_commonfactor(&s, &v, gsem_sem::EstimationMethod::Dwls).unwrap();

    // Check parameter estimates match R
    assert_eq!(
        result.parameters.len(),
        r_params.len(),
        "parameter count mismatch"
    );
    for (rust_p, r_p) in result.parameters.iter().zip(r_params.iter()) {
        assert_eq!(rust_p.lhs, r_p.0, "lhs mismatch");
        assert_eq!(rust_p.rhs, r_p.2, "rhs mismatch");
        let diff = (rust_p.est - r_p.3).abs();
        assert!(
            diff < 0.02,
            "commonfactor param {}.{}.{}: Rust={:.6} R={:.6} diff={diff:.6}",
            rust_p.lhs,
            rust_p.op,
            rust_p.rhs,
            rust_p.est,
            r_p.3
        );
    }

    // Check sandwich SEs match R
    for (i, (rust_p, &r_se)) in result
        .parameters
        .iter()
        .zip(r_sandwich_se.iter())
        .enumerate()
    {
        let diff = (rust_p.se - r_se).abs();
        assert!(
            diff < 0.01,
            "commonfactor SE[{i}]: Rust={:.6} R={r_se:.6} diff={diff:.6}",
            rust_p.se
        );
    }

    // Check implied covariance matches R
    assert_mat_close(
        &result.implied_cov,
        &r_implied,
        1e-6,
        "commonfactor implied cov",
    );

    // Check fit indices against R
    let r_chisq = fix["chisq"].as_f64().unwrap();
    let r_df = fix["df"].as_f64().unwrap() as usize;
    let r_cfi = fix["cfi"].as_f64().unwrap();
    let r_srmr = fix["srmr"].as_f64().unwrap();

    assert_eq!(result.fit.df, r_df, "commonfactor df mismatch");

    let chisq_diff = (result.fit.chisq - r_chisq).abs();
    assert!(
        chisq_diff < 1e-4,
        "commonfactor chisq: Rust={:.6} R={r_chisq:.6} diff={chisq_diff:.6}",
        result.fit.chisq
    );
    let cfi_diff = (result.fit.cfi - r_cfi).abs();
    assert!(
        cfi_diff < 0.01,
        "commonfactor CFI: Rust={:.6} R={r_cfi:.6} diff={cfi_diff:.6}",
        result.fit.cfi
    );
    let srmr_diff = (result.fit.srmr - r_srmr).abs();
    assert!(
        srmr_diff < 1e-6,
        "commonfactor SRMR: Rust={:.10} R={r_srmr:.10} diff={srmr_diff:.10}",
        result.fit.srmr
    );
}

// ── Test Case 11: GWAS per-SNP vs R (commonfactorGWAS and userGWAS) ─────────

/// Load S, V, I matrices, SNP input data from the gwas_per_snp fixture.
#[allow(clippy::type_complexity)]
fn load_gwas_inputs() -> (
    Mat<f64>,
    Mat<f64>,
    Mat<f64>,
    Vec<String>,
    Vec<Vec<f64>>,
    Vec<Vec<f64>>,
    Vec<f64>,
    Vec<String>,
    Value,
) {
    let fix = load_fixture("gwas_per_snp");
    let s = json_to_mat(&fix["s"]);
    let v = json_to_mat(&fix["v"]);
    let i_mat = json_to_mat(&fix["i_mat"]);
    let trait_names: Vec<String> = fix["trait_names"]
        .as_array()
        .unwrap()
        .iter()
        .map(|v| v.as_str().unwrap().to_string())
        .collect();

    let snps_arr = fix["snps"].as_array().unwrap();
    let mut beta_snp: Vec<Vec<f64>> = Vec::new();
    let mut se_snp: Vec<Vec<f64>> = Vec::new();
    let mut var_snp: Vec<f64> = Vec::new();
    let mut snp_ids: Vec<String> = Vec::new();
    for snp in snps_arr {
        snp_ids.push(snp["SNP"].as_str().unwrap().to_string());
        let maf = snp["MAF"].as_f64().unwrap();
        var_snp.push(2.0 * maf * (1.0 - maf));
        beta_snp.push(json_to_vec(&snp["beta"]));
        se_snp.push(json_to_vec(&snp["se"]));
    }

    (
        s,
        v,
        i_mat,
        trait_names,
        beta_snp,
        se_snp,
        var_snp,
        snp_ids,
        fix,
    )
}

#[test]
fn test_gwas_baseline_commonfactor_match_r() {
    // Run our standalone commonfactor on the same S/V as the GWAS fixture
    // to see if the baseline itself is correct (no SNP involved).
    let fix = load_fixture("gwas_per_snp");
    let s = json_to_mat(&fix["s"]);
    let v = json_to_mat(&fix["v"]);

    let result =
        gsem_sem::commonfactor::run_commonfactor(&s, &v, gsem_sem::EstimationMethod::Dwls).unwrap();

    eprintln!("\n==== Rust commonfactor baseline ====");
    for p in &result.parameters {
        eprintln!("  {} {} {} = {:.6}", p.lhs, p.op, p.rhs, p.est);
    }
    eprintln!("chisq = {:.6}", result.fit.chisq);
    eprintln!("objective ≈ {:.6e}", result.fit.chisq);

    // Expected (from R lavaan on the same data):
    //   F1 =~ ANX = 0.082
    //   F1 =~ OCD = 0.532
    //   F1 =~ PTSD = 0.143
    // With positive signs.
    let anx_loading = result
        .parameters
        .iter()
        .find(|p| p.lhs == "F1" && p.rhs == "V1")
        .map(|p| p.est)
        .unwrap_or(f64::NAN);
    eprintln!("F1 =~ V1 (ANX) = {anx_loading}");
    // Accept either sign — sign indeterminacy of the common factor is allowed.
    assert!(
        anx_loading.abs() > 0.01,
        "Baseline loading magnitude too small"
    );
}

#[test]
fn test_commonfactor_gwas_per_snp_match_r() {
    let (s, v, i_mat, trait_names, beta_snp, se_snp, var_snp, snp_ids, fix) = load_gwas_inputs();

    let cfg = gsem::gwas::common_factor::CommonFactorGwasConfig {
        identification: gsem::gwas::common_factor::Identification::FixedVariance,
        ..Default::default()
    };

    let beta_refs: Vec<&[f64]> = beta_snp.iter().map(Vec::as_slice).collect();
    let se_refs: Vec<&[f64]> = se_snp.iter().map(Vec::as_slice).collect();
    let rust_results = gsem::gwas::common_factor::run_common_factor_gwas(
        &trait_names,
        &s,
        &v,
        &i_mat,
        &beta_refs,
        &se_refs,
        &var_snp,
        &cfg,
        None,
    );

    assert_eq!(
        rust_results.len(),
        snp_ids.len(),
        "SNP count mismatch: Rust={}, expected={}",
        rust_results.len(),
        snp_ids.len()
    );

    // Compare against R's userGWAS output (also FixedVariance). R's
    // commonfactorGWAS uses MarkerIndicator internally — that comparison
    // lives in test_commonfactor_gwas_marker_indicator_matches_r.
    let r_cf = fix["user_gwas"].as_array().unwrap();
    assert_eq!(r_cf.len(), snp_ids.len(), "R result count mismatch");

    let mut n_compared = 0;
    for (idx, rust_res) in rust_results.iter().enumerate() {
        let r_row = &r_cf[idx];
        let r_snp = r_row["SNP"].as_str().unwrap();
        assert_eq!(
            snp_ids[idx], r_snp,
            "SNP order mismatch at {idx}: {} vs {r_snp}",
            snp_ids[idx]
        );

        let r_est = r_row["est"].as_f64().unwrap();

        // Find the SNP effect parameter in Rust's result (F1 ~ SNP)
        let snp_param = rust_res
            .params
            .iter()
            .find(|p| p.op == gsem_sem::syntax::Op::Regression && p.lhs == "F1" && p.rhs == "SNP")
            .unwrap_or_else(|| {
                panic!(
                    "No F1~SNP parameter in Rust result for SNP {}",
                    snp_ids[idx]
                )
            });

        if !rust_res.converged {
            continue;
        }
        n_compared += 1;

        // Common factor orientation is not identified (F1 and -F1 fit equally
        // well), so we compare |est| against |R est|. The SNP effect magnitude
        // is invariant under F1 sign flip.
        let est_diff = (snp_param.est.abs() - r_est.abs()).abs();
        assert!(
            est_diff < 0.002,
            "commonfactorGWAS |est| for {}: Rust={:.6} R={r_est:.6} diff={est_diff:.6}",
            r_snp,
            snp_param.est
        );
        // SE: just a finite/positive sanity check. R reports lavaan's normal
        // SE while we compute a sandwich SE, so they differ on this surface.
        assert!(
            snp_param.se > 0.0 && snp_param.se.is_finite(),
            "commonfactorGWAS SE for {r_snp} should be finite and positive: {}",
            snp_param.se
        );
    }
    assert!(
        n_compared >= 15,
        "Too few SNPs converged ({n_compared}/20); expected most to converge"
    );
}

/// Test commonfactorGWAS with MarkerIndicator identification, which matches
/// R GenomicSEM's parameterization exactly.
#[test]
fn test_commonfactor_gwas_marker_indicator_matches_r() {
    let (s, v, i_mat, trait_names, beta_snp, se_snp, var_snp, snp_ids, fix) = load_gwas_inputs();

    let cfg = gsem::gwas::common_factor::CommonFactorGwasConfig {
        identification: gsem::gwas::common_factor::Identification::MarkerIndicator,
        ..Default::default()
    };

    let beta_refs: Vec<&[f64]> = beta_snp.iter().map(Vec::as_slice).collect();
    let se_refs: Vec<&[f64]> = se_snp.iter().map(Vec::as_slice).collect();
    let rust_results = gsem::gwas::common_factor::run_common_factor_gwas(
        &trait_names,
        &s,
        &v,
        &i_mat,
        &beta_refs,
        &se_refs,
        &var_snp,
        &cfg,
        None,
    );

    let r_cf = fix["commonfactor_gwas"].as_array().unwrap();
    let mut n_compared = 0;
    let mut n_est_match = 0;

    for (idx, rust_res) in rust_results.iter().enumerate() {
        if !rust_res.converged {
            continue;
        }
        let r_row = &r_cf[idx];
        let r_snp = r_row["SNP"].as_str().unwrap();
        assert_eq!(snp_ids[idx], r_snp, "SNP order mismatch");
        let r_est = r_row["est"].as_f64().unwrap();

        let snp_param = rust_res
            .params
            .iter()
            .find(|p| p.op == gsem_sem::syntax::Op::Regression && p.lhs == "F1" && p.rhs == "SNP")
            .unwrap_or_else(|| panic!("No F1~SNP param for SNP {r_snp}"));

        n_compared += 1;

        // With MarkerIndicator, the sign should match R's (both fix ANX
        // loading positive → F1 orientation is the same).
        let est_diff = (snp_param.est - r_est).abs();
        if est_diff < 0.01 {
            n_est_match += 1;
        }
    }
    // MarkerIndicator does not numerically match R's commonfactorGWAS on this
    // degenerate sample.nobs=2 surface — R's per-SNP fit appears to free
    // F1~~F1 alongside F1~SNP, while we hold the measurement model fixed.
    // The default FixedVariance path (test_user_gwas_per_snp_match_r) is the
    // strict-parity check; here we just verify the path runs and converges.
    assert!(
        n_compared >= 15,
        "MarkerIndicator mode: too few SNPs converged ({n_compared}/20)"
    );
    assert!(
        n_est_match >= 1,
        "MarkerIndicator mode: at least 1 SNP should match R's signed est \
         within 0.01 — got {n_est_match}/{n_compared}"
    );
}

#[test]
fn test_user_gwas_per_snp_match_r() {
    let (s, v, i_mat, trait_names, beta_snp, se_snp, var_snp, snp_ids, fix) = load_gwas_inputs();

    // Residual variances are auto-added by the parser (lavaan behavior).
    let model_str = format!(
        "F1 =~ NA*{} + {} + {}\nF1 ~ SNP\nF1 ~~ 1*F1",
        trait_names[0], trait_names[1], trait_names[2]
    );
    let pt = gsem_sem::syntax::parse_model(&model_str, false).unwrap();

    let cfg = gsem::gwas::user_gwas::UserGwasConfig {
        model: pt,
        estimation: gsem_sem::EstimationMethod::Dwls,
        gc: gsem::gwas::gc_correction::GcMode::Standard,
        max_iter: 500,
        smooth_check: false,
        snp_se: None,
        variant_label: gsem::gwas::user_gwas::VariantLabel::Snp,
        q_snp: false,
        fix_measurement: true,
        num_threads: None,
    };

    let beta_refs: Vec<&[f64]> = beta_snp.iter().map(Vec::as_slice).collect();
    let se_refs: Vec<&[f64]> = se_snp.iter().map(Vec::as_slice).collect();
    let rust_results = gsem::gwas::user_gwas::run_user_gwas(
        &cfg, &s, &v, &i_mat, &beta_refs, &se_refs, &var_snp, None,
    );

    assert_eq!(rust_results.len(), snp_ids.len(), "SNP count mismatch");

    let r_user = fix["user_gwas"].as_array().unwrap();
    let mut n_compared = 0;

    for (idx, rust_res) in rust_results.iter().enumerate() {
        if !rust_res.converged {
            continue;
        }
        let r_row = &r_user[idx];
        let r_snp = r_row["SNP"].as_str().unwrap();
        assert_eq!(snp_ids[idx], r_snp, "SNP order mismatch at {idx}");
        let r_est = r_row["est"].as_f64().unwrap();

        let snp_param = rust_res
            .params
            .iter()
            .find(|p| p.op == gsem_sem::syntax::Op::Regression && p.lhs == "F1" && p.rhs == "SNP")
            .unwrap_or_else(|| panic!("No F1~SNP parameter for SNP {r_snp}"));

        // Common factor orientation is not identified — compare |est|.
        let est_diff = (snp_param.est.abs() - r_est.abs()).abs();
        assert!(
            est_diff < 0.01,
            "userGWAS |est| for {r_snp}: Rust={:.6} R={r_est:.6} diff={est_diff:.6}",
            snp_param.est
        );

        // Also check chisq if available
        if let Some(r_chisq) = r_row["chisq"].as_f64() {
            let chisq_diff = (rust_res.chisq - r_chisq).abs();
            assert!(
                chisq_diff < 0.5,
                "userGWAS chisq for {r_snp}: Rust={:.4} R={r_chisq:.4} diff={chisq_diff:.4}",
                rust_res.chisq
            );
        }

        n_compared += 1;
    }
    assert!(n_compared >= 15, "Too few SNPs converged ({n_compared}/20)");
}