Learn R Programming

PoweR (version 1.0.7)

checklaw: Check proper behaviour of a random generator

Description

It is desirable to check if a newly added random generator coded in C behaves correctly. To perform this operation, one can superimpose the theoretical density on a histogram of the generated values.

Usage

checklaw(law.index, sample.size = 50000, law.pars = NULL, density =
NULL, trunc = c(-Inf, Inf), center = FALSE, scale = FALSE)

Arguments

law.index

index of the desired law, as given by getindex.

sample.size

number of observations to generate.

law.pars

vector of parameters for the law. The length of this parameter should not exceed 4. If not provided, the default values are used by means of getindex function.

density

a function of two arguments x and pars. Can also be a function of the arguments x and pars[1], ..., pars[k]. See the two examples below.

trunc

vector of left and right truncation thresholds for the generated sample values. Only those values in between will be kept to build the histogram. This can be useful for a distribution with extreme values.

center

Logical. Should we center the data.

scale

Should we scale the data.

Value

Returns invisibly the data generated and make a plot showing histogram and density superimposed.

References

Pierre Lafaye de Micheaux, Viet Anh Tran (2016). PoweR: A Reproducible Research Tool to Ease Monte Carlo Power Simulation Studies for Goodness-of-fit Tests in R. Journal of Statistical Software, 69(3), 1--42. doi:10.18637/jss.v069.i03

See Also

gensample

Examples

Run this code
# NOT RUN {
dlaplace1 <- function(x, mu, b) {dexp(abs(x - mu), 1 / b) / 2}
checklaw(1, density = dlaplace1)
dlaplace2 <- function(x, pars) {dexp(abs(x - pars[1]), 1 / pars[2]) / 2}
checklaw(1, density = dlaplace2)


checklaw(law.index = 2, sample.size = 50000, law.pars = c(2, 3), density
= dnorm)

## We use the 'trunc' argument to display the density in a region where
## no extreme values are present.
checklaw(27, density = dlaw27, trunc = c(-Inf,10))

# This one (Tukey) does not have a closed form expression for
# the density. But we can use the stats::density() function as
# follows.
res <- checklaw(18)
lines(density(res$sample), col = "blue")
# }

Run the code above in your browser using DataLab