Learn R Programming

ggdmc (version 0.1.3.9)

dtnorm: Truncated Normal Distribution

Description

The two wrapper functions call Rcpp functions to speed up computation. The codes are based on Christopher Jackson's msm (1.5) package and Jonathan Olmsted's RcppTN (0.1-8) package.

Usage

dtnorm(x = 0, mean = 0, sd = 1, lower = -Inf, upper = Inf, log = 0,
  checks = FALSE)

rtnorm(n = 1, mean = 0, sd = 1, lower = -Inf, upper = Inf, checks = TRUE)

Arguments

x, n
x in dtnorm is a vector of quantiles; n in rtnorm indicates how many random number to generate
mean
vector of means
sd
vector of standard deviations
lower
lower truncation point. Default as -Inf
upper
upper truncation point. Default as Inf.
log
default 0 (FALSE). Enter 1 to make it TRUE. Whether to calculate density. Use only in dtnorm.
checks
a switch to turn on check functions to check inputs and ouput. Default to FALSE for the sake of speed.

Details

dtnorm calculates probability density for a truncated normal distribution with mean equal to mean and standard deviation equal to sd before truncation, and truncated on the interval [lower, upper]. rtnorm generates a random number from a truncated Normal distribution with mean equal to mean and standard deviation equal to sd before truncation, and truncated on the interval [lower, upper].

Examples

Run this code
## Use dtnorm and rtnorm with their default values
dtnorm()
rtnorm()

## A similar curve plotting example extracted from dnorm functions
plot(function(x) dnorm(x, log = FALSE), -2.5, 2.5,
     main = "Normal Distribution", ylim=c(0,0.45), ylab="Density")
curve(dtnorm(x, lower=-2, upper=2), add=TRUE, col="tomato", lwd=2)
mtext("dnorm(x)", adj = 0)
mtext("dtnorm(x)", col = "tomato", adj = 1)

Run the code above in your browser using DataLab