Create a 'LaTeX' table with the summarized results of a Monte Carlo Simulation
Source:R/latex_table.R
tidy_mc_latex.Rd
Create a 'LaTeX' table containing the
summarized results of a Monte Carlo simulation run
by future_mc()
and summarized by summary.mc()
.
Usage
tidy_mc_latex(
x,
repetitions_set = NULL,
which_setup = NULL,
parameter_comb = NULL,
which_out = NULL,
kable_options = NULL
)
Arguments
- x
An object of class
summary.mc
. For restrictions see details.- repetitions_set
A vector of integers specifying at which repetitions the summarized results should be displayed in the table. Default: The argument
repetitions
infuture_mc()
, which means that the summarized results after the last repetition are displayed in the table.- which_setup
A character vector containing the
nice_names
for the different parameter combinations (returned byfuture_mc()
), which should be presented in the table. Default: All parameter combinations are presented.- parameter_comb
Alternative to
which_setup
. A named list whose components are named after (some of) the parameters inparam_list
infuture_mc()
. Each component is a vector containing the values for the parameters to be filtered by. Default: All parameter combinations are presented.- which_out
A character vector containing the names of (some of) the named outputs (the names of the returned list of
fun
infuture_mc()
), which should be displayed in the table. Default: All outputs are displayed.- kable_options
A list whose components are named after possible parameters of
kableExtra::kbl()
customizing the generated table.
Value
An object of class knitr_kable
which can be modified by the functions in the kableExtra package is returned.
Details
Only one of the arguments which_setup
and parameter_comb
can be specified at one time.
Only (output - parameter combination)-pairs
for which the summary function specified
in the sum_funs
argument of summary.mc()
returns a single scalar value appear as
non-NA
values in the 'LaTeX' table.
If a specific output is summarized with functions
that do not return a single numeric value over all parameter combinations,
then this output is discarded from the table.
Similarly, if for a specific parameter combination all fun
outputs are
summarized with functions which do not return a single numeric value,
then this parameter combination is discarded as well.
In summary, all outputs must be summarized with functions
which return just one numeric value.
Examples
test_func <- function(param = 0.1, n = 100, x1 = 1, x2 = 2) {
data <- rnorm(n, mean = param) + x1 + x2
stat <- mean(data)
stat_2 <- var(data)
if (x2 == 5) {
stop("x2 can't be 5!")
}
return(list(mean = stat, var = stat_2))
}
param_list <- list(
param = seq(from = 0, to = 1, by = 0.5),
x1 = 1:2
)
test_mc <- future_mc(
fun = test_func,
repetitions = 1000,
param_list = param_list,
n = 10,
x2 = 2
)
#> Running single test-iteration for each parameter combination...
#>
#> Test-run successfull: No errors occurred!
#> Running whole simulation: Overall 6 parameter combinations are simulated ...
#>
#> Simulation was successfull!
#> Running time: 00:00:00.973095
tidy_mc_latex(summary(test_mc))
#> \begin{table}
#>
#> \caption{Monte Carlo simulations results}
#> \centering
#> \begin{tabular}[t]{cccc}
#> \toprule
#> param & x1 & mean & var\\
#> \midrule
#> 0.0 & 1 & 2.997 & 1.018\\
#> 0.0 & 2 & 3.986 & 1.047\\
#> 0.5 & 1 & 3.499 & 1.010\\
#> 0.5 & 2 & 4.498 & 0.999\\
#> 1.0 & 1 & 3.983 & 1.000\\
#> \addlinespace
#> 1.0 & 2 & 4.998 & 1.001\\
#> \bottomrule
#> \multicolumn{4}{l}{\textsuperscript{} Total repetitions = 1000,}\\
#> \multicolumn{4}{l}{total parameter combinations}\\
#> \multicolumn{4}{l}{= 6}\\
#> \end{tabular}
#> \end{table}
set.seed(101)
tidy_mc_latex(
summary(test_mc),
repetitions_set = c(10, 1000),
which_out = "mean",
kable_options = list(caption = "Mean MCS results")
)
#> \begin{table}
#>
#> \caption{Mean MCS results}
#> \centering
#> \begin{tabular}[t]{ccc}
#> \toprule
#> param & x1 & mean\\
#> \midrule
#> \addlinespace[0.3em]
#> \multicolumn{3}{l}{\textbf{N = 10}}\\
#> \hspace{1em}0.0 & 1 & 2.900\\
#> \hspace{1em}0.0 & 2 & 3.918\\
#> \hspace{1em}0.5 & 1 & 3.446\\
#> \hspace{1em}0.5 & 2 & 4.424\\
#> \hspace{1em}1.0 & 1 & 4.032\\
#> \hspace{1em}1.0 & 2 & 4.956\\
#> \addlinespace[0.3em]
#> \multicolumn{3}{l}{\textbf{N = 1000}}\\
#> \hspace{1em}0.0 & 1 & 2.997\\
#> \hspace{1em}0.0 & 2 & 3.986\\
#> \hspace{1em}0.5 & 1 & 3.499\\
#> \hspace{1em}0.5 & 2 & 4.498\\
#> \hspace{1em}1.0 & 1 & 3.983\\
#> \hspace{1em}1.0 & 2 & 4.998\\
#> \bottomrule
#> \multicolumn{3}{l}{\textsuperscript{} Total repetitions =}\\
#> \multicolumn{3}{l}{1000, total}\\
#> \multicolumn{3}{l}{parameter}\\
#> \multicolumn{3}{l}{combinations = 6}\\
#> \end{tabular}
#> \end{table}