Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up pbetaMix and qbetaMix #9 #10

Merged
merged 53 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
50ef5b1
cleaning up dbetabinom
audreyyeoCH Aug 14, 2023
4342b5a
removed rounding, not needed
audreyyeoCH Aug 14, 2023
b095ae6
rmarkdown language and assert for dbetabinom
audreyyeoCH Aug 22, 2023
889e97d
changed x and m to number instead of numeric
audreyyeoCH Aug 22, 2023
2eafdc5
test
audreyyeoCH Aug 22, 2023
9dc428b
pbetaMix function and test
audreyyeoCH Aug 23, 2023
dd1d8c4
added new tests and changed documentation
audreyyeoCH Aug 24, 2023
6d7f478
upper case for test_that comments and more tests for qbeta
audreyyeoCH Aug 24, 2023
d520ff0
Merge branch 'main' into 9_pbeta
audreyyeoCH Aug 25, 2023
0b361e3
test_thats for pbetaMix and qbetaMix
audreyyeoCH Aug 27, 2023
8b20f72
tidy syntax and last checks
audreyyeoCH Aug 28, 2023
b0c2218
tidy syntax and last checks
audreyyeoCH Aug 28, 2023
9f53521
Merge remote-tracking branch 'origin/9_pbeta' into 9_pbeta
audreyyeoCH Aug 28, 2023
5384c3d
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
f5403fa
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Aug 28, 2023
0dd2bab
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
ca0236f
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
9cf54d0
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
2eb3bd2
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
c08435f
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
e36facd
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
f45d2f0
Update R/dbetabinom.R
audreyyeoCH Aug 28, 2023
0505982
Update examples/pbetaMix.R
audreyyeoCH Aug 28, 2023
469ed6e
p and q inputs
audreyyeoCH Aug 29, 2023
1abea50
q,p changes
audreyyeoCH Aug 29, 2023
b7b523c
Update R/dbetabinom.R
audreyyeoCH Aug 29, 2023
34a663b
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Aug 29, 2023
f384100
documentation to reflect p and q changes
audreyyeoCH Aug 29, 2023
f0d5d00
Merge remote-tracking branch 'origin/9_pbeta' into 9_pbeta
audreyyeoCH Aug 29, 2023
b9e50b2
amending qt
audreyyeoCH Aug 29, 2023
0ab047d
intro x-> q
audreyyeoCH Aug 29, 2023
4164b46
amented postprob file
audreyyeoCH Aug 29, 2023
5d98627
amended postprob file
audreyyeoCH Aug 29, 2023
205e574
fixed mistake on x
audreyyeoCH Aug 29, 2023
01bd1bf
Merge remote-tracking branch 'origin/9_pbeta' into 9_pbeta
audreyyeoCH Aug 29, 2023
2d093ba
qbetaMix was in postprobDist
audreyyeoCH Aug 29, 2023
c7dbbf9
trying to find R CMD check error
audreyyeoCH Aug 30, 2023
640f9f3
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Aug 30, 2023
3f01c97
manual of ocPostprobDist
audreyyeoCH Aug 30, 2023
99d6eb5
Merge remote-tracking branch 'origin/9_pbeta' into 9_pbeta
audreyyeoCH Aug 30, 2023
cc069c3
fixing roxygen type to p
audreyyeoCH Aug 30, 2023
626345c
see if this fixes the CMD check
audreyyeoCH Aug 30, 2023
3882982
see if fixes CMD check error
audreyyeoCH Aug 30, 2023
f6c7df2
xi removed
audreyyeoCH Aug 30, 2023
80eee89
example post prob works
audreyyeoCH Aug 30, 2023
1d115b4
clean
audreyyeoCH Aug 30, 2023
2185dea
Merge branch 'tmp' into 9_pbeta
audreyyeoCH Aug 30, 2023
5bb074e
assert_number
audreyyeoCH Aug 30, 2023
9d4b62c
in postprobDist, I changed p-> q
audreyyeoCH Aug 30, 2023
db8fb65
change p to q
audreyyeoCH Aug 30, 2023
51ed0c9
Merge remote-tracking branch 'origin/9_pbeta' into 9_pbeta
audreyyeoCH Aug 30, 2023
ac40906
change q to p
audreyyeoCH Aug 30, 2023
1a03d33
fix postprobDist (for now)
Aug 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions R/dbetabinom.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,44 +147,67 @@ dbetaMix <- Vectorize(dbetaMix, vectorize.args = "x")

#' Beta-mixture cdf
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#'
#' Note that `x` can be a vector.
#' @description `r lifecycle::badge("experimental")`
#'
#' @param x the abscissa.
#' @param par the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components.
#' @param weights the mixture weights of the beta mixture prior.
#' @param lower.tail logical; if TRUE (default), probabilities are `P[X <= x]`,
#' and otherwise `P[X > x]`.
#' @return the (one minus) cdf value.
#' Calculates the cdf of the beta-mixture
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @typed x : number
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#' the abscissa.
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#' @typed par : matrix
#' the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components.
#' @typed weights : matrix
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#' the mixture weights of the beta mixture prior.
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#' @typed lower.tail : logical # TODO Why not flag
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#' if TRUE (default), probabilities are `P[X <= x]`,
danielinteractive marked this conversation as resolved.
Show resolved Hide resolved
#' and otherwise `P[X > x]`.
#' @return the (one minus) cdf value # TODO DO WE NEED THIS return and where is the "1-".
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @note `x` can be a vector.
#'
#' @example examples/pbetaMix.R
#' @export
pbetaMix <- function(x, par, weights, lower.tail = TRUE) {
assert_numeric(x, lower = 0, finite = TRUE)
danielinteractive marked this conversation as resolved.
Show resolved Hide resolved
assert_numeric(weights, lower = 0, upper = 1, finite = TRUE)
assert_matrix(par)
assert_flag(lower.tail)
ret <- sum(weights * pbeta(x, par[, 1], par[, 2], lower.tail = lower.tail))
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
return(ret)
ret
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
}
pbetaMix <- Vectorize(pbetaMix, vectorize.args = "x")


#' Beta-mixture quantile function
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#'
#' Note that `x` can be a vector.
#' @description `r lifecycle::badge("experimental")`
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#'
#' Calculates the quantile where x support is at the intersection of cdf and quantile function
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @param q the required quantile.
#' @param par the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components.
#' @param weights the mixture weights of the beta mixture prior.
#' @typed q : numeric
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#' the required quantile.
danielinteractive marked this conversation as resolved.
Show resolved Hide resolved
#' @typed par : number
#' the beta parameters matrix, with K rows and 2 columns,
#' corresponding to the beta parameters of the K components.
#' @typed weights : matrix
#' the mixture weights of the beta mixture prior.
#' @typed lower.tail : flag
#' whether cdf at x taken at lower or upper tail
#' @return the abscissa.
#'
#' @example examples/qbetaMix.R
#' @export
qbetaMix <- function(q, par, weights) {
qbetaMix <- function(q, par, weights, lower.tail) {
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
f <- function(pi) {
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
assert_numeric(pi, lower = 0, finite = TRUE)
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
pbetaMix(x = pi, par = par, weights = weights) - q
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
}
unirootResult <- uniroot(f, lower = 0, upper = 1)
assert_number(unirootResult$f.root)
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
if (unirootResult$iter < 0) {
return(NA)
NA
} else {
return(unirootResult$root)
unirootResult$root
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
}
}
qbetaMix <- Vectorize(qbetaMix, vectorize.args = "q")
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 1 addition & 17 deletions examples/pbetaMix.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
## Calculating the CDF of a mixture
## of beta densities at x, x = 0.3; a = 0.2; b = 0.4
##
##
## Only 1 mixture component, i.e., weights = 1
## Compare to pbeta(0.3,0.2,0.4) = 0.5947341
##
pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1)

## Can get the one minus CDF values
## Need to specify lower.tail = FALSE, 1 - 0.5947341 = 0.4052659
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
##
##
pbetaMix(x = 0.3, par = rbind(c(0.2, 0.4)), weights = 1, lower.tail = FALSE)

## With 2 mixture components
## Weight 0.6 for component 1; a = 0.2, b = 0.4
## Weight 0.4 for component 2; a = 1.0, b = 1.0
## Compare to 0.6*pbeta(0.3,0.2,0.4) + 0.4*pbeta(0.3,1,1) = 0.4768404
##
pbetaMix(
x = 0.3, par = rbind(c(0.2, 0.4), c(1, 1)),
weights = c(0.6, 0.4)
)

## Can also specify x as a vector, x = seq(0,1,.01)
##
##
## Can also specify x as a vector.
pbetaMix(
x = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)),
weights = c(0.6, 0.4)
Expand Down
22 changes: 8 additions & 14 deletions examples/qbetaMix.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
## Calculating the quantile (inverse CDF) of a mixture
## of beta densities at x where q = 0.6; a = 0.2; b = 0.4
##
##
## Only 1 mixture component, i.e., weights = 1
## Compare to qbeta(0.6,0.2,0.4) = 0.3112065
##
qbetaMix(q = 0.60, par = rbind(c(0.2, 0.4)), weights = 1)
qbetaMix(
q = 0.60,
par = rbind(c(0.2, 0.4)),
weights = 1
)

## With 2 mixture components
## Weight 0.6 for component 1; a = 0.2, b = 0.4
## Weight 0.4 for component 2; a = 1.0, b = 1.0
##
qbetaMix(
q = 0.6, par = rbind(c(0.2, 0.4), c(1, 1)),
weights = c(0.6, 0.4)
)

## Can also specify q as a vector : q = seq(0,1,.01)
##
##
## Can also specify q as a vector
qbetaMix(
q = seq(0, 1, .01), par = rbind(c(0.2, 0.4), c(1, 1)),
q = seq(0, 1, .01),
par = rbind(c(0.2, 0.4), c(1, 1)),
weights = c(0.6, 0.4)
)
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -3239,6 +3239,7 @@ resizable
ResourceRef
responder
responders
ret
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
rf
rG
RG
Expand Down
35 changes: 29 additions & 6 deletions man/pbetaMix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 31 additions & 5 deletions man/qbetaMix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions tests/testthat/test-dbetabinom.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,73 @@ test_that("Beta mixture density has the correct numeric result", {
)
expect_equal(result, 0.03905469, tolerance = 1e-6)
})

## pbetaMix ----

test_that("The pbetaMix has incrementally higher cdf with increase x support", {
is_lower <- pbetaMix(
x = 0.3,
par = rbind(c(0.2, 0.4)),
weights = 1
)
is_higher <- pbetaMix(
x = 0.5,
par = rbind(c(0.2, 0.4)),
weights = 1
)
expect_true(is_lower < is_higher)
})

test_that("The pbetaMix has the correct number result", {
result <- pbetaMix(
x = 0.3,
par = rbind(c(0.2, 0.4), c(1, 1)),
weights = c(0.6, 0.4)
)
expect_equal(result, 0.4768404, tolerance = 1e-5)
})

test_that("The complement of pbetaMix can be derived with a different lower.tail flag", {
result <- pbetaMix(
x = 0.3,
par = rbind(c(0.2, 0.4)),
weights = 1,
lower.tail = FALSE
)
result_inversed <- pbetaMix(
x = 0.3,
par = rbind(c(0.2, 0.4)),
weights = 1,
lower.tail = TRUE
)
expect_equal(result, 1 - result_inversed, tolerance = 1e-5)
})

## qbetaMix ----

test_that("The qbetaMix has the correct number result", {
result <- qbetaMix(
q = 0.6,
par = rbind(c(0.2, 0.4)),
weights = 1
)
expect_equal(result, 0.3112068, tolerance = 1e-6)
})

test_that("The qbetaMix has the correct number result", {
result <- qbetaMix(
q = 0.6,
par = rbind(c(0.2, 0.4), c(1, 1)),
weights = c(0.6, 0.4)
)
expect_equal(result, 0.488759, tolerance = 1e-6)
})

test_that("The qbetaMix has a number result", {
result <- qbetaMix(
q = seq(0, 1, .01),
par = rbind(c(0.2, 0.4), c(1, 1)),
weights = c(0.6, 0.4)
)
expect_numeric(result)
})
Loading