Last data update: 2014.03.03

R: Random Generation from a Truncated Conditional Normal...
rtcmvnormR Documentation

Random Generation from a Truncated Conditional Normal Distribution

Description

Samples from the Truncated Conditional Normal Distribution

Usage

rtcmvnorm(n = 1, mean = 0, V = 1, x=0, keep=1, lower = -Inf, upper = Inf)

Arguments

n

integer: number of samples to be drawn

mean

vector of means

V

covariance matrix

x

vector of observations to condition on

keep

element of x to be sampled

lower

left truncation point

upper

right truncation point

Value

vector

Author(s)

Jarrod Hadfield j.hadfield@ed.ac.uk

Examples

par(mfrow=c(2,1))
V1<-cbind(c(1,0.5), c(0.5,1))
x1<-rtcmvnorm(10000, c(0,0), V=V1, c(0,2), keep=1, lower=-1, upper=1)
x2<-rtnorm(10000, 0, 1, lower=-1, upper=1)
plot(density(x1), main="Correlated conditioning observation")
lines(density(x2), col="red")
# denisties of conditional (black) and unconditional (red) distribution
# when the two variables are correlated (r=0.5) 

V2<-diag(2)
x3<-rtcmvnorm(10000, c(0,0), V=V2, c(0,2), keep=1, lower=-1, upper=1)
x4<-rtnorm(10000, 0, 1, lower=-1, upper=1)
plot(density(x3), main="Uncorrelated conditioning observation")
lines(density(x4), col="red")
# denisties of conditional (black) and unconditional (red) distribution
# when the two variables are uncorrelated (r=0) 

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(MCMCglmm)
Loading required package: Matrix
Loading required package: coda
Loading required package: ape
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/MCMCglmm/rtcmvnorm.Rd_%03d_medium.png", width=480, height=480)
> ### Name: rtcmvnorm
> ### Title: Random Generation from a Truncated Conditional Normal
> ###   Distribution
> ### Aliases: rtcmvnorm
> ### Keywords: distribution
> 
> ### ** Examples
> 
> par(mfrow=c(2,1))
> V1<-cbind(c(1,0.5), c(0.5,1))
> x1<-rtcmvnorm(10000, c(0,0), V=V1, c(0,2), keep=1, lower=-1, upper=1)
> x2<-rtnorm(10000, 0, 1, lower=-1, upper=1)
> plot(density(x1), main="Correlated conditioning observation")
> lines(density(x2), col="red")
> # denisties of conditional (black) and unconditional (red) distribution
> # when the two variables are correlated (r=0.5) 
> 
> V2<-diag(2)
> x3<-rtcmvnorm(10000, c(0,0), V=V2, c(0,2), keep=1, lower=-1, upper=1)
> x4<-rtnorm(10000, 0, 1, lower=-1, upper=1)
> plot(density(x3), main="Uncorrelated conditioning observation")
> lines(density(x4), col="red")
> # denisties of conditional (black) and unconditional (red) distribution
> # when the two variables are uncorrelated (r=0) 
> 
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>