File: predict.ecoNPX.R

package info (click to toggle)
r-cran-eco 3.1-6-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 672 kB
  • ctags: 163
  • sloc: ansic: 4,183; makefile: 7
file content (42 lines) | stat: -rw-r--r-- 1,504 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
predict.ecoNPX <- function(object, newdraw = NULL, subset = NULL,
                           obs = NULL, cond = FALSE, verbose = FALSE, ...){

  if (is.null(newdraw) && is.null(object$mu))
    stop("Posterior draws of mu and Sigma must be supplied")
  else if (!is.null(newdraw)){
    if (is.null(newdraw$mu) && is.null(newdraw$Sigma))
      stop("Posterior draws of both mu and Sigma must be supplied.")
    object <- newdraw
  }

  n.draws <- dim(object$mu)[1]
  n <- dim(object$mu)[3]
  mu <- aperm(coef(object, subset = subset, obs = obs), c(2,3,1))
  
  if (is.null(subset))
    subset <- 1:n.draws
  if (is.null(obs))
    obs <- 1:n
  Sigma <- aperm(object$Sigma[subset,,obs], c(2,3,1))

  if (cond) { # conditional prediction
    X <- object$X
    res <- .C("preDPX", as.double(mu), as.double(Sigma), as.double(X),
              as.integer(n), as.integer(n.draws), as.integer(2),
              as.integer(verbose), pdStore = double(n.draws*2*n),
              PACKAGE="eco")$pdStore
    res <- matrix(res, ncol=2, nrow=n.draws*n, byrow=TRUE)
    colnames(res) <- c("W1", "W2")
  }
  else { # unconditional prediction
    res <- .C("preDP", as.double(mu), as.double(Sigma), as.integer(n),
              as.integer(n.draws), as.integer(3), as.integer(verbose),
              pdStore = double(n.draws*3*n), PACKAGE="eco")$pdStore
    
    res <- matrix(res, ncol=3, nrow=n.draws*n, byrow=TRUE)
    colnames(res) <- c("W1", "W2", "X")
  }
  
  class(res) <- c("predict.eco", "matrix")
  return(res)
}