Installation
import Pkg; Pkg.add("Metida")
Simple example
Step 1: Load data
Load provided data with CSV and DataFrames:
using Metida, CSV, DataFrames, CategoricalArrays
df = CSV.File(joinpath(dirname(pathof(Metida)), "..", "test", "csv", "df0.csv")) |> DataFrame;
Check that all categorical variables are categorical.
transform!(df, :subject => categorical, renamecols=false)
transform!(df, :period => categorical, renamecols=false)
transform!(df, :sequence => categorical, renamecols=false)
transform!(df, :formulation => categorical, renamecols=false)
Step 2: Make model
Make model with @formula
macro from StatsModels
. Define random
and repreated
effects with Metida.VarEffect
using Metida.@covstr
macros. Left side of @covstr
is model of effect and right side is a effect itself. Metida.HeterogeneousCompoundSymmetry
and Metida.Diag
(Diagonal) in example bellow is a model of variance-covariance structure. See also Metida.@lmmformula
macro.
In some cases levels of repeated effect should not be equal inside each level of subject or model will not have any sense. For example, it is assumed that usually CSH or UN (Unstructured) using with levels of repeated effect is different inside each level of subject. Metida does not check this!
lmm = LMM(@formula(var~sequence+period+formulation), df;
random = VarEffect(@covstr(formulation|subject), CSH),
repeated = VarEffect(@covstr(formulation|subject), DIAG));
Linear Mixed Model: var ~ sequence + period + formulation
Random 1:
Model: formulation|subject
Type: CSH (3), Subjects: 5
Repeated:
Model: formulation|subject
Type: DIAG (2)
Blocks: 5, Maximum block size: 4
Not fitted.
Also Metida.@lmmformula
macro can be used:
lmm = LMM(@lmmformula(var~sequence+period+formulation,
random = formulation|subject:CSH,
repeated = formulation|subject:DIAG),
df)
Step 3: Fit
Just fit the model.
fit!(lmm)
Linear Mixed Model: var ~ sequence + period + formulation
Random 1:
Model: formulation|subject
Type: CSH (3), Subjects: 5
Repeated:
Model: formulation|subject
Type: DIAG (2)
Blocks: 5, Maximum block size: 4
Status: converged (No Errors)
-2 logREML: 10.0652 BIC: 23.9282
Fixed-effects parameters:
───────────────────────────────────────────────────────
Coef. Std. Error z Pr(>|z|)
───────────────────────────────────────────────────────
(Intercept) 1.57749 0.334543 4.72 <1e-05
sequence: 2 -0.170833 0.384381 -0.44 0.6567
period: 2 0.195984 0.117228 1.67 0.0946
period: 3 0.145014 0.109171 1.33 0.1841
period: 4 0.157363 0.117228 1.34 0.1795
formulation: 2 -0.0791667 0.0903709 -0.88 0.3810
───────────────────────────────────────────────────────
Variance components:
θ vector: [0.455584, 0.367656, 1.0, 0.143682, 0.205657]
Random 1 σ² formulation: 1 var 0.207557
Random 1 σ² formulation: 2 var 0.135171
Random 1 ρ rho 1.0
Residual σ² formulation: 1 var 0.0206445
Residual σ² formulation: 2 var 0.0422948
Check warnings and errors in log.
lmm.log
3-element Vector{Metida.LMMLogMsg}:
INFO : Initial θ: [0.21085850593184405, 0.21085850593184405, 0.0001, 0.21085850593184405, 0.21085850593184405]
INFO : Resulting θ: [0.455584290622586, 0.36765619780142395, 0.9999999999967146, 0.1436824286508881, 0.20565739028506558]; 25 iterations.
INFO : Model fitted.
Confidence intervals for coefficients
confint(lmm)
6-element Vector{Tuple{Float64, Float64}}:
(0.543714563732745, 2.611271160890932)
(-1.3939795005801323, 1.0523128339134646)
(-0.07012519946854545, 0.46209405823764527)
(-0.09895082360335769, 0.3889793743560057)
(-0.10874644952798812, 0.4234728081782028)
(-0.3056725658640148, 0.14733923253068223)
Satterthwaite approximation for the denominator degrees of freedom used by default.
StatsBsae API
StatsBsae API implemented: Metida.islinear
, Metida.confint
, Metida.coef
, Metida.coefnames
, Metida.dof_residual
, Metida.dof
, Metida.loglikelihood
, Metida.aic
, Metida.bic
, Metida.aicc
, Metida.isfitted
, Metida.vcov
, Metida.stderror
, Metida.modelmatrix
, Metida.response
, Metida.crossmodelmatrix
, Metida.coeftable
, Metida.responsename
Type III Tests of Fixed Effects
Experimental
typeiii(lmm)
------------- ---------- ----- --------- -----------
Name F ndf ddf pval
------------- ---------- ----- --------- -----------
(Intercept) 22.2346 1.0 3.16597 0.0159885
sequence 0.197525 1.0 3.00055 0.686828
period 1.0789 3.0 8.66853 0.407683
formulation 0.767409 1.0 5.46311 0.417867
------------- ---------- ----- --------- -----------
Model construction
To construct model you can use LMM
constructor.
model
- example:@formula(var ~ sequence + period + formulation)
random
- effects can be specified like this:VarEffect(@covstr(formulation|subject), CSH)
.@covstr
is a effect model:@covstr(formulation|subject)
.CSH
is a CovarianceType structure. Premade constants: SI, DIAG, AR, ARH, CS, CSH, ARMA, TOEP, TOEPH, UN, ets. If not specified only repeated used.repeated
- can be specified like random effect. If not specifiedVarEffect(@covstr(1|1), SI)
used. If no repeated effects specified vector of ones used.