Add further results to an existing power_array (created by
PowerGrid or by another call of Refine), adding further values in
pars and/or larger n_iter.
Details
This function allows extending the number of iterations, as well as
changing the parameter grid across which the function of interest is
evaluated. Being able to do both at the same time is handy in cases where
calculation takes significant time: You may first only evaluate a broad
grid of parameters with a low number of iterations using PowerGrid. Then,
using Refine, you can zoom in on a certain section of the parameter grid
(and increase resolution in the focal range), and evaluate with a higher
number of iterations.
If pars == NULL, Refine extends old by adding iterations
n_iter_add to the existing power_array (applying the original
function attr(old, which = 'sim_function').
If pars is given and different from the original pars
(attr(old, which = 'pars')), the function is evaluated at
additional crossings of parameters. This means that the resulting array
may have empty cells, or cells with different numbers of iterations. This
is a feature, not a bug. It may result in less aesthetic plotting,
however.
For details about handling the random seed, see PowerGrid.
Examples
## ============================================
## very simple example with one parameter
## ============================================
pars = list(x = 1:2)
fun = function(x){round(x+runif(1, 0, .2), 3)} # nonsense function
set.seed(1)
original = PowerGrid(pars = pars,
fun = fun,
n_iter = 3,
summarize = FALSE)
refined = Refine(original, n_iter_add = 2, pars = list(x = 2:3))
## note that refined does not have each parameter sampled in each iteration
## ============================================
## a realistic example, simply increasing n_iter
## ============================================
PowFun <- function(n, delta){
x1 = rnorm(n = n/2, sd = 1)
x2 = rnorm(n = n/2, mean = delta, sd = 1)
t.test(x1, x2)$p.value < .05
}
sse_pars = list(
n = seq(10, 100, 5),
delta = seq(.5, 1.5, .1))
##
n_iter = 20
set.seed(1)
power_array = PowerGrid(pars = sse_pars,
fun = PowFun,
n_iter = n_iter,
summarize = FALSE)
summary(power_array)
#> Object of class: power_array
#> Containing output of 20 individual iterations.
#> Range of values: [0, 1]
#> Evaluated at:
#> n 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65,
#> n 70, 75, 80, 85, 90, 95, 100
#> delta 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4,
#> delta 1.5
## add iterations
power_array_up = Refine(power_array, n_iter_add = 30)
summary(power_array_up)
#> Object of class: power_array
#> Containing output of 50 individual iterations.
#> Range of values: [0, 1]
#> Evaluated at:
#> n 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65,
#> n 70, 75, 80, 85, 90, 95, 100
#> delta 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4,
#> delta 1.5
## ============================================
## Starting coarsely, then zooming in
## ============================================
sse_pars = list(
n = c(10, 50, 100, 200), # finding n "ballpark"
delta = c(.5, 1, 1.5)) # finding delta "ballpark"
n_iter = 60
power_array = PowerGrid(pars = sse_pars,
fun = PowFun,
n_iter = n_iter,
summarize = FALSE)
summary(power_array)
#> Object of class: power_array
#> Containing output of 60 individual iterations.
#> Range of values: [0, 1]
#> Evaluated at:
#> n 10, 50, 100, 200
#> delta 0.5, 1, 1.5
PowerPlot(power_array)
#> Warning: The power array you supplied to contains
#> individual iterations. To be used further these
#> were automatically summarized across iterations
#> by function: mean.
## Based on figure above, let's look at n between 50 and 100, delta around .9
# \donttest{
sse_pars = list(
n = seq(50, 100, 5),
delta = seq(.7, 1.1, .05))
set.seed(1)
power_array_up = Refine(power_array, n_iter_add = 555, pars = sse_pars)
summary(power_array_up)
#> Object of class: power_array
#> Containing output of 615 individual iterations.
#> Range of values: [0, 1]
#> Evaluated at:
#> n 10, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100,
#> n 200
#> delta 0.5, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.05,
#> delta 1.1, 1.5
PowerPlot(power_array_up)
#> Warning: The power array you supplied to contains
#> individual iterations. To be used further these
#> were automatically summarized across iterations
#> by function: mean.
## That looks funny! It's because the default summary mean does not deal
## with the empty value in the grid. Solution is in illustration below.
## A visual illustration of this zooming in, in three figures
layout(t(1:3))
PowerPlot(power_array, title = 'Course grid to start with')
#> Warning: The power array you supplied to contains
#> individual iterations. To be used further these
#> were automatically summarized across iterations
#> by function: mean.
PowerPlot(power_array_up, summary_function = function(x)mean(x, na.rm = TRUE),
title = 'Extra samples at finer grid\n(looks funny with large empty cells)')
#> Warning: The power array you supplied to contains
#> individual iterations. To be used further these
#> were automatically summarized across iterations
#> by function: anonymous function.
PowerPlot(power_array_up,
slicer = list(n = seq(50, 100, 5),
delta = seq(.7, 1.1, .05)),
summary_function = function(x)mean(x, na.rm = TRUE),
title = 'Zoomed in')
#> Warning: The power array you supplied to contains
#> individual iterations. To be used further these
#> were automatically summarized across iterations
#> by function: anonymous function.
layout(1)
# }