83
83
# ' in the FishSET database.
84
84
# ' @param spatID Required when `alt_var = 'nearest point'`. Variable in `spat`
85
85
# ' that identifies the individual zones or areas.
86
+ # ' @param grid_sample Logical, TRUE if randomly sampling from all alternatives or
87
+ # ' from a separate point-grid table. If TRUE, then also need to specify the
88
+ # ' sample size (i.e., number of alternatives for each observation).
89
+ # ' @param grid_sample_n Integer, indicating the sample size for grid sampling
90
+ # ' (i.e., number of alternative to sample).
86
91
# ' @param outsample Logical, indicating whether this is for primary data or out-of
87
92
# ' sample data.
88
93
# ' @importFrom DBI dbExecute
@@ -137,110 +142,91 @@ create_alternative_choice <-
137
142
fish.cent.name = NULL ,
138
143
spatname = NULL ,
139
144
spatID = NULL ,
145
+ grid_sample = FALSE ,
146
+ grid_sample_n = NULL ,
140
147
outsample = FALSE ) {
141
148
142
149
# Call in datasets
143
150
out <- data_pull(dat , project )
144
151
dataset <- out $ dataset
145
152
153
+ # Parse main data table
146
154
if (outsample ){ # save dat as out-of-sample dataset
147
155
outsample_dat <- dat
148
156
dat <- parse_data_name(outsample_dat , " outsample" , project )
149
157
} else {
150
158
dat <- parse_data_name(dat , " main" , project )
151
159
}
152
160
161
+ # Parse spatial data object
153
162
spat_out <- data_pull(spatname , project )
154
163
spatdat <- spat_out $ dataset
155
164
spat <- parse_data_name(spatname , " spat" , project )
156
165
157
- column_check( dataset , cols = c( zoneID , occasion_var ))
166
+ # Check args ----
158
167
168
+ # Run a series of checks for various input args
169
+ # Stop executing code in this function if anything here fails
170
+ column_check(dataset , cols = c(zoneID , occasion_var ))
159
171
o_len <- length(occasion_var )
160
-
161
-
162
172
if (occasion == " zonal centroid" ) {
163
-
164
173
if (o_len != 2 & is_value_empty(zone.cent.name )) {
165
-
166
174
stop(" 'zone.cent.name' is required." , call. = FALSE )
167
175
}
168
176
}
169
177
170
178
if (occasion == " fishing centroid" ) {
171
-
172
179
if (o_len != 2 & is_value_empty(fish.cent.name )) {
173
-
174
180
stop(" 'fish.cent.name' is required." , call. = FALSE )
175
181
}
176
182
}
177
183
178
184
if (alt_var == " zonal centroid" & is_value_empty(zone.cent.name )) {
179
-
180
185
stop(" 'zone.cent.name' is required." , call. = FALSE )
181
186
}
182
187
183
188
if (alt_var == " fishing centroid" & is_value_empty(fish.cent.name )) {
184
-
185
189
stop(" 'fish.cent.name' is required." , call. = FALSE )
186
190
}
187
191
188
- zone_cent <- NULL
189
- fish_cent <- NULL
190
-
191
- # check args ----
192
-
193
192
occasion_opts <- c(" zonal centroid" , " fishing centroid" , " port" , " lon-lat" )
194
193
alt_opts <- c(" zonal centroid" , " fishing centroid" , " nearest point" )
195
194
196
195
if (o_len > 2 ) stop(" Invalid values for 'occasion_var'." , call. = FALSE )
197
196
198
197
if (! occasion %in% occasion_opts ) {
199
-
200
198
stop(" Invalid option for 'occasion'. Options are " ,
201
199
paste0(occasion_opts , collapse = " , " ), " ." , call. = FALSE )
202
200
}
203
201
204
202
if (! alt_var %in% alt_opts ) {
205
-
206
203
stop(" Invalid option for 'alt_var'. Options are " ,
207
204
paste0(alt_opts , collapse = " , " ), " ." , call. = FALSE )
208
205
}
209
206
210
207
# check occasion_var for lon-lat string match
211
208
ll_occ_check <- function (occ ) {
212
-
213
209
ll_check <- grepl(" lon|lat" , occ , ignore.case = TRUE )
214
-
215
210
if (! any(ll_check )) {
216
-
217
211
warning(" Check that 'occasion_var' contains longitude and latitude " ,
218
- " variables: " , paste0(occ , collapse = " , " ),
219
- call. = FALSE )
212
+ " variables: " , paste0(occ , collapse = " , " ), call. = FALSE )
220
213
}
221
214
}
222
215
223
216
cent_check <- function (project , cent.tab , type = " zone" ) {
224
-
225
217
cent_type <- switch (type , zone = " Zonal" , fish = " Fishing" )
226
-
227
218
cent_exists <- table_exists(cent.tab , project )
228
-
229
219
if (! cent_exists ) {
230
-
231
220
stop(cent_type , " centroid table must be saved to FishSET Database. Run " ,
232
221
" create_centroid()." , call. = FALSE )
233
222
}
234
223
235
224
cent_tab <- table_view(cent.tab , project )
236
-
237
225
if (! any(cent_tab $ ZoneID %in% unique(dataset [[zoneID ]]))) {
238
-
239
226
stop(' Zones do not match between centroid table and zonal assignments ' ,
240
227
' in primary data table. Rerun find_centroid() using same spatial data file ' ,
241
228
' as was using with the assignment_column() function.' , call. = FALSE )
242
229
}
243
-
244
230
cent_tab
245
231
}
246
232
@@ -249,37 +235,33 @@ create_alternative_choice <-
249
235
' mile' , ' miles' , ' nmile' , ' nmiles' )
250
236
251
237
if (! dist.unit %in% valid_units ) {
252
-
253
238
stop(dist.unit , " is not an available unit. Unit options are: " ,
254
239
paste0(valid_units , collapse = " , " ), call. = FALSE )
255
240
}
256
241
257
242
# alt_var ----
258
- # # zonal centroid ----
243
+ # Initialize empty centroid vars
244
+ zone_cent <- NULL
245
+ fish_cent <- NULL
259
246
247
+ # # zonal centroid ----
260
248
if (alt_var == " zonal centroid" ) {
261
-
262
249
zone_cent <- cent_check(project , zone.cent.name , " zone" )
263
250
}
264
251
265
252
# # fishing centroid ----
266
-
267
253
if (alt_var == " fishing centroid" ) {
268
-
269
254
fish_cent <- cent_check(project , fish.cent.name , " fish" )
270
255
}
271
256
272
257
# # nearest point ----
273
258
if (alt_var == " nearest point" ) {
274
-
275
259
if (is_value_empty(spatdat ) | is_value_empty(spatID )) {
276
-
277
260
stop(" 'spat' and 'spatID' are required for alt_var = 'nearest point'" ,
278
261
call. = FALSE )
279
262
}
280
263
281
264
if (! any(unique(spatdat [[spatID ]]) %in% unique(dataset [[zoneID ]]))) {
282
-
283
265
stop(" There are no shared zones between dat and spat. Check that 'spatID' " ,
284
266
" and 'zoneID' are correct, or rerun assignment_column()." , call. = FALSE )
285
267
}
@@ -292,53 +274,48 @@ create_alternative_choice <-
292
274
if (is_value_empty(occasion_var ) | o_len == 1 ) {
293
275
294
276
if (is.null(zone_cent )) {
295
-
296
277
zone_cent <- cent_check(project , zone.cent.name , " zone" )
297
278
}
298
279
299
280
} else if (o_len == 2 ) {
300
-
301
281
ll_occ_check(occasion_var )
302
282
303
283
} else {
304
-
305
284
stop(" Invalid 'occasion_var'." , call. = FALSE )
306
285
}
286
+
307
287
# # fishing centroid ----
308
288
} else if (occasion == " fishing centroid" ) {
309
289
310
290
if (is_value_empty(occasion_var ) | o_len == 1 ) {
311
291
312
292
if (is.null(fish_cent )) {
313
-
314
293
fish_cent <- cent_check(project , fish.cent.name , " fish" )
315
294
}
316
295
317
296
} else if (o_len == 2 ) {
318
-
319
297
ll_occ_check(occasion_var )
320
298
321
299
} else {
322
300
323
301
stop(" Invalid 'occasion_var'." , call. = FALSE )
324
- } # # port ----
325
-
302
+ }
303
+
304
+ # # port ----
326
305
} else if (occasion == " port" ) {
327
306
328
307
if (is_value_empty(occasion_var )) {
329
-
330
308
stop(" Port column name required for 'occasion = port'." , call. = FALSE )
331
309
}
332
310
333
311
if (o_len == 2 ) {
334
-
335
312
ll_occ_check(occasion_var )
336
313
}
314
+
337
315
# # lon-lat ----
338
316
} else if (occasion == " lon-lat" ) {
339
317
340
318
if (o_len != 2 ) {
341
-
342
319
stop(" 'occasion_var' must contain a longitude and latitude column." ,
343
320
call. = FALSE )
344
321
}
@@ -354,7 +331,6 @@ create_alternative_choice <-
354
331
choice <- dataset [[zoneID ]]
355
332
356
333
if (anyNA(choice ) == TRUE ) {
357
-
358
334
warning(" No zone identified for " , sum(is.na(choice )), " observations. These " ,
359
335
" observations will be removed in future analyses." , call. = FALSE )
360
336
}
@@ -366,7 +342,6 @@ create_alternative_choice <-
366
342
zoneCount [zoneCount $ n < min.haul , zoneID ] <- NA
367
343
368
344
if (all(is.na(zoneCount [[zoneID ]]))) {
369
-
370
345
stop(" No zones meet criteria. No data will be included in further analyses." ,
371
346
" Check the 'min.haul' parameter or zone identification." , call. = FALSE )
372
347
}
@@ -390,6 +365,21 @@ create_alternative_choice <-
390
365
spat_out <- spat
391
366
}
392
367
368
+ # Grid-sampling ----
369
+ if (grid_sample ){
370
+ # Create a matrix with nrows = observations, and columns = alts. The first column = choice.
371
+ rand_alts_mat <- matrix (choice , nrow = length(choice ), ncol = 1 )
372
+
373
+ draw_alts <- function (choice , all_alts , numAlts ){
374
+ c(choice , sample(all_alts [all_alts != choice ], numAlts ))
375
+ }
376
+
377
+ rand_alts_mat <- do.call(rbind , lapply(rand_alts_mat , draw_alts , all_alts = zone_cent $ ZoneID , numAlts = grid_sample_n ))
378
+
379
+ } else {
380
+ rand_alts_mat <- NULL
381
+ }
382
+
393
383
# alt choice list ----
394
384
Alt <- list (
395
385
dataZoneTrue = dataZoneTrue , # index to identify which obs to use in model
@@ -410,7 +400,10 @@ create_alternative_choice <-
410
400
fish_cent_name = fish.cent.name ,
411
401
spat = spatdat ,
412
402
spatID = spatID ,
413
- spatname = spat
403
+ spatname = spat ,
404
+ grid_sample = grid_sample ,
405
+ grid_sample_n = grid_sample_n ,
406
+ rand_alts_mat = rand_alts_mat
414
407
)
415
408
416
409
# write Alt to datafile ----
0 commit comments