library(tldr)
This package gives a number of functions to aid common data analysis processes and reporting statistical results in an RMarkdown file. Data analysis functions combine multiple base R functions used to describe simple bivariate relationships into a single, easy to use function. Reporting functions will return character strings to report p-values, confidence intervals, and hypothesis test and regression results. Strings will be LaTeX-formatted as necessary and will knit pretty in an RMarkdown document. The package also provides a wrapper for the CreateTableOne function in the tableone package to make the results knitable.
Suppose we have the following data:
pred1 = sample(letters[1:3], size=50, replace=TRUE) out1 = sample(letters[4:6], size=50, replace=TRUE) out2 = rnorm(50)
We can investigate the relationship between pred1
and out1
using cat_compare()
:
cat_compare(x=pred1, y=out1)
We can investigate the distribution of out2
across levels of pred1
using num_compare()
:
num_compare(y=out2, grp=pred1)
inline
and write
functionsinline_test()
inline_reg()
inline_coef()
inline_anova()
write_int()
write_p()
as_perc()
Using the data above, we can obtain some inferential results:
x = rnorm(50) y = rnorm(50) a = sample(letters[1:3], size=50, replace=TRUE) b = sample(letters[1:3], size=50, replace=TRUE) test1 = t.test(x) test2 = chisq.test(table(a,b)) model1 = lm(y ~ x) model2 = lm(y ~ a)
We can then report the results of the hypothesis test inline using inline_test(test1)
and get the following: r inline_test(test1)
. Simiarly, inline_test(test2)
will report the results of the chi-squared test: r inline_test(test2)
. So far inline_test
only works for $t$ and chi-squared tests, but the goal is to add more functionality - requests gladly accepted.
The regression results can be reported with inline_reg(model1)
and inline_coef(model1, 'x')
to get r inline_reg(model1)
and r inline_coef(model1, 'x')
, respectively. In addition, inline_anova(model2)
will report the ANOVA F statistic and relevant results: r inline_anova(model2)
. So far inline_reg
and inline_coef
currently work for lm
and glm
objects; inline_anova
only works for lm
objects.
We can also report the confidence intervals using write_int()
with a length-2 vector of interval endpoints. For example, write_int(c(3.04, 4.7))
and write_int(test1$conf.int)
yield r write_int(c(3.04, 4.7))
and r write_int(test1$conf.int)
, respectively. If a 2-column matrix is provided to write_int()
, the entries in each row will be formatted into an interval and a character vector will be returned.
P-values can be reported using write_p()
. This function will take either a numeric value or a list-like object with an element named p.value
. For example, write_p(0.00002)
gives r write_p(0.00002)
and write_p(test1)
gives r write_p(test1)
.
Many R functions produce proportions, though analysts may want to report the output as a percentage. as_perc()
will do this. For example, as_perc(0.01)
will produce r as_perc(0.01)
.
See the help files of all functions described above for more details and options. For example, all test and regression reporting functions have wrappers ending in _p
which report only the p-value of the input.
KreateTableOne
The package also provides the function KreateTableOne
, a wrapper for CreateTableOne
from the tableone
package which makes the results knitable. First use KreateTableOne
in an R chunk with results='hide'
(or ouside the RMarkdown document), then recall the saved data frame in a new chunk. For example:
table1 = KreateTableOne(x=mtcars, strata='am', factorVars='vs') colnames(table1)[1:2] = c('am = 0', 'am = 1')
Then
knitr::kable(table1[, 1:3], align='r')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.