Skip to content

Commit

Permalink
better error when formula variables missing from dataset (#707)
Browse files Browse the repository at this point in the history
* better error when formula variables missing from dataset

* NEWS and version bump

* JuliaFormatter
  • Loading branch information
palday authored Aug 20, 2023
1 parent 563d465 commit c9dbaf6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
MixedModels v4.18.0 Release Notes
==============================
* More user-friendly error messages when a formula contains variables not in the data. [#707]

MixedModels v4.17.0 Release Notes
==============================
* New kwarg `amalgamate` can be used to disable amalgation of random effects terms sharing a single grouping variable. Generally, `amalgamate=false` will result in a slower fit, but may improve convergence in some pathological cases. Note that this feature is experimental and changes to it are **not** considered breakings. [#673]
* **EXPERIMENTAL** New kwarg `amalgamate` can be used to disable amalgation of random effects terms sharing a single grouping variable. Generally, `amalgamate=false` will result in a slower fit, but may improve convergence in some pathological cases. Note that this feature is experimental and changes to it are **not** considered breakings. [#673]
* More informative error messages when passing a `Distribution` or `Link` type instead of the desired instance. [#698]
* More informative error message on the intentional decision not to define methods for the coefficient of determination. [#698]
* **EXPERIMENTAL** Return `finitial` when PIRLS drifts into a portion of the parameter space that yields a (numerically) invalid covariance matrix. This recovery strategy may be removed in a future release. [#616]
Expand Down Expand Up @@ -456,3 +460,4 @@ Package dependencies
[#694]: https://github.com/JuliaStats/MixedModels.jl/issues/694
[#698]: https://github.com/JuliaStats/MixedModels.jl/issues/698
[#703]: https://github.com/JuliaStats/MixedModels.jl/issues/703
[#707]: https://github.com/JuliaStats/MixedModels.jl/issues/707
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MixedModels"
uuid = "ff71e718-51f3-5ec2-a782-8ffcbfa3c316"
author = ["Phillip Alday <[email protected]>", "Douglas Bates <[email protected]>", "Jose Bayoan Santiago Calderon <[email protected]>"]
version = "4.17.0"
version = "4.18.0"

[deps]
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
Expand Down
15 changes: 12 additions & 3 deletions src/linearmixedmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ function LinearMixedModel(
f::FormulaTerm, tbl::Tables.ColumnTable; contrasts=Dict{Symbol,Any}(), wts=[],
σ=nothing, amalgamate=true,
)
fvars = StatsModels.termvars(f)
tvars = Tables.columnnames(tbl)
fvars tvars ||
throw(
ArgumentError(
"The following formula variables are not present in the table: $(setdiff(fvars, tvars))",
),
)

# TODO: perform missing_omit() after apply_schema() when improved
# missing support is in a StatsModels release
tbl, _ = StatsModels.missing_omit(tbl, f)
Expand Down Expand Up @@ -354,14 +363,14 @@ end

"""
confint(pr::MixedModelProfile; level::Real=0.95)
Compute profile confidence intervals for (fixed effects) coefficients, with confidence level `level` (by default 95%).
!!! note
The API guarantee is for a Tables.jl compatible table. The exact return type is an
The API guarantee is for a Tables.jl compatible table. The exact return type is an
implementation detail and may change in a future minor release without being considered
breaking.
"""
function StatsBase.confint(m::MixedModel{T}; level=0.95) where {T}
cutoff = sqrt.(quantile(Chisq(1), level))
Expand Down
7 changes: 7 additions & 0 deletions test/pls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ end
lrt = likelihoodratiotest(models(:pastes)...)
@test length(lrt.deviance) == length(lrt.formulas) == length(lrt.models )== 2
@test first(lrt.tests.pvalues) 0.5233767966395597 atol=0.0001

@testset "missing variables in formula" begin
ae = ArgumentError("The following formula variables are not present in the table: [:reaction, :joy, :subj]")
@test_throws(ae,
fit(MixedModel, @formula(reaction ~ 1 + joy + (1|subj)), dataset(:pastes)))

end
end

@testset "InstEval" begin
Expand Down

0 comments on commit c9dbaf6

Please sign in to comment.