Plotting coefplot

Hello everyone,

I am trying to plot something like this (attached below), but I am not able to find how to plot this particular way. How would you plot something like this? Thanks in advance.

1 Like

I’ve used Coefplot: it’s a Stata program to plot statistics & confidence intervals.

I’m not aware of this kind of package in Julia.
First we need more uniformity in the statistics/econometrics package APIs.
Suppose someone writes Coefplot.jl: it should plot estimates & conf intervals from GLM.jl, FixedEffects.jl, QuantileRegressions.jl etc

Currently, there is not enough coordination/cooperation.
Eg: if I wanna run a quantile regression w/ fixed effects, the two existing packages don’t work together…
Other parts of the Julia ecosystem are very modular & cohesive.

I prefer a horizontal arrangement with more information (a thicker line for the IQR, a dot for the median) — here is some code using PGFPlotsX, with five quantiles. Should be easy to customize.

using PGFPlotsX

function horiz_q5(x5, y; color = "black")
    plots = @pgf [Plot({ thin, color = color }, Table(x5[[1,5]], [y, y])),
                  Plot({ very_thick, color = color }, Table(x5[[2,4]], [y, y])),
                  Plot({ black, only_marks, color = color }, Table(x5[3:3], [y]))]
end

function horiz_q5_plots(name_q5_pairs; color = "blue")
    @pgf axis = Axis({ xmajorgrids, ytick = .-collect(axes(name_q5_pairs, 1)),
                       yticklabels = first.(name_q5_pairs) })
    for (i, (name, q5)) in enumerate(name_q5_pairs)
        append!(axis, horiz_q5(q5, -i; color = color))
    end
    axis
end

horiz_q5_plots(["Fuel consumption (l/100km)" => range(5, 7; length = 5),
                "Length (m)" => range(4.2, 5; length = 5),
                "Turn circle (m)" => range(7, 12; length = 5)])

3 Likes

Here’s a pretty trivial example to get you started. This is definitely feasible.

julia> function coefplot(m)
       n = coefnames(m)[2:end] # no intercept
       vals = coef(m)[2:end]
       errors = stderr(m)[2:end]
       scatter(
           n,
           vals,
           legend = false,
           yerror = 1.96 .* errors,
           title = "Coefficient plot"
       )
       end

julia> df = DataFrame(y = rand(100), x1 = rand(100), x2 = rand(100));

julia> m = lm(@formula(y ~ x1 + x2), df);

julia> coefplot(m)

3 Likes

Wow this is perfect. Thank you so much. And thank you to everyone else for your helpful replies.

Edit: cant believe there was an option under yerror. I guess I didnt search for the right terms.