grid_search {paramtest} | R Documentation |
Run a function iteratively using a grid search approach for parameter values, with options for parallel processing.
Description
grid_search
runs a user-defined function iteratively. Parameter values
can be given to grid_search
, which will fully cross all parameters so
that each parameter value is tested at all other values of all parameters.
Usage
grid_search(func, params = NULL, n.iter = 1, output = c("list",
"data.frame"), boot = FALSE, bootParams = NULL, parallel = c("no",
"multicore", "snow"), ncpus = 1, cl = NULL, beep = NULL, ...)
Arguments
func |
A user-defined function. The first argument to this function will be the iteration number. |
params |
A list of parameters to be passed to |
n.iter |
Number of iterations (per set of params). |
output |
Specifies how |
boot |
Whether or not to use bootstrapped data to pass along to
|
bootParams |
If |
parallel |
The type of parallel operation to be used (if any). |
ncpus |
Integer: the number of processes to be used in parallel operation. |
cl |
An optional |
beep |
Include a numeric value or character vector indicating the sound you wish to play once the tests are done running. Requires the 'beepr' package, and information about supported values is available in the documentation for that package. |
... |
Additional arguments to be passed to |
Value
Returns a list (by default) with one element per iteration. If
output
is specified as "data.frame", then func
must
return a (named) vector with the results you wish to capture.
See Also
Examples
lm_test <- function(iter, N, b0, b1) {
x <- rnorm(N, 0, 1)
y <- rnorm(N, b0 + b1*x, sqrt(1 - b1^2))
data <- data.frame(y, x)
model <- lm(y ~ x, data)
# capture output from model summary
est <- coef(summary(model))['x', 'Estimate']
se <- coef(summary(model))['x', 'Std. Error']
p <- coef(summary(model))['x', 'Pr(>|t|)']
return(c(xm=mean(x), xsd=sd(x), ym=mean(y), ysd=sd(y), est=est, se=se, p=p,
sig=est > 0 & p <= .05))
}
# test power for sample size N=200 and N=300, with 500 iterations for each
power_sim <- grid_search(lm_test, params=list(N=c(200, 300)), n.iter=500, b0=0, b1=.15)