gbs_multinom: Gibbs Algorithm for Generating Multinom Distribution

Description Usage Arguments Value Examples

View source: R/gbs_multinom.R

Description

MCMC method for generating vectors with multinom distribution using Gibbs sampler.

Usage

1
gbs_multinom(n, size, prob, init, burn = 1000)

Arguments

n

The numbers of samples one wants to obtain.

size, prob

Parameters for multinom distribution. size being a integer, say N, specifying the total number of objects that are put into d boxes in the typical multinomial experiment. prob being numeric non-negative vector of length d, specifying the probability for the d classes; should be normalized to sum 1 manually before providing.

init

The initial value vector, which indicates the dimensions.

burn

Times of iterations one wants to omit before recording.

Value

A "mcmcn" object 'list("chain" = chain)' with chain storing samples by row.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Generating Multinom Distribution Samples-----------------------------------

# setting some parameters
prob <- c(0.2, 0.1, 0.1, 0.1, 0.1, 0.1,
          0.05, 0.05, 0.05, 0.05, 0.02, 0.02, 0.02, 0.02, 0.02)
p <- sample(prob)

# generating random varaites using function `gbs_multinom`
# and exploring the results
par(mfrow = c(2, 1))
for (size in 10^(2:6)) {
  init <- size * p
  x.multinom <- gbs_multinom(10000, size, prob, init, burn = 0)
  plot(x.multinom[1:2000, 1], type = "l", ylab = "1st variable",
       ylim=c(0.95*0.2*size, 1.05*0.2*size),
       main = paste("size =", size, "first 2000 iters"))
  plot(density(x.multinom[-(1:1000), 2]), xlab = "occurs",
       xlim = c(0.08*size, 0.22*size),
       main = paste("size =", size))
  lines(density(x.multinom[-(1:1000), 1]))
}
par(mfrow = c(1, 1))

hjy78/mcmcn documentation built on Jan. 1, 2020, 1:03 p.m.