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

Rob Carnell carnellr at battelle.org
Wed Oct 9 15:37:15 CEST 2013


Johannes Radinger <johannesradinger <at> gmail.com> writes:

> 
> 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))
> 
> 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]]
> 
> 

Johannes,

I would modify my example (quoted above) as follows to meet your needs.  
Please feel free to email me directly about the lhs package if necessary.

require(lhs)
N <- 1000
set.seed(1919)

x <- randomLHS(N, 4)
y <- x
# uniform on 1-10
y[,1] <- ceiling(qunif(x[,1], 0, 10))
# three colors 1,2,3
y[,2] <- ceiling(qunif(x[,2], 0, 3))
# other distributions
y[,3] <- qunif(x[,3], 5, 10)
y[,4] <- qnorm(x[,4], 0, 2)

par(mfrow=c(2,2))
dummy <- apply(x, 2, hist, main="")

par(mfrow=c(2,2))
plot(1:10, c(table(y[,1])), type="h", col="blue", lwd=2, ylim=c(0,120), 
ylab="Frequency", xlab="y[,1]")
plot(1:3, c(table(y[,2])), type="h", col="blue", lwd=2, ylim=c(0,400), 
ylab="Frequency", xlab="y[,2]")
hist(y[,3], main="")
hist(y[,4], main="")

# change to color names
z <- as.data.frame(y)
z[,2] <- factor(y[,2], labels=c("R","G","B"))
z[1:10,]

Rob



More information about the R-help mailing list