## NB : Low number of draws for speedup. Consider using more draws!
## Gelman and Meng (1991) kernel function
GelmanMeng <- function(x, A = 1, B = 0, C1 = 3, C2 = 3, log = TRUE)
{
if (is.vector(x))
x <- matrix(x, nrow = 1)
r <- -.5 * (A * x[,1]^2 * x[,2]^2 + x[,1]^2 + x[,2]^2
- 2 * B * x[,1] * x[,2] - 2 * C1 * x[,1] - 2 * C2 * x[,2])
if (!log)
r <- exp(r)
as.vector(r)
}
## Run the AdMit function to fit the mixture approximation
set.seed(1234)
outAdMit <- AdMit(KERNEL = GelmanMeng,
mu0 = c(0.0, 0.1),
control = list(Ns = 2e3, Np = 5e2, Hmax = 4))
## Use importance sampling with the mixture approximation as the
## importance density
outAdMitIS <- AdMitIS(N = 1e4, KERNEL = GelmanMeng, mit = outAdMit$mit)
print(outAdMitIS)
## A scalar-valued function of interest: E[theta_1^2]
G.square <- function(theta) theta[,1]^2
print(AdMitIS(N = 1e4, KERNEL = GelmanMeng, G = G.square, mit = outAdMit$mit))
## A matrix-valued function of interest, with an extra argument passed
## through '...': the posterior covariance matrix around 'mu'
G.cov <- function(theta, mu)
{
G.cov_sub <- function(x)
(x - mu) %*% t(x - mu)
theta <- as.matrix(theta)
tmp <- apply(theta, 1, G.cov_sub)
if (length(mu) > 1)
t(tmp)
else
as.matrix(tmp)
}
outAdMitIS <- AdMitIS(N = 1e4, KERNEL = GelmanMeng, G = G.cov,
mit = outAdMit$mit, mu = c(1.459, 1.459))
print(matrix(outAdMitIS$ghat, 2, 2))
Run the code above in your browser using DataLab