sim {POSSA} | R Documentation |
Simulation procedure
Description
This function performs the simulation procedure in order to get
the p values that will eventually serve for power calculations (via
pow
). The observation values ("sample") to be tested are
simulated via the given fun_obs
function, and the significance
testing is performed via the given fun_test
function. The numbers of
observations per look (for a sequential design) are specified in
n_obs
.
Usage
sim(
fun_obs,
n_obs,
fun_test,
n_iter = 45000,
adjust_n = 1,
seed = 8,
pair = NULL,
ignore_suffix = FALSE,
prog_bar = FALSE,
hush = FALSE
)
Arguments
fun_obs |
A |
n_obs |
A numeric vector or a named list of numeric vectors. Specifies
the numbers of observations (i.e., samples sizes) that are to be generated
by |
fun_test |
The function for significance testing. The list of samples
returned by |
n_iter |
Number of iterations (default: 45000). |
adjust_n |
Adjust total number of observations via simple multiplication.
Might be useful in some specific cases, e.g. if for some reason multiple p
values are derived from the same sample without specifying grouping
( |
seed |
Number for |
pair |
Logical or |
ignore_suffix |
Set to |
prog_bar |
Logical, |
hush |
Logical, |
Details
To specify a variable that differs depending on whether the null hypothesis
("H0") or the alternative hypothesis ("H1") is true, a pair of samples are
needed for fun_test
, for which the argument names should have an
identical root and "_h0
" and "_h1
" endings, such as
"var_x_h0
" (for sample in case of H0) and "var_x_h1
" (for sample
in case of H1). Then, since the observation number for this pair will always
be the same, as a convenience, parameters with "_h0
" and "_h1
"
endings specifically can be specified together in n_obs
with the last
"0"/"1" character dropped, hence ending with "_h
". So, for example,
"var_x_h = c(30, 60, 90)
" will be automatically adjusted to specify the
observation numbers for both "var_x_h0
" and "var_x_h1
". In that
case, fun_obs
must have a single argument "var_x_h
", while
fun_test
must have both full names as arguments ("var_x_h0
" and
"var_x_h1
").
Optionally, fun_obs
can be provided in list
format for
the convenience of exploring varying factors (e.g., different effect sizes,
correlations) at once, without writing a dedicated fun_obs
function for
each combination, and each time separately running the simulation and the
power calculation. In this case, the first element of the list must be the
actual function
, which contains certain parameters for
specifying varying factors, while the rest of the elements should contain the
various argument values for these parameters of the function as named elements
of the list (e.g., list(my_function, factor1=c(1, 2, 3), factor2=c(0,
5))
), with the name corresponding to the parameter name in the function, and
the varying values (numbers or strings). When so specified, a separate
simulation procedure will be run for each combination of the given factors
(or, if only one factor is given, for each element of that factor). The
POSSA::pow
function will be able to automatically
detect (by default) the factors generated this way in the present
POSSA::sim
function, in order to calculate power
separately for each factor combination.
Value
Returns a data.frame
(with class "possa_sim_df"
)
that includes the columns .iter
(the iterations of the simulation
procedure numbered from 1
to n_iter
), .look
(the
interim "looks" numbered from 1
to the maximum number of looks,
including the final one), and the information returned by the
fun_test
function for H0 and H1 outcomes (mainly p values; but also
other, optional information, if any) and the corresponding observation
numbers, as well as the total observation number per each look under a
dedicated .n_total
column. When this data frame is printed to the
console (via POSSA's print()
method), the head (first few lines) of the data is shown, as well as, in
case of any varying factors included, summary information per factor
combination.
Note
For the replicability (despite the randomization), set.seed
is
executed in the beginning of this function, each time it is called; see the
seed
parameter.
See Also
Examples
# below is a (very) minimal example
# for more, see the vignettes via https://github.com/gasparl/possa#usage
# create sampling function
customSample = function(sampleSize) {
list(
sample1 = rnorm(sampleSize, mean = 0, sd = 10),
sample2_h0 = rnorm(sampleSize, mean = 0, sd = 10),
sample2_h1 = rnorm(sampleSize, mean = 5, sd = 10)
)
}
# create testing function
customTest = function(sample1, sample2_h0, sample2_h1) {
c(
p_h0 = t.test(sample1, sample2_h0, 'less', var.equal = TRUE)$p.value,
p_h1 = t.test(sample1, sample2_h1, 'less', var.equal = TRUE)$p.value
)
}
# run simulation
dfPvals = sim(
fun_obs = customSample,
n_obs = 80,
fun_test = customTest,
n_iter = 1000
)
# get power info
pow(dfPvals)