r - 如何使用来自 rjags/JAGS 的估计值来预测值

标签 r jags

在建立模型并使用 Gibbs Sampling 进行训练后,我得到了所有隐藏值预测的结果:

jags <- jags.model('example.bug',
               data = data,
               n.chains = 4,
               n.adapt = 100)

update(jags, 1000)

samples <- jags.samples(jags,
         c('r','alpha','alpha_i','alpha_u','u','i'),
         1000)

哪里r是一个评级列表,其中一些被保留用于模型的预测。假设我可以通过 r[test] 得到它们,其中 test是一个整数列表,指示保留评级的索引。但是当我试图让他们使用这种方式时:
summary(samples$r, mean)[test]

我刚得到这个:
$drop.dims
iteration     chain 
 1000         4 

你能告诉我如何获得期望值吗?先感谢您!

最佳答案

抽奖samples
如果没有您的数据或模型,我将使用简单示例 here 进行演示,修改以便 jags 监控预测结果。

library(rjags)

# simulate some data    
N <- 1000
x <- 1:N
epsilon <- rnorm(N, 0, 1)
y <- x + epsilon

# define a jags model
writeLines("
  model {
    for (i in 1:N){
      y[i] ~ dnorm(y.hat[i], tau)
      y.hat[i] <- a + b * x[i]
    }
    a ~ dnorm(0, .0001)
    b ~ dnorm(0, .0001)
    tau <- pow(sigma, -2)
    sigma ~ dunif(0, 100)
  }
", con = "example2_mod.jags")

# create a jags model object
jags <- jags.model("example2_mod.jags",
                   data = list('x' = x,
                               'y' = y,
                               'N' = N),
                   n.chains = 4,
                   n.adapt = 100)

# burn-in
update(jags, 1000)

# drawing samples gives mcarrays
samples <- jags.samples(jags, c('a', 'b'), 1000)
str(samples)
# List of 2
#  $ a: mcarray [1, 1:1000, 1:4] -0.0616 -0.0927 -0.0528 -0.0844 -0.06 ...
#   ..- attr(*, "varname")= chr "a"
#  $ b: mcarray [1, 1:1000, 1:4] 1 1 1 1 1 ...
#   ..- attr(*, "varname")= chr "b"
# NULL

提取预测

我们的结果,samples , 是 mcarray 的列表尺寸为 1 x 迭代 x 链的对象。此时您确实想运行诊断程序,但我们将跳到总结后验样本以进行预测。一种方法是在链和迭代中取平均值。
# extract posterior means from the mcarray object by marginalizing over 
# chains and iterations (alternative: posterior modes)
posterior_means <- apply(samples$y.hat, 1, mean)
head(posterior_means)
# [1] 0.9201342 1.9202996 2.9204649 3.9206302 4.9207956 5.9209609

# reasonable?
head(predict(lm(y ~ x)))
#         1         2         3         4         5         6 
# 0.9242663 1.9244255 2.9245847 3.9247439 4.9249031 5.9250622 

样本外预测

或者,您可以通过以下方法进行样本外预测。我将重用我们现有的特征向量 x ,但这可能是测试数据。
# extract posterior means from the mcarray object by marginalizing over chains and iterations (alternative: posterior modes)
posterior_means <- lapply(samples, apply, 1, "mean")
str(posterior_means)
# List of 3
#  $ a    : num -0.08
#  $ b    : num 1
#  $ y.hat: num [1:1000] 0.92 1.92 2.92 3.92 4.92 ...
# NULL


# create a model matrix from x
X <- cbind(1, x)
head(X)
#        x
# [1,] 1 1
# [2,] 1 2
# [3,] 1 3
# [4,] 1 4
# [5,] 1 5
# [6,] 1 6

# take our posterior means 
B <- as.matrix(unlist(posterior_means[c("a", "b")]))
#          [,1]
# a -0.07530888
# b  1.00015874

给定模型,预测结果为 a + b * x[i]正如我们在 jags 中所写的那样。
# predicted outcomes are the product of our model matrix and estimates
y_hat <- X %*% B
head(y_hat)
#           [,1]
# [1,] 0.9248499
# [2,] 1.9250086
# [3,] 2.9251673
# [4,] 3.9253261
# [5,] 4.9254848
# [6,] 5.9256436

关于r - 如何使用来自 rjags/JAGS 的估计值来预测值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36051765/

相关文章:

r - 如何使用 JAGS 对来自不同参数族的有限组件的混合进行建模?

model - 错误传递模型 JAGS

R中的RDF整数

r - 在 RMarkdown Slidy Presentations 中自定义 CSS

r - 如何在 R 中堆叠光栅文件?

bayesian - 在 JAGS 中运行逻辑模型 - 你可以矢量化而不是循环个别案例吗?

r - 如何使用 foreach 在 Windows 上实现并行锯齿?

r - 有没有办法为调试目的转储 JAGS 节点值?

R:使用通用图例比例分别绘制和保存光栅砖层

r - ggplot 和 rshiny plot 未刷新