rnormW: Normal random number generation with close fit to expected...

View source: R/rnormW.R

rnormWR Documentation

Normal random number generation with close fit to expected mean and sd

Description

This function allows creating a vector of random values similar to rnorm, but resulting value get recorrected to fit to expected mean and sd. When the number of random values to generate is low, the mean and sd of the resultant values may deviate from the expected mean and sd when using the standard rnorm function. In such cases the function rnormW helps getting much closer to the expected mean and sd.

Usage

rnormW(
  n,
  mean = 0,
  sd = 1,
  seed = NULL,
  digits = 8,
  silent = FALSE,
  callFrom = NULL
)

Arguments

n

(integer, length=1) number of observations. If length(n) > 1, the length is taken to be the number required.

mean

(numeric, length=1) expected mean

sd

(numeric, length=1) expected sd

seed

(integer, length=1) seed for generating random numbers

digits

(integer, length=1 or NULL) number of significant digits for output, set to NULL to get all digits

silent

(logical) suppress messages

callFrom

(character) allow easier tracking of messages produced

Details

For making result reproducible, a seed for generating random numbers can be set via the argument seed. However, with n=2 the resulting values are 'fixed' since no random component is possible at n <3.

Value

This function returns a numeric vector of random values

See Also

Normal

Examples

x1 <- (11:16)[-5]
mean(x1); sd(x1)
## the standard way
ra1 <- rnorm(n=length(x1), mean=mean(x1), sd=sd(x1))
## typically the random values deviate (slightly) from expected mean and sd
mean(ra1) -mean(x1) 
sd(ra1) -sd(x1)
## random numbers with close fit to expected mean and sd :
ra2 <- rnormW(length(x1), mean(x1), sd(x1))
mean(ra2) -mean(x1) 
sd(ra2) -sd(x1)          # much closer to expected value

wrMisc documentation built on Nov. 17, 2023, 5:09 p.m.