# NOT RUN {
m2 <- c(0.5, -1)
V2 <- matrix(c(3, 3, 3, 6), 2, 2)
lower <- a <- c(-1, -2.5)
upper <- b <- c(2, 1)
set.seed(1)
# generate a set of coordinates, pts, on the plane
pts <- matrix(runif(10, min=-1.5, max=1.5), nrow=5, ncol=2)
rownames(pts) <- LETTERS[1:nrow(pts)]
# compute PDF and CDF at the chosen coordinates, pts
pdf <- dmtruncnorm(pts, mean=m2, varcov=V2, lower, upper)
cdf <- pmtruncnorm(pts, mean=m2, varcov=V2, lower, upper)
print(cbind(pts, pdf, cdf))
#--
# generate a sample of random numbers
sample <- rmtruncnorm(300, mean=m2, varcov=V2, lower, upper)
#
# then plot the sample along with the pertaining box and the earlier points,
# just for graphical illustration
eps <- 0.5
ra <- lower -eps
rb <- upper + eps
plot(x=c(ra[1], rb[1]), y=c(ra[2], rb[2]), xlab="", ylab="",type="n")
points(sample, pch=20, col=4, cex=0.4)
xbox <- c(a[1], b[1], b[1], a[1], a[1])
ybox <- c(a[2], a[2], b[2], b[2], a[2])
polygon(xbox, ybox, lty=2, border="gray60")
text(pts[,1], pts[,2], col=2, labels=rownames(pts), cex=0.9)
# }
Run the code above in your browser using DataCamp Workspace