aboutsummaryrefslogtreecommitdiff
path: root/R/occuTTD.R
blob: c9965f592cc80fd311ee9269d57c0ed657c91cd0 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#  Fit the time-to-detection model of Garrard et al. 2008, 2013
#  with dynamic extension

occuTTD <- function(psiformula=~1, gammaformula=~1, epsilonformula=~1,
                    detformula=~1, data,  ttdDist=c('exp','weibull'),
                    linkPsi = c("logit", "cloglog"), starts, method = "BFGS",
                    se = TRUE, engine = c("C", "R"), ...) {

  #Check arguments-------------------------------------------------------------
  if(!is(data, "unmarkedFrameOccuTTD")){
    stop("Data is not an unmarkedFrameOccuTTD object.")
  }

  engine <- match.arg(engine, c("C", "R"))
  ttdDist <- match.arg(ttdDist, c("exp","weibull"))
  linkPsi <- match.arg(linkPsi, c("logit","cloglog"))

  formula <- list(psiformula, gammaformula, epsilonformula, detformula)
  check_no_support(formula)
  formula <- as.formula(paste(unlist(formula),collapse=" "))

  #Psi link function
  linkFunc <- plogis
  invlink <- "logistic"
  linkGrad <- "logistic.grad"
  if(linkPsi == "cloglog"){
    linkFunc <- cloglog
    invlink <- "cloglog"
    linkGrad <- "cloglog.grad"
  }

  #Process input data----------------------------------------------------------
  designMats <- getDesign(data, formula)
  #V = detection; W = occupancy
  V <- designMats$V; W <- designMats$W
  X.gam <- designMats$X.gam; X.eps <- designMats$X.eps
  y <- designMats$y
  removed <- designMats$removed.sites

  N <- nrow(y)
  R <- ncol(y)
  T <- data@numPrimary
  J <- R / T

  #Reformat data for likelihood
  yvec <- as.numeric(t(y))
  naflag <- as.numeric(is.na(yvec))
  surveyLength <- data@surveyLength
  if(length(removed>0)) surveyLength <- surveyLength[-removed,]
  ymax <- as.numeric(t(surveyLength))
  delta <- as.numeric(yvec<ymax)

  #Organize parameters---------------------------------------------------------
  detParms <- colnames(V); nDP <- ncol(V)
  occParms <- colnames(W); nOP <- ncol(W)
  psi_inds <- 1:nOP

  gamParms <- NULL; nGP <- 0; col_inds <- c(0,0)
  epsParms <- NULL; nEP <- 0; ext_inds <- c(0,0)
  if(T>1){

    #Remove final year from col/ext design matrices
    X.gam <- as.matrix(X.gam[-seq(T,N*T,by=T),,drop=FALSE])
    X.eps <- as.matrix(X.eps[-seq(T,N*T,by=T),,drop=FALSE])

    gamParms <- colnames(X.gam); nGP <- ncol(X.gam)
    epsParms <- colnames(X.eps); nEP <- ncol(X.eps)
    col_inds <- (nOP+1):(nOP+nGP)
    ext_inds <- (nOP+nGP+1):(nOP+nGP+nEP)
  }
  det_inds <- (nOP+nGP+nEP+1):(nOP+nGP+nEP+nDP)

  parms <- c(occParms,gamParms,epsParms,detParms)
  if(ttdDist == "weibull") parms <- c(parms, "k")
  nP <- length(parms)

  #Likelihood functions--------------------------------------------------------

  nll_R <- function(params){

    #Get occupancy and detection parameters
    psi <- linkFunc(W %*% params[psi_inds])
    psi <- cbind(1-psi, psi)
    lam <- exp(V %*% params[det_inds])

    #Simplified version of Garrard et al. 2013 eqn 5
    #Extended to Weibull
    if(ttdDist=='weibull'){
      k <- exp(params[nP])
      e_lamt <- ( k*lam*(lam*yvec)^(k-1) )^delta * exp(-1*(lam*yvec)^k)
    } else {
      e_lamt <- lam^delta * exp(-lam*yvec)
    }

    #Get probability of y for each z state
    get_Py <- function(e_lamt, delta){
      sum_delt <- as.numeric(sum(delta, na.rm=T)>0)
      c(1-sum_delt, prod(e_lamt[!is.na(e_lamt)]))
    }

    #If dynamic, get col/ext probs and transition prob matrix
    if(T>1){
      col <- plogis(X.gam %*% params[col_inds])
      ext <- plogis(X.eps %*% params[ext_inds])
      phi <- cbind(1-col, col, ext, 1-ext)
    }

    #Begin likelihood calculation
    lik <- rep(NA,N)
    ystart <- 1
    phi_index <- 1
    for (n in 1:N){

      phi_prod <- diag(2)
      #If dynamic, iterate through primary periods
      if(T>1){
        for (t in 1:(T-1)){
          yend <- ystart+J-1
          D_pt <- diag(get_Py(e_lamt[ystart:yend], delta[ystart:yend]))
          phi_t <- matrix(phi[phi_index,],nrow=2, byrow=TRUE)
          phi_prod <- phi_prod %*% ( D_pt %*% phi_t )
          ystart <- ystart + J
          phi_index <- phi_index + 1
        }
      }

      yend <- ystart+J-1
      p_T <- get_Py(e_lamt[ystart:yend], delta[ystart:yend])
      ystart <- ystart + J

      lik[n] <- psi[n,] %*% phi_prod %*% p_T
    }
    -sum(log(lik))
  }

  nll_C <- function(params){
    .Call("nll_occuTTD",
          params, yvec, delta, W, V, X.gam, X.eps,
          range(psi_inds)-1, range(det_inds)-1,
          range(col_inds)-1, range(ext_inds)-1,
          linkPsi, ttdDist, N, T, J, naflag,
          PACKAGE = "unmarked")
  }

  nll <- nll_C
  if(engine == "R") nll <- nll_R

  #Run optim()-----------------------------------------------------------------
  if(!missing(starts) && length(starts) != nP)
      stop(paste("The number of starting values should be", nP))
  if(missing(starts)) starts <- rep(0, nP)

  fm <- optim(starts, nll, method = method, hessian = se, ...)
  covMat <- invertHessian(fm, nP, se)

  #Build output object---------------------------------------------------------
  ests <- fm$par
  fmAIC <- 2 * fm$value + 2 * nP #+ 2*nP*(nP + 1)/(M - nP - 1)
  names(ests) <- parms

  psi <- unmarkedEstimate(name = "Occupancy", short.name = "psi",
                          estimates = ests[psi_inds],
                          covMat = as.matrix(covMat[psi_inds,psi_inds]),
                          invlink = invlink,
                          invlinkGrad = linkGrad)

  det <- unmarkedEstimate(name = "Detection", short.name = "lam",
                          estimates = ests[det_inds],
                          covMat = as.matrix(covMat[det_inds,det_inds]),
                          invlink = "exp",
                          invlinkGrad = "exp")

  if(T>1){
    col <- unmarkedEstimate(name = "Colonization", short.name = "col",
                          estimates = ests[col_inds],
                          covMat = as.matrix(covMat[col_inds,col_inds]),
                          invlink = "logistic",
                          invlinkGrad = "logistic.grad")

    ext <- unmarkedEstimate(name = "Extinction", short.name = "ext",
                          estimates = ests[ext_inds],
                          covMat = as.matrix(covMat[ext_inds,ext_inds]),
                          invlink = "logistic",
                          invlinkGrad = "logistic.grad")


    estimateList <- unmarkedEstimateList(list(psi = psi, col = col,
                                            ext = ext, det=det))
  } else {
    estimateList <- unmarkedEstimateList(list(psi = psi, det=det))
  }

  #Add Weibull shape parameter if necessary
  if(ttdDist=="weibull"){
    estimateList@estimates$shape <- unmarkedEstimate(name = "Weibull shape",
    short.name = "k", estimates = ests[nP],
    covMat = as.matrix(covMat[nP, nP]), invlink = "exp",
    invlinkGrad = "exp")
  }

  umfit <- new("unmarkedFitOccuTTD", fitType = "occuTTD",
               call = match.call(),
               formula = formula,
               psiformula = psiformula,
               gamformula = gammaformula,
               epsformula = epsilonformula,
               detformula = detformula,
               data = data, sitesRemoved = removed,
               estimates = estimateList,
               AIC = fmAIC, opt = fm, negLogLike = fm$value,
               nllFun = nll)

  return(umfit)
}