truncate: truncate Functions

Description Usage Arguments Details Value Author(s) References See Also Examples

Description

dtruncate, ptruncate, qtruncate and rtruncate generate R functions returning respectively density, probability, quantile and random truncated function for any existing dist.

Usage

1
2
3
4
5
6
7

Arguments

dist

The underlying distribution: norm, weibull, gamma, ... as a character string

Details

ptruncdist, dtruncdist, qtruncdist and rtruncdist are functions returning taking as input a distribution name and returning a functions.

The gamma distribution may present problems when parameter scale, rather than rate is specified. This is due to the check on arguments that is performed within the function body. See package vignettes for more details

Value

A function object corresponding to the truncated density function (dtruncate), distribution function (ptruncate), quantile function (qtruncate), random function (rtruncate) for the underlying dist provided as input.

Author(s)

Andrea Spano andrea.spano@quantide.com

References

Nadarajah, S. and S. Kotz, 2006. R Programs for Computing Truncated Distributions, Journal of Statistical Software, 16, August 2006, http://www.jstatsoft.org/v16/c02

Hadley Wickham. Advanced R programming http://adv-r.had.co.nz/

See Also

ptrunc dtrunc qtrunc rtrunc

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# example 1: Normal distribution 
tdnorm <- dtruncate(dist = "norm")
x <- seq( -3, 3, len = 1000)
d <- tdnorm(x = x, mean = 0 , sd = 1, L = -2, U = 2)
plot(x , d , xlim= c(-3, 3), 
     ylim = c(0, 1.1*max(d)), type = "l", 
     col = "red", lwd = 2)

# example 2: lognormal distribution 
tplnorm <- ptruncate("lnorm")
tqlnorm <- qtruncate("lnorm")
q <- tqlnorm(ppoints(100), meanlog = 5, sdlog = 2)# , L = 10^4, U = 2*10^4)
p <- tplnorm(q, meanlog = 5, sdlog = 2)# , L = 10^4, U = 2*10^4)
plot(q, p, pch = 16, col = "red", cex = 3/4, xlab = "Quantile", ylab = "Probability")

# example 3: Quantlies from truncated poisson  distribution 
tqpois <- qtruncate("pois")
pp <- ppoints(100)
q <- tqpois(pp, lambda = 5, log = FALSE, L = 4)
barplot(table(q))

#example 4: Random numbers from truncated gpd distribution 
require(evd)
trgpd <- rtruncate(dist = "gpd")
r <- trgpd (n = 1000, loc = 100, scale = 2, shape = .25 , L = 150, U = 200)
hist(r, xlim = c(140,210), col = "lightgray")

#Example 5: gamma example
# It does not work with qgamma() from stats package
# We need to redifine qgamma() as:
qgamma <- function (p, shape, rate = 1, lower.tail = TRUE, log.p = FALSE) {
  scale <-  1/rate
  .External(stats:::C_qgamma, p, shape, scale, lower.tail, log.p)
}

tqgamma <- qtruncate("gamma")
tqgamma(p = ppoints(10), shape = 1, rate = 1, L = 0.5 , U = 1.5)

andreaspano/qdist documentation built on Dec. 31, 2020, 7:47 p.m.