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

时间:2016-03-17 04:36:22

标签: r jags

在设置模型并使用Gi​​bbs 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 

您能告诉我如何获得预期价值吗?提前谢谢!

1 个答案:

答案 0 :(得分:2)

绘制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

提取预测

我们的结果samplesmcarray个对象的列表,其维度为1 x iterations x chains。此时您真的想要运行诊断程序,但我们会跳过总结来自后验的样本进行预测。一种方法是在链和迭代上取平均值。

# 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

鉴于该模型,我们用jags写的预测结果是a + b * x[i]

# 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