Skip to content

Commit

Permalink
2D density plots (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
palday authored Sep 12, 2023
1 parent c62c630 commit 081472e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
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
40 changes: 40 additions & 0 deletions src/ridge2d.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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))
scatter!(ax, x, y; color=:black, alpha=0.2)
plt = contour!(ax, collect(dens.x), collect(dens.y), dens.density;
color=:green, linewidth=3,
labelsize=30, labels=false) # reference points

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=)
tbl = bs.tbl
cnames = [string(x) for x in propertynames(tbl)[2:end]]
filter!(startswith(string(ptype)), cnames)
isempty(cnames) &&
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
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))
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

2 comments on commit 081472e

@palday
Copy link
Owner Author

@palday palday commented on 081472e Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/91278

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.27 -m "<description of version>" 081472e4a3ca37f0ecd14f000c3b4b9512266ccf
git push origin v0.3.27

Please sign in to comment.