MAJOR=4 MINOR=59 DEVNAME=tty59 6ˆl–ß=¿J¥4Ò‚€ ¸îõ1hß�þB9\Y±¶û�„C•–�üÿÁ ƒqð ÀÄÃö### R code from vignette source 'bfst.Rnw' ### Encoding: UTF-8 ################################################### ### code chunk number 1: foo ################################################### options(keep.source = TRUE, width = 65) ################################################### ### code chunk number 2: library ################################################### library(mcmc) ################################################### ### code chunk number 3: baz ################################################### baz <- library(help = "mcmc") baz <- baz$info[[1]] baz <- baz[grep("Version", baz)] baz <- sub("^Version: *", "", baz) bazzer <- paste(R.version$major, R.version$minor, sep = ".") ################################################### ### code chunk number 4: set-seed ################################################### set.seed(42) ################################################### ### code chunk number 5: frequentist ################################################### data(logit) out <- glm(y ~ x1 + x2 + x3 + x4, data = logit, family = binomial, x = TRUE) summary(out) ################################################### ### code chunk number 6: models ################################################### varnam <- names(coefficients(out)) varnam <- varnam[varnam != "(Intercept)"] nvar <- length(varnam) models <- NULL foo <- seq(0, 2^nvar - 1) for (i in 1:nvar) { bar <- foo %/% 2^(i - 1) bar <- bar %% 2 models <- cbind(bar, models, deparse.level = 0) } colnames(models) <- varnam models ################################################### ### code chunk number 7: neighbor ################################################### neighbors <- matrix(FALSE, nrow(models), nrow(models)) for (i in 1:nrow(neighbors)) { for (j in 1:ncol(neighbors)) { foo <- models[i, ] bar <- models[j, ] if (sum(foo != bar) == 1) neighbors[i, j] <- TRUE } } ################################################### ### code chunk number 8: ludfun ################################################### modmat <- out$x y <- logit$y ludfun <- function(state, log.pseudo.prior) { stopifnot(is.numeric(state)) stopifnot(length(state) == ncol(models) + 2) icomp <- state[1] stopifnot(icomp == as.integer(icomp)) stopifnot(1 <= icomp && icomp <= nrow(models)) stopifnot(is.numeric(log.pseudo.prior)) stopifnot(length(log.pseudo.prior) == nrow(models)) beta <- state[-1] inies <- c(TRUE, as.logical(models[icomp, ])) beta.logl <- beta beta.logl[! inies] <- 0 eta <- as.numeric(modmat %*% beta.logl) logp <- ifelse(eta < 0, eta - log1p(exp(eta)), - log1p(exp(- eta))) logq <- ifelse(eta < 0, - log1p(exp(eta)), - eta - log1p(exp(- eta))) logl <- sum(logp[y == 1]) + sum(logq[y == 0]) logl + sum(dnorm(beta, 0, 2, log = TRUE)) + log.pseudo.prior[icomp] } ################################################### ### code chunk number 9: try1 ################################################### state.initial <- c(nrow(models), out$coefficients) qux <- rep(0, nrow(models)) out <- temper(ludfun, initial = state.initial, neighbors = neighbors, nbatch = 1000, blen = 100, log.pseudo.prior = qux) names(out) out$time ################################################### ### code chunk number 10: what ################################################### ibar <- colMeans(out$ibatch) ibar ################################################### ### code chunk number 11: adjust ################################################### qux <- qux + pmin(log(max(ibar) / ibar), 10) qux <- qux - min(qux) qux ################################################### ### code chunk number 12: iterate ################################################### lout <- suppressWarnings(try(load("bfst1.rda"), silent = TRUE)) if (inherits(lout, "try-error")) { qux.save <- qux time.save <- out$time repeat{ out <- temper(out, log.pseudo.prior = qux) ibar <- colMeans(out