truncNormal | R Documentation |
Density, distribution function, quantile function and random numbers generator for the truncated normal distribution
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)
x, q, p, n, mean, sd |
Same as |
min.support, max.support |
Lower and upper truncation limits. |
log, log.p |
Same as |
Consider X \sim
N(\mu
,
\sigma^2
), with A < X < B
,
i.e., X
restricted to (A , B)
.
We denote A
= min.support
and
B
= max.support
.
Then the conditional
random variable
Y = X \cdot I_{(A , B)}
has a truncated normal distribution. Its p.d.f. is given by
f(y; \mu, \sigma, A, B) = (1 / \sigma) \cdot
\phi(y^*) / [ \Phi(B^*) - \Phi(A^*) ],
where y^* = (y - \mu)/ \sigma
,
A^* = (A - \mu) / \sigma
,
and
B^* = (B - \mu) / \sigma
.
Its mean is
\mu + \sigma \cdot [ \phi(A) - \phi(B) ] /
[\Phi(B) - \Phi(A)].
Here, \Phi
is the standard normal c.d.f and
\phi
is the standard normal p.d.f.
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.
Victor Miranda and Thomas W. Yee.
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, Second Edition (Chapter 13). Wiley, New York.
Normal
,
truncweibull
.
###############
## 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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.