Format ttest output by r for tex

后端 未结 1 1818
有刺的猬
有刺的猬 2020-12-03 15:56

For formatting my regression outputs generated by R for Tex, I use stargazer. However this command doesn\'t work for a simple t.test ouput (% Error: Unrecognized object type

相关标签:
1条回答
  • 2020-12-03 16:23

    Give Pander a try, it’s an all round good table formatting package for R, and supports the t.test result type. I’m not sure whether it leaves out too much information for your taste, though.

    result = t.test(…)
    pander(result)
    

    Pander produces Markdown rather than LaTeX tables, so the result needs to be transformed into LaTeX using pandoc.

    Alternatively, you can use broom to generate a regular table form your t.test result, and stargaze that:

    stargazer(tidy(result))
    

    Broom also knows the glance function for a reduced output, however, for t.test the result is the same.


    Extending stargazer for other types is effectively not possible, since everything is hard-coded in the function. The only thing you can do is put the data of interest into a data.frame and pass that to stargazer. You may want to play with this approach a bit. Here’s a basic example of what you could do:

    stargazer_htest = function (data, ...) {
        summary = data.frame(`Test statistic` = data$statistic,
                             DF = data$parameter,
                             `p value` = data$p.value,
                             `Alternative hypothesis` = data$alternative,
                             check.names = FALSE)
        stargazer(summary, flip = TRUE, summary = FALSE,
                  notes = paste(data$method, data$data.name, sep = ': '), ...)
    }
    

    And then use it like this:

    stargazer_htest(t.test(extra ~ group, data = sleep))
    

    To produce the following output:

    … Note the completely wonky alignment and the wrong formatting of negative numbers. I gave up trying to get this to work: I would suggest dropping stargazer, it doesn’t like customisation.

    In summary, the stargazer output isn’t nearly as “beautiful” or “easy to use” as they claim: their table formatting is cluttered and runs afoul of best practices for table formatting (which are summarised in the booktabs package documentation). The function is impossible to customise meaningfully for own types and instead offers a jungle of parameters. Oh, and despite their claim of supporting “a large number of models”, they don’t even support the base R hypothesis tests.

    At the risk of sounding divisive, stargazer is a pretty terrible package.

    0 讨论(0)
提交回复
热议问题