-
Notifications
You must be signed in to change notification settings - Fork 6
Fiml meanstructure argument #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -31,3 +31,17 @@ function check_acyclic(A::AbstractMatrix; verbose::Bool = false) | |||||||||||||||||
| return A | ||||||||||||||||||
| end | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| # Verify that the `meanstructure` argument aligns with the model specification. | ||||||||||||||||||
| function check_meanstructure_specification(meanstructure, ram_matrices) | ||||||||||||||||||
| if meanstructure & isnothing(ram_matrices.M) | ||||||||||||||||||
| throw(ArgumentError( | ||||||||||||||||||
| "You set `meanstructure = true`, but your model specification contains no mean parameters." | ||||||||||||||||||
| )) | ||||||||||||||||||
| end | ||||||||||||||||||
| if !meanstructure & !isnothing(ram_matrices.M) | ||||||||||||||||||
| throw(ArgumentError( | ||||||||||||||||||
| "If your model specification contains mean parameters, you have to set `Sem(..., meanstructure = true)`." | ||||||||||||||||||
| )) | ||||||||||||||||||
|
Comment on lines
+43
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
| end | ||||||||||||||||||
| end | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -43,7 +43,14 @@ end | |||||||||||||||||||
| ### Constructors | ||||||||||||||||||||
| ############################################################################################ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| function SemFIML(; observed::SemObservedMissing, specification, kwargs...) | ||||||||||||||||||||
| function SemFIML(; observed::SemObservedMissing, implied, specification, kwargs...) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||
| if implied.meanstruct isa NoMeanStruct | ||||||||||||||||||||
| throw(ArgumentError( | ||||||||||||||||||||
| "Full information maximum likelihood (FIML) can only be used with a meanstructure. | ||||||||||||||||||||
| Did you forget to set `Sem(..., meanstructure = true)`?")) | ||||||||||||||||||||
|
Comment on lines
+49
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||
| end | ||||||||||||||||||||
|
|
||||||||||||||||||||
| inverses = | ||||||||||||||||||||
| [zeros(nmeasured_vars(pat), nmeasured_vars(pat)) for pat in observed.patterns] | ||||||||||||||||||||
| choleskys = Array{Cholesky{Float64, Array{Float64, 2}}, 1}(undef, length(inverses)) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -39,6 +39,21 @@ end | |||||||||||||||||||||||||||||||||||||||||||||||||||
| ############################################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| function SemML(; observed::SemObserved, approximate_hessian::Bool = false, kwargs...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if observed isa SemObservedMissing | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw(ArgumentError( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Normal maximum likelihood estimation can't be used with `SemObservedMissing`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Use full information maximum likelihood (FIML) estimation or remove missing | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| values in your data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| A FIML model can be constructed with | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sem( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ..., | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| observed = SemObservedMissing, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| loss = SemFIML, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| meanstructure = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| )")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| obsmean = obs_mean(observed) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| obscov = obs_cov(observed) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| meandiff = isnothing(obsmean) ? nothing : copy(obsmean) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -51,12 +51,33 @@ SemWLS{HE}(args...) where {HE <: HessianEval} = | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| function SemWLS(; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| observed, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| implied, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wls_weight_matrix = nothing, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wls_weight_matrix_mean = nothing, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| approximate_hessian = false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| meanstructure = false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kwargs..., | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if observed isa SemObservedMissing | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw(ArgumentError( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "WLS estimation can't be used with `SemObservedMissing`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Use full information maximum likelihood (FIML) estimation or remove missing | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| values in your data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| A FIML model can be constructed with | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sem( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ..., | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| observed = SemObservedMissing, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| loss = SemFIML, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| meanstructure = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| )")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+63
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !(implied isa RAMSymbolic) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw(ArgumentError( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "WLS estimation is only available with the implied type RAMSymbolic at the moment.")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+76
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| nobs_vars = nobserved_vars(observed) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| tril_ind = filter(x -> (x[1] >= x[2]), CartesianIndices(obs_cov(observed))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| s = obs_cov(observed)[tril_ind] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -38,10 +38,24 @@ function SemObservedData(; | |||||||||||||||||||||||||||||||||||||||||||||||
| observed_var_prefix::Union{Symbol, AbstractString} = :obs, | ||||||||||||||||||||||||||||||||||||||||||||||||
| kwargs..., | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
| data, obs_vars, _ = | ||||||||||||||||||||||||||||||||||||||||||||||||
| prepare_data(data, observed_vars, specification; observed_var_prefix) | ||||||||||||||||||||||||||||||||||||||||||||||||
| obs_mean, obs_cov = mean_and_cov(data, 1) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if any(ismissing.(data)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| throw(ArgumentError( | ||||||||||||||||||||||||||||||||||||||||||||||||
| "Your dataset contains missing values. | ||||||||||||||||||||||||||||||||||||||||||||||||
| Remove missing values or use full information maximum likelihood (FIML) estimation. | ||||||||||||||||||||||||||||||||||||||||||||||||
| A FIML model can be constructed with | ||||||||||||||||||||||||||||||||||||||||||||||||
| Sem( | ||||||||||||||||||||||||||||||||||||||||||||||||
| ..., | ||||||||||||||||||||||||||||||||||||||||||||||||
| observed = SemObservedMissing, | ||||||||||||||||||||||||||||||||||||||||||||||||
| loss = SemFIML, | ||||||||||||||||||||||||||||||||||||||||||||||||
| meanstructure = true | ||||||||||||||||||||||||||||||||||||||||||||||||
| )")) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+47
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return SemObservedData(data, obs_vars, obs_cov, vec(obs_mean), size(data, 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -47,14 +47,13 @@ function test_params_api(semobj, spec::SemSpecification) | |||||||
| @test @inferred(param_labels(semobj)) == param_labels(spec) | ||||||||
| end | ||||||||
|
|
||||||||
| @testset "Sem(implied=$impliedtype, loss=$losstype)" for impliedtype in (RAM, RAMSymbolic), | ||||||||
| losstype in (SemML, SemWLS) | ||||||||
| @testset "Sem(implied=$impliedtype, loss=SemML)" for impliedtype in (RAM, RAMSymbolic) | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||
|
|
||||||||
| model = Sem( | ||||||||
| specification = ram_matrices, | ||||||||
| observed = obs, | ||||||||
| implied = impliedtype, | ||||||||
| loss = losstype, | ||||||||
| loss = SemML, | ||||||||
| ) | ||||||||
|
|
||||||||
| @test model isa Sem | ||||||||
|
|
@@ -69,7 +68,33 @@ end | |||||||
|
|
||||||||
| @test @inferred(loss(model)) isa SemLoss | ||||||||
| semloss = loss(model).functions[1] | ||||||||
| @test semloss isa losstype | ||||||||
| @test semloss isa SemML | ||||||||
|
|
||||||||
| @test @inferred(nsamples(model)) == nsamples(obs) | ||||||||
| end | ||||||||
|
|
||||||||
| @testset "Sem(implied=RAMSymbolic, loss=SemWLS)" begin | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||
|
|
||||||||
| model = Sem( | ||||||||
| specification = ram_matrices, | ||||||||
| observed = obs, | ||||||||
| implied = RAMSymbolic, | ||||||||
| loss = SemWLS, | ||||||||
| ) | ||||||||
|
|
||||||||
| @test model isa Sem | ||||||||
| @test @inferred(implied(model)) isa RAMSymbolic | ||||||||
| @test @inferred(observed(model)) isa SemObserved | ||||||||
|
|
||||||||
| test_vars_api(model, ram_matrices) | ||||||||
| test_params_api(model, ram_matrices) | ||||||||
|
|
||||||||
| test_vars_api(implied(model), ram_matrices) | ||||||||
| test_params_api(implied(model), ram_matrices) | ||||||||
|
|
||||||||
| @test @inferred(loss(model)) isa SemLoss | ||||||||
| semloss = loss(model).functions[1] | ||||||||
| @test semloss isa SemWLS | ||||||||
|
|
||||||||
| @test @inferred(nsamples(model)) == nsamples(obs) | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||
| end | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶