# [R] Latin Hypercube Sample and transformation to uniformly distributed integers or classes

Duncan Murdoch murdoch.duncan at gmail.com
Tue Oct 8 16:10:31 CEST 2013

```On 08/10/2013 9:37 AM, Johannes Radinger wrote:
> Hi,
>
> I'd like to use Latin Hypercube Sampling (LHC) in the the context of
> uncertainty / sensitivity analysis of a complex model with approximately 10
> input variables. With the LHC approach I'd like to generate parameter
> combinations for my model input variables.
> Therefore I came across an simple example here on the mailing list (
> https://stat.ethz.ch/pipermail/r-help/2011-June/279931.html):
>
> Easy Example
> Parameter 1: normal(1, 2)
> Parameter 2: normal(3, 4)
> Parameter 3: uniform(5, 10)
>
> require(lhs)
> N <- 1000
> x <- randomLHS(N, 3)
> y <- x
> y[,1] <- qnorm(x[,1], 1, 2)
> y[,2] <- qnorm(x[,2], 3, 4)
> y[,3] <- qunif(x[,3], 5, 10)
>
> par(mfrow=c(2,2))
> apply(x, 2, hist)
>
> par(mfrow=c(2,2))
> apply(y, 2, hist)
>
>
> However, some of my parameters are uniformly distributed integer values
> and/or uniformly distributed classes. So, for example one input parameter
> can be "yellow", "green", "red" with equal probability. Of course these
> attributes can be transformed into integers (1,2,3) with a uniform
> distribution.
>
> So far I've tried to use the round function:
>
> y[,3] <- round(qunif(x[,3], 5, 10))

Why round()?  floor() would make more sense.  And why have a lower limit
of 5?  I would use 0.

When I do that I get reasonable results:

table(floor(qunif(runif(100000), 0, 10)) + 1)

(You should put in your lhs values instead of runif.  You will run into
problems if they are ever exactly equal to 1; runif() would never do that.)

Duncan Murdoch

>
> which does not sample the 1 and 10 eqally to 2:8 (this is discussed already
> somewhere else here on the list in another context, and the function
> sample() is suggested). How can this be applied here and how can a column
> of the lhs-output be transformed in e.g integers 1:10 or the three colors
> as mentioned above?
>
> thanks,
>
> Johannes
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help