Although most likely to be called by uncert
, uncertMC
may be called directly.
If any of x
, u
, df
, distrib
or distrib.pars
are not lists,
they are coerced to lists. If x
is not named, arbitrary names of the form 'Xn'
are applied. If u
, df
, distrib
or distrib.pars
do not have
names, the names will be set to names(x)
if they are of length exactly
length(x)
; if not, an error is returned.
For Monte Carlo evaluation, distributions and distribution parameters are needed but
defaults are used if some or all are absent. If distrib
is missing, or
if it is a list with some members missing, the distribution is assumed Normal
and any missing member of distrib
is set to "norm".
Distributions are usually identified by the root of the distribution function name; for example
to specify the Normal, distrib$name="norm"
. At present, only the random value
generator (e.g. rnorm
) is used. Names of user-specified distributions functions can also be
used, provided they have a random value generator named r<dist>
where <dist>
is the abbreviated distribution. Parameters are passed to distribution functions using
do.call
, so the function must accept the parameters supplied in distrib.pars
.
If distrib.pars
or members of it are missing, an attempt is made to deduce
appropriate distribution parameters from x
, u
, df
and distrib
.
In doing so, the following assumptions and values apply for the respective distributions:
- norm
mean=x$name, sd=u$name
.
- unif
min=x-sqrt(3)*u, max=x+sqrt(3)*u
.
- tri
min=x-sqrt(6)*u, max=x+sqrt(6)*u, mode=x
.
- t, t.scaled
df=df, mean=x, sd=u
.
If either cor
or cov
are present, a test is made to see if off-diagonal
elements are significant. If not, uncertMC
treats the values as independent.
The test simply checks whether the sum of off-diagonal elements of cor
(calculated
from cov
if cov
is present) is bigger than
.Machine.double.eps*nrow^2
.
Correlation is supported as long as all correlated variables are normally distributed.
If correlation is present, uncertMC
follows a two-stage simulation procedure.
First, variables showing correlation are identified. Following a check that
their associated distrib
values are all "norm"
, mvrnorm
from
the MASS library is called to generate the simulated x
values for those variables.
Second, any remaining (i.e. independent) variables are simulated from their respective
distrib
and distrib.pars
.
Vectorisation makes a difference to execution speed. If vectorize=TRUE
, MC evaluation
uses eval
using the simulated data as the evaluation environment; if not, apply
is used row-wise on the simulated input matrix. This makes an appreciable difference to
execution speed (typically eval
is faster by a factor of 5 or more) so the default
assumes vectorised expressions. However, not all functions and expressions take vector arguments,
especially user functions involving complicated arithmetic or numerical solutions. Use vectorize=FALSE
for functions or expressions that do not take vector arguments.
Note: One common symptom of an expression that does not take vector arguments is
an R warning indicating that only the first element (typically of a parameter in x
) is used.
uncertMC may also return NA for u
on attempting to take the sd of a single simulated point.