R/initvals.R

Defines functions initvals

Documented in initvals

#' Generate initial status for nodes
#'
#' This function generates the initial status for nodes, in terms of the presence of management information or focal species 
#'
#' Updated 2020-09-05

#' @param init.p4 the proportion of initial locations for presence
#' @param dist4 the type of locations where initial presence occurs: 'random' indicates all equally likely, 'upedge' indicates that nodes closest to the upper edge have presence, 'rightedge' indicates that nodes closest to the right edge have presence
#' @param init.n4 the number of initial locations for presence
#' @param norp4 'num' indicates initial number for presence, 'prop' indicates initial proportion
#' @param plotmp if T, then map of presence and absence is plotted
#' @param geocoords4s n by 2 matrix of geographic coordinates, provided for a particular system or generated by function genlocs

#' @keywords initial conditions
#' @export
#' @examples

#' x2 <- initvals(j2 <- genlocs(xrange4=c(0,50), yrange4=c(0,50), numnodes4=100, randgeo4=TRUE),dist4='random', init.p4=0.1, norp4='prop', plotmp=T)

#' x3 <- initvals(j2 <- genlocs(xrange4=c(0,50), yrange4=c(0,50), numnodes4=100, randgeo4=TRUE),dist4='upedge', init.p4=0.3, norp4='prop', plotmp=T)

#' x4 <- initvals(j2 <- genlocs(xrange4=c(0,50), yrange4=c(0,50), numnodes4=100, randgeo4=TRUE), dist4='rightedge', init.p4=0.3, norp4='prop',plotmp=T)

#' x5 <- initvals(geocoords4s=matrix(c(1,5, 2,5, 3,5, 4,5), byrow=T, ncol=2), dist4='upedge', norp4='num', init.n4=2, plotmp=T) 

#' x6 <- initvals(geocoords4s=matrix(c(5,1, 5,2, 5,3, 5,4), byrow=T, ncol=2), dist4='rightedge', norp4='num', init.n4=2, plotmp=T) 


initvals <- function(geocoords4s, dist4, init.p4, init.n4, norp4, plotmp=F){

  nodlen <- dim(geocoords4s)[1]


  if(norp4 == 'prop'){init.n4 <- round(nodlen*init.p4)}

  initvec <- 0*(1:nodlen)

  if(dist4 == 'random'){
    j <- sample(x=1:nodlen, size=init.n4)
    initvec[j] <- 1
  }

  else if (dist4 == 'upedge'){
    temp1 <- rank(geocoords4s[,2]) > nodlen - init.n4
    temp2 <- cumsum(temp1) <= init.n4
    initvec[temp1 & temp2] <- 1
  }

  else if (dist4 == 'rightedge'){
    temp1 <- rank(geocoords4s[,1]) > nodlen - init.n4
    temp2 <- cumsum(temp1) <= init.n4
    initvec[temp1 & temp2] <- 1
  }

  if(plotmp){
    plot(geocoords4s)
    points(geocoords4s[initvec==1,], pch=16)
  }


  initvec
}
GarrettLab/SeaMonster2 documentation built on Dec. 17, 2021, 9:27 p.m.