From 92d2e1b702777ec14720d21e48dfc0a2cca19f65 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Wed, 9 Dec 2020 18:29:25 +0000 Subject: [PATCH] Modularize constructors (#449) * split constructor into steps * don't forget the weights * news update --- NEWS.md | 5 +++++ Project.toml | 2 +- src/linearmixedmodel.jl | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 51d5ac7bb..c0f1935f9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +MixedModels v3.1.4 Release Notes +======================== +* [experimental] Additional convenience constructors for `LinearMixedModel` [#449] + MixedModels v3.1.3 Release Notes ======================== * Compatibility updates @@ -120,3 +124,4 @@ Package dependencies [#444]: https://github.com/JuliaStats/MixedModels.jl/issues/444 [#446]: https://github.com/JuliaStats/MixedModels.jl/issues/446 [#447]: https://github.com/JuliaStats/MixedModels.jl/issues/447 +[#449]: https://github.com/JuliaStats/MixedModels.jl/issues/449 diff --git a/Project.toml b/Project.toml index a52d7e322..ab673997a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "MixedModels" uuid = "ff71e718-51f3-5ec2-a782-8ffcbfa3c316" author = ["Phillip Alday ", "Douglas Bates ", "Jose Bayoan Santiago Calderon "] -version = "3.1.3" +version = "3.1.4" [deps] Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45" diff --git a/src/linearmixedmodel.jl b/src/linearmixedmodel.jl index 8695b5fa7..dc0839184 100644 --- a/src/linearmixedmodel.jl +++ b/src/linearmixedmodel.jl @@ -69,6 +69,25 @@ function LinearMixedModel( y, Xs = modelcols(form, tbl) + return LinearMixedModel(y, Xs, form, wts) +end + +""" + LinearMixedModel(y, Xs, form) + +Private constructor for a LinearMixedModel. + +To construct a model, you only need the response (`y`), already assembled +model matrices (`Xs`), schematized formula (`form`) and weights (`wts`). +Everything else in the structure can be derived from these quantities. + +!!! note + This method is internal and experimental and so may change or disappear in + a future release without being considered a breaking change. +""" +function LinearMixedModel(y::AbstractArray, + Xs::Tuple, # can't be more specific here without stressing the compiler + form::FormulaTerm, wts = []) y = reshape(float(y), (:, 1)) # y as a floating-point matrix T = promote_type(Float64, eltype(y)) # ensure that eltype of model matrices is at least Float64 y = convert(Matrix{T}, y) @@ -97,6 +116,26 @@ function LinearMixedModel( end push!(feterms, FeMat(y, [""])) + return LinearMixedModel(feterms, reterms, form, wts) +end + +""" + LinearMixedModel(feterms, reterms, form, wts=[]) + +Private constructor for a `LinearMixedModel` given already assembled fixed and random effects. + +To construct a model, you only need a vector of `FeMat`s (the fixed-effects +model matrix and response), a vector of `AbstractReMat` (the random-effects +model matrices), the formula and the weights. Everything else in the structure +can be derived from these quantities. + +!!! note + This method is internal and experimental and so may change or disappear in + a future release without being considered a breaking change. +""" +function LinearMixedModel(feterms::Vector{FeMat{T}}, reterms::Vector{AbstractReMat{T}}, + form::FormulaTerm, wts=[]) where T + # detect and combine RE terms with the same grouping var if length(reterms) > 1 reterms = amalgamate(reterms)