R: Metropolis Hastings sampling from a Bivariate Normal...
bivnormMH
R Documentation
Metropolis Hastings sampling from a Bivariate Normal distribution
Description
This function uses the MetropolisHastings algorithm to draw a sample
from a correlated bivariate normal target density using a random walk
candidate and an independent candidate density respectively where we
are drawing both parameters in a single draw. It can also use the
blockwise Metropolis Hastings algorithm and Gibbs sampling
respectively to draw a sample from the correlated bivariate normal
target.
the correlation coefficient for the bivariate normal
rho1
the correlation of the candidate distribution. Only used
when type = 'ind'
sigma
the standard deviations of the marginal distributions of
the independent candidate density. Only used when type = 'ind'
steps
the number of Metropolis Hastings steps
type
the type of candidate generation to use. Can be one of
'rw' = random walk, 'ind' = independent normals, 'gibbs' = Gibbs
sampling or 'block' = blockwise. It is sufficient to use 'r','i','g',
or 'b'
Value
returns a list which contains a data frame called targetSample with
members x and y. These are the samples from the target density.
Examples
## independent chain
chain1.df<-bivnormMH(0.9)$targetSample
## random walk chain
chain2.df<-bivnormMH(0.9, type = 'r')$targetSample
## blockwise MH chain
chain3.df<-bivnormMH(0.9, type = 'b')$targetSample
## Gibbs sampling chain
chain4.df<-bivnormMH(0.9, type = 'g')$targetSample
oldPar <- par(mfrow=c(2,2))
plot(y ~ x, type = 'l', chain1.df, main = 'Independent')
plot(y ~ x, type = 'l', chain2.df, main = 'Random Walk')
plot(y ~ x, type = 'l', chain3.df, main = 'Blockwise')
plot(y ~ x, type = 'l', chain4.df, main = 'Gibbs')
par(oldPar)
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(Bolstad2)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Bolstad2/bivnormMH.Rd_%03d_medium.png", width=480, height=480)
> ### Name: bivnormMH
> ### Title: Metropolis Hastings sampling from a Bivariate Normal
> ### distribution
> ### Aliases: bivnormMH
>
> ### ** Examples
>
> ## independent chain
> chain1.df<-bivnormMH(0.9)$targetSample
>
> ## random walk chain
> chain2.df<-bivnormMH(0.9, type = 'r')$targetSample
>
>
> ## blockwise MH chain
> chain3.df<-bivnormMH(0.9, type = 'b')$targetSample
>
> ## Gibbs sampling chain
> chain4.df<-bivnormMH(0.9, type = 'g')$targetSample
>
> oldPar <- par(mfrow=c(2,2))
> plot(y ~ x, type = 'l', chain1.df, main = 'Independent')
> plot(y ~ x, type = 'l', chain2.df, main = 'Random Walk')
> plot(y ~ x, type = 'l', chain3.df, main = 'Blockwise')
> plot(y ~ x, type = 'l', chain4.df, main = 'Gibbs')
> par(oldPar)
>
>
>
>
>
> dev.off()
null device
1
>