StatsPlots.jl icon indicating copy to clipboard operation
StatsPlots.jl copied to clipboard

wip: recipes for regression models. should close #290

Open mkborregaard opened this issue 5 years ago • 3 comments

Here's a basic example usage:

using StatsPlots, GLM, RDatasets
iris = dataset("datasets", "iris")
mod = lm(@formula(PetalLength ~ PetalWidth), iris)
plot(mod)
@df iris scatter!(:PetalWidth, :PetalLength, ms = 2, c = :black, legend = :topleft)

model_example

This is really rough and does only address the bivariate case - we're waiting for a general interface to StatsModels (will open an issue there) cc @Tokazama

mkborregaard avatar Jan 30 '20 13:01 mkborregaard

Nice.

daschw avatar Jan 31 '20 08:01 daschw

(X-ref : discourse)

Thanks for this feature!

I have been able to successfully use it on simple models. However, it still fails in more complex examples, such as the following:

using DataFrames
data = DataFrame(x = rand(100));
data.y = 1 .+ 2*data.x .+ rand(100);

using GLM
model = lm(@formula(y ~ x + x^2), data)  # @formula(y ~ x) works well

using Plots; gr()
using StatsPlots
plot(xlabel="x", ylabel="y", legend=:bottomright)
plot!(data.x, data.y, label="data", seriestype=:scatter)
plot!(model)

for which I get the following error / stack trace:

julia> plot!(model)
ERROR: DimensionMismatch("second dimension of A, 2, does not match length of x, 3")
Stacktrace:
 [1] gemv!(::Array{Float64,1}, ::Char, ::Array{Float64,2}, ::Array{Float64,1}, ::Bo
ol, ::Bool) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.
4/LinearAlgebra/src/matmul.jl:456                                                 
 [2] mul! at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/
LinearAlgebra/src/matmul.jl:66 [inlined]                                          
 [3] mul! at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/
LinearAlgebra/src/matmul.jl:208 [inlined]                                         
 [4] * at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/Lin
earAlgebra/src/matmul.jl:47 [inlined]                                             
 [5] predict(::LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,L
inearAlgebra.Cholesky{Float64,Array{Float64,2}}}}, ::Array{Float64,2}; interval::Sy
mbol, level::Float64) at /home/francois/.julia/packages/GLM/6V3fS/src/lm.jl:201   
 [6] #predict#79 at /home/francois/.julia/packages/StatsModels/pMxlJ/src/statsmodel
.jl:125 [inlined]                                                                 
 [7] macro expansion at /home/francois/.julia/packages/StatsPlots/0rYaj/src/statsmo
dels.jl:4 [inlined]                                                               
 [8] apply_recipe(::Dict{Symbol,Any}, ::StatsModels.TableRegressionModel{LinearMode
l{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Flo
at64,Array{Float64,2}}}},Array{Float64,2}}) at /home/francois/.julia/packages/Recip
esBase/G4s6f/src/RecipesBase.jl:279                                               
 [9] _process_userrecipes(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tupl
e{StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.Den
sePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64
,2}}}) at /home/francois/.julia/packages/Plots/cc8wh/src/pipeline.jl:85           
 [10] _plot!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{StatsModels
.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Fl
oat64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}}) at /ho
me/francois/.julia/packages/Plots/cc8wh/src/plot.jl:178                           
 [11] #plot!#138 at /home/francois/.julia/packages/Plots/cc8wh/src/plot.jl:158 [inl
ined]                                                                             
 [12] plot!(::Plots.Plot{Plots.GRBackend}, ::StatsModels.TableRegressionModel{Linea
rModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesk
y{Float64,Array{Float64,2}}}},Array{Float64,2}}) at /home/francois/.julia/packages/
Plots/cc8wh/src/plot.jl:155                                                       
 [13] plot!(::StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64
,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},A
rray{Float64,2}}; kw::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tupl
e{}}}) at /home/francois/.julia/packages/Plots/cc8wh/src/plot.jl:150              
 [14] plot!(::StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64
,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},A
rray{Float64,2}}) at /home/francois/.julia/packages/Plots/cc8wh/src/plot.jl:144   
 [15] top-level scope at REPL[32]:1

Thanks anyway for StatsPlots, and please don't hesitate to tell me if I can help!

ffevotte avatar Apr 17 '20 22:04 ffevotte

I am getting error plot!(model) ERROR: Cannot convert StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}} to series data for plotting Stacktrace: [1] error(::String) at .\error.jl:33 [2] _prepare_series_data(::StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}) at C:\Users\hafez.julia\packages\RecipesPipeline\5RD7m\src\series.jl:8 [3] _series_data_vector(::StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}, ::Dict{Symbol,Any}) at C:\Users\hafez.julia\packages\RecipesPipeline\5RD7m\src\series.jl:27 [4] macro expansion at C:\Users\hafez.julia\packages\RecipesPipeline\5RD7m\src\series.jl:139 [inlined] [5] apply_recipe(::Dict{Symbol,Any}, ::Type{RecipesPipeline.SliceIt}, ::Nothing, ::StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}, ::Nothing) at C:\Users\hafez.julia\packages\RecipesBase\AN696\src\RecipesBase.jl:282 [6] _process_userrecipes!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}}) at C:\Users\hafez.julia\packages\RecipesPipeline\5RD7m\src\user_recipe.jl:35 [7] recipe_pipeline!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}}) at C:\Users\hafez.julia\packages\RecipesPipeline\5RD7m\src\RecipesPipeline.jl:68 [8] _plot!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}}) at C:\Users\hafez.julia\packages\Plots\ViMfq\src\plot.jl:167 [9] #plot!#127 at C:\Users\hafez.julia\packages\Plots\ViMfq\src\plot.jl:158 [inlined] [10] plot!(::Plots.Plot{Plots.GRBackend}, ::StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}) at C:\Users\hafez.julia\packages\Plots\ViMfq\src\plot.jl:155 [11] plot!(::StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}; kw::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}}) at C:\Users\hafez.julia\packages\Plots\ViMfq\src\plot.jl:150 [12] plot!(::StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}) at C:\Users\hafez.julia\packages\Plots\ViMfq\src\plot.jl:144 [13] top-level scope at REPL[12]:1 [14] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1088

hafez-ahmad avatar Aug 15 '20 09:08 hafez-ahmad