betalu implements the (d/p/q/r)
statistics functions for the beta distribution with support [l,u] (also called the four parameter beta) in R. It is ideal for using in other packages since it is light weight and leverages the (d/p/q/r)beta()
line of functions maintained by CRAN.
Right now, you have to get betalu from GitHub, since it's not on CRAN yet. Here's how you do that:
# install.packages("devtools")
devtools::install_github("dkahle/betalu")
The defining property of the betalu distribution is that it is the beta distribution drawn to a different support. In other words, it is the distribution of the random variable (Y = (u-l)*X + l), where (X) has a beta distribution.
The PDF (the f(x)) can be evaluated with the dbetalu()
function:
library(betalu)
library(ggplot2); theme_set(theme_bw())
x <- seq(-1, 1, .01)
qplot(x, dbetalu(x, 5, 5, -1, 1), geom = "line")
The distribution is the shifted and scaled version of the standard beta distribution with support [0,1], shown in red below:
qplot(x, dbetalu(x, 5, 5, -1, 1), geom = "line") +
stat_function(fun = dbeta, args = list(shape1 = 5, shape2 = 5), color = "red")
The CDF can be evaluated with the pbetalu()
function:
f <- function(x) dbetalu(x, 5, 5, -1, 1)
q <- -0.5
integrate(f, -1, q)
# 0.04892731 with absolute error < 5.4e-16
(p <- pbetalu(q, 5, 5, -1, 1))
# [1] 0.04892731
The quantile function can be evaluated with qbetalu()
:
qbetalu(p, 5, 5, -1, 1) # = q
# [1] -0.5
And random number generation can be performed with rbetalu()
:
set.seed(1)
rbetalu(5, 5, 5, -1, 1)
# [1] -0.22368019 0.06553507 -0.29829009 0.56115084 0.11761102
samples <- rbetalu(1e5, 5, 5, -1, 1)
qplot(samples, bins = 30)
rbetalu()
can be used to obtain a Monte Carlo estimate of the probability given by pbetalu()
above:
mean(samples <= q)
# [1] 0.04853
Moreover, we can check the consistency and correctness of the implementation with
qplot(samples, geom = "density") +
stat_function(fun = f, color = "red")