Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

latex - Format ttest output by r for tex

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...