From 7ea2cf5177431e6824ec0505ec0dc1f700342191 Mon Sep 17 00:00:00 2001 From: PharmCat Date: Tue, 17 Dec 2024 03:52:44 +0300 Subject: [PATCH] fix effect modef with constant , docs --- Project.toml | 2 +- docs/src/examples.md | 42 +++++++++++++++++++++++++++++++++--------- src/varstruct.jl | 14 +++++++++++--- test/test.jl | 12 ++++++++++++ 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/Project.toml b/Project.toml index 462f1ef8..5d03f490 100644 --- a/Project.toml +++ b/Project.toml @@ -3,7 +3,7 @@ uuid = "a1dec852-9fe5-11e9-361f-8d9fde67cfa2" keywords = ["lenearmodel", "mixedmodel"] desc = "Mixed-effects models with flexible covariance structure." authors = ["Vladimir Arnautov "] -version = "0.16.2" +version = "0.16.3" [deps] DiffResults = "163ba53b-c6d8-5494-b064-1a9d43ac40c5" diff --git a/docs/src/examples.md b/docs/src/examples.md index a10e836b..e14b40da 100644 --- a/docs/src/examples.md +++ b/docs/src/examples.md @@ -1,10 +1,12 @@ -### Example 1 - Continuous and categorical predictors +## Example 1 - Continuous and categorical predictors ```@example lmmexample using Metida, CSV, DataFrames, CategoricalArrays, MixedModels; rds = CSV.File(joinpath(dirname(pathof(Metida)), "..", "test", "csv", "1fptime.csv"); types = [String, String, Float64, Float64]) |> DataFrame +rds2 = CSV.File(joinpath(dirname(pathof(Metida)), "..", "test", "csv", "ftdf3.csv"); types = [String, Float64, Float64, String, String, String, String, String, Float64]) |> DataFrame + nothing; # hide ``` @@ -27,7 +29,7 @@ mm = fit(MixedModel, fm, rds, REML=true) println(mm) #hide ``` -### Example 2 - Two random factors (Penicillin data) +## Example 2 - Two random factors (Penicillin data) Metida: @@ -51,7 +53,7 @@ mm = fit(MixedModel, fm2, df, REML=true) println(mm) #hide ``` -### Example 3 - Repeated ARMA/AR/ARH +## Example 3 - Repeated ARMA/AR/ARH ```@example lmmexample rds = CSV.File(joinpath(dirname(pathof(Metida)), "..", "test", "csv", "1freparma.csv"); types = [String, String, Float64, Float64]) |> DataFrame @@ -91,9 +93,9 @@ repeated = VarEffect(@covstr(1|subject&factor), ARH), fit!(lmm) ``` -### Example 4 - SAS relation +## Example 4 - SAS relation -#### Model 1 +### Model 1 ``` df0 = CSV.File(joinpath(dirname(pathof(Metida)), "..", "test", "csv", "df0.csv")) |> DataFrame @@ -116,7 +118,7 @@ REPEATED/GRP=formulation SUB=subject R; RUN; ``` -#### Model 2 +### Model 2 ``` lmm = LMM( @@ -138,7 +140,7 @@ REPEATED/GRP=formulation SUB=subject R; RUN; ``` -#### Model 3 +### Model 3 ``` lmm = LMM(@formula(var ~ sequence + period + formulation), df0; @@ -157,7 +159,7 @@ RANDOM subject/TYPE=VC G V; RUN; ``` -### Example 5 - Working with Effects.jl +## Example 5 - Working with Effects.jl ``` using Effects, StatsModels @@ -172,4 +174,26 @@ table_model = StatsModels.TableRegressionModel(lmm, lmm.mf, lmm.mm) emmeans(tm) effects(Dict(:period => ["1", "2", "3", "4"]), tm) -``` \ No newline at end of file +``` + + +## Unstructured covariance + +Unstructured covariance example. + + +Metida result: + +```@example lmmexample +lmm = Metida.LMM(@formula(response~factor), ftdf3; + random = Metida.VarEffect(Metida.@covstr(r1|subject), UN), + ) +Metida.fit!(lmm) +``` + +MixedModels result: + +```@example lmmexample +mm = fit(MixedModel, @formula(response ~ factor+ (0+r1|subject)), ftdf3, REML = true) +println(mm) #hide +``` diff --git a/src/varstruct.jl b/src/varstruct.jl index e09a20d9..e1d8f17f 100644 --- a/src/varstruct.jl +++ b/src/varstruct.jl @@ -31,7 +31,7 @@ Macros for random/repeated effect model. # Example ```julia -@covstr(factor|subject) +@covstr(model|subject) ``` """ macro covstr(ex) @@ -64,7 +64,7 @@ Random/repeated effect. !!! note - Categorical factors are coded with `FullDummyCoding()` by default, use `coding` for other contrast codeing. + Categorical factors are coded with `FullDummyCoding()` by default, use `coding` for other contrast coding. # Example @@ -435,7 +435,15 @@ end # CONTRAST CODING ################################################################################ -function fill_coding_dict!(t::T, d::Dict, data) where T <: Union{ConstantTerm, InterceptTerm, FunctionTerm} +function fill_coding_dict!(t::T, d::Dict, data) where T <: Union{ConstantTerm, InterceptTerm} + return d +end +function fill_coding_dict!(t::T, d::Dict, data) where T <: FunctionTerm + if t.f === + + for i in t.args + fill_coding_dict!(i, d, data) + end + end return d end function fill_coding_dict!(t::T, d::Dict, data) where T <: Term diff --git a/test/test.jl b/test/test.jl index 3ddab10f..8b8139b8 100644 --- a/test/test.jl +++ b/test/test.jl @@ -289,6 +289,18 @@ include("testdata.jl") fit!(lmm) @test_nowarn show(io, lmm) + # Intercept term in random part + lmm = Metida.LMM(@formula(var~1), df0; + random = Metida.VarEffect(Metida.@covstr(1+formulation|subject))) + @test typeof( lmm.covstr.random[1].coding[:formulation]) <: StatsModels.FullDummyCoding + # Zero term in random part + lmm = Metida.LMM(@formula(var~1), df0; + random = Metida.VarEffect(Metida.@covstr(0+formulation|subject))) + @test typeof( lmm.covstr.random[1].coding[:formulation]) <: StatsModels.FullDummyCoding + # Intercept term in random part and coding + lmm = Metida.LMM(@formula(var~1), df0; + random = Metida.VarEffect(Metida.@covstr(1+formulation|subject), coding = Dict(:formulation => StatsModels.DummyCoding()))) + @test typeof( lmm.covstr.random[1].coding[:formulation]) <: StatsModels.DummyCoding end ################################################################################ # df0