Skip to content

Commit e28944d

Browse files
committed
Cleaned up the FAQ vignette
1 parent dc7fa0e commit e28944d

File tree

6 files changed

+81
-211
lines changed

6 files changed

+81
-211
lines changed

NAMESPACE

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export(optimumLHS)
2323
export(poly2int)
2424
export(poly_prod)
2525
export(poly_sum)
26-
export(q_dirichlet)
27-
export(q_factor)
28-
export(q_integer)
26+
export(qdirichlet)
27+
export(qfactor)
28+
export(qinteger)
2929
export(randomLHS)
3030
export(runifint)
3131
import(Rcpp)

R/quantile_transforms.R

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
# Copyright 2023 Robert Carnell
1+
# Copyright 2024 Robert Carnell
22

33
#' Quantile Transformations
44
#'
55
#' A collection of functions that transform the margins of a Latin hypercube
6-
#' sample in non-standard ways
6+
#' sample in multiple ways
77
#'
88
#' \code{qdirichlet} is not an exact quantile function since the quantile of a
9-
#' multivariate distribution is not unique. \code{qdirichlet} is also not the independent quantiles of the marginal distributions since
10-
#' those quantiles do not sum to one. \code{qdirichlet} is the quantile of the underlying gamma functions, normalized.
11-
#' This has been tested to show that \code{qdirichlet} approximates the Dirichlet distribution well and creates the correct marginal means and variances
12-
#' when using a Latin hypercube sample
9+
#' multivariate distribution is not unique. \code{qdirichlet} is also not the
10+
#' independent quantiles of the marginal distributions since
11+
#' those quantiles do not sum to one. \code{qdirichlet} is the quantile of the
12+
#' underlying gamma functions, normalized. This is the same procedure that
13+
#' is used to generate random deviates from the Dirichlet distribution therefore
14+
#' it will produce transformed Latin hypercube samples with the intended distribution.
1315
#'
1416
#' \code{q_factor} divides the [0,1] interval into \code{nlevel(fact)} equal sections
1517
#' and assigns values in those sections to the factor level.
@@ -31,11 +33,11 @@
3133
#' X <- randomLHS(20, 7)
3234
#' Y <- as.data.frame(X)
3335
#' Y[,1] <- qnorm(X[,1], 2, 0.5)
34-
#' Y[,2] <- q_factor(X[,2], factor(LETTERS[c(1,3,5,7,8)]))
35-
#' Y[,3] <- q_integer(X[,3], 5, 17)
36-
#' Y[,4:6] <- q_dirichlet(X[,4:6], c(2,3,4))
37-
#' Y[,7] <- q_factor(X[,7], ordered(LETTERS[c(1,3,5,7,8)]))
38-
q_factor <- function(p, fact)
36+
#' Y[,2] <- qfactor(X[,2], factor(LETTERS[c(1,3,5,7,8)]))
37+
#' Y[,3] <- qinteger(X[,3], 5, 17)
38+
#' Y[,4:6] <- qdirichlet(X[,4:6], c(2,3,4))
39+
#' Y[,7] <- qfactor(X[,7], ordered(LETTERS[c(1,3,5,7,8)]))
40+
qfactor <- function(p, fact)
3941
{
4042
if (!is.factor(fact)) {
4143
stop("fact must be a factor or ordered")
@@ -53,7 +55,7 @@ q_factor <- function(p, fact)
5355
#' @rdname quanttrans
5456
#'
5557
#' @export
56-
q_integer <- function(p, a, b)
58+
qinteger <- function(p, a, b)
5759
{
5860
if (!is.numeric(p) | any(p < 0) | any(p > 1)) {
5961
stop("p must be a numeric between 0 and 1")
@@ -74,7 +76,7 @@ q_integer <- function(p, a, b)
7476
#' @importFrom stats qgamma
7577
#'
7678
#' @export
77-
q_dirichlet <- function(X, alpha)
79+
qdirichlet <- function(X, alpha)
7880
{
7981
lena <- length(alpha)
8082
if (!is.matrix(X) & !is.data.frame(X)) {

man/quanttrans.Rd

+20-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-quantile_transforms.R

+20-20
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,65 @@
22

33
context("test-quantile_transformations")
44

5-
test_that("q_factor works", {
5+
test_that("qfactor works", {
66
p <- randomLHS(n=5, k=1)
77
fact <- factor(LETTERS[1:4])
8-
res <- q_factor(p, fact)
8+
res <- qfactor(p, fact)
99
expect_true(all(levels(res) %in% levels(fact)))
1010
expect_true(all(fact[floor(p[,1]*nlevels(fact)) + 1] == res))
1111

1212
p <- randomLHS(n=5, k=1)
1313
fact <- factor(LETTERS[1:4], levels = LETTERS[4:1], ordered = TRUE)
14-
res <- q_factor(p, fact)
14+
res <- qfactor(p, fact)
1515
expect_true(all(levels(res) %in% levels(fact)))
1616
expect_true(all(levels(fact)[floor(p[,1]*nlevels(fact)) + 1] == as.character(res)))
1717

1818
p <- randomLHS(n=25, k=1)
1919
fact <- factor(LETTERS[1:5])
20-
res <- q_factor(p, fact)
20+
res <- qfactor(p, fact)
2121
expect_true(all(levels(res) %in% levels(fact)))
2222
expect_true(all(fact[floor(p[,1]*nlevels(fact)) + 1] == res))
2323
expect_equivalent(rep(5, 5), c(table(res)))
2424

2525
p <- randomLHS(n=25, k=1)
2626
fact <- ordered(LETTERS[1:5])
27-
res <- q_factor(p, fact)
27+
res <- qfactor(p, fact)
2828
expect_true(all(levels(res) %in% levels(fact)))
2929
expect_true(all(levels(fact)[floor(p[,1]*nlevels(fact)) + 1] == as.character(res)))
3030
expect_equivalent(rep(5, 5), c(table(res)))
3131

32-
expect_error(q_factor("a", factor("a")))
33-
expect_error(q_factor(c(0.1, 0.2), "a"))
34-
expect_error(q_factor(1.1, factor("a")))
35-
expect_error(q_factor(-3, factor("a")))
32+
expect_error(qfactor("a", factor("a")))
33+
expect_error(qfactor(c(0.1, 0.2), "a"))
34+
expect_error(qfactor(1.1, factor("a")))
35+
expect_error(qfactor(-3, factor("a")))
3636
})
3737

38-
test_that("q_integer works", {
38+
test_that("qinteger works", {
3939
p <- randomLHS(n = 25, k = 1)
40-
res <- q_integer(p, 6, 12)
40+
res <- qinteger(p, 6, 12)
4141
expect_equal(6, min(res))
4242
expect_equal(12, max(res))
4343
expect_true(all(res %in% 6:12))
4444

4545
p <- randomLHS(n = 25, k = 1)
46-
res <- q_integer(p, -4L, 2L)
46+
res <- qinteger(p, -4L, 2L)
4747
expect_equal(-4, min(res))
4848
expect_equal(2, max(res))
4949
expect_true(all(res %in% -4:2))
5050

51-
expect_error(q_integer("a", 1, 5))
52-
expect_error(q_integer(c(0.1, 0.2), 1.1, 5))
53-
expect_error(q_integer(c(0.1, 0.2), 1, 5.2))
54-
expect_error(q_integer(c(0.1, 0.2), 8, 5))
55-
expect_error(q_integer(1.1, factor("a")))
56-
expect_error(q_integer(-3, factor("a")))
51+
expect_error(qinteger("a", 1, 5))
52+
expect_error(qinteger(c(0.1, 0.2), 1.1, 5))
53+
expect_error(qinteger(c(0.1, 0.2), 1, 5.2))
54+
expect_error(qinteger(c(0.1, 0.2), 8, 5))
55+
expect_error(qinteger(1.1, factor("a")))
56+
expect_error(qinteger(-3, factor("a")))
5757
})
5858

59-
test_that("q_dirichlet works", {
59+
test_that("qdirichlet works", {
6060
set.seed(19753)
6161
X <- randomLHS(500, 5)
6262
Y <- X
63-
Y[,1:3] <- q_dirichlet(X[,1:3], rep(2,3))
63+
Y[,1:3] <- qdirichlet(X[,1:3], rep(2,3))
6464
Y[,4] <- qnorm(X[,4], 2, 1)
6565
Y[,5] <- qunif(X[,5], 1, 3)
6666

vignettes/correlated_lhs.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ apply(lhs_B$transformed_lhs, 2, mean) # close to 4/10, 3/10, 2/10, 1/10
105105
### Method 2: `q_dirichlet`
106106

107107
```{r}
108-
lhs_B <- lhs::q_dirichlet(lhs::randomLHS(30, 4), c(4,3,2,1))
108+
lhs_B <- lhs::qdirichlet(lhs::randomLHS(30, 4), c(4,3,2,1))
109109
```
110110

111111
Check properties

0 commit comments

Comments
 (0)