|
1 | 1 | using Statistics: cov, mean |
| 2 | +using Random |
2 | 3 |
|
3 | 4 | ############################################################################################ |
4 | 5 | ### models w.o. meanstructure |
|
161 | 162 | ) |
162 | 163 | end |
163 | 164 |
|
| 165 | +############################################################################################ |
| 166 | +### data simulation |
| 167 | +############################################################################################ |
| 168 | + |
| 169 | +@testset "data_simulation_wo_mean" begin |
| 170 | + # parameters to recover |
| 171 | + params = start_simple( |
| 172 | + model_ml; |
| 173 | + start_loadings = 0.5, |
| 174 | + start_regressions = 0.5, |
| 175 | + start_variances_observed = 0.5, |
| 176 | + start_variances_latent = 1.0, |
| 177 | + start_covariances_observed = 0.2, |
| 178 | + ) |
| 179 | + # set seed for simulation |
| 180 | + Random.seed!(83472834) |
| 181 | + colnames = Symbol.(names(example_data("political_democracy"))) |
| 182 | + # simulate data |
| 183 | + model_ml_new = swap_observed( |
| 184 | + model_ml, |
| 185 | + data = rand(model_ml, params, 1_000_000), |
| 186 | + specification = spec, |
| 187 | + obs_colnames = colnames, |
| 188 | + ) |
| 189 | + model_ml_sym_new = swap_observed( |
| 190 | + model_ml_sym, |
| 191 | + data = rand(model_ml_sym, params, 1_000_000), |
| 192 | + specification = spec, |
| 193 | + obs_colnames = colnames, |
| 194 | + ) |
| 195 | + # fit models |
| 196 | + sol_ml = solution(sem_fit(model_ml_new)) |
| 197 | + sol_ml_sym = solution(sem_fit(model_ml_sym_new)) |
| 198 | + # check solution |
| 199 | + @test maximum(abs.(sol_ml - params)) < 0.01 |
| 200 | + @test maximum(abs.(sol_ml_sym - params)) < 0.01 |
| 201 | +end |
| 202 | + |
164 | 203 | ############################################################################################ |
165 | 204 | ### test hessians |
166 | 205 | ############################################################################################ |
|
332 | 371 | ) |
333 | 372 | end |
334 | 373 |
|
| 374 | +############################################################################################ |
| 375 | +### data simulation |
| 376 | +############################################################################################ |
| 377 | + |
| 378 | +@testset "data_simulation_with_mean" begin |
| 379 | + # parameters to recover |
| 380 | + params = start_simple( |
| 381 | + model_ml; |
| 382 | + start_loadings = 0.5, |
| 383 | + start_regressions = 0.5, |
| 384 | + start_variances_observed = 0.5, |
| 385 | + start_variances_latent = 1.0, |
| 386 | + start_covariances_observed = 0.2, |
| 387 | + start_means = 0.5, |
| 388 | + ) |
| 389 | + # set seed for simulation |
| 390 | + Random.seed!(83472834) |
| 391 | + colnames = Symbol.(names(example_data("political_democracy"))) |
| 392 | + # simulate data |
| 393 | + model_ml_new = swap_observed( |
| 394 | + model_ml, |
| 395 | + data = rand(model_ml, params, 1_000_000), |
| 396 | + specification = spec, |
| 397 | + obs_colnames = colnames, |
| 398 | + meanstructure = true, |
| 399 | + ) |
| 400 | + model_ml_sym_new = swap_observed( |
| 401 | + model_ml_sym, |
| 402 | + data = rand(model_ml_sym, params, 1_000_000), |
| 403 | + specification = spec, |
| 404 | + obs_colnames = colnames, |
| 405 | + meanstructure = true, |
| 406 | + ) |
| 407 | + # fit models |
| 408 | + sol_ml = solution(sem_fit(model_ml_new)) |
| 409 | + sol_ml_sym = solution(sem_fit(model_ml_sym_new)) |
| 410 | + # check solution |
| 411 | + @test maximum(abs.(sol_ml - params)) < 0.01 |
| 412 | + @test maximum(abs.(sol_ml_sym - params)) < 0.01 |
| 413 | +end |
| 414 | + |
335 | 415 | ############################################################################################ |
336 | 416 | ### fiml |
337 | 417 | ############################################################################################ |
|
0 commit comments