Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format ttest output by r for tex

Tags:

r

latex

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). I know the "xtable" and "schoRsch" packages, however there is some loss of information, when applying these two. Does anyone know another command? Thank you very much!

like image 591
Hausladen Carina Avatar asked Aug 16 '15 11:08

Hausladen Carina


1 Answers

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:

screenshot

… 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.

like image 132
Konrad Rudolph Avatar answered Nov 15 '22 15:11

Konrad Rudolph