1predict.ecoNPX <- function(object, newdraw = NULL, subset = NULL,
2                           obs = NULL, cond = FALSE, verbose = FALSE, ...){
3
4  if (is.null(newdraw) && is.null(object$mu))
5    stop("Posterior draws of mu and Sigma must be supplied")
6  else if (!is.null(newdraw)){
7    if (is.null(newdraw$mu) && is.null(newdraw$Sigma))
8      stop("Posterior draws of both mu and Sigma must be supplied.")
9    object <- newdraw
10  }
11
12  n.draws <- dim(object$mu)[1]
13  n <- dim(object$mu)[3]
14  mu <- aperm(coef(object, subset = subset, obs = obs), c(2,3,1))
15
16  if (is.null(subset))
17    subset <- 1:n.draws
18  if (is.null(obs))
19    obs <- 1:n
20  Sigma <- aperm(object$Sigma[subset,,obs], c(2,3,1))
21
22  if (cond) { # conditional prediction
23    X <- object$X
24    res <- .C("preDPX", as.double(mu), as.double(Sigma), as.double(X),
25              as.integer(n), as.integer(n.draws), as.integer(2),
26              as.integer(verbose), pdStore = double(n.draws*2*n),
27              PACKAGE="eco")$pdStore
28    res <- matrix(res, ncol=2, nrow=n.draws*n, byrow=TRUE)
29    colnames(res) <- c("W1", "W2")
30  }
31  else { # unconditional prediction
32    res <- .C("preDP", as.double(mu), as.double(Sigma), as.integer(n),
33              as.integer(n.draws), as.integer(3), as.integer(verbose),
34              pdStore = double(n.draws*3*n), PACKAGE="eco")$pdStore
35
36    res <- matrix(res, ncol=3, nrow=n.draws*n, byrow=TRUE)
37    colnames(res) <- c("W1", "W2", "X")
38  }
39
40  class(res) <- c("predict.eco", "matrix")
41  return(res)
42}
43