Skip to content

Commit

Permalink
docs, clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
PharmCat committed Aug 21, 2024
1 parent 9660960 commit a303064
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
6 changes: 6 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ Metida.ToeplitzParameterized
Metida.Unstructured
```

### Metida.ScaledWeightedCov

```@docs
Metida.ScaledWeightedCov
```

### Methods

### Metida.caic
Expand Down
17 changes: 5 additions & 12 deletions src/varstruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ function StatsModels.ContrastsMatrix(contrasts::RawCoding, levels::AbstractVecto
contrasts)
end
function StatsModels.modelcols(t::CategoricalTerm{RawCoding, T, N}, d::NamedTuple) where T where N
#v = d[t.sym]
#reshape(v, length(v), 1)
d[t.sym]
end

Expand Down Expand Up @@ -255,7 +253,6 @@ struct CovStructure{T, T2} <: AbstractCovarianceStructure
end
end
# RANDOM EFFECTS
#if random[1].covtype.z #IF NOT ZERO
@inbounds for i = 1:rn
if length(random[i].coding) == 0
fill_coding_dict!(random[i].model, random[i].coding, data)
Expand Down Expand Up @@ -283,7 +280,7 @@ struct CovStructure{T, T2} <: AbstractCovarianceStructure
fillur!(tr, i, t)
symbs = StatsModels.termvars(random[i].subj)
if length(symbs) > 0
cdata = tabcols(data, symbs) # Tuple(Tables.getcolumn(Tables.columns(data_), x) for x in symbs)
cdata = tabcols(data, symbs)
dicts[i] = Dict{Tuple{eltype.(cdata)...}, Vector{Int}}()
indsdict!(dicts[i], cdata)
else
Expand All @@ -310,11 +307,10 @@ struct CovStructure{T, T2} <: AbstractCovarianceStructure
end

schema[rn + i] = apply_schema(repeated[i].model, StatsModels.schema(data_, repeated[i].coding))
#rz_[i] = reduce(hcat, modelcols(schema[rn+i], data))
rz_[i] = modelcols(MatrixTerm(schema[rn+i]), data_)
symbs = StatsModels.termvars(repeated[i].subj)
if length(symbs) > 0
cdata = tabcols(data, symbs) # Tuple(Tables.getcolumn(Tables.columns(data), x) for x in symbs)
cdata = tabcols(data, symbs)
dicts[rn + i] = Dict{Tuple{eltype.(cdata)...}, Vector{Int}}()
indsdict!(dicts[rn + i], cdata)
else
Expand All @@ -336,9 +332,6 @@ struct CovStructure{T, T2} <: AbstractCovarianceStructure
# Theta length
tl = sum(t)
########################################################################
#if any(x-> 1 in keys(x), dicts[1:end-1])
# blocks = [first(dicts)[1]]
#else
if random[1].covtype.z # if first random effect not null
subjblockdict = dicts[1]
if length(dicts) > 2 # if more than 2 random effects
Expand All @@ -363,9 +356,7 @@ struct CovStructure{T, T2} <: AbstractCovarianceStructure
dicts[rn+i] = subjblockdict
end


blocks = collect(values(subjblockdict))
#end

sblock = Matrix{Vector{Tuple{Vector{Int}, Int}}}(undef, length(blocks), alleffl)
nblock = []
Expand Down Expand Up @@ -394,7 +385,9 @@ struct CovStructure{T, T2} <: AbstractCovarianceStructure
end
esb = EffectSubjectBlock(sblock, nblock)
#######################################################################
# Modify repeated effect covariance type for some types
# Postprocessing
# Modify repeated effect covariance type for some types
# Maybe it will be removed
for r in repeated
applycovschema!(r.covtype.s, blocks)
end
Expand Down
32 changes: 31 additions & 1 deletion src/vartypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,38 @@ function ScaledIdentity()
end
const SI = ScaledIdentity()

# docs need
# Experimental
"""
ScaledWeightedCov(wtsm::AbstractMatrix{T})
!!! warning
Experimental
Scaled weighted covariance matrix, where `wtsm` - `NxN` within block correlation matrix (N - total number of observations).
Used only for repeated effect.
SWC = ScaledWeightedCov
```math
R = Corr(W) * \\sigma_c^2
```
where ``Corr(W)`` - diagonal correlation matrix.
example:
```julia
matwts = Symmetric(UnitUpperTriangular(rand(size(df0,1), size(df0,1))))
lmm = LMM(@formula(var~sequence+period+formulation), df0;
repeated = VarEffect(@covstr(1|subject), SWC(matwts)))
fit!(lmm)
```
!!! note
There is no `wtsm` checks for symmetricity or values.
"""
function ScaledWeightedCov(wtsm::AbstractMatrix{T}) where T
wtsb = Matrix{T}[]
CovarianceType(SWC_(wtsm, wtsb))
Expand Down

0 comments on commit a303064

Please sign in to comment.