File: predict.eco.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 (34 lines) | stat: -rw-r--r-- 1,029 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
predict.eco <- function(object, newdraw = NULL, subset = NULL,
                        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
  }

  mu <- coef(object, subset = subset)
  n.draws <- nrow(mu)
  p <- ncol(mu)
  Sigma <- varcov(object, subset = subset)
  
  Wstar <- matrix(NA, nrow=n.draws, ncol=p)
  tmp <- floor(n.draws/10)
  inc <- 1
  for (i in 1:n.draws) {
    Wstar[i,] <- mvrnorm(1, mu = mu[i,], Sigma = Sigma[,,i])
    if (i == inc*tmp & verbose) {
      cat("", inc*10, "percent done.\n")
      inc <- inc + 1
    }
  }
  res <- apply(Wstar, 2, invlogit)
  if (ncol(res) == 2)
    colnames(res) <- c("W1", "W2")
  else # this is called from predict.ecoX
    colnames(res) <- c("W1", "W2", "X")
  class(res) <- c("predict.eco", "matrix")
  return(res)
}