Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better error when formula variables missing from dataset #707

Merged
merged 3 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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