rnormW | R Documentation |
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.
rnormW(
n,
mean = 0,
sd = 1,
seed = NULL,
digits = 8,
silent = FALSE,
callFrom = NULL
)
n |
(integer, length=1) number of observations. If |
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 |
silent |
(logical) suppress messages |
callFrom |
(character) allow easier tracking of messages produced |
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.
This function returns a numeric vector of random values
Normal
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.