R: Sample from a normal mixture model using Metropolis-Hastings
normMixMH
R Documentation
Sample from a normal mixture model using Metropolis-Hastings
Description
normMixMH uses the Metropolis-Hastings algorithm to draw a
sample from a univariate target distribution that is a mixture of two
normal distributions using an independent normal candidate density or a random
walk normal candidate density.
A vector of length two containing the mean and standard
deviation of the first component of the normal mixture
theta1
A vector of length two containing the mean and standard
deviation of the second component of the normal mixture
p
A value between 0 and 1 representing the mixture proportion,
so that the true density is p\times f(μ1,σ1) +
(1-p)\times f(μ_2,σ_2)
candidate
A vector of length two containing the mean and
standard deviation of the candidate density
steps
The number of steps to be used in the Metropolis-Hastings
algorithm. steps must be greater than 100
type
Either 'ind' or 'rw' depending on whether a independent
candidate density or random walk candidate density is to be
used. 'i' and 'r' may be used as alternative compact notation
startValue
A starting value for the chain
randomSeed
A seed for the random number generator. Only used
when you want the same sequence of random numbers in the chain
Value
A vector containing a sample from the normal mixture distribution.
Examples
## Set up the normal mixture
theta0 <- c(0,1)
theta1 <- c(3,2)
p <- 0.8
## Sample from an independent N(0,3^2) candidate density
candidate <- c(0, 3)
MCMCsampleInd <- normMixMH(theta0, theta1, p, candidate)
## If we wish to use the alternative random walk N(0, 0.5^2)
## candidate density
candidate <- c(0, 0.5)
MCMCsampleRW <- normMixMH(theta0, theta1, p, candidate, type = 'rw')
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/normMixMH.Rd_%03d_medium.png", width=480, height=480)
> ### Name: normMixMH
> ### Title: Sample from a normal mixture model using Metropolis-Hastings
> ### Aliases: normMixMH
>
> ### ** Examples
>
> ## Set up the normal mixture
> theta0 <- c(0,1)
> theta1 <- c(3,2)
> p <- 0.8
>
> ## Sample from an independent N(0,3^2) candidate density
> candidate <- c(0, 3)
> MCMCsampleInd <- normMixMH(theta0, theta1, p, candidate)
>
>
> ## If we wish to use the alternative random walk N(0, 0.5^2)
> ## candidate density
> candidate <- c(0, 0.5)
> MCMCsampleRW <- normMixMH(theta0, theta1, p, candidate, type = 'rw')
>
>
>
>
>
> dev.off()
null device
1
>