Skip to content

Commit

Permalink
stat_linear implemented, geom_smooth working
Browse files Browse the repository at this point in the history
  • Loading branch information
rdboyes committed Mar 23, 2024
1 parent 26f1a7f commit 9df22de
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/TidierPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ using CategoricalArrays
# Data manipulation, expression parsing
using TidierData

# for ... GLMS
using GLM

include("structs.jl")

include("addplots.jl")
Expand Down Expand Up @@ -45,7 +48,7 @@ include("geoms/geom_errorbar.jl")
include("geoms/geom_hvline.jl")
include("geoms/geom_path.jl")
include("geoms/geom_point.jl")
#include("geoms/geom_smooth.jl")
include("geoms/geom_smooth.jl")
include("geoms/geom_text.jl")
include("geoms/geom_violin.jl")

Expand All @@ -67,7 +70,7 @@ export geom_point
export geom_path
export geom_line
export geom_step
#export geom_smooth
export geom_smooth
export geom_errorbar
export geom_errorbarh
export geom_violin
Expand Down
1 change: 1 addition & 0 deletions src/draw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function Makie.SpecApi.Axis(plot::GGPlot)
visual_args_list = []

for req_aes in required_aes

if eltype(plot_data[!, aes_dict[req_aes]]) <: Union{AbstractString, AbstractChar}
if haskey(plot.axis_options, "cat_inorder")
cat_column = plot_data[!, aes_dict[req_aes]]
Expand Down
56 changes: 51 additions & 5 deletions src/geoms/geom_smooth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,63 @@ function geom_smooth(args...; kwargs...)
aes_dict, args_dict = extract_aes(args, kwargs)
args_dict["geom_name"] = "geom_smooth"

analysis = AlgebraOfGraphics.smooth()
analysis = stat_loess

if haskey(args_dict, "method")
if args_dict["method"] == "lm"
analysis = AlgebraOfGraphics.linear()
analysis = stat_linear
end
end

return build_geom(aes_dict,
# geom_smooth returns TWO makie plots:
# :Lines - the center line
# :Band - the uncertainty interval

return [build_geom(aes_dict,
args_dict,
["x", "y"],
nothing,
analysis)
:Lines,
analysis),
build_geom(aes_dict,
args_dict,
["x", "lower", "upper"],
:Band,
analysis)]
end

function stat_loess(aes_dict::Dict{String, Symbol},
args_dict::Dict{Any, Any}, required_aes::Vector{String}, plot_data::DataFrame)

return (aes_dict, args_dict, required_aes, plot_data)
end

function stat_linear(aes_dict::Dict{String, Symbol},
args_dict::Dict{String, Any}, required_aes::Vector{String}, plot_data::DataFrame)

x = plot_data[!, aes_dict["x"]]
y = plot_data[!, aes_dict["y"]]

# thanks AlgebraOfGraphics
function add_intercept_column(x::AbstractVector{T}) where {T}
mat = similar(x, float(T), (length(x), 2))
fill!(view(mat, :, 1), 1)
copyto!(view(mat, :, 2), x)
return mat
end

lin_model = GLM.lm(add_intercept_column(x), y)
= range(extrema(x)..., length = 100)
pred = DataFrame(GLM.predict(lin_model, add_intercept_column(x̂); interval = :confidence))

aes_dict["upper"] = :upper
aes_dict["lower"] = :lower

return_data = DataFrame(
String(aes_dict["x"]) => x̂,
String(aes_dict["y"]) => pred.prediction,
"lower" => pred.lower,
"upper" => pred.upper
)

return (aes_dict, args_dict, required_aes, return_data)
end

0 comments on commit 9df22de

Please sign in to comment.