truncNormal {VGAMextra} | R Documentation |
The Truncated Normal Distribution
Description
Density, distribution function, quantile function and random numbers generator for the truncated normal distribution
Usage
dtruncnorm(x, mean = 0, sd = 1, min.support = -Inf, max.support = Inf, log = FALSE)
ptruncnorm(q, mean = 0, sd = 1, min.support = -Inf, max.support = Inf)
qtruncnorm(p, mean = 0, sd = 1, min.support = -Inf, max.support = Inf, log.p = FALSE)
rtruncnorm(n, mean = 0, sd = 1, min.support = -Inf, max.support = Inf)
Arguments
x , q , p , n , mean , sd |
Same as |
min.support , max.support |
Lower and upper truncation limits. |
log , log.p |
Same as |
Details
Consider N(
,
), with
,
i.e.,
restricted to
.
We denote
=
min.support
and
=
max.support
.
Then the conditional
random variable
has a truncated normal distribution. Its p.d.f. is given by
where ,
,
and
.
Its mean is
Here, is the standard normal c.d.f and
is the standard normal p.d.f.
Value
dtruncnorm()
returns the density,
ptruncnorm()
gives the
distribution function,
qtruncnorm()
gives the quantiles, and
rtruncnorm()
generates random deviates.
dtruncnorm
is computed from the definition, as
in 'Details'. [pqr]
truncnormal are computed based
on their relationship to the normal distribution.
Author(s)
Victor Miranda and Thomas W. Yee.
References
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, Second Edition (Chapter 13). Wiley, New York.
See Also
Examples
###############
## Example 1 ##
mymu <- 2.1 # mu
mysd <- 1.0 # sigma
LL <- -1.0 # Lower bound
UL <- 3.0 # Upper bound
## Quantiles:
pp <- 1:10 / 10
(quants <- qtruncnorm(p = pp , min.support = LL, max.support = UL,
mean = mymu, sd = mysd))
sum(pp - ptruncnorm(quants, min.support = LL, max.support = UL,
mean = mymu, sd = mysd)) # Should be zero
###############
## Example 2 ##
## Parameters
set.seed(230723)
nn <- 3000
mymu <- 12.7 # mu
mysigma <- 3.5 # sigma
LL <- 6 # Lower bound
UL <- 17 # Upper bound
## Truncated-normal data
trunc_data <- rtruncnorm(nn, mymu, mysigma, LL, UL)
## non-truncated data - reference
nontrunc_data <- rnorm(nn, mymu, mysigma)
## Not run:
## Densities
par(mfrow = c(1, 2))
plot(density(nontrunc_data), main = "Non-truncated ND",
col = "green", xlim = c(0, 25), ylim = c(0, 0.15))
abline(v = c(LL, UL), col = "black", lwd = 2, lty = 2)
plot(density(trunc_data), main = "Truncated ND",
col = "red", xlim = c(0, 25), ylim = c(0, 0.15))
## Histograms
plot.new()
par(mfrow = c(1, 2))
hist(nontrunc_data, main = "Non-truncated ND", col = "green",
xlim = c(0, 25), ylim = c(0, 0.15), freq = FALSE, breaks = 22,
xlab = "mu = 12.7, sd = 3.5, LL = 6, UL = 17")
abline(v = c(LL, UL), col = "black", lwd = 4, lty = 2)
hist(trunc_data, main = "Truncated ND", col = "red",
xlim = c(0, 25), ylim = c(0, 0.15), freq = FALSE,
xlab = "mu = 12.7, sd = 3.5, LL = 6, UL = 17")
## End(Not run)
## Area under the estimated densities
# (a) truncated data
integrate(approxfun(density(trunc_data)),
lower = min(trunc_data) - 1,
upper = max(trunc_data) + 1)
# (b) non-truncated data
integrate(approxfun(density(nontrunc_data)),
lower = min(nontrunc_data),
upper = max(nontrunc_data))