简体   繁体   中英

How to predict values using estimates from rjags / JAGS

After setting up the model and training it with Gibbs Sampling, I got the result of all the prediction of hidden values with:

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)

Where r is a list of rating, and some of them are withheld for a prediction with the model. And suppose I can get them with r[test] , where test is a list of integer indicating the index of the rating withheld. But when I tried to get them using this way:

summary(samples$r, mean)[test]

I just got this:

$drop.dims
iteration     chain 
 1000         4 

Could you please tell me how to get the expected value? Thank you in advance!

draw samples

Absent your data or model I'll demonstrate using the simple example here , modified so that jags monitors the predicted outcomes.

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

extract predictions

Our result, samples , is a list of mcarray objects with dimensions 1 x iterations x chains. You'd really want to run diagnostics at this point, but we'll jump to summarizing the samples from the posterior for your predictions. One approach is taking the mean over chains and iterations.

# 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 

out-of-sample predictions

Alternatively, here's how you could make out-of-sample predictions. I'll just reuse our existing feature vector x , but this could be test data instead.

# 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

Given the model, the predicted outcome is a + b * x[i] as we wrote in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM