Helper function to generate simulated errors.
generate_errors.Rd
Generate simulated errors from the specified error function and passed arguments.
Arguments
- err
Function from which to generate simulate error vector. Default is
NULL
which returns an error vector of all zeros.- n
Number of samples.
- X
Data matrix or data frame. Used to determine n if n is missing.
- ...
Other arguments to pass to err() to generate the error vector.
Details
The arguments n
and X
(if provided) are automatically
passed to the function err
under arguments of the same name.
Note however that they may be unused arguments if err
does not take
in arguments named n
and/or X
as input.
Examples
# generate standard Gaussian error vector of length 150
errs <- generate_errors(err = rnorm, n = 150)
# or alternatively,
errs <- generate_errors(err = rnorm, X = iris)
# generate Gaussian error vector with mean 0 and sd 2
errs <- generate_errors(err = rnorm, n = 150, sd = 2)
# generate error vector of all 0s
errs <- generate_errors(err = NULL, n = 150)
# generate error vector from custom error function
err_fun <- function(n, rho) {
# simulate correlated errors from a autoregressive-1 Gaussian process
row1 <- rho^(0:(n - 1))
Sigma <- stats::toeplitz(row1)
return(MASS::mvrnorm(1, mu = rep(0, n), Sigma = Sigma))
}
errs <- generate_errors(err = err_fun, n = 100, rho = 0.75)