rbchan / unmarked

R package for hierarchical models in ecological research
https://rbchan.github.io/unmarked/
37 stars 25 forks source link

Add maxOrder argument to unmarkedFrameOccuMulti #136

Closed kenkellner closed 4 years ago

kenkellner commented 5 years ago

With large numbers of species you can easily run out of memory just building the unmarkedFrame with occuMulti, even if you plan to later limit your analysis to just two-way interactions. You should be able to limit the order of interactions when building the frame to avoid this.

kenkellner commented 4 years ago

Modified function:

unmarkedFrameOccuMulti <- function(y, siteCovs = NULL, obsCovs = NULL,
                                   maxOrder, mapInfo = NULL)
{
  ylist <- y
  y <- ylist[[1]]
  J <- ncol(y)
  if(is.null(names(ylist)))
    names(ylist) <- paste('sp',1:length(ylist),sep='')

  obsCovs <- covsToDF(obsCovs, "obsCovs", J, nrow(y))

  #f design matrix guide
  S <- length(ylist)
  z <- expand.grid(rep(list(1:0),S))[,S:1]
  colnames(z) <- names(ylist)

  if(missing(maxOrder)) maxOrder <- S
  if(maxOrder == 1){
    fDesign <- as.matrix(z)
  } else {
    fDesign <- model.matrix(as.formula(paste0("~.^",maxOrder,"-1")),z)
  }

  attr(fDesign,'assign') <- NULL
  zinds <- apply(z,1,function(x) paste(x,collapse=''))
  rownames(fDesign) <- paste('psi[',zinds,']',sep='')
  colnames(fDesign) <- paste('f',1:ncol(fDesign),'[',
                             colnames(fDesign),']',sep='')

  umfmo <- new("unmarkedFrameOccuMulti", y=y, ylist = ylist, fDesign=fDesign,
               obsCovs = obsCovs, siteCovs = siteCovs, obsToY = diag(J))
  return(umfmo)
}