brian-j-smith / Mamba.jl

Markov chain Monte Carlo (MCMC) for Bayesian analysis in julia
Other
253 stars 52 forks source link

Drawing :mean fails with Gadfly 1.0.1 #152

Closed th0br0 closed 5 years ago

th0br0 commented 5 years ago

Using Mamba 0.12.0 & Gadfly 1.0.1 drawing of the mean plots fails with below error:

>>> p = Mamba.plot(sim, :mean)
>>> draw(p)
ERROR: The following aesthetics are required by Geom.line to be of equal length: x, y, color

Stacktrace:
 [1] assert_aesthetics_equal_length(::String, ::Gadfly.Aesthetics, ::Symbol, ::Vararg{Symbol,N} where N) at /home/th0br0/.julia/packages/Gadfly/09PWZ/src/aesthetics.jl:176
 [2] render(::Gadfly.Geom.LineGeometry, ::Theme, ::Gadfly.Aesthetics) at /home/th0br0/.julia/packages/Gadfly/09PWZ/src/geom/line.jl:106
 [3] render(::Gadfly.Geom.LineGeometry, ::Theme, ::Gadfly.Aesthetics, ::Array{Gadfly.Aesthetics,1}, ::Array{Gadfly.Data,1}, ::Dict{Symbol,Gadfly.ScaleElement}) at /home/th0br0/.julia/packages/Gadfly/09PWZ/src/geometry.jl:46
 [4] (::getfield(Gadfly, Symbol("##100#101")){Dict{Symbol,Gadfly.ScaleElement}})(::Tuple{Layer,Gadfly.Aesthetics,Array{Gadfly.Aesthetics,1},Array{Gadfly.Data,1},Theme}) at ./none:0
 [5] collect(::Base.Generator{Base.Iterators.Zip{Tuple{Array{Layer,1},Array{Gadfly.Aesthetics,1},Array{Array{Gadfly.Aesthetics,1},1},Array{Array{Gadfly.Data,1},1},Array{Theme,1}}},getfield(Gadfly, Symbol("##100#101")){Dict{Symbol,Gadfly.ScaleElement}}}) at ./generator.jl:47
 [6] #render_prepared#99(::Bool, ::Bool, ::Function, ::Plot, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics, ::Array{Gadfly.Aesthetics,1}, ::Array{Array{Gadfly.StatisticElement,1},1}, ::Array{Array{Gadfly.Aesthetics,1},1}, ::Array{Array{Gadfly.Data,1},1}, ::Dict{Symbol,Gadfly.ScaleElement}, ::Array{Gadfly.GuideElement,1}) at /home/th0br0/.julia/packages/Gadfly/09PWZ/src/Gadfly.jl:814
 [7] render_prepared(::Plot, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics, ::Array{Gadfly.Aesthetics,1}, ::Array{Array{Gadfly.StatisticElement,1},1}, ::Array{Array{Gadfly.Aesthetics,1},1}, ::Array{Array{Gadfly.Data,1},1}, ::Dict{Symbol,Gadfly.ScaleElement}, ::Array{Gadfly.GuideElement,1}) at /home/th0br0/.julia/packages/Gadfly/09PWZ/src/Gadfly.jl:803
 [8] render(::Plot) at /home/th0br0/.julia/packages/Gadfly/09PWZ/src/Gadfly.jl:749
 [9] #draw#140(::Symbol, ::String, ::Measures.Length{:mm,Float64}, ::Measures.Length{:mm,Float64}, ::Int64, ::Int64, ::Bool, ::Bool, ::typeof(draw), ::Array{Plot,1}) at /home/th0br0/.julia/packages/Mamba/qNBKz/src/output/plot.jl:45
 [10] draw(::Array{Plot,1}) at /home/th0br0/.julia/packages/Mamba/qNBKz/src/output/plot.jl:10
 [11] top-level scope at none:0

Reason for this is that the y data vector has length (e.g. 4000) whereas x and colour only contain 12 elements whereas cummean returns a 1000x4 Array.

Using Julia 1.1

bdeonovic commented 5 years ago

For now you can try the package https://github.com/TuringLang/MCMCChains.jl which built plotting functions on top of Mamba chains which utilize the Plots.jl package.

brian-j-smith commented 5 years ago

@th0br0: Plots, including mean plots, seem to be working properly for the mcmc output generated by the tutorial example run in Julia 1.1 and with Gadfly 1.0.1. Could you provide an example that will reproduce the error.

th0br0 commented 5 years ago

@bdeonovic I had some user-error with unqualified usage of e.g. hpd that caused me to pick Mamba over MCMCChains due to inference issues, but in hindsight I might just as well switch back.

@brian-j-smith below snippet repros the error for me consistently:

using CmdStan
using StatsPlots
using Mamba
using StanMamba
using Gadfly

model= "
data {
  int<lower=1> N;
  vector[N] y;
}
parameters {
  real alpha;
  real<lower=0> sigma;
}
model {
  y ~ normal(alpha, sigma);
}";

stanmodel = Stanmodel(name="repro", monitors = ["alpha","sigma"],model=model,
  output_format=:mambachains);
data = Dict("N" => 25, "y" => 2:2:50);
_, sim, _ = stan(stanmodel, data; diagnostics=false);

Gadfly.draw(Mamba.plot(sim, :mean))
goedman commented 5 years ago

This seems to work using MCMCChain.plot:

using CmdStan
using StatsPlots
#using Mamba
#using StanMamba
#using Gadfly
using StanMCMCChain, MCMCChain
gr(size=(400,400))

ProjDir = @__DIR__
cd(ProjDir) do

  model= "
  data {
    int<lower=1> N;
    vector[N] y;
  }
  parameters {
    real alpha;
    real<lower=0> sigma;
  }
  model {
    y ~ normal(alpha, sigma);
  }";

  stanmodel = Stanmodel(name="repro", monitors = ["alpha","sigma"],model=model,
  #  output_format=:mambachains);
    output_format=:mcmcchain);

  data = Dict("N" => 25, "y" => 2:2:50);

  _, sim, _ = stan(stanmodel, data; diagnostics=false);

  #Gadfly.draw(Mamba.plot(sim, :mean))

  describe(sim) |> display

  # Plot the chains

  MCMCChain.plot(sim)

  savefig("test01.pdf")

end

We are doing a lot of work on MCMCChain right now. The new MCMCChains.jl has not yet been registered.

th0br0 commented 5 years ago

Yeah, MCMChain.meanplot works fine as a replacement. If Mamba.jl is being deprecated, I'll just close this issue. Thanks!

goedman commented 5 years ago

I sure hope Mamba.jl will never be deprecated. It is a very solid, full Julia mcmc package with performance that is often comparable to CmdStan. I feel bad I haven't had the time to redo the 7 or 8 original Stan.jl examples in StanMamba.jl yet (and your remark is a wake-up call for me).

Similarly, at some point I will do the StatisticalRethinking models in MambaModels, although for those I will use the new MCMCChains.jl, not Gadfly.jl.

brian-j-smith commented 5 years ago

Thanks for the kind words @goedman. As the maintainer and main developer of Mamba, I can say that there are no plans to deprecate the package.