R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 Patched (2005-08-04), ISBN 3-900051-07-0 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. Natural language support but running in an English locale 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 a HTML browser interface to help. Type 'q()' to quit R. > invisible(options(echo = TRUE)) > > foo <- read.table(url("http://www.stat.umn.edu/geyer/PhD/F03/logit.txt"), + header = TRUE) > > x <- foo > x$y <- NULL > x <- as.matrix(x) > x <- cbind(1, x) > dimnames(x) <- NULL > y <- foo$y > > lupost <- function(beta) { + eta <- x %*% beta + p <- 1 / (1 + exp(- eta)) # note: works even when eta is Inf or -Inf + logl <- sum(log(p[y == 1])) + sum(log(1 - p[y == 0])) + return(logl + sum(dnorm(beta, 0, 2, log = TRUE))) + } > > library(mcmc) > set.seed(42) > beta.init <- rep(0, ncol(x)) > out <- metrop(lupost, beta.init, blen = 100, nbatch = 1e2, scale = 0.4) > out$accept [1] 0.232 > > fred <- function(z) { + sally <- 1 / (1 + exp(- z[1])) + c(sally, sally^2) + } > > out <- metrop(out, nbatch = 1e3, outfun = fred) > out$accept [1] 0.23566 > > # plot(out$batch[ , 1]) > # plot(out$batch[ , 2]) > # acf(out$batch[ , 1]) > # acf(out$batch[ , 2]) > # barely signif lag-1, double batch length? > > mu.hat <- mean(out$batch[ , 1]) > mu.mcse <- sd(out$batch[ , 1]) / sqrt(out$nbatch) > print(mu.hat) [1] 0.6577262 > print(mu.mcse) [1] 0.0008380032 > > var.hat <- mean(out$batch[ , 2] - mu.hat^2) > ##### bogus > var.mcse <- sd(out$batch[ , 2] - mu.hat^2) / sqrt(out$nbatch) > ##### right > delta1 <- out$batch[ , 1] - mean(out$batch[ , 1]) > delta2 <- out$batch[ , 2] - mean(out$batch[ , 2]) > var.mcse.ok <- sqrt(mean((delta2 - 2 * mu.hat * delta1)^2) / out$nbatch) > print(var.hat) [1] 0.004427835 > print(var.mcse) [1] 0.001103689 > print(var.mcse.ok) [1] 6.48155e-05 > > sigma.hat <- sqrt(var.hat) > sigma.mcse <- var.mcse / (2 * sigma.hat) > sigma.mcse.ok <- var.mcse.ok / (2 * sigma.hat) > print(sigma.hat) [1] 0.06654198 > print(sigma.mcse) [1] 0.008293178 > print(sigma.mcse.ok) [1] 0.0004870272 > > ##### looks like merely doubling the batch length should do it > > out <- metrop(out, blen = 250, outfun = fred) > out$accept [1] 0.234812 > > mu.hat <- mean(out$batch[ , 1]) > mu.mcse <- sd(out$batch[ , 1]) / sqrt(out$nbatch) > print(mu.hat) [1] 0.6566396 > print(mu.mcse) [1] 0.0005460812 > > var.hat <- mean(out$batch[ , 2] - mu.hat^2) > ##### bogus > var.mcse <- sd(out$batch[ , 2] - mu.hat^2) / sqrt(out$nbatch) > ##### right > delta1 <- out$batch[ , 1] - mean(out$batch[ , 1]) > delta2 <- out$batch[ , 2] - mean(out$batch[ , 2]) > var.mcse.ok <- sqrt(mean((delta2 - 2 * mu.hat * delta1)^2) / out$nbatch) > print(var.hat) [1] 0.004376975 > print(var.mcse) [1] 0.0007199504 > print(var.mcse.ok) [1] 4.002801e-05 > > sigma.hat <- sqrt(var.hat) > sigma.mcse <- var.mcse / (2 * sigma.hat) > sigma.mcse.ok <- var.mcse.ok / (2 * sigma.hat) > print(sigma.hat) [1] 0.06615871 > print(sigma.mcse) [1] 0.005441085 > print(sigma.mcse.ok) [1] 0.000302515 > > print(out$nbatch) [1] 1000 > print(out$blen) [1] 250 > print(out$nspac) [1] 1 > print(out$nburn) NULL > print(out$nbatch * out$blen) [1] 250000 > > proc.time() [1] 42.42 0.09 42.93 0.00 0.00 >