Skip to content

Commit eafe4ef

Browse files
committed
common.jl: common vars API methods
1 parent 3bcd2f7 commit eafe4ef

3 files changed

Lines changed: 49 additions & 27 deletions

File tree

src/StructuralEquationModels.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ include("additional_functions/commutation_matrix.jl")
3131
include("frontend/fit/SemFit.jl")
3232
# specification of models
3333
include("additional_functions/params_array.jl")
34+
include("frontend/common.jl")
3435
include("frontend/specification/checks.jl")
3536
include("frontend/specification/ParameterTable.jl")
3637
include("frontend/specification/RAMMatrices.jl")

src/frontend/common.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# API methods supported by multiple SEM.jl types
2+
3+
"""
4+
nparams(semobj)
5+
6+
Return the number of parameters in a SEM model associated with `semboj`.
7+
8+
See also [`params`](@ref).
9+
"""
10+
nparams(semobj) = length(params(semobj))
11+
12+
"""
13+
nvars(semobj)
14+
15+
Return the number of variables in a SEM model associated with `semobj`.
16+
17+
See also [`vars`](@ref).
18+
"""
19+
nvars(semobj) = length(vars(semobj))
20+
21+
"""
22+
nobserved_vars(semobj)
23+
24+
Return the number of observed variables in a SEM model associated with `semobj`.
25+
"""
26+
nobserved_vars(semobj) = length(observed_vars(semobj))
27+
28+
"""
29+
nlatent_vars(semobj)
30+
31+
Return the number of latent variables in a SEM model associated with `semobj`.
32+
"""
33+
nlatent_vars(semobj) = length(latent_vars(semobj))
34+
35+
"""
36+
param_indices(semobj)
37+
38+
Returns a dict of parameter names and their indices in `semobj`.
39+
40+
# Examples
41+
```julia
42+
parind = param_indices(my_fitted_sem)
43+
parind[:param_name]
44+
```
45+
46+
See also [`params`](@ref).
47+
"""
48+
param_indices(semobj) = Dict(par => i for (i, par) in enumerate(params(semobj)))

src/types.jl

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,6 @@ Return the vector of SEM model parameters.
4444
"""
4545
params(model::AbstractSem) = model.params
4646

47-
"""
48-
nparams(semobj)
49-
50-
Return the number of SEM model parameters.
51-
"""
52-
nparams(model::AbstractSem) = length(params(model))
53-
54-
params(model::AbstractSemSingle) = params(model.imply)
55-
nparams(model::AbstractSemSingle) = nparams(model.imply)
56-
57-
"""
58-
param_indices(semobj)
59-
60-
Returns a dict of parameter names and their indices in `semobj`.
61-
62-
# Examples
63-
```julia
64-
parind = param_indices(my_fitted_sem)
65-
parind[:param_name]
66-
```
67-
"""
68-
param_indices(semobj) = Dict(par => i for (i, par) in enumerate(params(semobj)))
69-
7047
"""
7148
SemLoss(args...; loss_weights = nothing, ...)
7249
@@ -145,9 +122,6 @@ abstract type SemImply{MS <: MeanStructure, HE <: HessianEvaluation} end
145122
MeanStructure(::Type{<:SemImply{MS}}) where {MS <: MeanStructure} = MS
146123
HessianEvaluation(::Type{<:SemImply{MS, HE}}) where {MS, HE <: MeanStructure} = HE
147124

148-
params(imply::SemImply) = params(imply.ram_matrices)
149-
nparams(imply::SemImply) = nparams(imply.ram_matrices)
150-
151125
"Subtype of SemImply for all objects that can serve as the imply field of a SEM and use some form of symbolic precomputation."
152126
abstract type SemImplySymbolic{MS, HE} <: SemImply{MS, HE} end
153127

@@ -277,7 +251,6 @@ function SemEnsemble(models...; optimizer = SemOptimizerOptim, weights = nothing
277251
end
278252

279253
params(ensemble::SemEnsemble) = ensemble.params
280-
nparams(ensemble::SemEnsemble) = length(ensemble.params)
281254

282255
"""
283256
n_models(ensemble::SemEnsemble) -> Integer

0 commit comments

Comments
 (0)