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

allow predicting from a single observation #653

Merged
merged 10 commits into from
Oct 14, 2022
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
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
MixedModels v4.8.0 Release Notes
==============================
* Allow predicting from a single observation, as long as `Grouping()` is used for the grouping variables. The simplified implementation of `Grouping()` also removes several now unnecessary `StatsModels` methods that should not have been called directly by the user. [#653]

MixedModels v4.7.3 Release Notes
==============================
* More informative error message for formulae lacking random effects [#651]
Expand Down Expand Up @@ -375,3 +379,4 @@ Package dependencies
[#637]: https://github.com/JuliaStats/MixedModels.jl/issues/637
[#648]: https://github.com/JuliaStats/MixedModels.jl/issues/648
[#651]: https://github.com/JuliaStats/MixedModels.jl/issues/651
[#653]: https://github.com/JuliaStats/MixedModels.jl/issues/653
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.7.3"
version = "4.8.0"

[deps]
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
Expand Down
10 changes: 6 additions & 4 deletions src/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ julia> schema((; grp = string.(1:100_000)), Dict(:grp => Grouping()))
"""
struct Grouping <: StatsModels.AbstractContrasts end

# return an empty matrix
StatsModels.contrasts_matrix(::Grouping, baseind, n) = zeros(0, 0)
StatsModels.termnames(::Grouping, levels::AbstractVector, baseind::Integer) = levels

# this is needed until StatsModels stops assuming all contrasts have a .levels field
Base.getproperty(g::Grouping, prop::Symbol) = prop == :levels ? nothing : getfield(g, prop)

# special-case categorical terms with Grouping contrasts.
function StatsModels.modelcols(::CategoricalTerm{Grouping}, d::NamedTuple)
return error("can't create model columns directly from a Grouping term")
end

function StatsModels.ContrastsMatrix(
contrasts::Grouping, levels::AbstractVector
)
return StatsModels.ContrastsMatrix(zeros(0, 0), levels, levels, contrasts)
end
7 changes: 7 additions & 0 deletions test/predict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ include("modelcache.jl")
end

@testset "predict" begin

@testset "single obs" begin
kb07 = DataFrame(dataset(:kb07))
m = models(:kb07)[1]
only(predict(m, kb07[1:1, :])) ≈ first(fitted(m))
end

slp = DataFrame(dataset(:sleepstudy))
slp2 = transform(slp, :subj => ByRow(x -> (x == "S308" ? "NEW" : x)) => :subj)
slpm = allowmissing(slp, :reaction)
Expand Down