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

PIKAL Petr petr.pikal at precheza.cz
Tue Oct 8 16:02:04 CEST 2013

```Hi

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Johannes Radinger
> Sent: Tuesday, October 08, 2013 3:38 PM
> To: R help
> Subject: [R] Latin Hypercube Sample and transformation to uniformly
> distributed integers or classes
>
> 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)

This put 3 columns of uniformly distributed random numbers in x

> 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

Maybe

set.seed(333)
x<-sample(c("yellow", "green", "red"), 1000, replace=TRUE)
table(x)
x
green    red yellow
334    327    339

> course these attributes can be transformed into integers (1,2,3) with a
> uniform distribution.

I would use

xf <- factor(x)

to transform it to numbers and still retaining labels.

>
> So far I've tried to use the round function:
>
> y[,3] <- round(qunif(x[,3], 5, 10))
>
> 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?

Maybe cut.
as.numeric(as.factor(cut(x[,1], 10)))
factor(cut(x[,1], 3), labels=c("yellow", "green", "red"))

Regards
Petr

>
> thanks,
>
> Johannes
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

```