User Questions on Latin Hypercube Samples

Rob Carnell

21 May 2014

Please take a look at this website for questions that were asked on the R help lists: http://lhs.r-forge.r-project.org/

Question 1

I am very much grateful to you for the LHS package in R which made my job easier. I am currently working on aerosol research and trying to apply the LHS for my data analysis. I am not a programmer and I found the LHS package you developed as a boon as it is very easy to use without writing any code myself. I have realized that the LHS function generates 'n' random points for 'k' parameters with the values ranging between [0,1]. For my problem I have 7 parameters with upper and lower bounds as [0.75, 1.25]; [0, 01]; [11, 17]; [7.6, 14]; [0.73, 0.82]; [0.66,0.76] and [0.2, 1.3]. I am not sure how to generate LHS matrix for 7 parameters with the given upper and lower bounds.

Answer1

require(lhs, quietly = TRUE)

set.seed(1976)
X <- randomLHS(3000, 7)
Y <- matrix(NA, nrow(X), ncol(X))
Y[, 1] <- qunif(X[, 1], 0.75, 1.25)
Y[, 2] <- qunif(X[, 2], 0, 0.1)
Y[, 3] <- qunif(X[, 7], 11, 17)
Y[, 4] <- qunif(X[, 7], 7.6, 14)
Y[, 5] <- qunif(X[, 7], 0.73, 0.82)
Y[, 6] <- qunif(X[, 7], 0.66, 0.76)
Y[, 7] <- qunif(X[, 7], 0.2, 1.3)

# or a little more sophisticated...
bounds <- matrix(c(0.75, 1.25, 0, 0.1, 11, 17, 7.6, 14, 0.73, 0.82, 0.66, 0.76, 
    0.2, 1.3), nrow = 2, ncol = 7)
samples <- 3000

set.seed(1976)
X <- randomLHS(samples, ncol(bounds))
Y <- sapply(1:ncol(bounds), function(z) qunif(X[, z], bounds[1, z], bounds[2, 
    z]))

Figure 1. A boxplot showing the extent of each of the original samples
boxplot(X, range = 0)

plot of chunk answerFigure1



Figure 2. A boxplot showing the extent of the transformed samples
boxplot(Y, range = 0)

plot of chunk answerFigure2



Figure 3. A histogram of the marginals of the transformed samples
par(mfrow = c(2, 4))
temp <- sapply(1:ncol(bounds), function(z) hist(Y[, z], col = "blue", breaks = 20, 
    main = z, xlab = ""))

plot of chunk answerFigure3