Skip to content

Commit

Permalink
fix effect modef with constant , docs
Browse files Browse the repository at this point in the history
  • Loading branch information
PharmCat committed Dec 17, 2024
1 parent fc34d09 commit 7ea2cf5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "a1dec852-9fe5-11e9-361f-8d9fde67cfa2"
keywords = ["lenearmodel", "mixedmodel"]
desc = "Mixed-effects models with flexible covariance structure."
authors = ["Vladimir Arnautov <[email protected]>"]
version = "0.16.2"
version = "0.16.3"

[deps]
DiffResults = "163ba53b-c6d8-5494-b064-1a9d43ac40c5"
Expand Down
42 changes: 33 additions & 9 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
@@ -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
```

Expand All @@ -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:

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -116,7 +118,7 @@ REPEATED/GRP=formulation SUB=subject R;
RUN;
```

#### Model 2
### Model 2

```
lmm = LMM(
Expand All @@ -138,7 +140,7 @@ REPEATED/GRP=formulation SUB=subject R;
RUN;
```

#### Model 3
### Model 3

```
lmm = LMM(@formula(var ~ sequence + period + formulation), df0;
Expand All @@ -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
Expand All @@ -172,4 +174,26 @@ table_model = StatsModels.TableRegressionModel(lmm, lmm.mf, lmm.mm)
emmeans(tm)
effects(Dict(:period => ["1", "2", "3", "4"]), tm)
```
```


## 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
```
14 changes: 11 additions & 3 deletions src/varstruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Macros for random/repeated effect model.
# Example
```julia
@covstr(factor|subject)
@covstr(model|subject)
```
"""
macro covstr(ex)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7ea2cf5

Please sign in to comment.