R 中是否有内置函数可以计算四参数 beta 分布?即,具有两个形状参数和两个边界参数的分布,因此它不受 [0,1] 的限制?
我自己做了一个,但很好奇这个功能是否已经存在。无需重新发明轮子。
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)
Run Code Online (Sandbox Code Playgroud) 这看起来微不足道。我如何在这里使用pivot_longer代替melt:
foo <- matrix(1:9,3,3)
reshape2::melt(foo,varnames = c("x","y"),value.name = "z")
Run Code Online (Sandbox Code Playgroud)