Skip to content

Commit

Permalink
version 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bertcarnell authored and cran-robot committed Jul 1, 2024
1 parent a0b6f74 commit 19dc03d
Show file tree
Hide file tree
Showing 40 changed files with 3,170 additions and 2,185 deletions.
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: lhs
Title: Latin Hypercube Samples
Version: 1.1.6
Version: 1.2.0
Authors@R:
person(given = "Rob",
family = "Carnell",
Expand All @@ -12,14 +12,14 @@ Encoding: UTF-8
Depends: R (>= 3.4.0)
LinkingTo: Rcpp
Imports: Rcpp
Suggests: testthat, assertthat, DoE.base, knitr, rmarkdown
Suggests: testthat, DoE.base, knitr, rmarkdown
URL: https://github.com/bertcarnell/lhs
BugReports: https://github.com/bertcarnell/lhs/issues
RoxygenNote: 7.2.2
RoxygenNote: 7.3.2
VignetteBuilder: knitr
NeedsCompilation: yes
Packaged: 2022-12-17 19:41:08 UTC; bertc
Packaged: 2024-06-30 21:59:33 UTC; bertc
Author: Rob Carnell [aut, cre]
Maintainer: Rob Carnell <bertcarnell@gmail.com>
Repository: CRAN
Date/Publication: 2022-12-17 21:30:02 UTC
Date/Publication: 2024-06-30 23:10:02 UTC
68 changes: 39 additions & 29 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
f85d7ad91e5678df6269ab3fe3cd2fb5 *ChangeLog
2834f77c3ef7e7c7923b45caf9ff0b03 *DESCRIPTION
4ad42b6ea879a1ef4e8ca522e4ada103 *NAMESPACE
7682a7a4a5bf002eba4e023c8e1424fb *NEWS
62af58443b1ed159fd4dbf3b8a0cc8d0 *DESCRIPTION
4ed030018f031be62f46acbce7374459 *NAMESPACE
0678a6de0708199f921c87acbdfced58 *NEWS
53866b3921acf88d621f1b67d6bf5517 *R/augmentLHS.R
851e4e4b32ef0d1ff208e28d2469abfe *R/createOA.R
89492c484e87dcaa511bcd82bcafd870 *R/correlatedLHS.R
aa739eede2d19e93c67c4db832efc1c5 *R/createOA.R
e0aa8aefc35430288bf424573158dcfb *R/create_oalhs.R
b13a12e4abeb22949dee39121869939d *R/galois_field.R
ee054a9159cb67016025e58156c55044 *R/geneticLHS.R
Expand All @@ -15,27 +16,32 @@ fdb66b50d5d88b352e6989aa64d8d830 *R/maximinLHS.R
6ba4bc2876c5d5f1b2ae33186299aa6f *R/optAugmentLHS.R
d3569b956d2f4c5e30ac0e9284416c5e *R/optSeededLHS.R
b6508e5d04a13558e19c6534c768e2a9 *R/optimumLHS.R
601e0117b05e982b7c9a878f244e18e0 *R/quantile_transforms.R
28fe6e3db4a011dd782118c1574ceb9d *R/randomLHS.r
6f6a5d5f3f2e20c0fe869bfe603e0185 *R/runifint.r
56e6490634dd13cf2c388695e4de3878 *build/vignette.rds
c77ba8ff0a839cf9188ee61c77d37f1a *build/vignette.rds
c40fe474107a72a524c98cd5d077c125 *inst/doc/augment_lhs.R
2ea4584a4f8f5710e9e845e365d054ea *inst/doc/augment_lhs.Rmd
ac08a4276cafdafb5268dab36cdb007b *inst/doc/augment_lhs.html
cb576800238834f4f34527a2af27b28e *inst/doc/augment_lhs.html
ce027d1b820eebc79f3fc3a62ca6442b *inst/doc/correlated_lhs.R
c5932d5dece75c9a75a7af99bf83368f *inst/doc/correlated_lhs.Rmd
4c66ff03f6796043307ca3bcefe06a9a *inst/doc/correlated_lhs.html
c3dd307ccf782db9eb75f1404cf37fa1 *inst/doc/lhs_basics.R
c48d7934b6c01da2179998d83c78ac05 *inst/doc/lhs_basics.Rmd
c97e76e9d3071d61d708f1442572b78e *inst/doc/lhs_basics.html
21ae80800268e1193314907b21c47fd5 *inst/doc/lhs_faq.R
a098b867dd78ec2eb6963c40d7ba98ae *inst/doc/lhs_faq.Rmd
e4b9622aade7a84fc154c19b5c01f3f1 *inst/doc/lhs_faq.html
4b763a562f6a9baba6e945f8d2f0b002 *inst/doc/lhs_basics.html
96e9e0d002874232f9458386c38d85c3 *inst/doc/lhs_faq.R
c08df90f722ffd24fa2d4df16d2dee50 *inst/doc/lhs_faq.Rmd
5944cbfd5506f72acf172c1847e4ab16 *inst/doc/lhs_faq.html
385a89b5cd29ef880745c295821f72fa *man/augmentLHS.Rd
d10b89e1edef0cdb6573410491331111 *man/createAddelKemp.Rd
d93652e2d6e052941cef8e5570b5c9bf *man/createAddelKemp3.Rd
33a7e19bd547dccf1f27548b8f6f6062 *man/correlatedLHS.Rd
7b6968145fa99250e5417be9b22edf0d *man/createAddelKemp.Rd
4806482382403d66d025c050c04bb127 *man/createAddelKemp3.Rd
9c64475ba6ca348a2c5f8c36822a366b *man/createAddelKempN.Rd
ed72fa68c7d3e584fa2b80fa71d85bc0 *man/createBose.Rd
6e32bddc752e147cecb5f6a47db72fae *man/createBoseBush.Rd
4e272403221932985d0aac3e2521b617 *man/createBoseBushl.Rd
15dc9afb06907b23657f3eebdb4fd567 *man/createBush.Rd
83f303068e111a84a1ecdf1576a2991a *man/createBusht.Rd
846b4f87068e63a15c433b9564fa90fa *man/createBose.Rd
f72d6ddbfcefb07ae2ba08a46a91e846 *man/createBoseBush.Rd
e125dc387711a92a0c03fc13bb708173 *man/createBoseBushl.Rd
58c37e6cc6b78f73f9bca4f9e9cd4841 *man/createBush.Rd
40113363ae1df9a0296d2884e12e216e *man/createBusht.Rd
38387d12556c313404d634691e62dbd8 *man/create_galois_field.Rd
6099e0cdab76518961760e5b7ba376bb *man/create_oalhs.Rd
d6530dd864441ec8150f21105202585f *man/geneticLHS.Rd
Expand All @@ -50,6 +56,7 @@ cd434d0087818e7a7181d53b24d60280 *man/get_library_versions.Rd
647d3e752fefe10507ee4451b00bea62 *man/poly2int.Rd
821b9245eb2944e0804502895ef4ca62 *man/poly_prod.Rd
61ee05edb765e3ab2cb4e01583300f4c *man/poly_sum.Rd
84d5d237aabc7e3793aa30ccffc0afa3 *man/quanttrans.Rd
a159b822a2135b86a0f29ab62fbb8a74 *man/randomLHS.Rd
17181180f6a19291e342196042bf6e3f *man/runifint.Rd
f649001939ea771545339996d0bf614f *src/COrthogonalArray.cpp
Expand All @@ -58,9 +65,9 @@ f649001939ea771545339996d0bf614f *src/COrthogonalArray.cpp
66ad54cb00e04bda7bd043c888fb8094 *src/GaloisField.cpp
92dc5772d370e69822ec1db2035eb6cc *src/GaloisField.h
37769f1ff1637a4cb937ae9720561c2c *src/LHSCommonDefines.h
f638d0697d0c1eb3fe1afedfdd157c15 *src/Makevars
f3268dbce99fd476879b3695151fbb72 *src/Makevars.win
b7e05dd7cf58418b0be2c70f8412c233 *src/OACommonDefines.h
94f8191496cca73e709c465d323889a4 *src/Makevars
94f8191496cca73e709c465d323889a4 *src/Makevars.win
f2e8c65df356f45fd2c5fb44c78d61bc *src/OACommonDefines.h
dd1ee7c05925ea002a3177907d472373 *src/RStandardUniform.h
72cfdd82c1f1edd241169ad8404557f9 *src/ak.h
364d8874ce60d9cc5f9522c56592aa3d *src/ak3.cpp
Expand Down Expand Up @@ -97,29 +104,32 @@ edbce6a4953d22430c00f5c163af41b2 *src/optSeededLHS.cpp
87c5d2c644299dcf7797cbf412fb3421 *src/primes.cpp
3cbd87dbf024f0f2ba7bb9651ffbc424 *src/primes.h
2d00c5e4034621e2afffa47ccb044e08 *src/randomLHS.cpp
c86aac55d6f5d4e87f915d2c072ca915 *src/runif.cpp
2b9f7d7f9861e9fb2400963901bdf7b5 *src/runif.h
26a0b7412291e79921b093608db28de3 *src/runif.cpp
3f03595049a5e1bdc345b4e5fff505f4 *src/runif.h
2a58c2e95481fe9f2d175db2bfc37947 *src/rutils.cpp
bea92a5cc44b9602eedc5755adcb5e98 *src/rutils.h
afc1a761a5f5bb4b5db9ab34916ab507 *src/utilityLHS.cpp
4e63bafed4e840585c42c6d4bb49379e *src/utilityLHS.h
a9bf6acf833600dc23ea431f7cb8e97e *src/xtn.h
61a8d6137ec622105090092597f674f1 *tests/testthat.R
fd5d335eac06aec0e329285f5078006e *tests/testthat/helper-lhs.R
865a2e5b7e26745b142ca68350d7188f *tests/testthat.R
da6651eaf210fa98c427071829012e18 *tests/testthat/helper-lhs.R
2c7d56d1b00eda9f385a6ed433dcdc24 *tests/testthat/test-augmentlhs.R
b4fb47fe8ccfbc0beddaa2a7e0f5bdcd *tests/testthat/test-correlatedLHS.R
fe803b4ea05122c1f33700d69f4a4f42 *tests/testthat/test-create_oalhs.R
a5464d4c7547b57abc92effb9b759a87 *tests/testthat/test-createoa.R
1d3eeffbf5f5fcb720a7bf51e7151e62 *tests/testthat/test-galois_field.R
7cc11e06c2df3b7264e0fdbf260a0dd1 *tests/testthat/test-createoa.R
c287deed72e875c904ec4e99fbdcfcea *tests/testthat/test-galois_field.R
15105804ea6ef87a0d84eea60d8f94fd *tests/testthat/test-geneticlhs.R
5f5f6cae1a495855aefb68f51a254be2 *tests/testthat/test-get_library_versions.R
e48c9878ba49cecbade584e08d315db1 *tests/testthat/test-get_library_versions.R
505ec17330211656bc054d0f057b2968 *tests/testthat/test-improvedlhs.r
abe052846c141cde46a81566e8ca5199 *tests/testthat/test-maximinlhs.R
11321200de36a73acabbee6cf2b1da41 *tests/testthat/test-oa_to_oalhs.R
5463fe0a9c89f0f762a89cbd461f57d0 *tests/testthat/test-optaugmentlhs.R
f810d8f8b902a3398eb7d300701a4296 *tests/testthat/test-optimumlhs.R
e0518a44b176a6478581f36e770c3435 *tests/testthat/test-optseededlhs.R
1dfdaa44c78fdaca863983c32b1d804a *tests/testthat/test-randomlhs.r
08cd8f9ff50ebba27865bd541b4476a4 *tests/testthat/test-quantile_transforms.R
96ab06789300bcfe9a875b3fda47b218 *tests/testthat/test-randomlhs.r
8f248fe4625ca971df2ab8b8f503fc6d *vignettes/VignetteCommonCode.R
2ea4584a4f8f5710e9e845e365d054ea *vignettes/augment_lhs.Rmd
c5932d5dece75c9a75a7af99bf83368f *vignettes/correlated_lhs.Rmd
c48d7934b6c01da2179998d83c78ac05 *vignettes/lhs_basics.Rmd
a098b867dd78ec2eb6963c40d7ba98ae *vignettes/lhs_faq.Rmd
c08df90f722ffd24fa2d4df16d2dee50 *vignettes/lhs_faq.Rmd
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(augmentLHS)
export(correlatedLHS)
export(createAddelKemp)
export(createAddelKemp3)
export(createAddelKempN)
Expand All @@ -22,12 +23,16 @@ export(optimumLHS)
export(poly2int)
export(poly_prod)
export(poly_sum)
export(qdirichlet)
export(qfactor)
export(qinteger)
export(randomLHS)
export(runifint)
import(Rcpp)
importFrom(stats,dist)
importFrom(stats,na.exclude)
importFrom(stats,na.omit)
importFrom(stats,qgamma)
importFrom(stats,runif)
importFrom(utils,packageVersion)
useDynLib(lhs)
12 changes: 12 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,15 @@ Changes in version 1.1.5 (2022-03-22)

- std::iterator, std::binary_function, and std::unary_funcation were removed since they are deprecated in c++17 and later

Changes in version 1.1.6 (2022-12-17)

- Bug fix for a warning on CRAN "init.c:14:33: warning: a function declaration without a prototype is deprecated in all versions of C [-Wstrict-prototypes]"

Changes in version 1.1.7 (2023-06-30)

- Added correlated LHS samples and new quantile functions

Changes in version 1.2.0 (2023-06-30)

- Updates for CRAN checks

122 changes: 122 additions & 0 deletions R/correlatedLHS.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#' Transformed Latin hypercube with a multivariate distribution
#'
#' @description
#' A method to create a transformed Latin Hypercube sample where the marginal
#' distributions can be correlated according to an arbitrary set of criteria
#' contained in a minimized cost function
#'
#' @param lhs a Latin hypercube sample that is uniformly distributed on the
#' margins
#' @param marginal_transform_function a function that takes Latin hypercube sample
#' as the first argument and other passed-through variables as desired. \code{...} must
#' be passed as a argument. For example, \code{f <- function(W, second_argument, ...)}.
#' Must return a \code{matrix} or \code{data.frame}
#' @param cost_function a function that takes a transformed Latin hypercube sample
#' as the first argument and other passed-through variables as desired. \code{...} must
#' be passed as a argument. For example, \code{f <- function(W, second_argument, ...)}
#' @param debug Should debug messages be printed. Causes cost function output
#' and iterations to be printed to aid in setting the maximum number of iterations
#' @param maxiter the maximum number of iterations. The algorithm proceeds by
#' swapping one variable of two points at a time. Each swap is an iteration.
#' @param ... Additional arguments to be passed through to the \code{marginal_transform_function}
#' and \code{cost_function}
#'
#' @return a list of the Latin hypercube with uniform margins, the transformed
#' Latin hypercube, and the final cost
#' @export
#'
#' @examples
#' correlatedLHS(lhs::randomLHS(30, 2),
#' marginal_transform_function = function(W, ...) {
#' W[,1] <- qnorm(W[,1], 1, 3)
#' W[,2] <- qexp(W[,2], 2)
#' return(W)
#' },
#' cost_function = function(W, ...) {
#' (cor(W[,1], W[,2]) - 0.5)^2
#' },
#' debug = FALSE,
#' maxiter = 1000)
correlatedLHS <- function(lhs, marginal_transform_function,
cost_function, debug=FALSE, maxiter=10000,
...) {
Nlhs <- nrow(lhs)
k <- ncol(lhs)

## Initial transform and cost
lhs_t <- marginal_transform_function(lhs, ...)
if (!is.matrix(lhs_t) & !is.data.frame(lhs_t)) {
stop("The marginal_transform_function should return a matrix or data.frame")
}
if (!all(dim(lhs) == dim(lhs_t))) {
stop("resulting design after transformation with marginal_transform_function must be the same dimension as the original design")
}

initial_cost <- cost_function(lhs_t, ...)
if (!is.numeric(initial_cost) | length(initial_cost) != 1) {
stop("cost function must return a single cost as a numeric")
}

if (debug) {
cat(paste0("\nInitial cost: ", initial_cost, "\n"))
}

best_cost <- initial_cost

swap <- function(i1, i2, j1, j2, dat) {
temp <- dat[i1, j1]
dat[i1, j1] <- dat[i2, j2]
dat[i2, j2] <- temp
return(dat)
}

iter <- 1
recent_improvement <- TRUE
while (best_cost > 0 & iter < maxiter & recent_improvement) {
recent_improvement <- FALSE
if (debug && iter > 1) {
cat(paste0("Iteration ", iter, " with cost: ", best_cost, "\n"))
}
for (j in 1:k) {
if (debug) {
cat(paste0("\tSwapping on Variable ", j, " of ", k, " on Iteration ", iter, "\n"))
}
for (i1 in 1:(Nlhs-1)) {
for (i2 in i1:Nlhs) {
if (best_cost > 0) {
lhs <- swap(i1, i2, j, j, lhs)
lhs_t <- marginal_transform_function(lhs, ...)
cost <- cost_function(lhs_t, ...)
if (cost >= best_cost) {
# swap back
lhs <- swap(i2, i1, j, j, lhs)
} else {
# stay here and update best
best_cost <- cost
recent_improvement <- TRUE
break
}
iter <- iter + 1
if (best_cost == 0 | iter > maxiter) break
}
}
if (best_cost == 0 | iter > maxiter) break
}
if (best_cost == 0 | iter > maxiter) break
}
}

if (debug) {
cat(paste0("Ending at cost: ", best_cost, "\n"))
if (!all(apply(lhs, 2, function(x) sum(floor(x*Nlhs)+1)) == Nlhs*(Nlhs+1)/2)) {
stop("Latin hypercube not created in swapping process")
}
if (!all(lhs > 0 & lhs < 1)) {
stop("Latin hypercube sample outside bounds (0,1)")
}
}

return(list(lhs = lhs,
transformed_lhs = marginal_transform_function(lhs, ...), # the transform might not have been after swapping back
cost = best_cost))
}
Loading

0 comments on commit 19dc03d

Please sign in to comment.