R/covDiag.R

Defines functions print.covDiag covDiag.unmarkedFitPCount covDiag.unmarkedFramePCount covDiag.default covDiag

Documented in covDiag covDiag.default covDiag.unmarkedFitPCount covDiag.unmarkedFramePCount print.covDiag

##covariance diagnostic for N-mixture model
##code slightly modified from Dennis et al. 2015: Biometrics 71: 237-246
##values <= 0 suggest lambda is infinite (data too sparse) and is likely
##to introduce problems during model fitting

##generic
covDiag <- function(object, ...){
  UseMethod("covDiag", object)
}



covDiag.default <- function(object, ...){
  stop("\nFunction not yet defined for this object class\n")
}



##for unmarkedFramePcount
covDiag.unmarkedFramePCount <- function(object, ...){
  yMat <- object@y
  p1 <- ct <- 0
  nvisits <- ncol(yMat)
  for(i in 1:(nvisits - 1)){
    for(j in (i+1):nvisits){
      p1 <- p1 + yMat[,i]*yMat[,j]
      ct <- ct+1
    }
  }
  cov.diag <- mean(p1, na.rm = TRUE)/ct-mean(yMat, na.rm = TRUE)^2

  if(cov.diag <= 0) {
      msg <- "Warning: lambda is infinite, data too sparse"
  } else {
      msg <- NULL
  }
  out <- list("cov.diag" = cov.diag,
              "message" = msg)
  class(out) <- "covDiag"
  return(out)
}



##pcount
covDiag.unmarkedFitPCount <- function(object, ...){
  yMat <- object@data@y
  p1 <- ct <- 0
  nvisits <- ncol(yMat)
  for(i in 1:(nvisits - 1)){
    for(j in (i+1):nvisits){
      p1 <- p1 + yMat[,i]*yMat[,j]
      ct <- ct+1
    }
  }
  cov.diag <- mean(p1, na.rm = TRUE)/ct-mean(yMat, na.rm = TRUE)^2

  if(cov.diag <= 0) {
      msg <- "Warning: lambda is infinite, data too sparse"
  } else {
      msg <- NULL
  }

  out <- list("cov.diag" = cov.diag,
              "message" = msg)
  class(out) <- "covDiag"
  return(out)
}



print.covDiag <- function(x, digits = 4, ...) {
  cat("\nCovariance diagnostic: ", round(x$cov.diag, digits), "\n")
  if(!is.null(x$message)) {
    cat(x$message, "\n")
  }
}

Try the AICcmodavg package in your browser

Any scripts or data that you put into this service are public.

AICcmodavg documentation built on Nov. 17, 2023, 1:08 a.m.