|
1 | 1 | using StructuralEquationModels, Test, Statistics |
2 | 2 |
|
| 3 | +const SEM = StructuralEquationModels |
| 4 | + |
3 | 5 | dat = example_data("political_democracy") |
4 | 6 | dat_missing = example_data("political_democracy_missing")[:, names(dat)] |
5 | 7 |
|
|
73 | 75 |
|
74 | 76 | @test @inferred(nsamples(model)) == nsamples(obs) |
75 | 77 | end |
| 78 | + |
| 79 | +@testset "replace_observed() preserves WLS state through finite-diff wrappers" begin |
| 80 | + model = Sem( |
| 81 | + specification = ram_matrices, |
| 82 | + observed = obs, |
| 83 | + implied = RAMSymbolic, |
| 84 | + loss = SemWLS, |
| 85 | + ) |
| 86 | + wls_loss = sem_term(model) |
| 87 | + findiff_model = Sem(SEM.FiniteDiffWrapper(wls_loss)) |
| 88 | + |
| 89 | + new_data = randn(nsamples(obs), nobserved_vars(obs)) |
| 90 | + |
| 91 | + findiff_model_oldstate = |
| 92 | + replace_observed(findiff_model, new_data; update_internal_state = false) |
| 93 | + findiff_model_newstate = |
| 94 | + replace_observed(findiff_model, new_data; update_internal_state = true) |
| 95 | + |
| 96 | + loss_orig = SEM._unwrap(sem_term(findiff_model)) |
| 97 | + loss_oldstate = SEM._unwrap(sem_term(findiff_model_oldstate)) |
| 98 | + loss_newstate = SEM._unwrap(sem_term(findiff_model_newstate)) |
| 99 | + |
| 100 | + @test loss_orig isa SemWLS |
| 101 | + @test loss_oldstate isa SemWLS |
| 102 | + @test loss_newstate isa SemWLS |
| 103 | + @test loss_orig !== loss_oldstate |
| 104 | + @test loss_orig !== loss_newstate |
| 105 | + @test loss_oldstate.V === loss_orig.V |
| 106 | + @test loss_newstate.V !== loss_orig.V |
| 107 | + @test observed_vars(loss_oldstate) == observed_vars(loss_orig) |
| 108 | +end |
0 commit comments