Latex Output from MixedModels.jl

I was wondering if there is a LaTeX table package that supports MixedModels.jl similar to RegressionTables.jl for FixedEffectModels?

I have hacked something together usint PrettyTables.jl in case there is not:

function variance_components_latex(file, model)
    columnss = [reduce(hcat,
        [[y for y in propertynames(x)] for x in model.σs])..., ""]
    lens =  [[length(x) for x in model.σs]... ; 1]
    revars = [string.(propertynames(model.σs))...; "Residual"]
    revars = reduce(vcat, [fill(x, y) for (x, y) in zip(revars, lens)])
   stds = [reduce(hcat, [[x[n] for n in propertynames(x)] for x in cremod.σs])..., model.σ]
   vars = abs2.(stds)
   open(file, "w") do io
        fmt = (v,i,j) -> j == 1 ? replace(v, '_' => "\\_") : v
        pretty_table(io, [revars columnss vars stds],
            ["" "Column" "Variance" "Std.Dev." ],
            backend = :latex,
            tf = tf_latex_simple,
            formatters = (fmt,
            ft_printf("%5.7f", 3:4))
        )                                                                                                                                                    
    end
end

function coeftable_latex(file, model)
    tab_cols = reduce(hcat, coeftable(model).cols)
    tab_rown = coeftable(model).rownms
    tab_coln =  permutedims(hcat(["", coeftable(model).colnms...]))
    dv = replace(model.formula.lhs.exorig |> string, '_' => "\\_")
    open(file, "w") do io
        fmt = (v,i,j) -> j == 1 ? replace(v, '_' => "\\_") : v
        pretty_table(io, [tab_rown tab_cols],
            [dv "" "" "" "" ; tab_coln];
            backend = :latex,
            tf = tf_latex_simple,
           formatters = (fmt,
           ft_latex_sn(3, [5]),
           ft_printf("%5.3f", 2:4))
     )
    end
end

Thanks for the suggestion. Do you think that LaTeX or other tabular types of output should be included in the MixedModels package or in a separate package?

2 Likes

Personally, I don’t have a strong preference either way. RegressionTables.jl works fine for other packages and it feels unexpected that it doesn’t work for MixedModels. But PrettyTables is not a huge dependency so it could be included without much cost.

1 Like

@danielw2904 This is a currently stillborn issue on our tracker (show methods for other MIME types · Issue #273 · JuliaStats/MixedModels.jl · GitHub), but it’s always good get an extra push. I also wrote GPL’d R-Code that I could adapt and relicense for Julia.

I think the easy bit is converting the individual components to tabular format (e.g. VarCorr or CoefTable), but having the entire model summary in a single table is a bit harder. There’s nothing to stop wrapping multiple tables (tabular) in a single LaTeX table floating environment, but that’s not quite a single table. I have a proposal for a single table, but it looks nothing like the default show method for models in the REPL.

I think the solution is to add MIME/Markdown show methods to MixedModels.jl itself and maybe include support for RegressionTables.jl in the MixedModelsExtras.jl package that I’ve marked off (but not yet started filling in) for just such types of functionality. (ICC and potentially an inefficient implementation of the Kenward Roger approximation for the denominator degrees of freedom are other candidates.)

1 Like

It looks like we’ll soon have Markdown support for our show methods (Markdown show methods for our types by palday · Pull Request #474 · JuliaStats/MixedModels.jl · GitHub).

So you’ll be able to do this: show(open("file.md", write=true), MIME"text/markdown", model) or show(MIME"text/markdown", model). With pandoc, you can then convert those quite easily to LaTeX or HTML as needed. And all of this without adding any extra dependencies.

I probably won’t get around to direct support for LaTeX a little bit, because there are some options to think about there (e.g. booktabs or not? what tricks do we do to decimal-point alignment? etc.). In other words: mostly a bit of a tedious time investment i don’t have at the moment, especially when Markdown can be converted to LaTeX via pandoc. :slightly_smiling_face:

1 Like

Does MixedModels.jl support multiple regressions in a single table, like RegressionTables.jl does?

cc @palday @dmbates @jmboehm

I can’t speak for MixedModels.jl, but RegressionTables.jl supports (in the latest version) any <: RegressionModel, and should therefore work with output from MixedModels (I say should because I haven’t tried… if there are problems, please file an issue).

2 Likes

@jzr No, and that’s because different models are different entities and so get different tables. :wink: That said, internally it’s all Markdown tables that are then converted to different MIME types using the stdlib, so for comparable models(*) you should be able to concatenate those tables.

(*) There’s a lot of fine print on comparable. For the fixed effects, yes, because you can extract those with coeftable quite easily, but for the random effects/variance components, … That’s something rather specific which doesn’t correspond directly to anything in the RegressionModel API. But if you’re just comparing fixed-effects estimates across different models, then RegressionTables.jl will probably be fine. I don’t see anything in the estimator field that corresponds to mixed models though – we’re not using OLS (we’re using penalized least squares) but that’s not what’s relevant for interpreting the model anyway. By default, we’re using the maximum-likelihood estimates, though we also support REML. We don’t support any other pseudo-/quasi “likelihoods”.

1 Like