Format ttest output by r for tex
Asked Answered
S

1

8

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!

Semite answered 16/8, 2015 at 11:58 Comment(0)
S
15

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.

Structuralism answered 16/8, 2015 at 13:23 Comment(4)
Thank you very much Konrad for your answer! stargazer(tidy(result)) doesn't work at all. pander(result) produces an acceptable table, however it drops the mean and confidence intervals, which I want to keep.Semite
What does “doesn’t work at all” mean? It does produce a table for me. If the format isn’t right for you then you can also try stargazer(coefficients(summary(result))). In fact, that’s probably the most helpful output of them all …Structuralism
Thank you again for your answer Konrad. You are right, stargazer(tidy(result)) works, but the table was not suitable for my purpose. Thank you for the update. I think I'm missing sth, because this time, the code really doesn't work. If I use the example from r t.test(extra ~ group, data = sleep) and put it in stargazer(coefficients(summary(t.test(extra ~ group, data = sleep)))) I don't get an output. What am I missing here? I am looking forward to your answer.Semite
@HausladenCarina Apologies, you’re right. I accidentally ran lm in my toy example instead of t.test. I’ve updated my answer with some more attempts. But my take-away is this: stargazer is terrible, don’t use it.Structuralism

© 2022 - 2024 — McMap. All rights reserved.