aboutsummaryrefslogtreecommitdiff
path: root/R/unmarkedCrossVal.R
blob: a49d0675e0038ee60a9c1b29c2fa87377d36d81d (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
216
217
218
219
220
221
222
setGeneric("crossVal", function(object,
    method=c("Kfold","holdout","leaveOneOut"), folds=10, holdoutPct=0.25,
    statistic=RMSE_MAE, ...) standardGeneric("crossVal"))

setClass("unmarkedCrossVal",
    representation(stats = "data.frame",
                   summary = "data.frame",
                   method = "character",
                   folds = "numeric",
                   holdoutPct = "numeric"),
    validity=function(object){
      errors <- character(0)
      hp <- object@holdoutPct
      if(hp<0|hp>1){
        errors <- c(errors,"holdoutPct must be between 0 and 1")
      }
    }
)

#Constructor of crossVal objects
setMethod("crossVal", "unmarkedFit",
          function(object, method=c("Kfold","holdout","leaveOneOut"),
                   folds=10, holdoutPct=0.25,
                   statistic=RMSE_MAE, parallel=FALSE, ncores, ...){

  method <- match.arg(method, c('Kfold','holdout','leaveOneOut'))

  if(method=="Kfold" & !is.integer(folds) & folds < 0){
    stop("folds must be a positive integer")
  }
  if(method=="holdout" & (holdoutPct>1 | holdoutPct<0)){
    stop("holdoutPct must be a proportion between 0 and 1")
  }

  partitions <- switch(method,
    Kfold = partitionKfold(object, folds=folds),
    holdout = partitionHoldout(object, holdoutPct=holdoutPct),
    leaveOneOut = partitionLeaveOneOut(object)
  )

  n_reps <- length(partitions)

  check_stat <- statistic(object, ...)
  if(!is.numeric(check_stat)||is.null(names(check_stat))){
    stop("Function provided to statistic argument must return a named numeric vector")
  }

  do_crossval <- function(i, object, partitions, statistic, ...){
    newfit <- unmarked::update(object, data=partitions[[i]]$trainData)
    newfit@data <- partitions[[i]]$testData
    if(!is.null(attributes(newfit)$knownOcc)){
      newfit@knownOcc <- rep(FALSE,numSites(newfit@data))
    }

    statistic(newfit, ...)
  }

  if(parallel){
    if(missing(ncores)) ncores <- parallel::detectCores()-1
    cl <- parallel::makeCluster(ncores)
    on.exit(parallel::stopCluster(cl))
    stat_raw <- pblapply(1:n_reps, do_crossval, object,
                                     partitions, statistic, ..., cl = cl)
  } else {
    stat_raw <- pblapply(1:n_reps, do_crossval, object,
                       partitions, statistic, ...)
  }

  stats <- as.data.frame(do.call("rbind", stat_raw))

  summary <- data.frame(Estimate=sapply(stats, mean, na.rm=TRUE),
                        SD=sapply(stats, sd, na.rm=TRUE))

  out <- new("unmarkedCrossVal", stats=stats, summary=summary, method=method,
             folds=folds, holdoutPct=holdoutPct)

  out
})

#Kfold partition function
partitionKfold <- function(object, folds){

  site_inds <- 1:numSites(object@data)
  shuf_site_inds <- sample(site_inds,numSites(object@data))
  fold_inds <- cut(site_inds, breaks=folds, labels=FALSE)

  fold_list <- vector(length=folds,"list")
  for (i in 1:folds){

    trainInds <- shuf_site_inds[fold_inds!=i]
    testInds <- shuf_site_inds[fold_inds==i]

    fold_list[[i]]$trainData <- object@data[trainInds,]
    fold_list[[i]]$testData <-  object@data[testInds,]
  }
  fold_list
}

#Holdout partition function
partitionHoldout <- function(object, holdoutPct){

  site_inds <- 1:numSites(object@data)
  shuf_site_inds <- sample(site_inds,numSites(object@data))

  splitInd <- round(numSites(object@data)*(1-holdoutPct))
  trainInds <- shuf_site_inds[1:splitInd]
  testInds <- shuf_site_inds[(splitInd+1):length(shuf_site_inds)]

  fold_list <- vector(length=1,"list")
  fold_list[[1]]$trainData <- object@data[trainInds,]
  fold_list[[1]]$testData <- object@data[testInds,]

  fold_list
}

#leave-one-out
partitionLeaveOneOut <- function(object){

  fold_list <- vector(length=numSites(object@data),"list")
  for (i in seq_along(fold_list)){
    fold_list[[i]]$trainData <- object@data[-i,]
    fold_list[[i]]$testData <- object@data[i,]
  }
  fold_list

}

setMethod("show", "unmarkedCrossVal", function(object)
{
  st <- object@stats

  if(object@method=='Kfold'){
    cat(paste('Method: k-fold (',object@folds,' folds)\n\n',sep=''))
  } else if(object@method=='holdout'){
    cat(paste('Method: holdout (',round(object@holdoutPct*100),
              '% in test set)\n\n',sep=''))
  } else if(object@method=='leaveOneOut'){
    cat('Method: leave-one-out\n\n')
  }

  for (i in 1:length(st)){
    cat(paste0(names(st)[i],':\n'))
    print(data.frame(object@summary[i,]), row.names=FALSE, digits=4)
    if(i != length(st)) cat('\n')
  }
})

setClass("unmarkedCrossValList",
    representation(stats_list="list",
                   method = "character",
                   folds="numeric",
                   holdoutPct="numeric",
                   sort="character")
)

#CrossVal list constructor
setMethod("crossVal", "unmarkedFitList",
          function(object, method=c("Kfold","holdout","leaveOneOut"),
                   folds=10, holdoutPct=0.25,
                   statistic=RMSE_MAE, parallel=FALSE, ncores,
                   sort = c("none", "increasing", "decreasing"), ...){

    method <- match.arg(method, c('Kfold','holdout','leaveOneOut'))
    sort <- match.arg(sort, c('none','increasing','decreasing'))

    if(missing(ncores)) ncores <- parallel::detectCores()-1
    stats <- lapply(object@fits, crossVal, method, folds,
                    holdoutPct, statistic, parallel, ncores, ...)

    out <- new("unmarkedCrossValList", stats_list=stats, method=method,
               folds=folds, holdoutPct=holdoutPct, sort=sort)

})


setMethod("show", "unmarkedCrossValList", function(object){

  sl <- object@stats_list
  mod_names <- names(sl)
  nfits <- length(sl)
  nstats <- length(sl[[1]]@stats)
  stat_names <- names(sl[[1]]@stats)

  if(object@method=='Kfold'){
    cat(paste('Method: k-fold (',object@folds,' folds)\n\n',sep=''))
  } else if(object@method=='holdout'){
    cat(paste('Method: holdout (',round(object@holdoutPct*100),'% in test set)\n\n',sep=''))
  } else if(object@method=='leaveOneOut'){
    cat('Method: leave-one-out\n\n')
  }

  for (i in 1:nstats){
    cat(paste0(stat_names[i],':\n'))

    stat_sum = lapply(sl, function(x) x@summary[i,])
    stat_sum = do.call("rbind", stat_sum)

    sort_ind <- switch(object@sort,
                       none = 1:nrow(stat_sum),
                       increasing = order(stat_sum$Estimate),
                       decreasing = order(stat_sum$Estimate, decreasing=TRUE))
    stat_sum <- stat_sum[sort_ind, ]

    print(stat_sum, digits=4)
    if(i != nstats) cat('\n')
  }
})

#Function to calculate RMSE and MAE
#Default function for statistic argument
#Returns a named list
RMSE_MAE <- function(object){

  res <- residuals(object)
  if(is.list(res)) res <- unlist(res)

  mae <- mean(abs(res), na.rm=T)
  rmse <- sqrt(mean(res^2, na.rm=T))

  c(`Root mean square error`=rmse, `Mean absolute error`=mae)
}