Understanding error estimation used in mcmc package

时间:2019-03-19 15:06:40

标签: r bayesian mcmc

I am looking at the MCMC Example tutorial from the package mcmc in R. The code below is given there, where MCMC is used to compute the parameters in a logistic regression example.

library(mcmc) 

data(logit)
foo <- logit

out <- glm(y ~ x1 + x2 + x3 + x4, data = foo, family = binomial())

x <- foo
x$y <- NULL 
x <- as.matrix(x)
x <- cbind(1, x)
dimnames(x) <- NULL
y <- foo$y

lupost <- function(beta, x, y){
  eta <- x %*% beta
  p <- 1/(1 + exp(-eta))

  logl <- sum(log(p[y == 1])) + sum(log(1-p[y == 0]))

  return(logl + sum(dnorm(beta, 0, 2, log = TRUE)))
}

set.seed(42)
beta.init <- as.numeric(coefficients(out))
out <- metrop(lupost, beta.init, 1000, x=x, y = y)

The idea is to compute the standard Monte Carlo error. Therefore

> apply(out$batch, 2, mean)
[1] 0.6531950 0.7920342 1.1701075 0.5077331 0.7488265
[6] 0.5145751 0.7560775 1.4973807 0.3913837 0.7244162

is used. My question is, what is the meaning of the final 5 columns in the output of this command? The tutorial states that it is somehow E(X^2). But in which line is X generated? What is X here?

1 个答案:

答案 0 :(得分:1)

如果仅运行您在上面的问题中发布的代码,那么我只会得到5个数字:

tk.call

您所遵循的“教程”似乎是apply(out$batch, 2, mean) # [1] 0.5990174 0.8027482 1.1539329 0.4547702 0.7172724 小插图。该小插图完成了您在上面发布的所有步骤,然后还会计算

mcmc::demo

跟着

out <- metrop(out, nbatch = 100, blen = 100, outfun = function(z) c(z, z^2))

在这里您可以看到apply(out$batch, 2, mean) # [1] 0.6531950 0.7920342 1.1701075 0.5077331 0.7488265 # [6] 0.5145751 0.7560775 1.4973807 0.3913837 0.7244162 正在计算X和X ^ 2,并且使用outfun取平均值时,您可以获得E [X]和E [X ^ 2]的估计。

此处的X似乎来自模型参数(β)的后验分布。请注意,贝叶斯后均值(前5个数字)与代码第四行中用apply()计算的非贝叶斯点估计非常接近。