Format ttest output by r for tex
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.
R: t.test output for LaTex
This does the trick.
library(schoRsch)
library(xtable)
T.Test <- t.test(extra ~ group, data = sleep)
xtable(
t_out(toutput=T.Test, n.equal = TRUE, welch.df.exact = TRUE, welch.n = NA,
d.corr = TRUE, print = TRUE)
)
Test Results
1 Welch Two Sample t-test: t(17.78) = -1.86, p = .079, d = NA
% latex table generated in R 3.1.1 by xtable 1.7-3 package
% Mon Aug 25 21:01:13 2014
\begin{table}[ht]
\centering
\begin{tabular}{rll}
\hline
& Test & Results \\
\hline
1 & Welch Two Sample t-test: & t(17.78) = -1.86, p = .079, d = NA \\
\hline
\end{tabular}
\end{table}
Creating function with output table from t-test in R
the input arguments for your function should a) not have the same name and b) should not contain $
. Other than that your function works fine:
t_table <- function(col1, col2) {
t.test(col1, col2) %>%
broom::tidy() %>%
mutate(Cohens_d = cohensD(col1, col2)) %>% # calc. cohen's d
mutate_at(vars(- c(p.value,method,alternative)), round, 2)
}
set.seed(1)
t_table(rnorm(100), rnorm(100)+1/2)
estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative Cohens_d
1 -0.35 0.11 0.46 -2.69 0.007745151 197.19 -0.61 -0.09 Welch Two Sample t-test two.sided 0.38
Is it possible to extract the full output for the statistical tests performed in gtsummary?
This should be possible, but unfortunately it is not current possible with the aov()
results. I will make it possible in the next release, and you can follow this GitHub Issue to track the progress of the release. https://github.com/ddsjoberg/gtsummary/issues/956
Here is an example using a t-test, where it currently is possible.
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.4.2'
tbl <-
trial %>%
select(age, trt) %>%
tbl_summary(by = trt,
missing = "no") %>%
add_p(all_continuous() ~ "t.test")
# report any statistics in `tbl$table_body` with `inline_text()`
tbl$table_body
#> # A tibble: 1 x 18
#> variable test_name var_type var_label row_type label stat_1 stat_2 test_result
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <list>
#> 1 age t.test continuous Age label Age 46 (3~ 48 (3~ <named lis~
#> # ... with 9 more variables: estimate <dbl>, statistic <dbl>, parameter <dbl>,
#> # conf.low <dbl>, conf.high <dbl>, p.value <dbl>, estimate1 <dbl>,
#> # estimate2 <dbl>, alternative <chr>
# the columns about the t-test come from `t.test(...) %>% broom::tidy()`
t.test(age ~ trt, data = trial) %>% broom::tidy()
#> # A tibble: 1 x 10
#> estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 -0.438 47.0 47.4 -0.209 0.834 184. -4.57 3.69
#> # ... with 2 more variables: method <chr>, alternative <chr>
# t-statistic
inline_text(tbl, variable = "age", column = "statistic") %>% style_sigfig()
#> t
#> "-0.21"
# degrees of freedom
inline_text(tbl, variable = "age", column = "parameter") %>% style_number()
#> df
#> "184"
Created on 2021-08-10 by the reprex package (v2.0.1)
Related Topics
Find Names of Columns Which Contain Missing Values
Rjava Linker Error Licuuc with Anaconda & Fopenmp Error Without Anaconda for MACos Sierra 10.12.4
Substitute Dt1.X with Dt2.Y When Dt1.X and Dt2.X Match in R
Convert a Printed Message into a Character Vector
Adding Percentage Labels on Pie Chart in R
Calculating Sum of Previous 3 Rows in R Data.Table (By Grid-Square)
Problems Formatting Date into Format "%Y-%M"
Is There an Error in Round Function in R
Gsub in R with Unicode Replacement Give Different Results Under Windows Compared with Unix
How to Start a for Loop in R Programming
How to Stack Only Some Columns in a Data Frame
How to Plot a Heat Map on a Spatial Map
Show Content for Menuitem When Menusubitems Exist in Shiny Dashboard