API
Basic
@covstr
Metida.@covstr — Macro@covstr(ex)Macros for random/repeated effect model.
Example
@covstr(model|subject)@lmmformula
Metida.@lmmformula — Macro@lmmformula(formula, args...)Macro for made formula with variance-covariance structure representation. @lmmformula could be used for shorter LMM construction.
Example:
lmm = Metida.LMM(@lmmformula(var~sequence+period+formulation,
random = formulation|subject:CSH,
repeated = formulation|subject:DIAG),
df0)equal to:
lmm = LMM(@formula(var~sequence+period+formulation), df0;
random = VarEffect(@covstr(formulation|subject), CSH),
repeated = VarEffect(@covstr(formulation|subject), DIAG),
)@lmmformula have 3 components - 1'st is a formula for fixed effect, it's defined like in StstsModels (1st argument just provided to @formula macro). Other arguments should be defined like keywords. repeated keyword define repeated effect part, random - define random effect part. You can use several random factors as in example bellow:
lmm = LMM(@lmmformula(var~sequence+period+formulation,
random = formulation|subject:CSH,
random = 1|subject:DIAG,
repeated = formulation|subject:DIAG),
df0)random or repeated structure made by template:
effect formula | blocking factor [/ nested factor] [: covariance structure]
| - devide effect formula form blocking factor definition (necessarily), / and : modificator are optional.
/ work like in MixedModels or in RegressionFormulae - expand factor f|a/b to f|a + f|a&b. It can't be used in repeated effect definition.
: - covariance structure defined right after : (SI, DIAG, CS, CSH, ets...), if : not used then SI used for this effect.
Terms like a+b or a*b shuould not be used as a blocking factors.
Metida.CovarianceType
Metida.CovarianceType — TypeCovarianceType(cm::AbstractCovmatMethod)Make covariance type with AbstractCovmatMethod.
LMM
Metida.LMM — TypeLMM(model, data; contrasts=Dict{Symbol,Any}(), random::Union{Nothing, VarEffect, Vector{VarEffect}} = nothing, repeated::Union{Nothing, VarEffect} = nothing, wts::Union{Nothing, AbstractVector, AbstractMatrix, AbstractString, Symbol} = nothing)Make Linear-Mixed Model object.
model: is a fixed-effect model (@formula)
data: tabular data
contrasts: contrasts for fixed factors
random: vector of random effects or single random effect
repeated: is a repeated effect or vector
wts: regression weights (residuals).
Weigts can be set as Symbol or String, in this case weights taken from tabular data. If weights is vector then this vector applyed to R-side part of covariance matrix (see Weights details). If weights is matrix then R-side part of covariance matrix multiplied by corresponding part of weight-matrix.
See also: @lmmformula
VarEffect
Metida.VarEffect — TypeVarEffect(formula, covtype::T, coding) where T <: AbstractCovarianceType
VarEffect(formula, covtype::T; coding = nothing) where T <: AbstractCovarianceType
VarEffect(formula; coding = nothing)Random/repeated effect.
formulafrom @covstr(ex) macros.covtype- covariance type (SI, DIAG, CS, CSH, AR, ARH, ARMA, TOEP, TOEPH, TOEPP, TOEPHP)
Categorical factors are coded with FullDummyCoding() by default, use coding for other contrast coding.
Example
VarEffect(@covstr(1+factor|subject), CSH)
VarEffect(@covstr(1 + formulation|subject), CSH; coding = Dict(:formulation => StatsModels.DummyCoding()))Covariance structures
Metida.Autoregressive
Metida.Autoregressive — FunctionAutoregressive()Autoregressive covariance type.
AR = Autoregressive()
\[\begin{bmatrix} 1 & \rho & \rho^2 & \rho^3 \\ \rho & 1 & \rho & \rho^2 \\ \rho^2 & \rho & 1 & \rho \\ \rho^3 & \rho^2 & \rho & 1 \end{bmatrix}\sigma^2\]
Metida.AutoregressiveMovingAverage
Metida.AutoregressiveMovingAverage — FunctionAutoregressiveMovingAverage()Autoregressive moving average covariance type.
ARMA = AutoregressiveMovingAverage()
\[\begin{bmatrix} 1 & \gamma & \gamma\rho & \gamma\rho^2 \\ \gamma & 1 & \gamma & \gamma\rho \\ \gamma\rho & \gamma & 1 & \gamma \\ \gamma\rho^2 & \gamma\rho & \gamma & 1 \end{bmatrix}\sigma^2\]
Metida.CompoundSymmetry
Metida.CompoundSymmetry — FunctionCompoundSymmetry()Compound symmetry covariance type.
CS = CompoundSymmetry()
\[\begin{bmatrix} 1 & \rho & \rho & \rho \\ \rho & 1 & \rho & \rho \\ \rho & \rho & 1 & \rho \\ \rho & \rho & \rho & 1 \end{bmatrix}\sigma^2\]
Metida.Diag
Metida.Diag — FunctionDiag()Diagonal covariance type.
DIAG = Diag()
\[\begin{bmatrix} \sigma_a^2 & 0 & 0 \\ 0 & \sigma_b^2 & 0 \\ 0 & 0 & \sigma_c^2 \end{bmatrix}\]
Metida.HeterogeneousAutoregressive
Metida.HeterogeneousAutoregressive — FunctionHeterogeneousAutoregressive()Heterogeneous autoregressive covariance type.
ARH = HeterogeneousAutoregressive()
\[\begin{bmatrix} \sigma_a^2 & \rho\sigma_a\sigma_b & \rho^2\sigma_a\sigma_c & \rho^3\sigma_a\sigma_d \\ \rho\sigma_b\sigma_a & \sigma_b^2 & \rho\sigma_b\sigma_c & \rho^2\sigma_b\sigma_d \\ \rho^2\sigma_c\sigma_a & \rho\sigma_c\sigma_b & \sigma_c^2 & \rho\sigma_c\sigma_d \\ \rho^3\sigma_d\sigma_a & \rho^2\sigma_d\sigma_b & \rho\sigma_d\sigma_c & \sigma_d^2 \end{bmatrix}\]
Metida.HeterogeneousCompoundSymmetry
Metida.HeterogeneousCompoundSymmetry — FunctionHeterogeneousCompoundSymmetry()Heterogeneous compound symmetry covariance type.
CSH = HeterogeneousCompoundSymmetry()
\[\begin{bmatrix} \sigma_a^2 & \rho\sigma_a\sigma_b & \rho\sigma_a\sigma_c & \rho\sigma_a\sigma_d \\ \rho\sigma_b\sigma_a & \sigma_b^2 & \rho\sigma_b\sigma_c & \rho\sigma_b\sigma_d \\ \rho\sigma_c\sigma_a & \rho\sigma_c\sigma_b & \sigma_c^2 & \rho\sigma_c\sigma_d \\ \rho\sigma_d\sigma_a & \rho\sigma_d\sigma_b & \rho\sigma_d\sigma_c & \sigma_d^2 \end{bmatrix}\]
Metida.HeterogeneousToeplitz
Metida.HeterogeneousToeplitz — FunctionHeterogeneousToeplitz()Heterogeneous toeplitz covariance type. Only for G matrix.
TOEPH = HeterogeneousToeplitz()
\[\begin{bmatrix} \sigma_a^2 & \rho_1 \sigma_a \sigma_b & \rho_2 \sigma_a \sigma_c & \rho_3 \sigma_a \sigma_d \\ \rho_1 \sigma_b \sigma_a & \sigma_b^2 & \rho_1 \sigma_b \sigma_c & \rho_2 \sigma_b \sigma_d \\ \rho_2 \sigma_c \sigma_a & \rho_1 \sigma_c \sigma_b & \sigma_c^2 & \rho_1 \sigma_c \sigma_d \\ \rho_3 \sigma_d \sigma_a & \rho_2 \sigma_d \sigma_b & \rho_1 \sigma_d \sigma_c & \sigma_d^2 \end{bmatrix}\]
Metida.HeterogeneousToeplitzParameterized
Metida.HeterogeneousToeplitzParameterized — FunctionHeterogeneousToeplitzParameterized(p::Int)Heterogeneous toeplitz covariance type with parameter p, (number of bands = p - 1, if p = 1 it's equal DIAG structure).
TOEPHP(p) = HeterogeneousToeplitzParameterized(p)
Metida.ScaledIdentity
Metida.ScaledIdentity — FunctionScaledIdentity()Scaled identity covariance type.
SI = ScaledIdentity()
\[\begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}\sigma^{2}\]
Metida.SpatialExponential
Metida.SpatialExponential — FunctionSpatialExponential()Spatian Exponential covariance structure. Used only for repeated effect.
\[R_{i,j} = \sigma^{2} * exp(-dist(i,j)/\theta)\]
where dist - Euclidean distance between row-vectors of repeated effect matrix for subject i and j, θ > 0.
SPEXP = SpatialExponential()
Metida.SpatialGaussian
Metida.SpatialGaussian — FunctionSpatialGaussian()Spatian Gaussian covariance structure. Used only for repeated effect.
\[R_{i,j} = \sigma^{2} * exp(- dist(i,j)^2 / \theta^2)\]
where dist - Euclidean distance between row-vectors of repeated effect matrix for subject i and j, θ ≠ 0.
SPGAU = SpatialGaussian()
Metida.SpatialPower
Metida.SpatialPower — FunctionSpatialPower()Spatian Power covariance structure. Used only for repeated effect.
\[R_{i,j} = \sigma^{2} * \rho^{dist(i,j)}\]
where dist - Euclidean distance between row-vectors of repeated effect matrix for subject i and j, 1 > ρ > -1.
SPPOW = SpatialPower()
Metida.Toeplitz
Metida.Toeplitz — FunctionToeplitz()Toeplitz covariance type. Only for G matrix.
TOEP = Toeplitz()
\[\begin{bmatrix} 1 & \rho_1 & \rho_2 & \rho_3 \\ \rho_1 & 1 & \rho_1 & \rho_2 \\ \rho_2 & \rho_1 & 1 & \rho_1 \\ \rho_3 & \rho_2 & \rho_1 & 1 \end{bmatrix}\sigma^2\]
Metida.ToeplitzParameterized
Metida.ToeplitzParameterized — FunctionToeplitzParameterized(p::Int)Toeplitz covariance type with parameter p, (number of bands = p - 1, if p = 1 it's equal SI structure).
TOEPP(p) = ToeplitzParameterized(p)
Metida.Unstructured
Metida.Unstructured — FunctionUnstructured()Unstructured covariance structure with t*(t+1)/2-t paremeters where t - number of factor levels, t*(t+1)/2-2t of them is covariance (ρ) patemeters. All levels for repeated effect should be unique within each subject.
UN = Unstructured()
Methods
Metida.caic
Metida.caic — Functioncaic(lmm::LMM)Conditional Akaike Information Criterion.
Metida.coefn
Metida.coefn — Functioncoefn(lmm)Coef number.
Metida.dof_satter
Metida.dof_satter — Functiondof_satter(lmm::LMM{T}, l) where TReturn Satterthwaite approximation for the denominator degrees of freedom, where l is a contrast vector (estimable linear combination of fixed effect coefficients vector (β).
\[df = \frac{2(LCL')^{2}}{g'Ag}\]
Where: $A = 2H^{-1}$, $g = \triangledown_{\theta}(LC^{-1}_{\theta}L')$
dof_satter(lmm::LMM{T}, i::Int) where TReturn Satterthwaite approximation for the denominator degrees of freedom, where n - coefficient number.
dof_satter(lmm::LMM{T}) where TReturn Satterthwaite approximation for the denominator degrees of freedom for all coefficients.
dof_satter(lmm::LMM{T}, l::Matrix) where TReturn Satterthwaite approximation for the denominator degrees of freedom for conrast matrix l.
For size(l, 1) > 1:
\[df = \frac{2E}{E - rank(LCL')}\]
where:
- let $LCL' = QΛQ^{-1}$, where $QΛQ^{-1}$ - spectral decomposition of $LCL'$
- $Lq_i$ is the i-th row of $Q^{-1}L$
- $A = 2H^{-1}$, $g = \triangledown_{\theta}(Lq_i C^{-1}_{\theta} Lq_i')$
- $v_i = \frac{2*Λ_{i,i}^2}{g' * A * g}$
- $E = \sum_{i=1}^n {\frac{v_i}(v_i - 2)}$ for $v_i > 2$
Metida.estimate
Metida.estimate — Functionestimate(lmm, l::AbstractVector; level = 0.95, name = "Estimate")Estimate table for l vector. Satter DF used.
estimate(lmm; level = 0.95)Estimates table. Satter DF used.
Metida.getlog
Metida.getlog — Functiongetlog(lmm::LMM)Return fitting log.
Metida.gmatrix
Metida.gmatrix — Functiongmatrix(lmm::LMM{T}, r::Int) where TMetida.hessian
Metida.hessian — Functionhessian(lmm, theta)Calculate Hessian matrix of REML for theta.
Metida.lcontrast
Metida.lcontrast — Functionlcontrast(lmm::LMM, i::Int)L-contrast matrix for i fixed effect.
Metida.nblocks
Metida.nblocks — FunctionNumber of blocksMetida.rand
Base.rand — Functionrand(rng::AbstractRNG, lmm::LMM{T}) where TGenerate random responce vector for fitted 'lmm' model.
rand(rng::AbstractRNG, lmm::LMM{T}; theta) where TGenerate random responce vector 'lmm' model, theta covariance vector, and zero means.
rand(rng::AbstractRNG, lmm::LMM{T}; theta, beta) where TGenerate random responce vector 'lmm' model, theta covariance vector and mean's vector.
Metida.rand
Random.rand! — Functionrand!(v::AbstractVector, lmm::LMM) = rand!(default_rng(), v, lmm, lmm.result.theta, lmm.result.beta)Generate random responce vector for fitted 'lmm' model, store results in v.
rand!(rng::AbstractRNG, lmm::LMM{T}; theta) where TGenerate random responce vector 'lmm' model, theta covariance vector, and zero means, store results in v.
Metida.raneff
Metida.raneff — Functionraneff(lmm::LMM{T}, i)Vector of random effect coefficients for block i.
raneff(lmm::LMM{T})Vector of random effect coefficients for all subjects by each random effect.
Metida.raneffn
Metida.raneffn — Functionraneffn(lmm)Retuen number of random effects.
Metida.rankx
Metida.rankx — Functionrankx(lmm::LMM)Return rank of X matrix.
Metida.rmatrix
Metida.rmatrix — Functionrmatrix(lmm::LMM{T}, i::Int) where TMetida.theta
Metida.theta — Functiontheta(lmm::LMM)Return theta vector.
Metida.thetalength
Metida.thetalength — Functionthetalength(lmm::LMM)Length of theta vector.
Metida.vmatrix
Metida.vmatrix — Functionvmatrix(lmm::LMM, i::Int)Return variance-covariance matrix V for i bolock.
Metida.vmatrix!
Metida.vmatrix! — Functionvmatrix!(V, θ, lmm, i)Update variance-covariance matrix V for i bolock. Upper triangular updated.
Metida.m2logreml
Metida.m2logreml — Functionm2logreml(lmm::LMM, θ = theta(lmm); maxthreads::Int = num_cores())-2 logREML
Metida.logreml
Metida.logreml — Functionlogreml(lmm::LMM, θ = theta(lmm); maxthreads::Int = num_cores())logREML
StatsAPI
Metida.aic
StatsAPI.aic — FunctionStatsBase.aic(lmm::LMM)Akaike Information Criterion.
Metida.aicc
StatsAPI.aicc — FunctionStatsBase.aicc(lmm::LMM)Corrected Akaike Information Criterion.
Metida.bic
StatsAPI.bic — FunctionStatsBase.bic(lmm::LMM)Bayesian information criterion.
Metida.coef
StatsAPI.coef — FunctionStatsBase.coef(lmm::LMM) = copy(lmm.result.beta)Model coefficients (β).
Metida.coefnames
StatsAPI.coefnames — FunctionStatsBase.coefnames(lmm::LMM) = StatsBase.coefnames(lmm.mf)Coefficients names.
Metida.coeftable
StatsAPI.coeftable — Functioncoeftable(lmm::LMM)Return coefficients table.
Metida.confint
StatsAPI.confint — FunctionStatsBase.confint(lmm::LMM{T}; level::Real=0.95, ddf::Symbol = :satter) where TConfidece interval for coefficients.
ddf = :satter/:residual
\[CI_{U/L} = β ± SE * t_{ddf, 1-α/2}\]
See also: dof_satter, dof_residual
StatsBase.confint(lmm::LMM{T}, i::Int; level::Real=0.95, ddf::Symbol = :satter) where TConfidece interval for coefficient i.
StatsBase.confint(br::BootstrapResult, n::Int; level::Float64=0.95, method=:bp, metric = :coef, delrml = false)
Confidence interval for bootstrap result.
*method:
- :bp - bootstrap percentile;
- :rbp - reverse bootstrap percentile;
- :norm - Normal distribution;
- :bcnorm - Bias corrected Normal distribution;
- :jn - bias corrected (jackknife resampling).
Metida.crossmodelmatrix
StatsAPI.crossmodelmatrix — Functioncrossmodelmatrix(lmm::LMM)Return X'X.
Metida.dof
StatsAPI.dof — FunctionStatsBase.dof(lmm::LMM)DOF.
Metida.dof_residual
StatsAPI.dof_residual — FunctionStatsBase.dof_residual(lmm::LMM)DOF residuals: N - rank(X), where N - total number of observations.
Metida.fit
StatsAPI.fit — Functionfit(::Type{T}, f::FormulaTerm, data;
contrasts=Dict{Symbol,Any}(),
random::Union{Nothing, VarEffect, Vector{VarEffect}} = nothing,
repeated::Union{Nothing, VarEffect} = nothing,
kwargs...)Fit LMM model with @formula.
Keywords see fit!
fit(::Type{T}, f::LMMformula, data;
contrasts=Dict{Symbol,Any}(),
kwargs...) where T <: LMMFit LMM model with @lmmformula.
Keywords see fit!
Metida.fit
StatsAPI.fit! — Functionfit!(lmm::LMM{T}; kwargs...
) where TFit LMM model.
Keywords:
solver- :default / :nlopt for using with MetidaNLopt.jl/ :cuda for using with MetidaCu.jlverbose- :auto / 1 / 2 / 3 - - 1 - only log, 2 - log and print, 3 - print only errors, other log, 0 (or any other value) - no loggingvarlinkf- :exp / :sq / :identity refrholinkf- :sigm / :atan / :sqsigm / :psigmaifirst- first iteration with AI-like method - :default / :ai / :scoreaifmax- maximum pre-optimization stepsg_tol- absolute tolerance in the gradientx_tol- absolute tolerance of theta vectorf_tol- absolute tolerance in changes of the REMLhes- calculate REML Hessianinit- initial theta valuesio- output IOtime_limit- time limit = 120 seciterations- maximum iterations = 300refitinit- true/false - iftrue- use last values for initial condition (falseby default)optmethod- Optimization method. Look at Optim.jl documentation. (Newton by default)singtol- singular tolerance = 1e-8maxthreads- maximum threads = min(num_cores(), Threads.nthreads())
islinear
StatsAPI.islinear — FunctionStatsBase.islinear(model::LMM)isfitted
StatsAPI.isfitted — FunctionStatsBase.isfitted(lmm::LMM)Metida.loglikelihood
StatsAPI.loglikelihood — FunctionStatsBase.loglikelihood(lmm::LMM)Return loglikelihood value.
Metida.modelmatrix
StatsAPI.modelmatrix — FunctionStatsBase.modelmatrix(lmm::LMM)Fixed effects matrix.
Metida.nobs
StatsAPI.nobs — FunctionStatsBase.nobs(lmm::MetiaModel)Number of observations.
Metida.response
StatsAPI.response — FunctionStatsBase.response(lmm::LMM)Response vector.
Metida.responsename
StatsAPI.responsename — Functionresponsename(lmm::LMM)Responce varible name.
Metida.stderror
StatsAPI.stderror — FunctionStatsBase.stderror(lmm::LMM)Standard error
Metida.vcov
StatsAPI.vcov — FunctionStatsBase.vcov(lmm::LMM)Variance-covariance matrix of β.
Experimental
Metida.SpatialExponentialD
Metida.SpatialExponentialD — FunctionSpatialExponentialD()Same as SpatialExponential, but add D to all diagonal elements.
SPEXPD = SpatialExponentialD()
Metida.SpatialGaussianD
Metida.SpatialGaussianD — FunctionSpatialGaussianD()Same as SpatialGaussianD, but add D to all diagonal elements.
SPGAUD = SpatialGaussianD()
Metida.SpatialPowerD
Metida.SpatialPowerD — FunctionSpatialPowerD()Same as SpatialPower, but add D to all diagonal elements.
SPPOWD = SpatialPowerD()
Metida.ScaledWeightedCov
Metida.ScaledWeightedCov — FunctionScaledWeightedCov(wtsm::AbstractMatrix{T})Scaled weighted covariance matrix, where wtsm - NxN within block correlation matrix (N - total number of observations). Used only for repeated effect.
SWC = ScaledWeightedCov
\[R = Corr(W) * \sigma_c^2\]
where $Corr(W)$ - diagonal correlation matrix.
example:
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)
There is no wtsm checks for symmetricity or values.
Metida.ACOV
Metida.ACOV — FunctionACOV(c, action = 0)Augmented (adjusted) covariance. Add additional correlations to existed R-part of variance covariance matrix. Can be used with AR or CS types.
action if existed covariance not equal sero:
- 0 - add
- 1 - replace
- 2 - do nothing (use existed value)
- 3 - warning and add
- 4 - warning and replace
- 5 - warning and use existed value
- other - error
Metida.dof_contain
Metida.dof_contain — Functiondof_contain(lmm, i)Metida.typeiii
Metida.typeiii — FunctionMetida.MILMM
Metida.MILMM — TypeMILMM(lmm::LMM, data)Multiple imputation model.
Metida.RawCoding
Metida.RawCoding — Typemutable struct RawCoding <: AbstractContrastsContrast for CategoricalTerm to get column "as it is" for model matrix.
Not API functions
Metida.contrast
Metida.contrast — Functioncontrast(lmm, l::AbstractMatrix; name::String = "Contrast", ddf = :satter)User contrast table. ddf = :satter or :residual or any number for direct ddf setting.
Metida.fvalue
Metida.fvalue — Functionfvalue(lmm::LMM, l::AbstractMatrix)F value for contrast matrix l.
\[F = \frac{\beta'L'(LCL')^{-1}L\beta}{rank(LCL')}\]
Metida.mulαβαtinc
Metida.mulαβαtinc! — Functionmulαβαtinc!(θ::AbstractMatrix, A::AbstractMatrix, B::AbstractMatrix)θ + A * B * A'
Change θ (only upper triangle). B is symmetric.
mulαβαtinc!(θ::AbstractMatrix, A::AbstractMatrix, B::AbstractMatrix, alpha)θ + A * B * A' * alpha
Change θ (only upper triangle). B is symmetric.
mulαβαtinc!(θ::AbstractVector{T}, A::AbstractMatrix, B::AbstractMatrix, a::AbstractVector, b::AbstractVector, alpha) where Tθ + A * B * (a - b) * alpha
Change θ (only upper triangle). B is symmetric.
Metida.mulθ₃
Metida.mulθ₃ — Functionmulθ₃(y, X, β, V::AbstractMatrix{T})::T where T(y - X * β)' * (-V) * (y - X * β)
use only upper triangle of V
Metida.mulαtβinc
Metida.mulαtβinc! — Functionmulαtβinc!(θ::AbstractVector{T}, A::AbstractMatrix, b::AbstractVector) where Tθ + A' * b
Change θ.
Metida.tname
Metida.tname — FunctionTerm name.Metida.raneflenv
Metida.raneflenv — FunctionReturn number of subject foe each random effet in current block.Metida.edistance
Metida.edistance — Functionedistance(mx::AbstractMatrix{T}, i::Int, j::Int) where TDistance between vector mx[i, :] and mx[j, :].
Metida.m2logml
Metida.m2logml — Functionm2logml(lmm::LMM, β = coef(lmm), θ = theta(lmm); maxthreads::Int = num_cores())-2 logML
Metida.logml
Metida.logml — Functionlogml(lmm::LMM, beta = coef(lmm), θ = theta(lmm); maxthreads::Int = num_cores())logML