Skip to content

Commit 618e2df

Browse files
refactor chi2
1 parent f13704e commit 618e2df

1 file changed

Lines changed: 16 additions & 28 deletions

File tree

  • src/frontend/fit/fitmeasures

src/frontend/fit/fitmeasures/chi2.jl

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ Return the χ² value.
99
# Single Models
1010
############################################################################################
1111

12-
χ²(fit::SemFit, model::AbstractSemSingle) =
13-
sum(loss -> χ²(loss, fit, model), model.loss.functions)
12+
function χ²(fit::SemFit, model::AbstractSemSingle)
13+
check_single_lossfun(model; throw_error = true)
14+
return χ²(model.loss.functions[1], fit::SemFit, model::AbstractSemSingle)
15+
end
1416

15-
# RAM + SemML
16-
χ²(lossfun::SemML, fit::SemFit, model::AbstractSemSingle) =
17+
χ²(::SemML, fit::SemFit, model::AbstractSemSingle) =
1718
(nsamples(fit) - 1) *
1819
(fit.minimum - logdet(obs_cov(observed(model))) - nobserved_vars(observed(model)))
1920

2021
# bollen, p. 115, only correct for GLS weight matrix
21-
χ²(lossfun::SemWLS, fit::SemFit, model::AbstractSemSingle) =
22+
χ²(::SemWLS, fit::SemFit, model::AbstractSemSingle) =
2223
(nsamples(fit) - 1) * fit.minimum
2324

2425
# FIML
25-
function χ²(lossfun::SemFIML, fit::SemFit, model::AbstractSemSingle)
26+
function χ²(::SemFIML, fit::SemFit, model::AbstractSemSingle)
2627
ll_H0 = minus2ll(fit)
2728
ll_H1 = minus2ll(observed(model))
2829
return ll_H0 - ll_H1
@@ -32,38 +33,25 @@ end
3233
# Collections
3334
############################################################################################
3435

35-
function χ²(fit::SemFit, models::SemEnsemble)
36-
isempty(models.sems) && return 0.0
37-
38-
lossfun = models.sems[1].loss.functions[1]
39-
# check that all models use the same single loss function
40-
L = typeof(lossfun)
41-
for (i, sem) in enumerate(models.sems)
42-
if length(sem.loss.functions) > 1
43-
@error "Model for group #$i has $(length(sem.loss.functions)) loss functions. Only the single one is supported"
44-
end
45-
cur_lossfun = sem.loss.functions[1]
46-
if !isa(cur_lossfun, L)
47-
@error "Loss function for group #$i model is $(typeof(cur_lossfun)), expected $L. Heterogeneous loss functions are not supported"
48-
end
49-
end
50-
51-
return χ²(lossfun, fit, models)
36+
function χ²(fit::SemFit, model::SemEnsemble)
37+
check_single_lossfun(model; throw_error = true)
38+
lossfun = model.sems[1].loss.functions[1]
39+
return χ²(lossfun, fit, model)
5240
end
5341

54-
function χ²(lossfun::SemWLS, fit::SemFit, models::SemEnsemble)
55-
return (nsamples(models) - 1) * fit.minimum
42+
function χ²(::SemWLS, fit::SemFit, models::SemEnsemble)
43+
return (nsamples(models) - models.n) * fit.minimum
5644
end
5745

58-
function χ²(lossfun::SemML, fit::SemFit, models::SemEnsemble)
46+
function χ²(::SemML, fit::SemFit, models::SemEnsemble)
5947
G = sum(zip(models.weights, models.sems)) do (w, model)
6048
data = observed(model)
6149
w * (logdet(obs_cov(data)) + nobserved_vars(data))
6250
end
63-
return (nsamples(models) - 1) * (fit.minimum - G)
51+
return (nsamples(models) - models.n) * (fit.minimum - G)
6452
end
6553

66-
function χ²(lossfun::SemFIML, fit::SemFit, models::SemEnsemble)
54+
function χ²(::SemFIML, fit::SemFit, models::SemEnsemble)
6755
ll_H0 = minus2ll(fit)
6856
ll_H1 = sum(minus2ll observed, models.sems)
6957
return ll_H0 - ll_H1

0 commit comments

Comments
 (0)