Johannes Radinger
2013-Oct-08 13:37 UTC
[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) 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]]
PIKAL Petr
2013-Oct-08 14:02 UTC
[R] Latin Hypercube Sample and transformation to uniformly distributed integers or classes
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. OfMaybe 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.
Duncan Murdoch
2013-Oct-08 14:10 UTC
[R] Latin Hypercube Sample and transformation to uniformly distributed integers or classes
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 > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Rob Carnell
2013-Oct-09 13:37 UTC
[R] Latin Hypercube Sample and transformation to uniformly distributed integers or classes
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 approximately10> 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 discussedalready> 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
Possibly Parallel Threads
- Latin Hypercube Sampling when parameters are defined according to specific probability distributions
- Latin Hypercube Sampling when parameters are defined according to specific probability distributions
- Latin hyper cube sampling from expand.grid()
- Latin hypercube sampling from a non-uniform distribution
- Latin hypercube sampling from a non-uniform distribution