LHS random number generator

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

LHS random number generator

Shah Alam
Hello everyone,

I am using the LHS package to generate a combination of a
set of parameter values. However, I am not sure how to fix some parameter
values to 2 decimal points.
For example:

I want to make combinations in such a way that values for parameter
c("lmp", 0.40, 0.43) are taken as 0.40, 0.41, 0.42,0.43.

My codes are:

prior_lhs <- list(c("r_mu", 0.00299, 0.0032),
                  c("r_sd", 0.001, 0.002),
                  c("lmp", 0.40, 0.43),
                  c("gr_mu", 0.14, 0.16),
                  c("gr_sd", 0.01, 0.020),
                  c("alpha1", 0.0001, 0.0018),
                  c("alpha2", 0.0017, 0.0028),
                  c("alpha3", 0.005, 0.009),
                  c("beta", 0.69, 0.75)
)


### Simulation with priors from LHS ####
nb_simul <- 10000
nparam <- length(prior_lhs)
random_tab = randomLHS(nb_simul, nparam)
lhs_index = 1
param <- matrix(rep(0, nparam), nrow = 1, ncol = nparam)

for (i in 1:nb_simul) {
  temp_par <- matrix(rep(0, nparam), nrow = 1, ncol = nparam)
  for (j in 1:nparam) {
    temp_par[j] = as.numeric(prior_lhs[[j]][2]) +
      (as.numeric(prior_lhs[[j]][3]) - as.numeric(prior_lhs[[j]][2])) *
random_tab[lhs_index, j]
  }
  param <- rbind(param, temp_par)
  lhs_index <- lhs_index+1
}
param <- param[-1,]

Best regards,
Shah

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: [External] LHS random number generator

Richard M. Heiberger
I don't know the LHS package.  I don't see it on CRAN as either LHS, or more likely, as lhs (lower case).
When you write back to the list, using plain text, not HTML, please state where the LHS package is located.

I do see several potential problems.

You use a function randomLHS without first loading the package with a call something like
library(LHS)


Your use of the c() function turns the numbers into characters.  There is no indication
that you want to increment in units of .01:
> c("lmp", 0.40, 0.43)
[1] "lmp"  "0.4"  "0.43"



Explicit use of seq doesn't get what you are looking for.  Numbers in R (and most computer
programs) are binary and the representation in only a few decimal places is rounded. Thus:
> print(seq(.40, .43, .01), digits=17)
[1] 0.40000000000000002 0.41000000000000003 0.42000000000000004
[4] 0.42999999999999999


see FAQ 7.31 for discussion of number representations.
https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f



> On Apr 12, 2021, at 08:21, Shah Alam <[hidden email]> wrote:
>
> Hello everyone,
>
> I am using the LHS package to generate a combination of a
> set of parameter values. However, I am not sure how to fix some parameter
> values to 2 decimal points.
> For example:
>
> I want to make combinations in such a way that values for parameter
> c("lmp", 0.40, 0.43) are taken as 0.40, 0.41, 0.42,0.43.
>
> My codes are:
>
> prior_lhs <- list(c("r_mu", 0.00299, 0.0032),
>                  c("r_sd", 0.001, 0.002),
>                  c("lmp", 0.40, 0.43),
>                  c("gr_mu", 0.14, 0.16),
>                  c("gr_sd", 0.01, 0.020),
>                  c("alpha1", 0.0001, 0.0018),
>                  c("alpha2", 0.0017, 0.0028),
>                  c("alpha3", 0.005, 0.009),
>                  c("beta", 0.69, 0.75)
> )
>
>
> ### Simulation with priors from LHS ####
> nb_simul <- 10000
> nparam <- length(prior_lhs)
> random_tab = randomLHS(nb_simul, nparam)
> lhs_index = 1
> param <- matrix(rep(0, nparam), nrow = 1, ncol = nparam)
>
> for (i in 1:nb_simul) {
>  temp_par <- matrix(rep(0, nparam), nrow = 1, ncol = nparam)
>  for (j in 1:nparam) {
>    temp_par[j] = as.numeric(prior_lhs[[j]][2]) +
>      (as.numeric(prior_lhs[[j]][3]) - as.numeric(prior_lhs[[j]][2])) *
> random_tab[lhs_index, j]
>  }
>  param <- rbind(param, temp_par)
>  lhs_index <- lhs_index+1
> }
> param <- param[-1,]
>
> Best regards,
> Shah
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: LHS random number generator

Jim Lemon-4
In reply to this post by Shah Alam
Hi Shah,
I think what you are struggling toward is this:

prior_lhs <- list(r_mu=c( 0.00299, 0.0032),
                  r_sd=c( 0.001, 0.002),
                  lmp=c( 0.40, 0.43),
                  gr_mu=c( 0.14, 0.16),
                  gr_s=c( 0.01, 0.020),
                  alpha1=c( 0.0001, 0.0018),
                  alpha2=c(0.0017, 0.0028),
                  alpha3=c( 0.005, 0.009),
                  beta=c(0.69, 0.75))
for(i in 1:length(prior_lhs))
 prior_lhs[[i]]<-seq(prior_lhs[[i]][1],prior_lhs[[i]][2],length.out=4)

This gives you the "quartiles" you need for randomLHS function in the
"lhs" package if I read the help page correctly.

Jim

On Mon, Apr 12, 2021 at 10:21 PM Shah Alam <[hidden email]> wrote:

>
> Hello everyone,
>
> I am using the LHS package to generate a combination of a
> set of parameter values. However, I am not sure how to fix some parameter
> values to 2 decimal points.
> For example:
>
> I want to make combinations in such a way that values for parameter
> c("lmp", 0.40, 0.43) are taken as 0.40, 0.41, 0.42,0.43.
>
> My codes are:
>
> prior_lhs <- list(c("r_mu", 0.00299, 0.0032),
>                   c("r_sd", 0.001, 0.002),
>                   c("lmp", 0.40, 0.43),
>                   c("gr_mu", 0.14, 0.16),
>                   c("gr_sd", 0.01, 0.020),
>                   c("alpha1", 0.0001, 0.0018),
>                   c("alpha2", 0.0017, 0.0028),
>                   c("alpha3", 0.005, 0.009),
>                   c("beta", 0.69, 0.75)
> )
>
>
> ### Simulation with priors from LHS ####
> nb_simul <- 10000
> nparam <- length(prior_lhs)
> random_tab = randomLHS(nb_simul, nparam)
> lhs_index = 1
> param <- matrix(rep(0, nparam), nrow = 1, ncol = nparam)
>
> for (i in 1:nb_simul) {
>   temp_par <- matrix(rep(0, nparam), nrow = 1, ncol = nparam)
>   for (j in 1:nparam) {
>     temp_par[j] = as.numeric(prior_lhs[[j]][2]) +
>       (as.numeric(prior_lhs[[j]][3]) - as.numeric(prior_lhs[[j]][2])) *
> random_tab[lhs_index, j]
>   }
>   param <- rbind(param, temp_par)
>   lhs_index <- lhs_index+1
> }
> param <- param[-1,]
>
> Best regards,
> Shah
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: LHS random number generator

Shah Alam
Hi Richard and Jim

Thanks for your response. Sorry for the confusion. This is lhs package
(small letters), and after loading the package, I used the function
randomLHS.

The problem remained the same. I am trying to fix the length.out=2 for
lmp=c(0.40, 0.43), and length.out = 4 for all other variables/inputs.

Best regards,
Shah








On Tue, 13 Apr 2021 at 09:02, Jim Lemon <[hidden email]> wrote:

> Hi Shah,
> I think what you are struggling toward is this:
>
> prior_lhs <- list(r_mu=c( 0.00299, 0.0032),
>                   r_sd=c( 0.001, 0.002),
>                   lmp=c( 0.40, 0.43),
>                   gr_mu=c( 0.14, 0.16),
>                   gr_s=c( 0.01, 0.020),
>                   alpha1=c( 0.0001, 0.0018),
>                   alpha2=c(0.0017, 0.0028),
>                   alpha3=c( 0.005, 0.009),
>                   beta=c(0.69, 0.75))
> for(i in 1:length(prior_lhs))
>  prior_lhs[[i]]<-seq(prior_lhs[[i]][1],prior_lhs[[i]][2],length.out=4)
>
> This gives you the "quartiles" you need for randomLHS function in the
> "lhs" package if I read the help page correctly.
>
> Jim
>
> On Mon, Apr 12, 2021 at 10:21 PM Shah Alam <[hidden email]>
> wrote:
> >
> > Hello everyone,
> >
> > I am using the LHS package to generate a combination of a
> > set of parameter values. However, I am not sure how to fix some parameter
> > values to 2 decimal points.
> > For example:
> >
> > I want to make combinations in such a way that values for parameter
> > c("lmp", 0.40, 0.43) are taken as 0.40, 0.41, 0.42,0.43.
> >
> > My codes are:
> >
> > prior_lhs <- list(c("r_mu", 0.00299, 0.0032),
> >                   c("r_sd", 0.001, 0.002),
> >                   c("lmp", 0.40, 0.43),
> >                   c("gr_mu", 0.14, 0.16),
> >                   c("gr_sd", 0.01, 0.020),
> >                   c("alpha1", 0.0001, 0.0018),
> >                   c("alpha2", 0.0017, 0.0028),
> >                   c("alpha3", 0.005, 0.009),
> >                   c("beta", 0.69, 0.75)
> > )
> >
> >
> > ### Simulation with priors from LHS ####
> > nb_simul <- 10000
> > nparam <- length(prior_lhs)
> > random_tab = randomLHS(nb_simul, nparam)
> > lhs_index = 1
> > param <- matrix(rep(0, nparam), nrow = 1, ncol = nparam)
> >
> > for (i in 1:nb_simul) {
> >   temp_par <- matrix(rep(0, nparam), nrow = 1, ncol = nparam)
> >   for (j in 1:nparam) {
> >     temp_par[j] = as.numeric(prior_lhs[[j]][2]) +
> >       (as.numeric(prior_lhs[[j]][3]) - as.numeric(prior_lhs[[j]][2])) *
> > random_tab[lhs_index, j]
> >   }
> >   param <- rbind(param, temp_par)
> >   lhs_index <- lhs_index+1
> > }
> > param <- param[-1,]
> >
> > Best regards,
> > Shah
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: LHS random number generator

bertcarnell
In reply to this post by Shah Alam
Shah,

I am the maintainer of the lhs package.  Please feel free to contact
package maintainers directly for help specific to their package.

If I understood your request, this is how I would construct the lhs...

prior_lhs <- data.frame(
  name = c("r_mu", "r_sd", "lmp", "gr_mu", "gr_sd", "alpha1",
"alpha2", "alpha3", "beta"),
  lower = c(0.00299, 0.001, 0.40, 0.14, 0.01, 0.0001, 0.0017, 0.005, 0.69),
  upper = c(0.00320, 0.002, 0.43, 0.16, 0.20, 0.0018, 0.0028, 0.009, 0.75),
  place = c(100000, 1000, 100, 100, 100, 10000, 10000, 1000, 100)
)
require(lhs)
nb_simul <- 10000
nparam <- nrow(prior_lhs)
random_tab <- randomLHS(nb_simul, nparam)
X <- random_tab
for (i in 1:nparam)
{
  X[,i] <- floor(qunif(random_tab[,i],
  prior_lhs$lower[i]*prior_lhs$place[i],
  prior_lhs$upper[i]*prior_lhs$place[i] + 1)) / prior_lhs$place[i]
}
apply(X, 2, range)


> On Apr 12, 2021, at 08:21, Shah Alam <[hidden email]> wrote:
>
> Hello everyone,
>
> I am using the LHS package to generate a combination of a
> set of parameter values. However, I am not sure how to fix some parameter
> values to 2 decimal points.
> For example:
>
> I want to make combinations in such a way that values for parameter
> c("lmp", 0.40, 0.43) are taken as 0.40, 0.41, 0.42,0.43.
>
> My codes are:
>
> prior_lhs <- list(c("r_mu", 0.00299, 0.0032),
>                  c("r_sd", 0.001, 0.002),
>                  c("lmp", 0.40, 0.43),
>                  c("gr_mu", 0.14, 0.16),
>                  c("gr_sd", 0.01, 0.020),
>                  c("alpha1", 0.0001, 0.0018),
>                  c("alpha2", 0.0017, 0.0028),
>                  c("alpha3", 0.005, 0.009),
>                  c("beta", 0.69, 0.75)
> )
>
>
> ### Simulation with priors from LHS ####
> nb_simul <- 10000
> nparam <- length(prior_lhs)
> random_tab = randomLHS(nb_simul, nparam)
> lhs_index = 1
> param <- matrix(rep(0, nparam), nrow = 1, ncol = nparam)
>
> for (i in 1:nb_simul) {
>  temp_par <- matrix(rep(0, nparam), nrow = 1, ncol = nparam)
>  for (j in 1:nparam) {
>    temp_par[j] = as.numeric(prior_lhs[[j]][2]) +
>      (as.numeric(prior_lhs[[j]][3]) - as.numeric(prior_lhs[[j]][2])) *
> random_tab[lhs_index, j]
>  }
>  param <- rbind(param, temp_par)
>  lhs_index <- lhs_index+1
> }
> param <- param[-1,]
>
> Best regards,
> Shah
>
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.