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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With