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

2D density plots #80

Merged
merged 3 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ ridgeplot
ridgeplot(boot)
```

## Ridge 2D Plots

```@docs
ridge2d
```

```@example Coefplot
ridge2d(boot)
```

## Random effects and group-level predictions

Expand Down
3 changes: 3 additions & 0 deletions src/MixedModelsMakie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export RanefInfo,
qqcaterpillar!,
ranefinfo,
ranefinfotable,
ridge2d,
ridge2d!,
ridgeplot,
ridgeplot!,
shrinkageplot,
Expand All @@ -40,6 +42,7 @@ include("caterpillar.jl")
include("coefplot.jl")
include("profile.jl")
include("ridge.jl")
include("ridge2d.jl")
include("xyplot.jl")
include("recipes.jl")

Expand Down
39 changes: 39 additions & 0 deletions src/ridge2d.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function _ridge2d_panel!(ax::Axis, i::Int, j::Int, cnames::Vector{Symbol}, tbl)
x = Tables.getcolumn(tbl, cnames[j])
y = Tables.getcolumn(tbl, cnames[i])
dens = kde((x,y))
palday marked this conversation as resolved.
Show resolved Hide resolved
scatter!(ax, x, y; color=:black, alpha=0.2)
plt = contour!(ax, collect(dens.x), collect(dens.y), dens.density;
palday marked this conversation as resolved.
Show resolved Hide resolved
color=:green, linewidth=3,
labelsize=30, labels=false) # reference points

palday marked this conversation as resolved.
Show resolved Hide resolved
return plt
end

"""
ridge2d!(f::Union{Makie.FigureLike,Makie.GridLayout}, bs::MixedModelBootstrap;
ptype=:β)

Plot pairwise bivariate scatter plots with overlain densities for a bootstrap sample.


`ptype` specifies the set of parameters to examine, e.g. `:β`, `:σ`, `:ρ`.
"""
function ridge2d!(f::Union{Makie.FigureLike,Makie.GridLayout}, bs::MixedModelBootstrap; ptype=:β)
palday marked this conversation as resolved.
Show resolved Hide resolved
tbl = bs.tbl
cnames = [string(x) for x in propertynames(tbl)[2:end]]
filter!(startswith(string(ptype)), cnames)
isempty(cnames) &&
palday marked this conversation as resolved.
Show resolved Hide resolved
throw(ArgumentError("No parameters $ptype found."))
length(cnames) == 1 &&
throw(ArgumentError("Only 1 $ptype-paramater found: 2D plots require at least 2."))
splomaxes!(f, cnames, _ridge2d_panel!, Symbol.(cnames), tbl)
return f
end

"""$(@doc ridge2d!)"""
function ridge2d(bs::MixedModelBootstrap, args...; kwargs...)
f = Figure(; resolution=(1000, 1000)) # use an aspect ratio of 1 for the whole figure

return ridge2d!(f, bs, args...; kwargs...)
end
17 changes: 17 additions & 0 deletions test/ridge2d.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using CairoMakie, MixedModels, MixedModelsMakie, Random
progress = true

m1 = fit(MixedModel,
@formula(1000 / reaction ~ 1 + days + (1 + days | subj)),
MixedModels.dataset(:sleepstudy); progress)


palday marked this conversation as resolved.
Show resolved Hide resolved
b1 = parametricbootstrap(MersenneTwister(42), 500, m1; progress,
optsum_overrides=(;ftol_rel=1e-6))
palday marked this conversation as resolved.
Show resolved Hide resolved

@test_throws(ArgumentError("No parameters x found."),
ridge2d(b1; ptype=:x))
@test_throws(ArgumentError("Only 1 ρ-paramater found: 2D plots require at least 2."),
ridge2d(b1; ptype=:ρ))
ridge2d(b1)
ridge2d(b1; ptype=:σ)
13 changes: 11 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ m2 = fit(MixedModel,
(1 + spkr | item)),
MixedModels.dataset(:kb07); progress)

b1 = parametricbootstrap(MersenneTwister(42), 100, m1; hide_progress=!progress)

b1 = parametricbootstrap(MersenneTwister(42), 500, m1; progress,
optsum_overrides=(;ftol_rel=1e-6))
palday marked this conversation as resolved.
Show resolved Hide resolved
g1 = fit(MixedModel,
@formula(r2 ~ 1 + anger + gender + btype + situ +
(1 | subj) + (1 + gender | item)),
Expand Down Expand Up @@ -141,6 +141,15 @@ end
save(joinpath(OUTDIR, "ridge_sleepstudy.png"), f)
end

@testset "ridge2d" begin
@test_throws(ArgumentError("No parameters x found."),
ridge2d(b1; ptype=:x))
@test_throws(ArgumentError("Only 1 ρ-paramater found: 2D plots require at least 2."),
ridge2d(b1; ptype=:ρ))
save(joinpath(OUTDIR, "ridge2d_beta.png"), ridge2d(b1))
save(joinpath(OUTDIR, "ridge2d_sigma.png"), ridge2d(b1; ptype=:σ))
end

@testset "shrinkageplot" begin
f = shrinkageplot(m1)
save(joinpath(OUTDIR, "shrinkage_sleepstudy.png"), f)
Expand Down
Loading