4

Is there a built-in function to calculate a four parameter beta distribution in R? I.e., the distribution with two shape parameters and two boundary parameters so that it's not bounded [0,1]?

I made my own but curious if this functionality exists already. No need to reinvent the wheel.

shp1 <- 20
shp2 <- 5
X <- seq(0,1,length.out = 100)
Y <- dbeta(x = X,shape1 = shp1, shape2 = shp2)
plot(X,Y)
# scaled between two boundaries
dbeta4param <- function(x,shp1,shp2,bnd1,bnd2){
  mask = (x>=bnd1 & x<=bnd2)
  xScale = (x-bnd1)/(bnd2-bnd1)
  mask * dbeta(xScale,shape1=shp1,shape2=shp2)/(bnd2-bnd1)
}
bnd1 <- 2
bnd2 <- 4
X2 <- seq(bnd1,bnd2,length.out = 100)
Y2 <- dbeta4param(x=X2, shp1 = shp1, shp2 = shp2, bnd1 = bnd1, bnd2 = bnd2)
plot(X2,Y2)
user111024
  • 723
  • 3
  • 15

1 Answers1

5

CRAN Task View: Probability Distributions is always a good resource about where to find various R probability distributions. According to it, you should use the extraDistr package (extraDistr::rnsbeta for example).

HFBrowning
  • 2,196
  • 3
  • 23
  • 42
  • Indeed. I was looking for the generalization as @Onyambu commented. I called the special function beta() to make my own function: `dbeta4param <- function(x,shp1,shp2,bnd1,bnd2){ ((x-bnd1)^(shp1-1) * (bnd2 - x)^(shp2-1)) / ((bnd2 - bnd1)^(shp1+shp2-1) * beta(shp1,shp2)) }` . And indeed `extraDistr::rnsbeta` looks quite similar and does what I want. Thanks. – user111024 Mar 21 '18 at 22:12