truncdist/0000755000177400001440000000000012761142357012473 5ustar murdochuserstruncdist/NAMESPACE0000644000177400001440000000023412761114407013704 0ustar murdochusersimport( stats, evd, graphics, stats4 ) export( dtrunc, extrunc, vartrunc, ptrunc, qqtrunc, qtrunc, rtrunc ) truncdist/R/0000755000177400001440000000000012761114407012667 5ustar murdochuserstruncdist/R/extrunc.R0000644000177400001440000000133212761114407014501 0ustar murdochusersextrunc <- function( spec, a = -Inf, b = Inf, ... ) { ### ### This function computes the expected value of a truncated random variable ### with the given specification over the truncated domain using ### numerical integration ### ### Arguments ### spec = a character value to specify the distribution ### a = a numeric value for the lower bound of the truncation interval ### b = a numeric value for the upper bound of the truncation interval ### ... = one or more arguments used by the density function ### if ( a >= b ) stop( "argument a is greater than or equal to b" ) f <- function( x ) { x * dtrunc( x, spec, a = a, b = b, ... ) } return( integrate( f, lower = a, upper = b )$value ) } truncdist/R/vartrunc.R0000644000177400001440000000130212761114407014652 0ustar murdochusersvartrunc <- function( spec, a = -Inf, b = Inf, ... ) { ### ### This function computes the variance of the truncated random variable ### ### Arguments ### spec = a character value to specify the distribution ### a = a numeric value for the lower bound of the truncation interval ### b = a numeric value for the upper bound of the truncation interval ### ... = one or more arguments used by the density function ### if ( a >= b ) stop( "argument a is greater than or equal to b" ) ex <- extrunc( spec, a = a, b = b, ... ) f <- function( x ) { ( x - ex )^2 * dtrunc( x, spec, a = a, b = b, ... ) } tt <- integrate( f, lower = a, upper = b )$value return( tt ) } truncdist/R/qtrunc.R0000644000177400001440000000175212761114407014333 0ustar murdochusersqtrunc <- function( p, spec, a = -Inf, b = Inf, ... ) { ### ### This function evaluates the inverse of the truncated distribution function ### or quantile function for the given vector of probabilities and ### the specified distribution ### ### Arguments ### p = a numeric vector of probabilities ### spec = a character value for the name of the distribution (e.g., "norm") ### ... = other arguments passed to the corresponding density function ### if ( a >= b ) stop( "argument a is greater than or equal to b" ) tt <- p G <- get( paste( "p", spec, sep="" ), mode = "function" ) Gin <- get( paste( "q", spec, sep="" ), mode = "function" ) G.a <- G( a, ... ) G.b <- G( b, ... ) if ( G.a == G.b ) { stop( "Trunction interval is not inside the domain of the quantile function" ) } result <- pmin( pmax( a, Gin( G( a, ... ) + p * ( G( b, ... ) - G( a, ... )), ... ) ), b ) return( result ) } truncdist/R/rtrunc.R0000644000177400001440000000113312761114407014325 0ustar murdochusersrtrunc <- function( n, spec, a = -Inf, b = Inf, ... ) { ### ### This function generates n random numbers that are drawn from ### the specified truncated distribution. ### ### Arguments ### n = a positive integer for the number of random numbers generated ### spec = a character value for the name of the distribution (e.g., "norm") ### ... = other arguments passed to the corresponding density function ### if ( a >= b ) stop( "argument a is greater than or equal to b" ) u <- runif( n, min = 0, max = 1 ) x <- qtrunc( u, spec, a = a, b = b, ... ) return( x ) } truncdist/R/qqtrunc.R0000644000177400001440000000125412761114407014511 0ustar murdochusersqqtrunc <- function( x, spec, a=-Inf, b=Inf, title="Truncated Distribution Q-Q Plot", xlabel="Theoretical Quantiles", ylabel="Sample Quantiles", ... ) { ### ### This function produces a q-q plot of sample quantiles against theoretical ### quantiles for a truncated random variable ### ### Arguments ### x = a numeric vector of sample values ### min = a numeric value for the lower bound of the uniform distribution ### max = a numeric value for the upper bound of the uniform distribution ### plot( qtrunc(ppoints(x), spec, a=a, b=b, ...), sort(x), main=title, xlab=xlabel, ylab=ylabel ) abline( 0,1 ) return( invisible( 0 ) ) } truncdist/R/dtrunc.R0000644000177400001440000000173012761114407014312 0ustar murdochusersdtrunc <- function( x, spec, a = -Inf, b= Inf, ... ) { ### ### this function computes the density function defined by the spec argument ### for the vector of quantile values in x. The random variable is truncated ### to be in the interval ( a, b ) ### ### Arguments ### x = a numeric vector of quantiles ### spec = a character value for the name of the distribution (e.g., "norm") ### ... = other arguments passed to the corresponding density function ### if ( a >= b ) stop( "argument a is greater than or equal to b" ) tt <- rep( 0, length( x ) ) g <- get( paste( "d", spec, sep="" ), mode="function" ) G <- get( paste( "p", spec, sep="" ), mode="function" ) G.a <- G( a, ... ) G.b <- G( b, ... ) if ( G.a == G.b ) { stop( "Trunction interval is not inside the domain of the density function" ) } tt[x >= a & x <= b] <- g( x[x >= a & x <= b], ...) / ( G( b, ... ) - G( a, ... ) ) return( tt ) } truncdist/R/ptrunc.R0000644000177400001440000000206012761114407014323 0ustar murdochusersptrunc <- function( q, spec, a = -Inf, b = Inf, ... ) { ### ### this function computes the distribution function defined by the spec argument ### for the vector of quantile values in x. The random variable is truncated ### to be in the interval ( a, b ) ### ### Arguments ### q = a numeric vector of quantiles ### spec = a character value for the name of the distribution (e.g., "norm") ### ... = other arguments passed to the corresponding density function ### if ( a >= b ) stop( "argument a is greater than or equal to b" ) tt <- q aa <- rep( a, length( q ) ) bb <- rep( b, length( q ) ) G <- get( paste( "p", spec, sep="" ), mode="function" ) tt <- G( apply( cbind( apply( cbind( q, bb ), 1, min ), aa ), 1, max ), ... ) tt <- tt - G ( aa, ... ) G.a <- G( aa, ... ) G.b <- G( bb, ... ) if ( any( G.a == G.b ) ) { stop( "Trunction interval is not inside the domain of the distribution function" ) } result <- tt / ( G( bb, ... ) - G ( aa, ... ) ) return( result ) } truncdist/MD50000644000177400001440000000135512761142357013007 0ustar murdochusersa77f3b30c66ca6aa996b21b5b3658420 *DESCRIPTION a90491ba20a4649554d184a3e0ea0a3b *NAMESPACE 00cd0febcbac203ca7fa7119fe2df344 *R/dtrunc.R 3db892134006e66c840b2f6aa4b2f951 *R/extrunc.R ba5162e3b8db70fe5f7697fe01653f7c *R/ptrunc.R 1a81750656b33c0b908027fa7cb8ca3f *R/qqtrunc.R e5adca688762d98e031698bc9af4d672 *R/qtrunc.R ef5fe3a691bb8cf72f09156d19c4545d *R/rtrunc.R e21aaa64d9edd2a6d9bce0e01e3cc64b *R/vartrunc.R 75f75d3ab619b4a27e8bf98e8b4b0bf8 *man/dtrunc.Rd 285f2cae8c7f7937dc1f62d36f652758 *man/extrunc.Rd 0335ac65cdd501489330e8116d51b7ed *man/ptrunc.Rd d52c301cc8c963b66a07809434ee1ccf *man/qqtrunc.Rd c4dbcc1006555477641e85d53981f0ba *man/qtrunc.Rd b647aac902c4f8231c03c40af2d4f746 *man/rtrunc.Rd d8211e1f9b71b6976b5f9156b9018181 *man/vartrunc.Rd truncdist/DESCRIPTION0000644000177400001440000000163712761142357014210 0ustar murdochusersPackage: truncdist Version: 1.0-2 Date: 2016-08-29 Author: Frederick Novomestky , Saralees Nadarajah Maintainer: Frederick Novomestky Depends: R (>= 2.0.1), stats4,evd Description: A collection of tools to evaluate probability density functions, cumulative distribution functions, quantile functions and random numbers for truncated random variables. These functions are provided to also compute the expected value and variance. Nadarajah and Kotz (2006) developed most of the functions. QQ plots can be produced. All the probability functions in the stats, stats4 and evd packages are automatically available for truncation.. Title: Truncated Random Variables License: GPL (>= 2) NeedsCompilation: no Packaged: 2016-08-29 20:20:23 UTC; fred Repository: CRAN Date/Publication: 2016-08-30 01:27:43 truncdist/man/0000755000177400001440000000000012761114407013241 5ustar murdochuserstruncdist/man/rtrunc.Rd0000644000177400001440000000223012761114407015042 0ustar murdochusers\name{rtrunc} \alias{rtrunc} \title{ Generate truncated random deviates } \description{ This function generates n random deviates that are drawn from the specified truncated distribution. } \usage{ rtrunc(n, spec, a = -Inf, b = Inf, ...) } \arguments{ \item{n}{a positive integer for the number of random deviates generated } \item{spec}{ a character value that specifies the underlying probability distribution } \item{a}{ a numeric value for the lower bound of the random variable } \item{b}{ a numeric value for the upper bound of the random variable } \item{\dots}{ other arguments are are passed to the corresponding quantile function } } \value{ A vector with one or more random deviates. } \references{ Nadarajah, S. and S. Kotz, 2006. \code{R} Programs for Computing Truncated Distributions, \emph{Journal of Statistical Software}, 16, August 2006, \url{http://www.jstatsoft.org/v16/c02} } \author{ Frederick Novomestky \email{fn334@nyu.edu} Saralees Nadarajah \email{saralees.nadarajah@manchester.ac.uk} } \seealso{ \code{\link{qtrunc}} } \examples{ x <- rtrunc( 500, spec="norm", a=1, b=2 ) } \keyword{ math } truncdist/man/qtrunc.Rd0000644000177400001440000000243212761114407015045 0ustar murdochusers\name{qtrunc} \alias{qtrunc} \title{ Quantitle Function for Truncated Random Variable } \description{ This function evaluates the inverse of the cumulative distribution funciton for a truncated random variable or so the so called quantile function for a given vector of probabilities and the specified distribution. } \usage{ qtrunc(p, spec, a = -Inf, b = Inf, ...) } \arguments{ \item{p}{ A vector of probabilities } \item{spec}{ a character value that specifies the underlying probability distribution } \item{a}{ a numeric value for the lower bound of the random variable } \item{b}{ a numeric value for the upper bound of the random variable } \item{\dots}{ other arguments are are passed to the corresponding quantile and distribution function } } \value{ A vector of quantile values in the range of the truncated random variable. } \references{ Nadarajah, S. and S. Kotz, 2006. \code{R} Programs for Computing Truncated Distributions, \emph{Journal of Statistical Software}, 16, August 2006, \url{http://www.jstatsoft.org/v16/c02} } \author{ Frederick Novomestky \email{fn334@nyu.edu} Saralees Nadarajah \email{saralees.nadarajah@manchester.ac.uk} } \examples{ p <- seq( .1, .9, .01 ) q <- qtrunc( p, spec="norm", a=1, b=2 ) } \keyword{ math } truncdist/man/vartrunc.Rd0000644000177400001440000000234512761114407015400 0ustar murdochusers\name{vartrunc} \alias{vartrunc} \title{ Variance of a truncated random variable } \description{ This function estimates the variance of a truncated random variable using numerical integration. } \usage{ vartrunc(spec, a = -Inf, b = Inf, ...) } \arguments{ \item{spec}{ a character value that specifies the underlying probability distribution } \item{a}{ a numeric value for the lower bound of the random variable } \item{b}{ a numeric value for the upper bound of the random variable } \item{\dots}{ other arguments are are passed to the corresponding quantile function } } \details{ Numerical integration of the truncated density function is performed to compute the variance and the associated expected value. } \value{ A numeric value. } \references{ Nadarajah, S. and S. Kotz, 2006. \code{R} Programs for Computing Truncated Distributions, \emph{Journal of Statistical Software}, 16, August 2006, \url{http://www.jstatsoft.org/v16/c02} } \author{ Frederick Novomestky \email{fn334@nyu.edu} Saralees Nadarajah \email{saralees.nadarajah@manchester.ac.uk} } \seealso{ \code{\link{extrunc}}, \code{\link{dtrunc}} } \examples{ var <- vartrunc( spec="norm", a=1, b=2 ) } \keyword{ math } truncdist/man/extrunc.Rd0000644000177400001440000000231212761114407015216 0ustar murdochusers\name{extrunc} \alias{extrunc} \title{ Expected value of a truncated random variable } \description{ This function computes the expected value of a truncated random variable using numerical integration. } \usage{ extrunc(spec, a = -Inf, b = Inf, ...) } \arguments{ \item{spec}{ a character value that specifies the underlying probability distribution } \item{a}{ a numeric value for the lower bound of the random variable } \item{b}{ a numeric value for the upper bound of the random variable } \item{\dots}{ other arguments are are passed to the corresponding quantile function } } \details{ Numerical integration of the truncated density function is performed to compute the expected value. } \value{ A numerical value for the expectation. } \references{ Nadarajah, S. and S. Kotz, 2006. \code{R} Programs for Computing Truncated Distributions, \emph{Journal of Statistical Software}, 16, August 2006, \url{http://www.jstatsoft.org/v16/c02} } \author{ Frederick Novomestky \email{fn334@nyu.edu} Saralees Nadarajah \email{saralees.nadarajah@manchester.ac.uk} } \seealso{ \code{\link{dtrunc}} } \examples{ mu <- extrunc( spec="norm",a=1, b=2 ) } \keyword{ math } truncdist/man/dtrunc.Rd0000644000177400001440000000246712761114407015040 0ustar murdochusers\name{dtrunc} \alias{dtrunc} \title{ Probability density function of truncated random variables } \description{ This function computes values for the probability density function of a truncated random variable. } \usage{ dtrunc(x, spec, a = -Inf, b = Inf, ...) } \arguments{ \item{x}{ A numeric vector of quantile values } \item{spec}{ a character value that specifies the underlying probability distribution } \item{a}{ a numeric value for the lower bound of the random variable } \item{b}{ a numeric value for the upper bound of the random variable } \item{\dots}{ other arguments are are passed to the corresponding quantile function } } \details{ The \code{R} function is used to access the required probability density and cumulative distribution functions of the underlying unrestricted random variable. } \value{ A vector of density function values. } \references{ Nadarajah, S. and S. Kotz, 2006. \code{R} Programs for Computing Truncated Distributions, \emph{Journal of Statistical Software}, 16, August 2006, \url{http://www.jstatsoft.org/v16/c02} } \author{ Frederick Novomestky \email{fnovomes@poly.edu} Saralees Nadarajah \email{saralees.nadarajah@manchester.ac.uk} } \examples{ x <- seq( 0, 3, .1 ) pdf <- dtrunc( x, spec="norm", a=1, b=2 ) } \keyword{ math } truncdist/man/ptrunc.Rd0000644000177400001440000000224712761114407015050 0ustar murdochusers\name{ptrunc} \alias{ptrunc} \title{ Cumulative distribution function of a truncated random variable } \description{ This function compute values of the cumulative distribution function for truncated random variables. } \usage{ ptrunc(q, spec, a = -Inf, b = Inf, ...) } \arguments{ \item{q}{ a numeric vector of quantile values } \item{spec}{ a character value that specifies the underlying probability distribution } \item{a}{ a numeric value for the lower bound of the random variable } \item{b}{ a numeric value for the upper bound of the random variable } \item{\dots}{ other arguments are are passed to the corresponding quantile function } } \value{ A vector of probability values. } \references{ Nadarajah, S. and S. Kotz, 2006. \code{R} Programs for Computing Truncated Distributions, \emph{Journal of Statistical Software}, 16, August 2006, \url{http://www.jstatsoft.org/v16/c02} } \author{ Frederick Novomestky \email{fn334@nyu.edu} Saralees Nadarajah \email{saralees.nadarajah@manchester.ac.uk} } \seealso{ \code{\link{dtrunc}} } \examples{ x <- seq( 0, 3, .1 ) cdf <- ptrunc( x, spec="norm", a=1, b=2 ) } \keyword{ math } truncdist/man/qqtrunc.Rd0000644000177400001440000000267612761114407015240 0ustar murdochusers\name{qqtrunc} \alias{qqtrunc} \title{ QQ Plot for a Truncated Random Variable } \description{ This function produces a QQ plot of sample quantiles against theoretical quantiles for a truncated random variable. } \usage{ qqtrunc(x, spec, a = -Inf, b = Inf, title = "Truncated Distribution Q-Q Plot", xlabel = "Theoretical Quantiles", ylabel = "Sample Quantiles", ...) } \arguments{ \item{x}{ a numeric vector of sample values } \item{spec}{ a character value that specifies the underlying probability distribution } \item{a}{ a numeric value for the lower bound of the random variable } \item{b}{ a numeric value for the upper bound of the random variable } \item{title}{ A character value for the chart title } \item{xlabel}{ A character value for the horizon axis label } \item{ylabel}{ A character value for the vertical axis label } \item{\dots}{ other arguments are are passed to the corresponding quantile function } } \value{ A plot. } \references{ Nadarajah, S. and S. Kotz, 2006. \code{R} Programs for Computing Truncated Distributions, \emph{Journal of Statistical Software}, 16, August 2006, \url{http://www.jstatsoft.org/v16/c02} } \author{ Frederick Novomestky \email{fn334@nyu.edu} Saralees Nadarajah \email{saralees.nadarajah@manchester.ac.uk} } \seealso{ \code{\link{qtrunc}} } \examples{ x <- rtrunc( 500, spec="norm", a=1, b=2 ) qqtrunc( x, spec="norm", a=1, b=2 ) } \keyword{ math }