Description Usage Arguments Details Value Examples

View source: R/eval_design_mc.R

Evaluates the power of an experimental design, given the run matrix and the statistical model to be fit to the data, using monte carlo simulation. Simulated data is fit using a generalized linear model and power is estimated by the fraction of times a parameter is significant. Returns a data frame of parameter powers.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |

`design` |
The experimental design. Internally, |

`model` |
The model used in evaluating the design. If this is missing and the design was generated with skpr, the generating model will be used. It can be a subset of the model used to generate the design, or include higher order effects not in the original design generation. It cannot include factors that are not present in the experimental design. |

`alpha` |
Default '0.05'. The type-I error. p-values less than this will be counted as significant. |

`blocking` |
Default 'NULL'. If 'TRUE', |

`nsim` |
Default '1000'. The number of Monte Carlo simulations to perform. |

`glmfamily` |
Default 'gaussian'. String indicating the family of distribution for the 'glm' function ("gaussian", "binomial", "poisson", or "exponential"). |

`calceffect` |
Default 'TRUE'. Calculates effect power for a Type-III Anova (using the car package) using a Wald test. this ratio can be a vector specifying the variance ratio for each subplot. Otherwise, it will use a single value for all strata. |

`varianceratios` |
Default 'NULL'. The ratio of the whole plot variance to the run-to-run variance. If not specified during design generation, this will default to 1. For designs with more than one subplot this ratio can be a vector specifying the variance ratio for each subplot (comparing to the run-to-run variance). Otherwise, it will use a single value for all strata. |

`rfunction` |
Default 'NULL'.Random number generator function for the response variable. Should be a function of the form f(X, b, delta), where X is the model matrix, b are the anticipated coefficients, and delta is a vector of blocking errors. Typically something like rnorm(nrow(X), X * b + delta, 1). You only need to specify this if you do not like the default behavior described below. |

`anticoef` |
Default 'NULL'.The anticipated coefficients for calculating the power. If missing, coefficients
will be automatically generated based on the |

`effectsize` |
Helper argument to generate anticipated coefficients. See details for more info.
If you specify |

`contrasts` |
Default |

`parallel` |
Default 'FALSE'. If 'TRUE', uses all cores available to speed up computation. WARNING: This can slow down computation if nonparallel time to complete the computation is less than a few seconds. |

`detailedoutput` |
Default 'FALSE'. If 'TRUE', return additional information about evaluation in results. |

`advancedoptions` |
Default 'NULL'. Named list of advanced options. 'advancedoptions$anovatype' specifies the Anova type in the car package (default type 'III'), user can change to type 'II'). 'advancedoptions$anovatest' specifies the test statistic if the user does not want a 'Wald' testâ€“other options are likelyhood-ratio 'LR' and F-test 'F'. 'advancedoptions$progressBarUpdater' is a function called in non-parallel simulations that can be used to update external progress bar.'advancedoptions$GUI' turns off some warning messages when in the GUI. If 'advancedoptions$save_simulated_responses = TRUE', the dataframe will have an attribute 'simulated_responses' that contains the simulated responses from the power evaluation. |

`...` |
Additional arguments. |

Evaluates the power of a design with Monte Carlo simulation. Data is simulated and then fit
with a generalized linear model, and the fraction of simulations in which a parameter
is significant (its p-value, according to the fit function used, is less than the specified `alpha`

)
is the estimate of power for that parameter.

First, if `blocking = TURE`

, the random noise from blocking is generated with `rnorm`

.
Each block gets a single sample of Gaussian random noise, with a variance as specified in
`varianceratios`

,
and that sample is copied to each run in the block. Then, `rfunction`

is called to generate a simulated
response for each run of the design, and the data is fit using the appropriate fitting function.
The functions used to simulate the data and fit it are determined by the `glmfamily`

and `blocking`

arguments
as follows. Below, X is the model matrix, b is the anticipated coefficients, and d
is a vector of blocking noise (if `blocking = FALSE`

then d = 0):

glmfamily | blocking | rfunction | fit |

"gaussian" | F | `rnorm(mean = X %*% b + d, sd = 1)` | `lm` |

"gaussian" | T | `rnorm(mean = X %*% b + d, sd = 1)` | `lme4::lmer` |

"binomial" | F | `rbinom(prob = 1/(1+exp(-(X %*% b + d))))` | `glm(family = "binomial")` |

"binomial" | T | `rbinom(prob = 1/(1+exp(-(X %*% b + d))))` | `lme4::glmer(family = "binomial")` |

"poisson" | F | `rpois(lambda = exp((X %*% b + d)))` | `glm(family = "poisson")` |

"poisson" | T | `rpois(lambda = exp((X %*% b + d)))` | `lme4::glmer(family = "poisson")` |

"exponential" | F | `rexp(rate = exp(-(X %*% b + d)))` | `glm(family = Gamma(link = "log"))` |

"exponential" | T | `rexp(rate = exp(-(X %*% b + d)))` | `lme4:glmer(family = Gamma(link = "log"))` |

Note that the exponential random generator uses the "rate" parameter, but `skpr`

and `glm`

use
the mean value parameterization (= 1 / rate), hence the minus sign above. Also note that
the gaussian model assumes a root-mean-square error of 1.

Power is dependent on the anticipated coefficients. You can specify those directly with the `anticoef`

argument, or you can use the `effectsize`

argument to specify an effect size and `skpr`

will auto-generate them.
You can provide either a length-1 or length-2 vector. If you provide a length-1 vector, the anticipated
coefficients will be half of `effectsize`

; this is equivalent to saying that the *linear predictor*
(for a gaussian model, the mean response; for a binomial model, the log odds ratio; for an exponential model,
the log of the mean value; for a poisson model, the log of the expected response)
changes by `effectsize`

when a continuous factor goes from its lowest level to its highest level. If you provide a
length-2 vector, the anticipated coefficients will be set such that the *mean response* (for
a gaussian model, the mean response; for a binomial model, the probability; for an exponential model, the mean
response; for a poisson model, the expected response) changes from
`effectsize[1]`

to `effectsize[2]`

when a factor goes from its lowest level to its highest level, assuming
that the other factors are inactive (their x-values are zero).

The effect of a length-2 `effectsize`

depends on the `glmfamily`

argument as follows:

For `glmfamily = 'gaussian'`

, the coefficients are set to `(effectsize[2] - effectsize[1]) / 2`

.

For `glmfamily = 'binomial'`

, the intercept will be
`1/2 * log(effectsize[1] * effectsize[2] / (1 - effectsize[1]) / (1 - effectsize[2]))`

,
and the other coefficients will be
`1/2 * log(effectsize[2] * (1 - effectsize[1]) / (1 - effectsize[2]) / effectsize[1])`

.

For `glmfamily = 'exponential'`

or `'poisson'`

,
the intercept will be
`1 / 2 * (log(effectsize[2]) + log(effectsize[1]))`

,
and the other coefficients will be
`1 / 2 * (log(effectsize[2]) - log(effectsize[1]))`

.

A data frame consisting of the parameters and their powers, with supplementary information stored in the data frame's attributes. The parameter estimates from the simulations are stored in the "estimates" attribute. The "modelmatrix" attribute contains the model matrix that was used for power evaluation, and also provides the encoding used for categorical factors. If you want to specify the anticipated coefficients manually, do so in the order the parameters appear in the model matrix.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | ```
#We first generate a full factorial design using expand.grid:
factorialcoffee = expand.grid(cost = c(-1, 1),
type = as.factor(c("Kona", "Colombian", "Ethiopian", "Sumatra")),
size = as.factor(c("Short", "Grande", "Venti")))
#And then generate the 21-run D-optimal design using gen_design.
designcoffee = gen_design(factorialcoffee,
model = ~cost + type + size, trials = 21, optimality = "D")
#To evaluate this design using a normal approximation, we just use eval_design
#(here using the default settings for contrasts, effectsize, and the anticipated coefficients):
eval_design(design = designcoffee, model = ~cost + type + size, 0.05)
#To evaluate this design with a Monte Carlo method, we enter the same information
#used in eval_design, with the addition of the number of simulations "nsim" and the distribution
#family used in fitting for the glm "glmfamily". For gaussian, binomial, exponential, and poisson
#families, a default random generating function (rfunction) will be supplied. If another glm
#family is used or the default random generating function is not adequate, a custom generating
#function can be supplied by the user. Like in `eval_design()`, if the model isn't entered, the
#model used in generating the design will be used.
## Not run: eval_design_mc(designcoffee, nsim = 100, glmfamily = "gaussian")
#We see here we generate approximately the same parameter powers as we do
#using the normal approximation in eval_design. Like eval_design, we can also change
#effectsize to produce a different signal-to-noise ratio:
## Not run: eval_design_mc(design = designcoffee, nsim = 100,
glmfamily = "gaussian", effectsize = 1)
## End(Not run)
#Like eval_design, we can also evaluate the design with a different model than
#the one that generated the design.
## Not run: eval_design_mc(design = designcoffee, model = ~cost + type, alpha = 0.05,
nsim = 100, glmfamily = "gaussian")
## End(Not run)
#And here it is evaluated with additional interactions included:
## Not run: eval_design_mc(design = designcoffee, model = ~cost + type + size + cost * type, 0.05,
nsim = 100, glmfamily = "gaussian")
## End(Not run)
#We can also set "parallel = TRUE" to use all the cores available to speed up
#computation.
## Not run: eval_design_mc(design = designcoffee, nsim = 10000,
glmfamily = "gaussian", parallel = TRUE)
## End(Not run)
#We can also evaluate split-plot designs. First, let us generate the split-plot design:
factorialcoffee2 = expand.grid(Temp = c(1, -1),
Store = as.factor(c("A", "B")),
cost = c(-1, 1),
type = as.factor(c("Kona", "Colombian", "Ethiopian", "Sumatra")),
size = as.factor(c("Short", "Grande", "Venti")))
vhtcdesign = gen_design(factorialcoffee2,
model = ~Store, trials = 6, varianceratio = 1)
htcdesign = gen_design(factorialcoffee2, model = ~Store + Temp, trials = 18,
splitplotdesign = vhtcdesign, blocksizes = rep(3, 6), varianceratio = 1)
splitplotdesign = gen_design(factorialcoffee2,
model = ~Store + Temp + cost + type + size, trials = 54,
splitplotdesign = htcdesign, blocksizes = rep(3, 18),
varianceratio = 1)
#Each block has an additional noise term associated with it in addition to the normal error
#term in the model. This is specified by a vector specifying the additional variance for
#each split-plot level. This is equivalent to specifying a variance ratio of one between
#the whole plots and the run-to-run variance for gaussian models.
#Evaluate the design. Note the decreased power for the blocking factors.
## Not run: eval_design_mc(splitplotdesign, blocking = TRUE, nsim = 100,
glmfamily = "gaussian", varianceratios = c(1, 1, 1))
## End(Not run)
#We can also use this method to evaluate designs that cannot be easily
#evaluated using normal approximations. Here, we evaluate a design with a binomial response and see
#whether we can detect the difference between each factor changing whether an event occurs
#70% of the time or 90% of the time.
factorialbinom = expand.grid(a = c(-1, 1), b = c(-1, 1))
designbinom = gen_design(factorialbinom, model = ~a + b, trials = 90, optimality = "D")
## Not run: eval_design_mc(designbinom, ~a + b, alpha = 0.2, nsim = 100, effectsize = c(0.7, 0.9),
glmfamily = "binomial")
## End(Not run)
#We can also use this method to determine power for poisson response variables.
#Generate the design:
factorialpois = expand.grid(a = as.numeric(c(-1, 0, 1)), b = c(-1, 0, 1))
designpois = gen_design(factorialpois, ~a + b, trials = 70, optimality = "D")
#Evaluate the power:
## Not run: eval_design_mc(designpois, ~a + b, 0.05, nsim = 100, glmfamily = "poisson",
anticoef = log(c(0.2, 2, 2)))
## End(Not run)
#The coefficients above set the nominal value -- that is, the expected count
#when all inputs = 0 -- to 0.2 (from the intercept), and say that each factor
#changes this count by a factor of 4 (multiplied by 2 when x= +1, and divided by 2 when x = -1).
#Note the use of log() in the anticipated coefficients.
``` |

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.