Skip to content

Commit

Permalink
version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Atchanut authored and cran-robot committed May 11, 2024
0 parents commit b3198d5
Show file tree
Hide file tree
Showing 21 changed files with 471 additions and 0 deletions.
21 changes: 21 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Package: tpwb
Type: Package
Title: The Three Parameter Weibull Distribution
Version: 0.1.0
Authors@R: c(person("Atchanut","Rattanalertnusorn",role=c("aut","cre"),
email="atchanut_r@rmutt.ac.th"))
Maintainer: Atchanut Rattanalertnusorn <atchanut_r@rmutt.ac.th>
Description: Density, distribution function, the quantile function,
random generation function, and maximum likelihood estimation.
License: GPL-3
Language: en-US
Encoding: UTF-8
RoxygenNote: 7.1.2
Imports: graphics, stats
Suggests: testthat (>= 3.0.0)
Config/testthat/edition: 3
NeedsCompilation: no
Packaged: 2024-05-09 02:36:30 UTC; COM
Author: Atchanut Rattanalertnusorn [aut, cre]
Repository: CRAN
Date/Publication: 2024-05-10 13:50:02 UTC
20 changes: 20 additions & 0 deletions MD5
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
4b1160372b65448eab18a6fd160d459c *DESCRIPTION
d4aa8aa7aaa0abb8e5541d3f748e00d3 *NAMESPACE
38424077dc514030427ba3aa30d1c207 *R/cdfplot.R
5c667207fc1eac951396a81e6ec1fa8b *R/mlewb.R
d6e643bcfd8ef123b71606d495be3b4a *R/pdfplot.R
fdb1009f2f2143edb082d2fd0255ed91 *R/tpwb.R
6322b3439dfe6f15f81c31dda9bfdf69 *man/cdfplot.Rd
c294c9280a90169d3ff9b0d3762c01ff *man/mlewb.Rd
26c239f3c7763fe5b1003cdf299dc3f7 *man/pdfplot.Rd
447c9ffba14021de220ed34320b4209d *man/tpwb.Rd
d7505900994f5d5932847f5f6af8aceb *tests/testthat.R
92e6d751800eee84fb9545e6909441fe *tests/testthat/Rplots.pdf
6105f483b3fcbe42ef85d768203c2aaa *tests/testthat/test-cdfplot.R
030f505c913cecda94aeff482e74281b *tests/testthat/test-dtpwb.R
82f81fc3c40948d3abbe1b72c855c3e1 *tests/testthat/test-mlewb.R
9d85a879afdc97fa25abe21334a35bf0 *tests/testthat/test-pdfplot.R
48536e79ecb66ded8120a83f1fb3e386 *tests/testthat/test-ptpwb.R
3725bfe9f6198572c47f266042e168c1 *tests/testthat/test-qtpwb.R
ca4b78fd216f9ca079fe99b2403dfac9 *tests/testthat/test-rtpwb.R
3fa51c69ac31f52437971299f78b72da *tests/testthat/test-tpwb.R
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Generated by roxygen2: do not edit by hand

export(cdfplot)
export(dtpwb)
export(mlewb)
export(pdfplot)
export(ptpwb)
export(qtpwb)
export(rtpwb)
import(graphics)
import(stats)
35 changes: 35 additions & 0 deletions R/cdfplot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#' Distribution function plot of the three-parameter Weibull distribution
#'
#' Distribution function plot of the three-parameter Weibull distribution with specified \code{shape}, \code{scale} and \code{location}.
#'
#' @param x vector of quantiles
#' @param shape shape parameter (\eqn{\beta}) of the three-parameter Weibull distribution, where \eqn{\beta >0}.
#' @param scale scale parameter (\eqn{\alpha}) of the three-parameter Weibull distribution, where \eqn{\alpha > 0}.
#' @param location location parameter (\eqn{\delta}) of the three-parameter Weibull distribution, where \eqn{\delta \ge 0}.
#'
#' @return Distribution function plot of the three-parameter Weibull distribution.
#' @export
#'
#' @references Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 1, chapter 21. Wiley, New York.
#'
#' @examples
#' x <- rtpwb(100,1.5,2,1)
#' cdfplot(x,1.5,2,1)
#'
cdfplot <-function(x,shape,scale,location){
beta <- shape; alpha <- scale; delta <- location
x <- x[x>=delta]
xs <- sort(x)
fx <- ptpwb(xs,shape = beta, scale = alpha, location = delta )
plot(x=xs,y=fx,type = "b",xlab="",ylab="")
title(main = "CDF of the three-parameter Weibull distribution",
xlab="t or x",ylab="F(t) or F(x)")
sshape <- beta
sscale <- alpha
slocation <- delta
txtshape <- paste("shape=",sshape)
txtscale <- paste("scale=",sscale)
txtlocation <- paste("location=",slocation)
leg.txt <- c(txtshape,txtscale,txtlocation)
legend("bottomright",legend = leg.txt, pch = 1,title = "parameters")
}
33 changes: 33 additions & 0 deletions R/mlewb.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#' Maximum likelihood estimation (MLE) for the three-parameter Weibull distribution.
#'
#' This function for estimating parameter of the three-parameter Weibull distribution.

#' @param x vector of quantiles.
#' @param shape shape parameter, where \eqn{\beta > 0}.
#' @param scale scale parameter, where \eqn{\alpha > 0}.
#' @param location location parameter, where \eqn{\delta \ge 0}.
#'
#' @return the estimated shape, scale and location values of the three-parameter Weibull distribution.
#' @export
#'
#' @note the result of this function may produce a Warning message, but not effect to the estimated parameter.
#'
#' @references Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 1, chapter 21. Wiley, New York.
#'
#' @examples
#' x<- rtpwb(1000,2,3,1) #n=1000 large sample
#' mlewb(x,2,3,1)
#' x<- rtpwb(50,2,3,1) #n=50 medium sample
#' mlewb(x,2,3,1)
#' x<- rtpwb(10,2,3,1) #n=10 small sample
#' mlewb(x,2,3,1)
mlewb <- function(x,shape,scale,location){
negll<- function(par){
-sum(dtpwb(x,shape=par[1],scale=par[2],location=par[3],log=TRUE))
}
objmle<- nlminb(start = c(shape,scale,location),negll)
mle.shape <- objmle$par[1]
mle.scale <- objmle$par[2]
mle.location <- objmle$par[3]
return(c(mle.shape,mle.scale,mle.location))
}
35 changes: 35 additions & 0 deletions R/pdfplot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#' Probability density function plot of the three-parameter Weibull distribution
#'
#' Probability density function plot of the three-parameter Weibull distribution with specified \code{shape}, \code{scale} and \code{location}.
#'
#' @param x vector of quantiles
#' @param shape shape parameter (\eqn{\beta}) of the three-parameter Weibull distribution, where \eqn{\beta >0}.
#' @param scale scale parameter (\eqn{\alpha}) of the three-parameter Weibull distribution, where \eqn{\alpha > 0}.
#' @param location location parameter (\eqn{\delta}) of the three-parameter Weibull distribution, where \eqn{\delta \ge 0}.
#'
#' @return Probability density function plot of the three-parameter Weibull distribution.
#' @export
#'
#' @references Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 1, chapter 21. Wiley, New York.
#'
#' @examples
#' x <- rtpwb(100,1.5,2,1)
#' pdfplot(x,1.5,2,1)
#'
pdfplot <-function(x,shape,scale,location){
beta <- shape; alpha <- scale; delta <- location
x <- x[x>=delta]
xs <- sort(x)
fx <- dtpwb(xs,shape = beta, scale = alpha, location = delta )
plot(x=xs,y=fx,type = "b",xlab="",ylab="")
title(main = "PDF of the three-parameter Weibull distribution",
xlab="t or x",ylab="f(t) or f(x)")
sshape <- beta
sscale <- alpha
slocation <- delta
txtshape <- paste("shape=",sshape)
txtscale <- paste("scale=",sscale)
txtlocation <- paste("location=",slocation)
leg.txt <- c(txtshape,txtscale,txtlocation)
legend("topright",legend = leg.txt, pch = 1,title = "parameters")
}
104 changes: 104 additions & 0 deletions R/tpwb.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#' The three-parameter Weibull distribution(tpwb)
#'
#' @description Density, distribution function, quantile function, and random generation function
#' for the three-parameter Weibull distribution with \code{shape}, \code{scale} and \code{location}
#'
#' @param x,q vector of quantiles.
#' @param p vector of probabilities
#' @param n number of observations. If \code{length(n) > 1}, the length is taken to be the number required.
#' @param shape shape parameter, where \eqn{\beta > 0}.
#' @param scale scale parameter, where \eqn{\alpha > 0}.
#' @param location location parameter, where \eqn{\delta \ge 0}.
#' @param log,log.p logical; (default = \code{FALSE}), if \code{TRUE}, then probabilities are given as \code{log(p)}.
#' @param lower.tail logical; if \code{TRUE} (default), probabilities are \eqn{P[X \le x]}, otherwise, \eqn{P[X > x]}.
#'
#' @import graphics
#' @import stats
#'
#' @note If location parameter, \eqn{\delta = 0} , it reduced to the two-parameter Weibull distribution.
#'
#'@references Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 1, chapter 21. Wiley, New York.
#'
#' @return
#' \code{dtpwb} gives the density,
#' \code{ptpwb} gives the distribution function,
#' \code{qtpwb} gives the quantile function,
#' and \code{rtpwb} generates random samples.
#'
#' @name tpwb
#' @examples
#'
NULL

#' @export
#' @rdname tpwb
#' @examples
#' x <- rtpwb(20,1.5,3,1)
#' dtpwb(x,1.5,3,1)
#' dtpwb(x,1.5,3,1,log=TRUE)
#'
dtpwb <- function(x, shape, scale, location=1, log = FALSE){
beta<- shape; alpha <- scale; delta <- location
xs <- x[x>=delta]
fx <- (beta/(alpha^beta))*((xs-delta)^(beta-1))*exp(-((xs-delta)/alpha)^beta)
if (log==TRUE)
return (log(fx))
else
return(fx)
}

#' @export
#' @rdname tpwb
#' @examples
#' q <- rtpwb(20,1.5,3,1)
#' ptpwb(q,1.5,3,1 )
#' ptpwb(q,1.5,3,1, lower.tail = FALSE)
#'
ptpwb <- function(q, shape, scale,location=1, lower.tail = TRUE, log.p = FALSE){
beta<- shape; alpha <- scale; delta <- location
xs <- q[q>=delta]
cdf <- 1-exp(-((xs-delta)/alpha)^beta)
if(lower.tail==TRUE)
p <- cdf
else
p <- 1-cdf
if (log.p==TRUE)
return (log(p))
else
return(p)
}

#' @export
#' @rdname tpwb
#' @examples
#' q <- rtpwb(20,1.5,3,1); q
#' p<- ptpwb(q,1.5,3,1 ); p
#' qtpwb(p,1.5,3,1)
#'
qtpwb <- function(p, shape, scale, location = 1, lower.tail = TRUE, log.p = FALSE){
beta<- shape; alpha <- scale; delta <- location
if (log.p==TRUE)
p <- exp(p)
if (lower.tail == FALSE)
p <- 1 - p
x <- delta + alpha * (-log(1 - p))^(1/beta)
return(x)
}

#' @export
#' @rdname tpwb
#' @examples
#' rtpwb(5, 1.5, 3, 0) # the same as rweibull(5,1.5,3)
#' rtpwb(25,0.5, 2, 1)
#'
rtpwb <- function(n, shape, scale, location = 1){
beta<- shape; alpha <- scale; delta <- location
u<- runif(n)
if (beta <= 0){
stop(paste("beta must be larger than 0!", "\n"))
}
# x based on the inverse transform method : F(x)=u => x=(inv[F(u)])
# where u=runif(n,0,1)
x <- delta + alpha*(-log(1 - u))^(1/beta)
return(x)
}
31 changes: 31 additions & 0 deletions man/cdfplot.Rd

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

37 changes: 37 additions & 0 deletions man/mlewb.Rd

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

31 changes: 31 additions & 0 deletions man/pdfplot.Rd

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

Loading

0 comments on commit b3198d5

Please sign in to comment.