# Generate random samples from truncated normal
n <- 1e5
p0 <- 0
p1 <- 1
lower <- 0
upper <- Inf
rtnorm_dat <- rtnorm(n, p0, p1, lower, upper)
# Density at quantiles
x <- seq(-5, 5, length.out = 1e3)
dtnorm_dat <- dtnorm(x, p0, p1, lower = -2, upper = 2, log_p = FALSE)
# Cumulative probabilities
q <- seq(-5, 5, length.out = 1e3)
ptnorm_dat <- ptnorm(q, p0, p1, lower = -2, upper = 3,
lower_tail = TRUE, log_p = FALSE)
# Plotting
cex_lab <- 1
cex_axis <- 0.5
line_width <- 1.5
hist(rtnorm_dat, breaks = "fd", freq = FALSE, xlab = "",
cex.lab = cex_lab, cex.axis = cex_axis, main = "")
plot(x, dtnorm_dat, type = "l", lwd = line_width, xlab = "",
ylab = "Density", cex.lab = cex_lab, cex.axis = cex_axis, main = "")
Run the code above in your browser using DataLab