Sample using a lognormal distribution
sample_lognormal.RdSample from a standard normal in log space and apply this
exponentiated observation error to the observations, i.e, x, after the
simulated error values are bias corrected.
Arguments
- x
A numeric vector of observations to sample from. These observations should be on the normal scale. If a single value is provided, it will be recycled to the length of
sd. Otherwise, it must be the same length assd.- sd
A numeric vector of standard deviations to use in
rnorm(). If a single value is provided, it will be recycled to the length ofx. Otherwise, it must be the same length asx.
Details
Historical methods
atlantisom
A lognormal distribution was used to sample using a mean of the natural log of the expected value minus the input variance divided by two and the input standard deviation. Where the input variance was calculated as the natural log of the coefficient of variation squared plus one.
Examples
# Note that setting the seed on a vectorized implementation of this function
# will not lead to the same sample being returned for each element of x
# because a single random number is generated for the entire vector.
set.seed(123)
sample_lognormal(1:10, sd = 1)
#> [1] 0.3462911 0.9636469 8.6479353 2.6033596 3.4512126 20.2230786
#> [7] 6.7316909 1.3694104 2.7466218 3.8842236
set.seed(123)
sample_lognormal(1, sd = 1)
#> [1] 0.3462911
sample_lognormal(2, sd = 1)
#> [1] 0.9636469
# You can also use the function with a vector of standard deviations
set.seed(123)
sample_lognormal(1:10, sd = 1:10)
#> [1] 3.462911e-01 1.708093e-01 3.577761e+00 1.779057e-03 3.556589e-05
#> [6] 2.690978e-03 4.037354e-09 4.077497e-18 4.793452e-20 2.237642e-23