File: MCmodels.R

package info (click to toggle)
r-cran-mcmcpack 1.7-1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,492 kB
  • sloc: cpp: 21,271; ansic: 4,372; makefile: 2; sh: 1
file content (311 lines) | stat: -rw-r--r-- 9,942 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
##########################################################################
## simple instructional models using Monte Carlo simulation
##
## This software is distributed under the terms of the GNU GENERAL
## PUBLIC LICENSE Version 2, June 1991.  See the package LICENSE
## file for more information.
##
## Copyright (C) 2003-2007 Andrew D. Martin and Kevin M. Quinn
## Copyright (C) 2007-present Andrew D. Martin, Kevin M. Quinn,
##    and Jong Hee Park
##########################################################################

## Monte Carlo simulation from the likelihood of a
## binomial distribution with a Beta(alpha, beta) prior
## ADM 1/25/2006

#' Monte Carlo Simulation from a Binomial Likelihood with a Beta Prior
#'
#' This function generates a sample from the posterior distribution of a
#' binomial likelihood with a Beta prior.
#'
#' \code{MCbinomialbeta} directly simulates from the posterior distribution.
#' This model is designed primarily for instructional use.  \eqn{\pi} is
#' the probability of success for each independent Bernoulli trial.  We assume
#' a conjugate Beta prior:
#'
#' \deqn{\pi \sim \mathcal{B}eta(\alpha, \beta)}
#'
#' \eqn{y} is the number of successes in \eqn{n} trials.  By
#' default, a uniform prior is used.
#'
#' @param y The number of successes in the independent Bernoulli trials.
#'
#' @param n The number of independent Bernoulli trials.
#'
#' @param alpha Beta prior distribution alpha parameter.
#'
#' @param beta Beta prior distribution beta parameter.
#'
#' @param mc The number of Monte Carlo draws to make.
#'
#' @param ... further arguments to be passed
#'
#' @return An mcmc object that contains the posterior sample.  This object can
#' be summarized by functions provided by the coda package.
#'
#' @export
#'
#' @seealso \code{\link[coda]{plot.mcmc}}, \code{\link[coda]{summary.mcmc}}
#'
#' @keywords models
#'
#' @examples
#'
#' \dontrun{
#' posterior <- MCbinomialbeta(3,12,mc=5000)
#' summary(posterior)
#' plot(posterior)
#' grid <- seq(0,1,0.01)
#' plot(grid, dbeta(grid, 1, 1), type="l", col="red", lwd=3, ylim=c(0,3.6),
#'   xlab="pi", ylab="density")
#' lines(density(posterior), col="blue", lwd=3)
#' legend(.75, 3.6, c("prior", "posterior"), lwd=3, col=c("red", "blue"))
#' }
#'
MCbinomialbeta <- function(y, n, alpha=1, beta=1, mc=1000, ...) {

   # check data
   if (any(y < 0)) {
        cat("Error: Number of successes negative.\n")
        stop("Please respecify and call function again.")
    }
    if (any(n < 0)) {
        cat("Error: Number of trials negative.\n")
        stop("Please respecify and call function again.")
    }
    if (any(y > n)) {
        cat("Error: Number of successes greater than number of trials.\n")
        stop("Please respecify and call function again.")
    }

   # check other parameters
   check.beta.prior(alpha, beta)
   check.mc.parameter(mc)

   # draw sample and return
   output <- mcmc(matrix(rbeta(mc, alpha+y, beta+n-y),mc,1))
   varnames(output) <- as.list("pi")
   attr(output,"title") <- "MCbinomialbeta Posterior Sample"

   return(output)
}

## Monte Carlo simulation from the likelihood of a
## Poisson distribution with a Gamma(alpha, beta) prior
## ADM 1/25/2006
#' Monte Carlo Simulation from a Poisson Likelihood with a Gamma Prior
#'
#' This function generates a sample from the posterior distribution of a
#' Poisson likelihood with a Gamma prior.
#'
#' \code{MCpoissongamma} directly simulates from the posterior distribution.
#' This model is designed primarily for instructional use.
#' \eqn{\lambda} is the parameter of interest of the Poisson
#' distribution.  We assume a conjugate Gamma prior:
#'
#' \deqn{\lambda \sim \mathcal{G}amma(\alpha, \beta)}
#'
#' \eqn{y} is a vector of counts.
#'
#' @param y A vector of counts (must be non-negative).
#'
#' @param alpha Gamma prior distribution shape parameter.
#'
#' @param beta Gamma prior distribution scale parameter.
#'
#' @param mc The number of Monte Carlo draws to make.
#'
#' @param ... further arguments to be passed
#'
#' @return An mcmc object that contains the posterior sample.  This object can
#' be summarized by functions provided by the coda package.
#'
#' @export
#'
#' @seealso \code{\link[coda]{plot.mcmc}}, \code{\link[coda]{summary.mcmc}}
#'
#' @keywords models
#'
#' @examples
#'
#' \dontrun{
#' data(quine)
#' posterior <- MCpoissongamma(quine$Days, 15, 1, 5000)
#' summary(posterior)
#' plot(posterior)
#' grid <- seq(14,18,0.01)
#' plot(grid, dgamma(grid, 15, 1), type="l", col="red", lwd=3, ylim=c(0,1.3),
#'   xlab="lambda", ylab="density")
#' lines(density(posterior), col="blue", lwd=3)
#' legend(17, 1.3, c("prior", "posterior"), lwd=3, col=c("red", "blue"))
#' }
#'
MCpoissongamma <- function(y, alpha, beta, mc=1000, ...) {

   # check data
   if(any(y < 0)) {
      cat("Error: Some counts negative in y.\n")
      stop("Please respecify and call function again.")
    }
   n <- length(y)

   # check other parameters
   check.gamma.prior(alpha, beta)
   check.mc.parameter(mc)

   # draw sample and return
   output <- mcmc(matrix(rgamma(mc, alpha+sum(y), beta+n),mc,1))
   varnames(output) <- as.list("lambda")
   attr(output,"title") <- "MCpoissongamma Posterior Sample"

   return(output)
}

## Monte Carlo simulation from the likelihood of a
## Normal distribution with a Normal(mu0, tau20) prior
## the variance sigma2 is known
## ADM 1/26/2006
#' Monte Carlo Simulation from a Normal Likelihood (with known variance) with a
#' Normal Prior
#'
#' This function generates a sample from the posterior distribution of a Normal
#' likelihood (with known variance) with a Normal prior.
#'
#' \code{MCnormalnormal} directly simulates from the posterior distribution.
#' This model is designed primarily for instructional use.  \eqn{\mu} is
#' the parameter of interest of the Normal distribution.  We assume a conjugate
#' normal prior:
#'
#' \deqn{\mu \sim \mathcal{N}(\mu_0, \tau^2_0)}
#'
#' \eqn{y} is a vector of observed data.
#'
#' @param y The data.
#'
#' @param sigma2 The known variance of y.
#'
#' @param mu0 The prior mean of mu.
#'
#' @param tau20 The prior variance of mu.
#'
#' @param mc The number of Monte Carlo draws to make.
#'
#' @param ... further arguments to be passed
#'
#' @return An mcmc object that contains the posterior sample.  This object can
#' be summarized by functions provided by the coda package.
#'
#' @export
#'
#' @seealso \code{\link[coda]{plot.mcmc}}, \code{\link[coda]{summary.mcmc}}
#'
#' @keywords models
#'
#' @examples
#'
#' \dontrun{
#' y <- c(2.65, 1.80, 2.29, 2.11, 2.27, 2.61, 2.49, 0.96, 1.72, 2.40)
#' posterior <- MCMCpack:::MCnormalnormal(y, 1, 0, 1, 5000)
#' summary(posterior)
#' plot(posterior)
#' grid <- seq(-3,3,0.01)
#' plot(grid, dnorm(grid, 0, 1), type="l", col="red", lwd=3, ylim=c(0,1.4),
#'    xlab="mu", ylab="density")
#' lines(density(posterior), col="blue", lwd=3)
#' legend(-3, 1.4, c("prior", "posterior"), lwd=3, col=c("red", "blue"))
#' }
#'
MCnormalnormal <- function(y, sigma2, mu0, tau20, mc=1000, ...) {

   n <- length(y)
   if(sigma2 <= 0) {
      cat("Error: Known variance sigma2 is less than or equal to zero.\n")
      stop("Please respecify and call function again.")
    }

   # check other parameters
   check.normal.prior(mu0, tau20)
   check.mc.parameter(mc)

   # draw sample and return
   mu1 = (1/tau20 * mu0 + n/sigma2 * mean(y)) / (1/tau20 + n/sigma2)
   tau21 = 1 / (1/tau20 + n/sigma2)
   output <- mcmc(matrix(rnorm(mc, mu1, sqrt(tau21)),mc,1))
   varnames(output) <- as.list("mu")
   attr(output,"title") <- "MCnormalnormal Posterior Sample"

   return(output)
}

## Monte Carlo simulation from the likelihood of a
## multinomal distribution with a Dirichlet(alpha) prior
#' Monte Carlo Simulation from a Multinomial Likelihood with a Dirichlet Prior
#'
#' This function generates a sample from the posterior distribution of a
#' multinomial likelihood with a Dirichlet prior.
#'
#' \code{MCmultinomdirichlet} directly simulates from the posterior
#' distribution.  This model is designed primarily for instructional use.
#' \eqn{\pi} is the parameter of interest of the multinomial distribution.
#' It is of dimension \eqn{(d \times 1)}. We assume a conjugate
#' Dirichlet prior:
#'
#' \deqn{\pi \sim \mathcal{D}irichlet(\alpha_0)}
#'
#' \eqn{y} is a \eqn{(d \times 1)} vector of
#' observed data.
#'
#' @param y A vector of data (number of successes for each category).
#'
#' @param alpha0 The vector of parameters of the Dirichlet prior.
#'
#' @param mc The number of Monte Carlo draws to make.
#'
#' @param ... further arguments to be passed
#'
#' @return An mcmc object that contains the posterior sample.  This object can
#' be summarized by functions provided by the coda package.
#'
#' @export
#'
#' @seealso \code{\link[coda]{plot.mcmc}}, \code{\link[coda]{summary.mcmc}}
#' @keywords models
#'
#' @examples
#'
#' \dontrun{
#' ## Example from Gelman, et. al. (1995, p. 78)
#' posterior <- MCmultinomdirichlet(c(727,583,137), c(1,1,1), mc=10000)
#' bush.dukakis.diff <- posterior[,1] - posterior[,2]
#' cat("Pr(Bush > Dukakis): ",
#'    sum(bush.dukakis.diff > 0) / length(bush.dukakis.diff), "\n")
#' hist(bush.dukakis.diff)
#' }
#'
MCmultinomdirichlet <- function(y, alpha0, mc=1000, ...) {

   # check data
   d <- length(y)
   if(any(y < 0)) {
      cat("Error: Some counts negative in y.\n")
      stop("Please respecify and call function again.")
    }

   # check alpha
   if(length(alpha0) != d) {
      cat("Error: Dimension of alpha and y do not match.\n")
      stop("Please respecify and call function again.")
   }
   if(any(alpha0 <= 0)) {
      cat("Error: At least one alpha in Dirichlet prior less than or equal to zero.\n")
      stop("Please respecify and call function again.")
   }

   # draw sample and return
   output <- mcmc(rdirichlet(mc,y + alpha0))
   varnames(output) <- paste("pi.", 1:d, sep="")
   attr(output,"title") <- "MCmultinomdirichlet Posterior Sample"

   return(output)
}