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]]
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 <dr.alamsolangi at gmail.com> 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]] > > ______________________________________________ > R-help at r-project.org 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.
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 <dr.alamsolangi at gmail.com> 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]] > > ______________________________________________ > R-help at r-project.org 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.