Package 'FishSET'

Title: Spatial Economics Toolbox for Fisheries
Description: The Spatial Economics Toolbox for Fisheries (FishSET) is a set of tools for organizing data; developing, improving and disseminating modeling best practices.
Authors: Lisa Pfeiffer [aut, cre], Paul G Carvalho [aut] (ORCID: <https://orcid.org/0000-0003-2106-4954>), Anna Abelman [aut], Alan Haynie [aut] (ORCID: <https://orcid.org/0000-0003-0981-2016>)
Maintainer: Lisa Pfeiffer <[email protected]>
License: MIT + file LICENSE
Version: 2.0.1
Built: 2026-10-07 09:08:46 UTC
Source: https://github.com/noaa-nwfsc/FishSET

Help Index


Aggregating function

Description

Aggregating function

Usage

agg_helper(
  dataset,
  value,
  period = NULL,
  group = NULL,
  within_group = NULL,
  fun = "sum",
  count = FALSE,
  format_tab = "decimal"
)

Arguments

dataset

'MainDataTable' to aggregate.

value

String, name of variable to aggregate.

period

String, name of period variable to aggregate by. Primarily for internal use. Places temporal variables to the right-end of the summary table.

group

String, name of grouping variable(s) to aggregate by.

within_group

String, name of grouping variable(s) for calculating within group percentages. fun = "percent" and period or group are required.

fun

String, function name to aggregate by. Also accepts anonymous functions. To calculate percentage, set fun = "percent"; this will return the percent of total when within_group = NULL.

count

Logical, if TRUE then returns the number of observations by period and/or group.

format_tab

String. Options include "decimal" (default), "scientific", and "PrettyNum" (rounds to two decimal places and uses commas).

Examples

## Not run: 

# total catch by port
agg_helper(pollockMainDataTable, value = "OFFICIAL_TOTAL_CATCH_MT", 
           group = "PORT_CODE", fun = "sum")

# count permits
agg_helper(pollockMainDataTable, value = "PERMIT", count = TRUE, fun = NULL)

# count permits by gear type
agg_helper(pollockMainDataTable, value = "PERMIT", group = "GEAR_TYPE",
           count = TRUE, fun = NULL)

# percent of total by gear type
agg_helper(pollockMainDataTable, value = "PERMIT", group = "GEAR_TYPE",
           count = TRUE, fun = "percent")
 
# within group percentage          
agg_helper(pollockMainDataTable, value = "OFFICIAL_TOTAL_CATCH_MT", 
           fun = "percent", group = c("PORT_CODE", "GEAR_TYPE"), 
           within_group = "PORT_CODE")

## End(Not run)

Get Alternative Choice List

Description

Returns the Alternative Choice list from the FishSET database.

Usage

alt_choice_list(project, name = NULL)

Arguments

project

Name of project.

name

Name of Alternative Choice list in the FishSET database. The table name will contain the string "AltMatrix". If NULL, the default table is returned. Use tables_database to see a list of FishSET database tables by project.


Set x-axis labels to 45 degrees

Description

Set x-axis labels to 45 degrees

Usage

angled_theme()

Assign observations to fishing zones

Description

Assign each observation in the primary dataset to a fishery management or regulatory zone. This function is primarily called by other functions that require zone assignment but can also be used on its own.

Usage

assignment_column(
  dat,
  project,
  spat,
  lon_dat,
  lat_dat,
  zoneID_spat,
  name = "ZoneID",
  closest_pt = FALSE,
  bufferval = NULL,
  lon_spat = NULL,
  lat_spat = NULL,
  hull_polygon = FALSE,
  epsg = NULL,
  log_fun = TRUE
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

name of project.

spat

Spatial data containing information on fishery management or regulatory zones. sf objects are recommended, but sp objects can be used as well. If using a spatial table read from a csv file, then arguments lon_spat and lat_spat are required. To upload your spatial data to the FishSETFolder see load_spatial.

lon_dat

Longitude variable in dat.

lat_dat

Latitude variable in dat.

zoneID_spat

Variable or list in spat that identifies the individual areas or zones. If spat is class sf, zoneID_spat should be name of list containing information on zones.

name

The name of the new assignment column. Defaults to "ZoneID".

closest_pt

Logical, if TRUE, observations that fall outside zones are classed as the closest zone polygon to the point.

bufferval

Maximum buffer distance, in meters, for assigning observations to the closest zone polygon. If the observation is not within the defined bufferval, then it will not be assigned to a zone polygon. Required if closest_pt = TRUE.

lon_spat

Variable or list from spat containing longitude data. Required for spatial tables read from csv files. Leave as NULL if spat is an sf or sp object.

lat_spat

Variable or list from spat containing latitude data. Required for spatial tables read from csv files. Leave as NULL if spat is an sf or sp object.

hull_polygon

Logical, if TRUE, creates convex hull polygon. Use if spatial data creating polygon are sparse or irregular.

epsg

EPSG code. Manually set the epsg code, which will be applied to spat and dat. If epsg is not specified but is defined for spat, then the spat epsg will be applied to dat. In addition, if epsg is not specified and epsg is not defined for spat, then a default epsg value will be applied to spat and dat (epsg = 4326). See http://spatialreference.org/ to help identify optimal epsg number.

log_fun

Logical, whether to log function call (for internal use).

Details

Function uses the specified latitude and longitude from the primary dataset to assign each row of the primary dataset to a zone. Zone polygons are defined by the spatial dataset. Set hull_polygon to TRUE if spatial data is sparse or irregular. Function is called by other functions if a zone identifier does not exist in the primary dataset.

Value

Returns primary dataset with new assignment column.

Examples

## Not run: 
pollockMainDataTable <- 
     assignment_column(pollockMainDataTable, "pollock", spat = pollockNMFSSpatTable,
                       lon_dat = "LonLat_START_LON", lat_dat = "LonLat_START_LAT")

## End(Not run)

Creates numeric variables divided into equal sized groups

Description

Creates numeric variables divided into equal sized groups

Usage

bin_var(dat, project, var, br, name = "bin", labs = NULL, ...)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

var

Numeric variable in dat to bin into a factor.

br

Numeric vector. If a single number, the range of var is divided into br even groups. If two or more values are given, var is divided into intervals.

name

Variable name to return. Defaults to 'bin'.

labs

A character string of category labels.

...

Additional arguments passed to cut.

Details

Function adds a new factor variable, labeled by name, to the primary dataset. The numeric variable is divided into equal sized groups if the length of br is equal to one and into intervals if the length of br is greater than one.

Value

Returns the primary dataset with binned variable added.

Examples

## Not run: 
 pollockMainDataTable <- bin_var(pollockMainDataTable, 'pollock', 'HAUL', 10, 'HAULCAT')
 pollockMainDataTable <- bin_var(pollockMainDataTable, 'pollock', 'HAUL', c(5,10), 'HAULCAT')

## End(Not run)

Compare bycatch CPUE and total catch/percent of total catch for one or more species

Description

Compare bycatch CPUE and total catch/percent of total catch for one or more species

Usage

bycatch(
  dat,
  project,
  cpue,
  catch,
  date,
  period = "year",
  names = NULL,
  group = NULL,
  sub_date = NULL,
  filter_date = NULL,
  date_value = NULL,
  filter_by = NULL,
  filter_value = NULL,
  filter_expr = NULL,
  facet_by = NULL,
  conv = "none",
  tran = "identity",
  format_lab = "decimal",
  value = "stc",
  combine = FALSE,
  scale = "fixed",
  output = "tab_plot",
  format_tab = "wide"
)

Arguments

dat

Primary data frame over which to apply function. Table in FishSET database should contain the string 'MainDataTable'.

project

name of project.

cpue

A string of CPUE variable names. The function outputs the mean CPUE by period. The variable names must match the order of variable names in catch and names.

catch

A character string of names of catch variables to aggregate. The function outputs the total catch or share of total catch by period depending on the value argument. The order of the catch variable string must match those of the cpue and names arguments.

date

A variable containing dates to aggregate by.

period

Period to aggregate by. Options include 'year', month', and weeks'.

names

An optional string of species names that will be used in the plot. If NULL, then species names from catch will be used.

group

A categorical variable in dat to group by.

sub_date

Date variable used for subsetting, grouping, or splitting by date.

filter_date

The type of filter to apply to 'MainDataTable'. To filter by a range of dates, use filter_date = "date_range". To filter by a given period, use "year-day", "year-week", "year-month", "year", "month", "week", or "day". The argument date_value must be provided.

date_value

This argument is paired with filter_date. To filter by date range, set filter_date = "date_range" and enter a start- and end-date into date_value as a string: date_value = c("2011-01-01", "2011-03-15").

To filter by period (e.g. "year", "year-month"), use integers (4 digits if year, 1-2 digits if referencing a day, month, or week). Use a vector if filtering by a single period: date_filter = "month" and date_value = c(1, 3, 5). This would filter the data to January, March, and May.

Use a list if using a year-period type filter, e.g. "year-week", with the format: list(year, period). For example, filter_date = "year-month" and date_value = list(2011:2013, 5:7) will filter the data table from May through July for years 2011-2013.

filter_by

String, variable name to filter 'MainDataTable' by. the argument filter_value must be provided.

filter_value

A vector of values to filter 'MainDataTable' by using the variable in filter_by. For example, if filter_by = "GEAR_TYPE", filter_value = 1 will include only observations with a gear type of 1.

filter_expr

String, a valid R expression to filter 'MainDataTable' by using the variable in filter_by.

facet_by

Variable name to facet by. Accepts up to two variables. Facetting by "year", "month", or "week" is available if a date variable is added to sub_date.

conv

Convert catch variable to "tons", "metric_tons", or by using a function entered as a string. Defaults to "none" for no conversion.

tran

A function to transform the y-axis. Options include log, log2, log10, sqrt.

format_lab

Formatting option for y-axis labels. Options include "decimal" or "scientific".

value

Whether to return raw catch ("raw") or share of total catch ('stc').

combine

Logical, whether to combine variables listed in group.

scale

Scale argument passed to facet_grid. Defaults to "fixed". Other options include "free_y", "free_x", and "free_xy".

output

Output type. Options include 'table' or 'plot'.

format_tab

How table output should be formatted. Options include 'wide' (the default) and 'long'.

Details

Returns a plot and/or table of the mean CPUE and share of total catch or raw count for each species entered. For optimal plot size in an R Notebook/Markdown document, we recommend including no more than four species. The order of variables in the cpue and catch arguments must be in the same order as in the names argument. The names argument is used to join the catch and cpue variables together.

Value

bycatch() compares the average CPUE and catch total/share of total catch between one or more species. The data can be filtered by date and/or by a variable. filter_date specifies the type of date filter to apply–by date-range or by period. date_value should contain the values to filter the data by. To filter by a variable, enter its name as a string in filter_by and include the values to filter by in filter_value. Only one grouping variable will be displayed; however, any number of variables can be combined by using combine = TRUE, but no more than three is recommended. For faceting, any variable in the dataset can be used, but "year" and "month" are also available provided a date variable is added to sub_date. Generally, no more than four species should be compared, and even fewer when faceting due to limited plot space. A list containing a table and plot are printed to the console and viewer by default. For optimal plot size in an R Notebook/Markdown document, use the chunk option fig.asp = 1.

Examples

## Not run: 
cpue(pollockMainDataTable, "myproject", xWeight = "f1Weight",
  xTime = "Hour", "f1_cpue"
)

bycatch(pollockMainDataTable, "myproject", 
        cpue = c("f1_cpue", "f2_cpue", "f3_cpue", "f4_cpue"),
        catch = c("f1", "f2", "f3", "f4"), date = "FISHING_START_DATE",
        names = c("fish_1", "fish_2", "fish_3", "fish_4"), period = "month",
        date_filter = "year", date_value = 2011, value = "stc", 
        output = "table")

## End(Not run)

Calculate trip centroid variable

Description

Calculate latitude and longitude variables (columns) containing the geographic centroid of each trip

Usage

calc_trip_centroid(dat, project, lon, lat, trip_id, weight_var = NULL)

Arguments

dat

String or data frame. A string for the name of the main data table in the FishSET database (contains 'MainDataTable' in the name). Or a data frame of the main data table.

project

String. Project name.

lon

String. Column name in dat containing longitudinal data.

lat

String. Column name in dat containing latitudinal data.

trip_id

String. Column name that represents the unique trip identifier in dat.

weight_var

String. Optional. Column name in dat to use for computing a weighted average centroid. If NULL (the default), an unweighted (simple) average is calculated.

Details

This function computes the average longitude and latitude for each unique trip, as defined by the trip_id column. If weight_var is specified, the function calculates the weighted centroid.

Value

Returns the original data frame (dataset) with two new columns added: cent_lon (centroid longitude) and cent_lat (centroid latitude).

Examples

## Not run: 
# Assuming 'pollockMainDataTable' is a data frame

pollockMainDataTable <- calc_trip_centroid(
  dat = pollockMainDataTable, 
  project = 'pollock', 
  lon = 'LonLat_START_LON',
  lat = 'LonLat_START_LAT',
  trip_id = "TRIP_ID",
  weight_var = NULL
)

## End(Not run)

Calculate Trip Distance

Description

Calculates the total distance for a fishing trip and adds it as a new column to the dataset.

Usage

calc_trip_distance(
  name = "trip_distance",
  project,
  dat,
  port,
  trip_id,
  haul_order,
  starting_port,
  return_port,
  start_haul_lat,
  start_haul_lon,
  end_haul_lat,
  end_haul_lon,
  distance_unit = "miles",
  a = 6378137,
  f = 1/298.257223563
)

Arguments

name

String. Name of new variable for trip distance. Defaults to 'trip_distance'.

project

String. project name.

dat

String or data frame. A string for the name of the main data table in the FishSET database (contains 'MainDataTable' in the name). Or a data frame of the main data table.

port

String or data frame. A string for the name of the port table in the FishSET project database. Or a data frame of the port table. Note that the port table must at least include a port name, port latitude and port longitude.

trip_id

String. Column name that represents the unique trip identifier in dat.

haul_order

String. Column name in dat that identifies haul order within a trip. Can be time, coded variable, etc.

starting_port

String. Column name in dat containing departure port for each trip.

return_port

String. Column name in dat containing landing port for each trip.

start_haul_lat

String. Column name in dat containing haul starting latitude.

start_haul_lon

String. Column name in dat containing haul starting longitude.

end_haul_lat

String. Column name in dat containing haul end latitude.

end_haul_lon

String. Column name in dat containing haul end longitude.

distance_unit

String. The desired unit for the output distance. Options are "miles" (default), "kilometers", or "meters". Aliases "mi" and "km" are also accepted.

a

Numeric. Major (equatorial) radius of the ellipsoid. The default value is for WGS84 ellipsoid.

f

Numeric. Ellipsoid flattening. The default value is for WGS84 ellipsoid.

Details

This function calculates the total distance traveled during a fishing trip by summing its four key segments: 1. Distance from the departure port to the start of the first haul. 2. The cumulative distance covered *within* each haul. 3. The cumulative distance traveled *between* consecutive hauls. 4. The final distance from the end of the last haul to the return port.

The function uses a vectorized approach with the 'dplyr' package for efficiency and relies on 'geosphere::distGeo' for accurate geodesic distance calculations based on the WGS84 ellipsoid model.

Value

Returns the main data table with a new variable for trip distance.

Examples

## Not run: 
main_data_with_distance <- calc_trip_distance(
  name = "total_trip_miles",
  project = "my_project",
  dat = main_data,
  port = port_data,
  trip_id = "TRIP_ID",
  haul_order = "HAUL_NUMBER",
  starting_port = "DEPARTURE_PORT_NAME",
  return_port = "LANDING_PORT_NAME",
  start_haul_lat = "START_LATITUDE",
  start_haul_lon = "START_LONGITUDE",
  end_haul_lat = "END_LATITUDE",
  end_haul_lon = "END_LONGITUDE",
  distance_unit = "miles"
)

## End(Not run)

Linear Model for Catch

Description

First stage regression model for catch.

Usage

catch_lm(
  dat,
  project,
  catch.formula,
  zoneID = NULL,
  exp.name = NULL,
  new.name = NULL,
  date,
  output = "matrix"
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

catch.formula

A formula object specifying the linear model.See stats::lm().

zoneID

zone ID Variable in dat that identifies the individual zones or areas. Required if merging expected catch into dat using exp.name, or when creating a new expected catch matrix and exp.name is NULL (see output below).

exp.name

Name(s) of expected catch matrix to merge into dat.

new.name

Optional, string. When output = 'matrix', new.name will become the name of the new expected catch matrix saved to the FishSET DB expected catch list. When output = 'dataset', new.name will become the name of the new expected catch variable added to the primary dataset.

date

Date variable from dat used to create expected catch matrix.

output

Whether to output dat with the expected catch variable added ('dataset') or to save an expected catch matrix to the expected catch FishSET DB table ('matrix'). Defaults to output = 'matrix'.

Details

catch_lm() can merge an expected catch matrix into the primary dataset before running the linear model. This is done using by passing exp.name and zoneID to merge_expected_catch() and is for convenience; users can do this separately using merge_expected_catch() if desired, just make sure to leave exp.name empty before running catch_lm(). Merging expected catch in a separate step is useful for creating tables and plots before running a first stage linear regression.

Value

catch_lm() has two output options: dataset and matrix. When output == 'dataset', the primary dataset will be returned with the fitted values from the model added as a new column. The new column is named using new.name.

When output == 'matrix' an expected catch matrix is created and saved to the FishSET DB expected catch list (it is not outputted to the console). There are two ways to create an expected catch matrix: by using an existing expected catch matrix in catch.formula, or by using a zone-identifier column (i.e. zoneID) in the catch.formula. For example, if you have created an expected catch matrix named 'user1' using create_expectations(), catch.formula could equal catch ~ vessel_length * user1. In this case exp.name would equal 'user1'. Alternatively, you could create an expected catch matrix by specifying catch.formula as catch ~ vessel_length * zone. In this case, exp.name = NULL and zoneID = 'zone'.

See Also

merge_expected_catch()


Save Primary Table's Centroid Columns to FishSET Database

Description

Save the unique centroid values from the primary table to the FishSET Database. Use this function if zone ID and centroid longitude/latitude are included in the primary table.

Usage

centroid_to_fsdb(
  dat,
  spat.name = NULL,
  project,
  zoneID,
  cent.lon,
  cent.lat,
  type = "zone"
)

Arguments

dat

Required, main data frame containing data on hauls or trips. Table in FishSET database should contain the string MainDataTable.

spat.name

Optional, a name to associate with the centroid table.

project

Name of project.

zoneID

Variable in dat that identifies the individual zones or areas.

cent.lon

Required, variable in dat that identifies the centroid longitude of zones or areas.

cent.lat

Required, variable in dat that identifies the centroid latitude of zones or areas.

type

The type of centroid. Options include "zone" for zonal centroids and "fish" for fishing centroids.

Details

In certain cases, the user may have the necessary spatial variables to run a discrete choice model included in the primary table when uploaded to FishSET, and does not need a spatial table to assign observations to zones or find centroids (e.g. by using create_centroid()). However, a centroid table table must be saved to the FishSET Database if a centroid option is used to define alternative choice (see create_alternative_choice()). cent_to_fsdb() allows users to save a zonal or fishing centroid table provided they have the required variables: a zone ID (zoneID), a centroid longitude (cent.lon), and a centroid latitude (cent.lat) column.


Change variable data class

Description

View data class for each variable and call appropriate functions to change data class as needed.

Usage

change_class(dat, project, x = NULL, new_class = NULL, save = FALSE)

Arguments

dat

Primary data frame over which to apply function. Table in FishSET database should contain the string 'MainDataTable'.

project

Name of project.

x

A character string of variable(s) in dat that will be changed to new_class. One ore more variables may be included. Default set to NULL.

new_class

A character string of data classes that x should be changed to. Length of new_class should match the length of x unless all variables in x should be the same new_class. Defaults to NULL. Options are "numeric", "factor", "date", "character". Must be in quotes.

save

Logical. Should the data table be saved in the FishSET database, replacing the working data table in the database? Defaults to FALSE.

Details

Returns a table with data class for each variable in dat and changes variable classes. To view variable classes run the function with default settings, specifying only dat and project. If variable class should be changed, run the function again, specifying the variable(s) (x) to be changed and the new_class(es) (new_class). Set save to TRUE to save modified data table.

Value

Table with data class for each variable and the working data with modified data class as specified.

Examples

## Not run: 
#View table without changing class or saving
change_class(pollockMainDataTable, "myproject")

#Change class for a single variable and save data table to FishSET database
change_class(pollockMainDataTable, "myproject", x = "HAUL", new_class = 'numeric', save=TRUE)

#Change class for multiple variables and save data table to FishSET database
change_class(pollockMainDataTable, "myproject", x = c("HAUL","DISEMBARKED_PORT"),
 new_class = c('numeric', 'factor'), save=TRUE)

## End(Not run)

Check and correct spatial data format

Description

Converts spatial data to a sf object

Usage

check_spatdat(spatdat, lon = NULL, lat = NULL, id = NULL)

Arguments

spatdat

Spatial data containing information on fishery management or regulatory zones.

lon

Longitude variable in spatdat. This is required for csv files or if spatdat is a dataframe (i.e. is not a sf or sp object).

lat

Latitude variable in spatdat. This is required for csv files or if spatdat is a dataframe (i.e. is not a sf or sp object).

id

Polygon ID column. This is required for csv files or if spatdat is a dataframe (i.e. is not a sf or sp object).

Details

This function checks whether spatdat is a sf object and attempts to convert it if not. It also applies clean_spat which fixes certain spatial issues such as invalid or empty polygons, whether a projected CRS is used (converts to WGS84 if detected), and if longitude should be shifted to Pacific view (0-360 format) to avoid splitting the Alaska region during plotting.


Retrieve closure scenario names

Description

A helper function used to display the names of currently saved closure scenarios.

Usage

close_names(project)

Arguments

project

Name of project

Details

To retrieve the complete closure scenario file, use get_closure_scenario.


Combine zone and closure area

Description

Creates a new spatial dataset that merges regulatory zones with closure areas.

Usage

combine_zone(spat, closure, grid.nm, closure.nm, recast = TRUE)

Arguments

spat

Spatial file containing regulatory zones.

closure

Closure file containing closure areas.

grid.nm

Character, column name containing grid ID.

closure.nm

Character, column name containing closure ID.

recast

Logical, if TRUE combined is passed to recast_multipoly.

Details

To combine zones with closure areas, this function performs the following steps:

  1. Create the union of the closure area

  2. Take the difference between the closure union and the zone file

  3. Take the intersection of zone and the closure union

  4. Combine the difference and intersection objects into one spatial dataframe

  5. Assign new zone IDs to intersecting polygons

The result is a single spatial dataset containing all polygons from both spat and closure with overlapping (intersecting) polygons receiving new IDs (see new_zone_id). This allows users to partially close regulatory zones during the model design stage.

See Also

recast_multipoly new_zone_id


Confidentialy cache exists

Description

Returns TRUE if confidentiality cache file is found in the project output folder.

Usage

confid_cache_exists(project)

Arguments

project

Name of project.

Examples

## Not run: 
confid_cache_exists("pollock")

## End(Not run)

Create catch or revenue per unit effort variable

Description

Add catch per unit effort (CPUE) or revenue per unit effort variable to the primary dataset. Catch should be a weight variable but can be a count. Effort should be in a duration of time, such as days, hours, or minutes.

Usage

cpue(dat, project, xWeight = NULL, xTime, price = NULL, name = NULL)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Project name.

xWeight

Catch variable in dat. Variable should be a measure of weight (pounds, metric tons, etc) but can also be count. If calculating revenue per unit effort (RPUE) and a revenue column exists in dat, then add the revenue column to price and set xWeight = NULL.

xTime

Duration of time variable in dat representing effort, such as weeks, days, hours, or minutes.

price

Optional, variable from dat containing price/value data. Price is multiplied against the catch variable, xWeight, to generated revenue. If revenue exists in dat and you wish to use this revenue instead of price, then xWeight must be NULL. Defaults to NULL.

name

String, name of created variable. Defaults to "cpue" or "rpue" if price is not NULL.

Details

Creates the catch or revenue per unit effort variable. Catch variable should be in weight (lbs, mts). Effort variable should be a measurement of duration in time. New variable is added to the primary dataset with the column name defined by the name argument. CPUE for individual species should be calculated separately.

Value

Returns primary dataset with CPUE variable added.

Examples

## Not run: 
pollockMainDataTable <- cpue(pollockMainDataTable, 'pollock', 
                             xWeight = 'OFFICIAL_TOTAL_CATCH_MT', 
                             xTime = 'DURATION_IN_MIN', name = 'cpue')

## End(Not run)

Define alternative fishing choice

Description

Required step. Creates a list identifying how alternative fishing choices should be defined. Output is saved to the FishSET database. Run this function before running models. dat must have a zone assignment column (see assignment_column()). In certain cases a centroid table must be saved to the FishSET Database, see occasion_var for details.

Usage

create_alternative_choice(
  dat,
  project,
  alt_name = NULL,
  zoneID,
  occasion = "zonal centroid",
  occasion_var = NULL,
  alt_var = "zonal centroid",
  min_haul = 0,
  spatname = NULL,
  spatID = NULL,
  outsample = FALSE
)

Arguments

dat

Required, Primary data frame containing data on hauls or trips. Table in FishSET database should contain the string MainDataTable.

project

Required, name of project.

alt_name

String, Required. The name to be assigned to this alternative choice list within the FishSET database. If a list with this name already exists, the function will stop.

zoneID

Variable in dat that identifies the individual zones or areas.

occasion

String, determines the starting point when calculating the distance matrix. Options are "zonal centroid", "fishing centroid", "port", or "lon-lat". See occasion_var for requirements.

occasion_var

Identifies an ID column or set of lon-lat variables needed to create the distance matrix. Possible options depend on the value of occasion:

Centroid

When ⁠occasion = zonal centroid⁠ the possible options are NULL, the name of a zone ID variable, or a set coordinate variables (in Lon-Lat order).

NULL

This will merge centroid lon-lat data to the primary table using the column enter in zoneID. A centroid table must be saved to the FishSET Database.

Zone ID

This option specifies the zone ID variable to merge the centroid table to. For example, a column containing the previous zonal area. A centroid table must be saved to the FishSET Database.

Lon-Lat

A string vector of length two containing the longitude and latitude of an existing set centroid variables in dat.

Port

When occasion = port the possible options include the name of a port ID variable or a set of lon-lat variables describing the location of the port. A value of NULL will return an error.

Port ID

The name of a port ID variable in dat that will be used to join the port table to the primary table. A port table is required (see load_port()) which contains the port name and the longitude and latitude of each port.

Lon-Lat

A string vector of length two containing a port's longitude and latitude in dat.

Lon-Lat

When occasion = lon-lat, occasion_var must contain a string vector of length two containing the longitude and latitude of a vessel's location in the dat. For example, the current or previous haul location.

alt_var

Determines the alternative choices used to calculate the distance matrix. alt_var may be the centroid of zonal assignment ("zonal centroid"), "fishing centroid", or the closest point in fishing zone ("nearest point"). The centroid options require that the appropriate centroid table has been saved to the project's FishSET Database. See create_centroid() to create and save centroids. List existing centroid tables by running list_tables("project", type = "centroid").

min_haul

Required, numeric, minimum number of hauls. Zones with fewer hauls than the min_haul value will not be included in model data.

spatname

Required when alt_var = 'nearest point'. spat is a spatial data file containing information on fishery management or regulatory zones boundaries. sf objects are recommended, but sp objects can be used as well. See dat_to_sf() to convert a spatial table read from a csv file to an sf object. To upload your spatial data to the FishSETFolder see load_spatial().If spat should come from the FishSET database, it should be the name of the original file name, in quotes. For example, "pollockNMFSZonesSpatTable". Use tables_database() or list_tables("project", type = "spat") to view the names of spatial tables in the FishSET database.

spatID

Required when alt_var = 'nearest point'. Variable in spat that identifies the individual zones or areas.

outsample

Logical, indicating whether this is for primary data or out-of sample data.

Details

Defines the alternative fishing choices. These choices are used to develop the matrix of distances between observed and alternative fishing choices (where they could have fished but did not). The distance matrix is calculated by the format_model_data() function. occasion defines the observed fishing location and alt_var the alternative fishing location. occasion_var identifies an ID column or set of lon-lat variables needed to create the distance matrix.

Parts of the alternative choice list are pulled by create_expectations(), format_model_data(), and the model run fishset_fit()) functions. These output include choices of which variable to use for catch and which zones to include in analyses based on a minimum number of hauls per trip within a zone. Note that if the alternative choice list is modified, the create_expectations() and format_model_data() functions should also be updated before rerunning models.

Value

Function saves a list of alternative choice matrices to the FishSET database as projectAlternativeChoice. The list includes the alternative choice list from the user-defined choices. Multiple alternative choice cases can be added to the list by specifying unique names. The list is automatically saved to the FishSET database and is called in format_model_data.


Create Centroid Table

Description

Create a zonal or fishing centroid table. The centroid can be joined with the primary data if output = "dataset". The centroid table is automatically saved to the FishSET Database.

Usage

create_centroid(
  spat = NULL,
  dat = NULL,
  project,
  spatID = NULL,
  zoneID = NULL,
  lon.dat = NULL,
  lat.dat = NULL,
  weight.var = NULL,
  type = "zonal centroid",
  names = NULL,
  cent.name = NULL,
  output = "dataset"
)

Arguments

spat

Spatial data containing information on fishery management or regulatory zones. Required for type = "zonal centroid", not required for type = "fishing centroid". spat will be included in centroid table name.

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'. dat is not required if type = "zonal centroid" and output = "centroid table".

project

Name of project.

spatID

Variable or list in spat that identifies the individual areas or zones. If spat is class sf, spatID should be name of list containing information on zones. Ignored if type = "fishing centroid".

zoneID

Variable in dat that identifies zonal assignments. zoneID is not required if type = "zonal centroid" and output = "centroid table".

lon.dat

Longitude variable in dat. Required for type = "fishing centroid".

lat.dat

Latitude variable in dat. Required for type = "fishing centroid".

weight.var

Variable from dat for weighted average (for type = "fishing centroid". only). If weight.var is defined, the centroid is defined by the latitude and longitude of fishing locations in each zone weighted by weight.var.

type

The type of centroid to create. Options include "zonal centroid" and "fishing centroid". See other arguments for type requirements.

names

Character vector of length two containing the names of the fishing centroid columns. The order should be c("lon_name", "lat_name"). The default names are c("weight_cent_lon", "weight_cent_lat") for weighted fishing centroid and c("fish_cent_lon", "fish_cent_lat") for unweighted fishing centroid.

cent.name

A string to include in the centroid table name. Table names take the form of "projectNameZoneCentroid" for zonal centroids and "projectNameFishCentroid" for fishing centroids.

output

Options are "centroid table", "dataset", or "both". "centroid table" returns a table containing the zone name and the longitude and latitude of the centroid. "dataset" returns the primary table joined with the centroid table. "both" returns a list containing the merged primary table and the centroid table.


Interactive application to create distance between points variable

Description

Adds a variable for distance between two points to the primary dataset. There are two versions of this function. The difference between the two versions is how additional arguments specific to start and end locations are added. This version requires only five arguments to be specified before running. Additional arguments specific to identifying the lat/lon of start or end points are added through prompts. This function is designed for an interactive session. The create_dist_between_for_gui function requires all necessary arguments to be specified before running and is best used in a non-interactive session. Both versions of the distance between function require that the start and end points be different vectors. If the start or ending points are from a port then PortTable must be specified to obtain lat/lons. If the start or ending points are the center of a fishing zone or area then spat, lon.dat, lat.dat, cat, lon.spat, and lat.spat must be specified to obtain latitude and longitude.

Usage

create_dist_between(
  dat,
  project,
  start,
  end,
  units = c("miles", "meters", "km", "midpoint"),
  zoneid = NULL,
  name = "distBetween"
)

Arguments

dat

Primary data frame over which to apply function. Table in FishSET database should contain the string 'MainDataTable'.

project

Project name.

start, end

Starting and ending location. Should be a port, lat/lon location, or the fishery management zone/area centroid. or area. If port is desired, start should be the column name in the dat containing the port Latitude and longitude for the port are extracted from the port table. If a lat/lon location is desired then start should be a character string of column names from dat. The order must be lon, lat. If fishery management centroid is used then set start="centroid" or end="centroid". find_centroid and assignment_column will be called to identify the latitude and longitude if the centroid table does not exist in the FishSET database.

units

Unit of measurement for calculated distance between start and ending points. Can be in "miles", "meters", "kilometers", or "midpoint" location.

zoneid

Variable in dat that identifies the individual zones or areas. Define if exists in dat and is not names 'ZoneID'.

name

String, output variable name. Defaults to 'distBetween'.

Details

Additional arguments.
Further arguments are required to identify the latitude and longitude of the starting or ending location if start or end is defined as zonal centroid or a column from primary dataset containing port information, such as departing or embarking port. Prompts will appear asking for required arguments.

Port arguments required:

portTable: Port table from FishSET database. Required if start or end is a port vector.



Centroids arguments required:

spat: Spatial data set containing information on fishery management or regulatory zones. Can be shape file, json, geojson, data frame, or list data frame or list. Required if start or end is centroid.
lon.dat: Longitude variable from dat.
lat.dat: Latitude variable from dat.
lon.spat: Variable or list from spat containing longitude data. Required if start or end is centroid. Leave as NULL if spat is a shape or json file.
lat.spat: Variable or list from spat containing latitude data. Required if start or end is centroid. Leave as NULL if spat is a shape or json file.
cat: Variable or list in spat that identifies the individual areas or zones. If spat is class sf, cat should be the name of list containing information on zones.

Value

Returns primary data set with distance between variable.

Examples

## Not run: 
pollockMainDataTable <- create_dist_between(pollockMainDataTable, 'pollock', 'centroid',
 'EMBARKED_PORT', units = 'miles', 'DistCentPort')

pollockMainDataTable <- create_dist_between(pollockMainDataTable, 'pollock', c('LonLat_START_LON',
 'LonLat_START_LAT'), c('LonLat_END_LON','LonLat_END_LAT'), units='midpoint', 'DistLocLock')
 
pollockMainDataTable <- create_dist_between(pollockMainDataTable, 'pollock', 'DISEMBARKED_PORT',
  'EMBARKED_PORT', units='meters', 'DistPortPort')

## End(Not run)

Create distance between points variable - non-interactive version

Description

Adds distance between two points to the primary data set. There are two versions of this function. The difference between the two versions is how additional arguments specific to start and end locations are added. This version requires all necessary arguments to be specified before running and is best used in a non-interactive session. The create_dist_between version requires only five arguments to be specified before running. Additional arguments specific to identifying the lat/long of start or end points are added through prompts. This function is designed for an interactive session. Both versions of the distance between function require that the start and end points be different vectors. If the start or ending points are from a port, then PortTable must be specified to obtain lat/lons. If the start or ending points are the center of a fishing zone or area then spat, lon.dat, lat.dat, cat, lon.spat, and lat.spat must be specified to obtain latitude and longitude.

Usage

create_dist_between_for_gui(
  dat,
  project,
  start = c("lat", "lon"),
  end = c("lat", "lon"),
  units,
  name = "DistBetwen",
  portTable = NULL,
  zoneid,
  spat = NULL,
  lon.dat = NULL,
  lat.dat = NULL,
  cat = NULL,
  lon.spat = NULL,
  lat.spat = NULL
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Project name

start, end

Starting location. Should be a port, lat/lon location, or centroid of regulatory zone/area.

port: start should be the column name in dat containing the port names. Latitude and longitude for the port are extracted from the port table.
lat/lon: start should be a character string of column names from dat. The order must be `lat` then `lon` start=c('lat', 'lon').
units

Unit of distance. Choices are "miles", "kilometers", or "midpoint".

name

String, name of new variable. Defaults to 'DistBetween'.

portTable

Data table containing port data. Required if start or end are a vector from the dat containing port names.

zoneid

Variable in dat that identifies the individual zones or areas. Required if zone identifier variable exists and is not 'ZoneID'. Defaults to NULL.

spat

Spatial data containing information on fishery management or regulatory zones. Shape, json, geojson, and csv formats are supported. Required if start or end are "centroid" and a centroid table doesn't exist in the FishSET database.

lon.dat

Longitude variable from dat. Required if start or end are ‘centroid’.

lat.dat

Latitude variable from dat. Required if start or end are ‘centroid’.

cat

Variable or list in spat that identifies the individual areas or zones. If spat is class sf, cat should be name of list containing information on zones. Required if start or end are "centroid".

lon.spat

Variable or list from spat containing longitude data. Required for csv files. Leave as NULL if spat is a shape or json file, Required if start or end are "centroid".

lat.spat

Variable or list from spat containing latitude data. Required for csv files. Leave as NULL if spat is a shape or json file, Required if start or end are "centroid".

Value

Primary data set with distance between points variable added.


Create duration of time variable

Description

Create duration of time variable based on start and ending dates in desired temporal units.

Usage

create_duration(
  dat,
  project,
  start,
  end,
  units = c("week", "day", "hour", "minute"),
  name = "create_duration"
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Project name.

start

Date variable from dat indicating start of time period.

end

Date variable from dat indicating end of time period.

units

String, unit of time for calculating duration. Must be "week", "day", "hour", or "minute".

name

String, name of created vector. Defaults to name of the function if not defined.

Details

Calculates the duration of time between two temporal variables based on defined time unit. The new variable is added to the dataset. A duration of time variable is required for other functions, such as cpue.

Value

Returns primary dataset with duration of time variable added.

Examples

## Not run: 
pollockMainDataTable <- create_duration(pollockMainDataTable, 'pollock', 'TRIP_START', 'TRIP_END',
  units = 'minute', name = 'TripDur')

## End(Not run)

Create expected catch/expected revenue matrix

Description

Create expected catch or expected revenue matrix. The matrix is required for the conditional logit model. Multiple matrices (with unique names) can be saved in a project.

Usage

create_expectations(
  dat,
  project,
  name,
  alt_name,
  catch,
  price = NULL,
  define_group = NULL,
  temp_var = NULL,
  temporal = "daily",
  calc_method = "standardAverage",
  temp_window = 7,
  day_lag = 1,
  year_lag = 0,
  empty_catch = NULL,
  empty_expectation = 1e-04,
  dummy_exp = FALSE,
  weight_avg = FALSE,
  outsample = FALSE
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

name

Name of the expected matrix to be saved

alt_name

Name of the alternative choice matrix.

catch

Variable from dat containing catch data.

price

Optional, variable from dat containing price/value data. Price is multiplied against catch to generated revenue. If revenue exists in dat and you wish to use this revenue instead of price, then catch must be a vector of 1 of length equal to dat. Defaults to NULL.

define_group

Optional, variable from dat that defines how to split the fleet. Defaults to treating entire dataframe dat as a fleet.

temp_var

Optional, temporal variable from dat. Set to NULL if temporal patterns in catch should not be considered.

temporal

String, choices are "daily" or "sequential". Should time, if temp_var is defined, be included as a daily timeline or sequential order of recorded dates. For daily, catch on dates with no record are filled with NA. The choice affects how the rolling average is calculated. If temporal is daily then the window size for average and the temporal lag are in days. If sequential, then averaging will occur over the specified number of observations, regardless of how many days they represent.

calc_method

String, how catch values are average over window size. Select standard average ("standardAverage"), simple lag regression (autoregressive) of catch ("simpleLag"), or weights of regressed groups ("weights")

temp_window

Numeric, temporal window size. If temp_var is not NULL, set the window size to average catch over. Defaults to 14 (14 days if temporal is "daily").

day_lag

Numeric, temporal lag time. If temp_var is not NULL, how far back to lag temp_window.

year_lag

If expected catch should be based on catch from previous year(s), set year_lag to the number of years to go back.

empty_catch

String, replace empty catch with NA, 0, mean of all catch ("allCatch"), or mean of grouped catch ("groupCatch").

empty_expectation

Numeric, how to treat empty expectation values. Choices are to not replace (NULL) or replace with 0.0001 or 0.

dummy_exp

Logical, should a dummy variable be created? If TRUE, output dummy variable for originally missing value. If FALSE, no dummy variable is outputted. Defaults to FALSE.

weight_avg

Logical, if TRUE then all observations for a given zone on a given date will be included when calculating the mean, thus giving more weight to days with more observations in a given zone. If FALSE, then the daily mean for a zone will be calculated prior to calculating the mean across the time window.

outsample

Logical, if TRUE then generate expected catch matrix for out-of-sample data. If FALSE generate for primary data table. Defaults to outsample = FALSE

Details

Function creates an expectation of catch or revenue for alternative fishing zones (zones where they could have fished but did not). The output is saved to the FishSET database and called by the format_model_data function. create_alternative_choice must be called first as observed catch and zone inclusion requirements are defined there.
The primary choices are whether to treat data as a fleet or to group the data (define_group) and the time frame of catch data for calculating expected catch. Catch is averaged along a daily or sequential timeline (temporal) using a rolling average. temp_window and day_lag determine the window size and temporal lag of the window for averaging. Use temp_obs_table before using this function to assess the availability of data for the desired temporal moving window size. Sparse data is not suited for shorter moving window sizes. For very sparse data, consider setting temp_var to NULL and excluding temporal patterns in catch.
Empty catch values are considered to be times of no fishing activity. Values of 0 in the catch variable are considered times when fishing activity occurred but with no catch. These points are included in the averaging and dummy creation as points in time when fishing occurred.

Value

Function saves a list of expected catch matrices to the FishSET database as projectExpectedCatch. The list includes the expected catch matrix from the user-defined choices. Multiple expected catch cases can be added to the list by specifying unique names. The list is automatically saved to the FishSET database and is called in format_model_data. The expected catch output does not need to be loaded when defining or running the model.

Examples

## Not run: 
create_expectations(pollockMainDataTable, "pollock", "exp1", "OFFICIAL_TOTAL_CATCH_MT",
  price = NULL, define_group = "fleet", temp_var = "DATE_FISHING_BEGAN",
  temporal = "daily", calc_method = "standardAverage", 
  empty_catch = "allCatch", empty_expectation = 0.0001, temp_window = 4,
  day_lag = 2, year_lag = 0, dummy_exp = FALSE, 
  weight_avg = FALSE, outsample = FALSE
)

## End(Not run)

Creates haul midpoint latitude and longitude variables

Description

Calculates latitude and longitude of the haul midpoint and adds two variables to the primary data set: the midpoint latitude and the midpoint longitude.

Usage

create_mid_haul(
  dat,
  project,
  start = c("lon", "lat"),
  end = c("lon", "lat"),
  name = "mid_haul"
)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

Project name.

start

Character string, variables in dat defining the longitude and latitude of the starting location of haul. Must be in decimal degrees.

end

Character string, variables in dat defining the longitude and latitude of the ending location of haul. Must be in decimal degrees.

name

String, name of new variable. Defaults to 'mid_haul'.

Details

Each row of data must be a unique haul. Requires a start and end point for each observation.

Value

Returns primary dataset with two new variables added: latitude and longitude of haul midpoint.

Examples

## Not run: 
pollockMainDataTable <- create_mid_haul(pollockMainDataTable, 'pollock', 
    start = c('LonLat_START_LON', 'LonLat_START_LAT'), 
   end = c('LonLat_END_LON', 'LonLat_END_LAT'), name = 'mid_haul')

## End(Not run)

Create fishery season identifier variable

Description

Create fishery season identifier variable

Usage

create_seasonal_ID(
  dat,
  project,
  seasonal.dat,
  use.location = c(TRUE, FALSE),
  use.geartype = c(TRUE, FALSE),
  sp.col,
  target = NULL
)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

Project name.

seasonal.dat

Table containing date of fishery season(s). Can be pulled from the FishSET database.

use.location

Logical, should fishery season dates depend on fishery location? Column names containing location in dat and seasonal.dat must match.

use.geartype

Logical, should fishery season dates depend on gear type. Column names containing gear type in dat and seasonal.dat must match.

sp.col

Variable in seasonal.dat containing species names.

target

Name of target species. If target is NULL, runs through fisheries in order listed in seasonal.dat

Details

Uses a table of fishery season dates to create fishery season identifier variables. Output is a SeasonID variable and/or multiple SeasonID*fishery variables. If fishery season dates vary by location or gear type, then use.location and use.geartype should be TRUE.

The function matches fishery season dates provided in seasonal.dat to the earliest date variable in dat. The 'seasonID' variable is a vector of fishery seasons whereas the 'SeasonID*fishery' variables are 1/0 depending on whether the fishery was open on the observed date.

If target is not defined, then each row of seasonID is defined as the earliest fishery listed in seasonal.dat for which the fishery season date encompasses the date variable in the primary dataset. If target fishery is defined, then 'SeasonID' is defined by whether the target fishery is open on the date in the primary dataset or a different fishery. The vector is filled with 'target' or 'other'.

'SeasonID*fishery' variables are a 1/0 seasonID vector for each fishery (labeled by seasonID and fishery) where 1 indicates the dates for a given row in the primary data table fall within the fishery dates for that fishery.

Value

Returns the primary dataset with the variable SeasonID, or a series of variables identifying by the individual fisheries included (seasonID*fishery).

Examples

## Not run: 
pcodMainDataTable <- create_seasonal_ID("pcodMainDataTable", seasonal_dat,
  use.location = TRUE, use.geartype = TRUE, sp.col = "SPECIES", target = "POLLOCK"
)

## End(Not run)

Create numeric variable using arithmetic expression

Description

Creates a new variable based on the arithmetic operation between two variables. Function is useful for creating rate variables or the summation of two related variables.

Usage

create_var_num(dat, project, x, y, method, name = "create_var_num")

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

Project name.

x

Variable in dat. Variable will be the numerator if method is division.

y

Variable in dat or numeric value. Variable will be the denominator if method is division.

method

String, arithmetic expression. Options include: "sum", addition ("add"), subtraction ("sub"), multiplication ("mult"), and division ("div").

name

String, name of created vector. Defaults to name of the function if not defined.

Details

Creates a new numeric variable based on the defined arithmetic expression method. New variable is added to the primary dataset.

Value

Returns primary dataset with new variable added.

Examples

## Not run: 
pollockMainDataTable <- create_var_num(pollockMainDataTable, 'pollock', x = 'HAUL_CHINOOK',
    y = 'HAUL_CHUM', method = 'sum', name = 'tot_salmon')

## End(Not run)

Convert dataframe to sf

Description

Used to convert spatial data with no spatial class to a sf object. This is useful if the spatial data was read from a non-spatial file type, e.g. a CSV file.

Usage

dat_to_sf(dat, lon, lat, id, cast = "POLYGON", multi = FALSE, crs = 4326)

Arguments

dat

Spatial data containing information on fishery management or regulatory zones.

lon

Longitude variable in spatdat.

lat

Latitude variable in spatdat.

id

Spatial feature ID column.

cast

Spatial feature type to create. Commonly used options are "POINT", "LINESTRING", and "POLYGON". See st_cast for details.

multi

Logical, use if needing to convert to a multi-featured (grouped) sf object, e.g. MULTIPOLYGON or MULTILINESTRING.

crs

Coordinate reference system to assign to dat. Defaults to WGS 84 (EPSG: 4326).


Check for common data quality issues

Description

Check primary data for common data quality issues, such as NaNs, NAs, outliers, unique rows, and empty variables.

Usage

data_check(dat, project, x)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

String, name of project.

x

Variable in dat to check for outliers. Must be in quotes if called from the FishSET database.

Details

Prints summary stats for all variables in dat. Prints column names that contain NaNs or NAs. Checks for outliers for specified variable x. Checks that all column names are unique, whether any columns in dat are empty, whether each row is a unique choice occurrence at the haul or trip level, that data for either lat/lon or fishing area are included. The function is also called by other functions.

Examples

## Not run: 
data_check(pcodMainDataTable, "OFFICIAL_TOTAL_CATCH_MT")

## End(Not run)

Upload data from file, FishSET DB, or working environment

Description

Helper function that can read data from file, from FishSET DB, or a dataframe in the working environment. Used for data upload functions: load_maindata, load_port, load_aux, load_grid, load_spatial.

Usage

data_upload_helper(dat, type, ...)

Arguments

dat

Reference to a dataframe. This can be a filepath, the name of an existing FishSET table, or a dataframe object in the working environment.

type

The type of data to upload. Options include "main", "port", "grid", "aux", and "spat".

...

Additional arguments passed to read_dat.

Examples

## Not run: 
dataset <- data_upload_helper(dat, type = "main")

## End(Not run)

Check and convert lat/lon to decimal degrees

Description

Check that latitude and longitude are in decimal degrees and the variable sign is correct. Correct lat/lon if required.

Usage

degree(
  dat,
  project,
  lat = NULL,
  lon = NULL,
  latsign = FALSE,
  lonsign = FALSE,
  replace = TRUE
)

Arguments

dat

Dataset containing latitude and longitude data.

project

Project name.

lat

Variable(s) containing latitude data. If NULL the function will attempt to search for all latitude variables by name (e.g. by matching "lat" or "LAT").

lon

Variable(s) containing longitude data. If NULL the function will attempt to search for all longitude variables by name (e.g. by matching "lon" or "LON").

latsign

How should the sign value of lat be changed? Choices are NULL for no change, "neg" to convert all positive values to negative, "pos" to convert all negative values to positive, and "all" to change all values.

lonsign

How should the sign value of lon be changed? Choices are NULL for no change, "neg" to convert all positive values to negative, "pos" to convert all negative values to positive, and "all" to change all values.

replace

Logical, should lat and lon in dat be converted to decimal degrees? Defaults to TRUE. Set to FALSE if checking for compliance.

Details

First checks whether any variables containing 'lat' or 'lon' in their names are numeric. Returns a message on results. To convert a variable to decimal degrees, identify the lat or lon variable(s) and set replace = TRUE. To change the sign, set latsign (for lat) or lonsign (for lon = TRUE. FishSET requires that latitude and longitude be in decimal degrees.

Value

Returns the primary dataset with the latitudes and longitudes converted to decimal degrees if replace = TRUE or if Changing the sign. Otherwise, a message indicating whether selected longitude and latitude variables are in the correct format.

Examples

## Not run: 
# check format
degree(pollockMainDataTable, 'pollock', lat = 'LatLon_START_LAT',
       lon = 'LatLon_START_LON')

# change signs and convert to decimal degrees
pollockMainDataTable <- degree(pollockMainDataTable, 'pollock', 
                               lat = 'LatLon_START_LAT', 
                               lon = 'LatLon_START_LON', latsign = FALSE, 
                               lonsign = FALSE, replace = TRUE)

## End(Not run)

Delete table meta data or project meta file

Description

Delete table meta data or project meta file

Usage

delete_meta(project, tab.name = NULL, delete_file = FALSE)

Arguments

project

Project name.

tab.name

String, table name.

delete_file

Logical, whether to delete project meta file.


Create KDE, CDF, or empirical CDF plots

Description

Creates a kernel density estimate, empirical cumulative distribution function, or cumulative distribution function plot of selected variable. Grouping, filtering, and several plot options are available.

Usage

density_plot(
  dat,
  project,
  var,
  type = "kde",
  group = NULL,
  combine = TRUE,
  date = NULL,
  filter_date = NULL,
  date_value = NULL,
  filter_by = NULL,
  filter_value = NULL,
  filter_expr = NULL,
  facet_by = NULL,
  conv = "none",
  tran = "identity",
  format_lab = "decimal",
  scale = "fixed",
  bw = 1,
  position = "identity",
  pages = "single"
)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

String, name of project.

var

String, name of variable to plot.

type

String, type of density plot. Options include "kde" (kernel density estimate), "ecdf" (empirical cdf), "cdf" (cumulative distribution function), or "all" (all plot types). Two or more plot types can be chosen.

group

Optional, string names of variables to group by. If two or grouping variables are included, the default for "cdf" and "ecdf" plots is to not combine groups. This can be changed using combine = TRUE. "kde" plots always combine two or more groups. "cdf" and "ecdf" plots can use up to two grouping variables if combine = FALSE: the first variable is represented by color and second by line type.

combine

Logical, whether to combine the variables listed in group for plot.

date

Date variable from dat used to subset and/or facet the plot by.

filter_date

The type of filter to apply to 'MainDataTable'. To filter by a range of dates, use filter_date = "date_range". To filter by a given period, use "year-day", "year-week", "year-month", "year", "month", "week", or "day". The argument date_value must be provided.

date_value

This argument is paired with filter_date. To filter by date range, set filter_date = "date_range" and enter a start- and end-date into date_value as a string: date_value = c("2011-01-01", "2011-03-15").

To filter by period (e.g. "year", "year-month"), use integers (4 digits if year, 1-2 digits if referencing a day, month, or week). Use a vector if filtering by a single period: date_filter = "month" and date_value = c(1, 3, 5). This would filter the data to January, March, and May.

Use a list if using a year-period type filter, e.g. "year-week", with the format: list(year, period). For example, filter_date = "year-month" and date_value = list(2011:2013, 5:7) will filter the data table from May through July for years 2011-2013.

filter_by

String, variable name to filter 'MainDataTable' by. the argument filter_value must be provided.

filter_value

A vector of values to filter 'MainDataTable' by using the variable in filter_by. For example, if filter_by = "GEAR_TYPE", filter_value = 1 will include only observations with a gear type of 1.

filter_expr

String, a valid R expression to filter 'MainDataTable' by.

facet_by

Variable name to facet by. This can be a variable that exists in dat or a variable created by density_plot() such as "year", "month", or "week". date is required if facetting by period.

conv

Convert catch variable to "tons", "metric_tons", or by using a function entered as a string. Defaults to "none" for no conversion.

tran

String; name of function to transform variable, for example "log" or "sqrt".

format_lab

Formatting option for x-axis labels. Options include "decimal" or "scientific".

scale

Scale argument passed to facet_grid. Defaults to "fixed". Other options include "free_y", "free_x", and "free".

bw

Adjusts KDE bandwidth. Defaults to 1.

position

The position of the grouped variable for KDE plot. Options include "identity", "stack", and "fill".

pages

Whether to output plots on a single page ("single", the default) or multiple pages ("multi").

Details

The data can be filtered by date or by variable (see filter_date and filter_by). If type contains "kde" or "all" then grouping variables are automatically combined. Any variable in dat can be used for faceting, but "year", "month", or "week" are also available if date is provided.

Value

denstiy_plot() can return up to three plots in a single call. When pages = "single" all plots are combined and stacked vertically. pages = "multi" will return separate plots.

Examples

## Not run: 

density_plot(pollockMainDataTable, "pollock", var = "OFFICIAL_TOTAL_CATCH_MT",
             type = c("kde", "ecdf"))

# facet 
density_plot(pollockMainDataTable, "pollock", var = "OFFICIAL_TOTAL_CATCH_MT",
             type = c("kde", "ecdf"), facet_by = "GEAR_TYPE")

# filter by period
density_plot(pollockMainDataTable, "pollock", var = "OFFICIAL_TOTAL_CATCH_MT", 
             type = "kde", date = "FISHING_START_DATE", filter_date = "year-month", 
             filter_value = list(2011, 9:11))

## End(Not run)

Create dummy matrix from a coded ID variable

Description

Create dummy matrix from a coded ID variable

Usage

dummy_matrix(dat, project, x)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Project name.

x

Variable in dat used to generate dummy matrix.

Details

Creates a dummy matrix of 1/0 with dimensions [(number of observations in dataset) x (number of factors in x)] where each column is a unique factor level. Values are 1 if the value in the column matches the column factor level and 0 otherwise.

Examples

## Not run: 
PortMatrix <- dummy_matrix(pollockMainDataTable, 'pollock', 'PORT_CODE')

## End(Not run)

Create a binary vector from numeric, date, and character or factor vectors.

Description

Create a binary vector from numeric, date, and character or factor vectors.

Usage

dummy_num(dat, project, var, value, opts = "more_less", name = "dummy_num")

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

Project name.

var

Variable in dat to create dummy variable from.

value

String, value to set dummy variable by. If var is a date, value should be a year, If var is a factor, value should be a factor level. If var is numeric, value should be a single number or range of numbers [use c(1,5)].

opts

String, how dummy variable should be defined. Choices are "x_y" and "more_less’". For "x_y", each element of var is set to 1 if the element matches value, otherwise 0. For "more_less", each element of var less than value is set to 0 and all elements greater than value set to 1. If var is a factor, then elements that match value will be set to 1 and all other elements set to 0. Default is set to "more_less".

name

String, name of created dummy variable. Defaults to name of the function if not defined.

Details

For date variables, the dummy variable is defined by a date (year) and may be either year x versus all other years ("x_y") or before vs after year x ("more_less"). Use this function to create a variable defining whether or not a policy action had been implemented.
Example: before vs. after a 2008 amendment:
dummy_num('pollockMainDataTable', 'Haul_date', 2008, 'more_less', 'amend08')

For factor variables, both choices in opts compare selected factor level(s) against all other factor levels.
Example: Fishers targeting pollock vs. another species:
dummy_num('pollockMainDataTable', 'GF_TARGET_FT', c('Pollock - bottom', 'Pollock - midwater'), 'x_y', 'pollock_target')

For numeric variables, value can be a single number or a range of numbers. The dummy variable is the selected value(s) against all others (x_y) or less than the selected value versus more than the selected value (more_less). For more_less, the mean is used as the critical value if a range of values is provided.

Value

Returns primary dataset with dummy variable added.

Examples

## Not run: 
pollockMainDataTable <- dummy_num(pollockMainDataTable, 'pollock', 'Haul_date', 2008, 
  'more_less', 'amend80')

## End(Not run)

Check variables are not empty

Description

Check for and remove empty variables from dataset. Empty variables are columns in the data that contain all NAs and/or empty strings.

Usage

empty_vars_filter(dat, project, remove = FALSE)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

String, name of project.

remove

Logical, whether to remove empty variables. Defaults to FALSE.

Details

Function checks for empty variables and prints an outcome message to the console. If empty variables are present and remove = TRUE, then empty variables will be removed from the dataset. Empty variables are columns in the dataset that contain all NAs or empty strings.

Value

Returns the dataset with empty variables removed if remove = TRUE.

Examples

## Not run: 
# check for empty vars
empty_vars_filter(pollockMainDataTable)

# remove empty vars from data
mod.dat <- empty_vars_filter(pollockMainDataTable, 'pollock', remove = TRUE)

## End(Not run)

Return names of expected catch matrices

Description

Return the names of expected catch matrices saved to the FishSET database.

Usage

exp_catch_names(project)

Arguments

project

Name of project.


Get Expected Catch List

Description

Returns the Expected Catch list from the FishSET database.

Usage

expected_catch_list(project, name = NULL)

Arguments

project

Name of project.

name

Name of expected catch table from the FishSET database. The table name will contain the string "ExpectedCatch". If NULL, the default table is returned. Use tables_database to see a list of FishSET database tables by project.


Remove rows based on filter expressions defined in 'filterTable'

Description

Remove rows based on filter expressions defined in 'filterTable'

Usage

filter_dat(dat, project, exp, filterTable = NULL)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

Project name.

exp

How to filter. May be a row in the filter table generated by filter_table that contains a filter expression or the filter expression to apply to the data. If the filter expression is supplied, it should take on the form of "x < 100" or "is.na(x) == FALSE".

filterTable

Name of filter table in FishSET database. Name should contain the phrase 'filterTable'.

Details

Filter data frame based on a predefined filter expression from filter_table or a filter expression. We recommend creating a filter table using filter_table so that filter expressions are stored and easily accessed in the future.

Value

Filtered data frame

Examples

## Not run: 
newdat <- filter_dat(pcodMainDataTable, 'pcod', exp = 3, 
                     filterTable = 'pcodfilterTable01012011')
                     
newdat <- filter_dat(pcodMainDataTable, 'pcod', 
                     exp = 'PERFORMANCE_Code == 1', filteTable = NULL)
                     
newdat <- filter_dat(pcodMainDataTable, "pcod", exp = "SEASON == 'A'",
                     filterTable = NULL)

## End(Not run)

Define and store filter expressions

Description

Define and store filter expressions

Usage

filter_table(dat, project, x, exp)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

String, name of project.

x

Variable in dat over which filter will be applied.

exp

Filter expression. Should take on the form of "x < 100" or "is.na(x) == FALSE".

Details

This function allows users to define and store data filter expressions which can then be applied to the data. The filter table will be saved in the FishSET database under the project name and 'filterTable'. The new filter functions are added each time the function is run and the table is automatically updated in the FishSET database. The function call will be logged in the log file.

Value

Filter expressions saved as a table to the FishSET database.

Examples

## Not run: 
filter_table(pcodMainDataTable, 'pcod', x = 'PERFORMANCE_Code',
             exp = 'PERFORMANCE_Code == 1')

## End(Not run)

Identify geographic centroid of fishery management or regulatory zone

Description

Identify geographic centroid of fishery management or regulatory zone

Usage

find_centroid(
  spat,
  project,
  spatID,
  lon.spat = NULL,
  lat.spat = NULL,
  cent.name = NULL,
  log.fun = TRUE
)

Arguments

spat

Spatial data containing information on fishery management or regulatory zones. Can be shape file, json, geojson, data frame, or list.

project

Name of project

spatID

Variable or list in spat that identifies the individual areas or zones. If spat is class sf, spatID should be name of list containing information on zones.

lon.spat

Variable or list from spat containing longitude data. Required for csv files. Leave as NULL if spat is a shape or json file.

lat.spat

Variable or list from spat containing latitude data. Required for csv files. Leave as NULL if spat is a shape or json file.

cent.name

String, name to include in centroid table. Centroid name take the form of '"projectNameZoneCentroid"'. Defaults to 'NULL' (e.g. '"projectZoneCentroid"').

log.fun

Logical, whether to log function call (for internal use).

Details

Returns the geographic centroid of each area/zone in spat. The centroid table is saved to the FishSET database. Function is called by the create_alternative_choice and create_dist_between functions.

Value

Returns a data frame where each row is a unique zone and columns are the zone ID and the latitude and longitude defining the centroid of each zone.


Create fishing or weighted fishing centroid

Description

Create fishing or weighted fishing centroid

Usage

find_fishing_centroid(
  dat,
  project,
  zoneID,
  weight.var = NULL,
  lon.dat,
  lat.dat,
  names = NULL,
  cent.name = NULL,
  log.fun = TRUE
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Name of project

zoneID

Variable in dat that identifies zonal assignments or the If spat is class sf, zoneID should be name of list containing information on zones.

weight.var

Variable from dat for weighted average. If weight.var is defined, the centroid is defined by the latitude and longitude of fishing locations in each zone weighted by weight.var.

lon.dat

Required. Longitude variable in dat.

lat.dat

Required. Latitude variable in dat.

names

Then names of the fishing centroid columns to be added. A vector of length two in the order of c("lon", "lat"). The default is c("fish_cent_lon", "fish_cent_lat") and c("weight_cent_lon", "weight_cent_lat") if weight.var is used.

cent.name

A string to include in the centroid table name. Table names take the form of '"projectNameFishCentroid"' for fishing centroids.

log.fun

Logical, whether to log function call (for internal use).

Details

Fishing centroid defines the centroid by mean latitude and longitude of fishing locations in each zone. Weighted centroid defines the centroid by the mean latitude and longitude of fishing locations in each zone weighted by the weight.var. The fishing and weighted centroid variables can be used anywhere latitude/longitude variables appear. Each observation in dat must be assigned to a fishery or regulatory area/zone. If the zone identifier exists in dat and is not called 'ZoneID', then zoneID should be the variable name containing the zone identifier. If a zone identifier variable does not exist in dat, spat must be be specified and zoneID must be zone identifier in spat. The assignment_column function will be run and a zone identifier variable added to dat.

Value

Returns primary dataset with fishing centroid and, if weight.var is specified, the weighted fishing centroid.


Compare imported data table to the previously saved version of the data table

Description

Compare imported data table to the previously saved version of the data table

Usage

fishset_compare(x, y, compare = c(TRUE, FALSE), project)

Arguments

x

Updated data table to be saved.

y

Previously saved version of data table.

compare

Logical, if TRUE, compares x to y before saving x to FishSET database.

project

Name of project

Details

Function is optional. It is designed to check for consistency between versions of the same data frame so that the logged functions can be used to rerun the previous analysis on the updated data. The column names, including spelling and capitalization, must match the previous version to use the logged functions to rerun code after data has been updated (i.e., new year of data). The function is called by the data import functions (load_maindata, load_port, load_aux, load_grid). Set the compare argument to TRUE to compare column names of the new and previously saved data tables. The new data tables will be saved to the FishSET database if column names match. Set the compare argument to FALSE if no previous versions of the data table exist in the FishSET database. No comparison will be made and the new file will be saved to the database.


K-Fold Cross Validation for FishSET Models

Description

Performs k-fold cross-validation on a fitted FishSET model to evaluate out-of-sample predictive performance. Bypasses formula parsing by directly subsetting the design matrix.

Usage

fishset_cv(
  project,
  base_model_name,
  k = 5,
  seed = 42,
  distribution = NULL,
  ...
)

Arguments

project

Character string. Name of the project.

base_model_name

Character string. The name of the original model design to cross-validate.

k

Integer. The number of folds to create. Default is 5.

seed

Integer. Random seed for reproducible fold generation. Default is 42.

distribution

Character string. Distribution for the continuous catch component in EPMs.

...

Additional control arguments passed to fishset_fit().

Value

A list containing the average out-of-sample accuracy, log-likelihood, PAPE, AIC, fold details, and estimated coefficients across folds.

Examples

## Not run: 
# Standard logit
cv_results <- fishset_cv(
  project = "MyProject",
  base_model_name = "clogit_design"
)

# EPM - normal catch function
cv_epm_results <- fishset_cv(
  project = "MyProject",
  base_model_name = "epm_design",
  distribution = "normal"
)

## End(Not run)

Create FishSET Model Design Object

Description

Constructs the design object required for discrete choice model fitting within the FishSET framework. This function parses the model formula, validates the formatted data, and generates the design matrix (X) and choice vector (y). It handles both alternative-specific variables and trip- or haul-specific variables (automatically creating interactions with zone constants). The resulting design object is the primary input for the fishset_fit function, which performs the parameter estimation.

Usage

fishset_design(
  formula,
  project,
  model_name,
  formatted_data_name,
  unique_obs_id,
  zone_id,
  catch_formula = NULL,
  price_var = NULL,
  scale = FALSE,
  overwrite = FALSE
)

Arguments

formula

A two-part formula specifying the model structure (e.g., chosen ~ expected_catch + distance | income). The left-hand side specifies the binary choice variable, which is always specified as chosen from the format_model_data function. The right-hand side is separated by a pipe (|): Part 1 contains alternative-specific variables, and Part 2 contains trip- or haul-specific variables (i.e., do not vary across fishing zones).

project

Name of the project.

model_name

Name for this specific model design instance. Must be unique within the project's design list.

formatted_data_name

Name of the formatted data object to use. This must correspond to a name previously created by format_model_data.

unique_obs_id

Variable name in the dataset representing the unique observation identifier.

zone_id

Variable name in the dataset representing the zone (alternative) identifier.

catch_formula

A formula specifying the expected catch for an Expected Profit Model.

price_var

Variable name in the dataset representing price. This input is only used for Expected Profit Models, and the price variable must be included in the formatted dataset created in the format_model_data function.

scale

Logical. Default = FALSE. If TRUE, numeric predictors in the design matrix (X) are centered and scaled (z-score normalization) before saving. Scaling factors are stored to allow unscaling of parameters after estimation. Recommended for numerical stability.

overwrite

Logical. Default FALSE. If TRUE, overwrites an existing model design if model_name already exists in the project folder.

Details

The resulting design object is saved as a compressed file in the 'Models/ModelDesigns' folder, which is located inside the project folder.

Value

A list object of class 'fishset_design' containing the design matrices, choice vector, and metadata. The list is saved as a compressed file in the project folder.

Examples

## Not run: 
# 1. Standard Conditional Logit
# "chosen" is ALWAYS the response, "expected_catch" and "distance" are site attributes.
fishset_design(
  formula = chosen ~ expected_catch + distance,
  project = "MyProject",
  model_name = "clogit_model1",
  formatted_data_name = "my_formatted_data",
  unique_obs_id = "haul_id",
  zone_id = "zone_id"
)

# 2. Zonal Logit with Alternative-Specific Constants (ASCs)
# Add the zone_id variable ("zone_id") to the formula to generate fixed effects.
fishset_design(
  formula = chosen ~ expected_catch + distance + zone_id,
  project = "MyProject",
  model_name = "zonal_logit_model1",
  formatted_data_name = "my_formatted_data",
  unique_obs_id = "haul_id",
  zone_id = "zone_id",
  scale = TRUE
) 

# 3. Zonal Logit with trip- or haul-specific variables
# Add the variable that does not vary across zones (e.g., vessel length) after the pipe ("|").
fishset_design(
  formula = chosen ~ expected_catch + distance | vessel_length,
  project = "MyProject",
  model_name = "zonal_logit_model2",
  formatted_data_name = "my_formatted_data",
  unique_obs_id = "haul_id",
  zone_id = "zone_id",
  scale = TRUE
)

# 4. Expected profit model - normal distribution
# The expected catch (catch_var in this example) does not vary across zones in this 
# example (e.g., vessel length).
fishset_design(
  formula = chosen ~ distance | catch_var,
  project = "MyProject",
  model_name = "epm1",
  formatted_data_name = "my_formatted_data",
  unique_obs_id = "haul_id",
  zone_id = "ZoneID",
  catch_formula = actual_catch ~ catch_var:ZoneID,
  price_var = "price_var",
  scale = TRUE
) 

## End(Not run)

Fit FishSET Discrete Choice Model

Description

Estimates parameters for logit models using the RTMB (R Template Model Builder) framework. This function takes a design object created by fishset_design, optimizes the negative log-likelihood, and returns a comprehensive list of model results, fit statistics, and diagnostics.

Usage

fishset_fit(
  project,
  model_name,
  fit_name = NULL,
  distribution = NULL,
  robust = FALSE,
  return_full_prob_mat = FALSE,
  se_calc = TRUE,
  overwrite = FALSE,
  ...
)

Arguments

project

Character string. Name of the project.

model_name

Character string. Name of the specific model design to fit. Must match a name saved in the project's 'ModelDesigns' table.

fit_name

Character string (Optional). Name to assign to the resulting fit object in the database. Defaults to paste0(model_name, "_fit").

distribution

Character string. Distribution for the continuous catch component in EPMs. Options: "normal", "lognormal", "weibull", default = NULL.

robust

Logical. Default FALSE. If TRUE, uses numerically stable utility values.

return_full_prob_mat

Logical. If TRUE, returns the full N_obs x J_alts matrix of probabilities for every alternative. Default is FALSE (returns only chosen probs) to save memory on large datasets.

se_calc

Logical. Set "se_calc" = TRUE (default) to calculate standard errors. Set to FALSE for faster runtime during model selection.

overwrite

Logical. Default FALSE. If TRUE, overwrites an existing model fit if fit_name already exists in the project database.

...

Additional arguments passed to the optimization control.

  • control: A list of control parameters passed to nlminb (e.g., list(eval.max = 2000, iter.max = 2000)).

  • start_values: A numeric vector of initial parameter values. Must match the number of predictors in the design matrix.

Value

A list object of class "fishset_fit" containing, this list is also saved in the project database:

coefficients

Named vector of estimated parameters.

coef_table

Data frame with Estimates, Std. Errors, Z-values, and P-values.

vcov

Variance-covariance matrix of the parameters.

opt

Raw optimization output from nlminb.

logLik

The maximum log-likelihood value of the fitted model.

null_logLik

The log-likelihood of a null model (random guessing).

pseudo_R2

McFadden's Pseudo-R-squared.

AIC, AICc, BIC

Information criteria for model comparison.

accuracy

The proportion of observations where the model assigned the highest probability to the actual choice.

fitted_values

Vector of predicted probabilities for the chosen alternatives.

prob_matrix

Matrix of predicted probabilities for all alternatives (N_obs x J_alts).

diagnostics

A list containing the Hessian, gradients, eigenvalues, and condition number.

See Also

fishset_design for creating the input design object.

Examples

## Not run: 
# 1. Standard fit using default settings
# This uses the design object named "clogit_design" saved in "MyProject"
fit_result <- fishset_fit(
  project = "MyProject",
  model_name = "clogit_design"
)
  
# 2. Advanced fit with custom optimization settings and start values
# 'control' and 'start_values' are passed via the '...' argument
fit_custom <- fishset_fit(
  project = "MyProject",
  model_name = "clogit_design",
  fit_name = "clogit_custom_fit",
    
  # Pass control list to nlminb (e.g., increase max iterations, turn on tracing)
  control = list(eval.max = 5000, iter.max = 5000, trace = 1),
    
  # Pass initial start values for the parameters (e.g., for 2 predictors)
  start_values = c(0.5, -0.2)
)
  
# 3. EPM - normal catch function
epm_fit <- fishset_fit(project = project,
  model_name = "epm1",
  fit_name = "epm_fit1",
  distribution = "normal"
)

## End(Not run)

Hausman-McFadden Test for IIA

Description

Performs the Hausman-McFadden specification test to check the Independence of Irrelevant Alternatives (IIA) assumption for logit models.

Usage

fishset_iia_test(
  project,
  model_name,
  fit_name = NULL,
  omitted_zones = NULL,
  robust = FALSE,
  ...
)

Arguments

project

Character string. Name of the project.

model_name

Character string. Name of the specific model design used.

fit_name

Character string. Name of the full model fit object previously saved in the project database (created by fishset_fit).

omitted_zones

Character vector (Optional). The names of the zones (alternatives) to exclude from the restricted model. These must match the zone labels found in the original data. If NULL (default), a random zone is selected.

robust

Logical. Default FALSE. If TRUE, uses numerically stable utility values, mirroring the fit in fishset_fit.

...

Additional arguments passed to the optimization of the restricted model (e.g., control, start_values).

Details

The test compares the estimates from a full model (fitted on all alternatives) against a restricted model (fitted on a subset of alternatives). If the IIA assumption holds, the coefficients common to both models should not be systematically different.

A significant p-value (typically < 0.05) indicates that the IIA assumption has been violated, suggesting that the logit model may be misspecified (e.g., unobserved correlation between alternatives). Note: Alternative-Specific Constants (ASCs) are excluded from the statistical comparison to prevent reference-level shift bias.

Value

A list object of class "fishset_iia" containing:

statistic

The Hausman chi-squared test statistic.

p_value

The p-value of the test.

df

Degrees of freedom (number of coefficients compared).

full_coefs

Coefficients from the full model (subsetted for comparison).

restricted_coefs

Coefficients from the restricted model.

description

Text interpretation of the result.


Show all SQL Tables in FishSET Folder

Description

Returns a data frame containing all tables from each project by project name and table type.

Usage

fishset_tables(project = NULL)

Arguments

project

Project name. If NULL, tables from all available projects will be displayed.

Examples

## Not run: 
# return all tables for all projects
fishset_tables()

# return all tables for a specific project
fishset_tables("pollock")

## End(Not run)

Create fleet variable using fleet definition table

Description

Add a fleet ID column to the primary data using a fleet table (see fleet_table for details).

Usage

fleet_assign(
  dat,
  project,
  fleet_tab,
  assign = NULL,
  overlap = FALSE,
  format_var = "string"
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

fleet_tab

String, name of the fleet table stored in FishSET database. Should contain the string 'FleetTable'.

assign

Integer, a vector of row numbers from fleet_tab. Only fleet definitions in these rows will be used and added to 'MainDataTable'. If assign = NULL (the default), all fleet definitions in the table will be used.

overlap

Logical; whether overlapping fleet assignments are allowed. Defaults to FALSE.

format_var

String. If format_var = "string", a single column named "fleet" will be added to 'MainDataTable'. If overlap = TRUE, observations with multiple fleet assignments are duplicated. format_var ="dummy" outputs a binary column for each fleet in the fleet table. Defaults to "string".

Value

Returns the primary dataset with added fleet variable(s).

See Also

fleet_table

Examples

## Not run: 
fleet_assign(pollockMainDataTable, 'pollock', fleet_tab = 'pollockFleetTable', 
             overlap = TRUE)

## End(Not run)

Define and store fleet expressions

Description

fleet_table saves a table of fleet expression to the FishSET database which can then be applied to a dataset with fleet_assign. The table must contain a 'condition' and 'fleet' column with each row corresponding to a set of expressions that will be used to assign observations to fleets. A table can be created with the cond and fleet_val arguments or by uploading an existing table that matches the format requirements. See 'Details' below for examples of how tables can be formatted.

Usage

fleet_table(
  dat,
  project,
  cond = NULL,
  fleet_val = NULL,
  table = NULL,
  save = TRUE
)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

String, name of project.

cond

String; a vector containing valid R expressions saved as strings. Must be used with the fleet_val argument. Each expression should be in quotes (double or single) with nested quotes indicated with escaped quotes (\') or with the opposite quote used to contain the expression. For example, "species == 'cod'" and "species == \'cod\'" are both valid.

fleet_val

String; a vector of fleet names to be assigned. Must be used with the cond argument.

table

A data frame that has one condition column and one fleet column. See 'Details' for table formatting.

save

Logical; whether to save the current fleet_table to the FishSET database. Defaults to TRUE. Tables are saved in the format of 'projectFleetTable'. Each project can only have one fleet table. New fleet definitions are appended to the exiting fleet table. See table_remove to delete a table.

Details

Below is a simple example of a fleet table. For a fleet table to be created, it must contain one "condition" column and one "fleet" column. Each fleet definition can be as long as necessary. For example, the first expression in the condition column example could also be "GEAR == 8 & species == 'pollock'". Use the '&' operator when combining expressions.

condition fleet
'GEAR == 8' 'A'
'species == "cod"' 'B'
'area %in% c(640, 620)' 'C'

Value

Returns a table of fleet conditions that is saved to the FishSET database with the name 'projectFleetTable'.

Examples

## Not run:  
fleet_table("MainDataTable", "myProject", 
            cond = c("GEAR == 8", "species == 'cod'", "area %in% c(640, 620)"),
            fleet_val = c("A", "B", "C"), save = TRUE
            ) 

## End(Not run)

Format Gridded Data

Description

Change the format of a gridded dataset from wide to long (or vice versa) and remove any unmatched area/zones from grid. This is a necessary step for including gridded variables in the conditional logit model.

Usage

format_grid(
  grid,
  dat,
  project,
  dat.key,
  area.dat,
  area.grid = NULL,
  id.cols,
  from.format = "wide",
  to.format = "wide",
  val.name = NULL,
  save = FALSE
)

Arguments

grid

Gridded dataset to format.

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

Name of project.

dat.key

String, name of column(s) in MainDataTable to join by. The number of columns must match id.cols.

area.dat

String, the name of the area or zone column in dat.

area.grid

String, the name of the area or zone column in dat if from.format = "long". Ignored if from.format = "wide".

id.cols

String, the names of columns from grid that are neither area (area.grid) or value (val.name) columns, for example date or period column(s).

from.format

The original format of grid. Options include "long" or "wide". Use "long" if a single area column exists in grid. Use "wide" if grid contains a column for each area.

to.format

The desired format of grid. Options include "long" or "wide". Use "long" if you want a single area column with a corresponding value column. Use "wide" if you would like each area to have its own column.

val.name

Required if converting from wide to long or long to wide format. When from.format = "wide" and to.format = "long", val.name will be the name of the new value variable associated with the area column. When from.format = "long" and to.format = "wide", val.name will be the name of the existing value variable associated with the area column.

save

Logical, whether to save formatted grid. When TRUE, the table will be saved with the string "Wide" or "Long" appended depending on the value of to.format.

See Also

merge_dat()


Format Model Data

Description

Reshapes the project datasets into a single long format suitable for discrete choice modeling using RTMB. This function integrates various data sources-including distance matrices, expectation (catch and/or revenue) matrices, auxiliary data, and gridded environmental data- and performs optional missing data imputation.

Usage

format_model_data(
  project,
  name,
  alt_name,
  zone_id,
  unique_obs_id,
  select_vars = NULL,
  aux_data = NULL,
  aux_key = NULL,
  gridded_data = NULL,
  expectations = NULL,
  distance = TRUE,
  distance_units = NULL,
  impute = NULL,
  crs = NULL,
  overwrite = FALSE
)

Arguments

project

Name of the project.

name

Name for this specific formatted model data instance. Must be unique within the project's formatted data list.

alt_name

Name of the alternative choice matrix.

zone_id

Variable name in the dataset representing the zone identifier.

unique_obs_id

Variable name in the dataset representing the unique observation identifier (unique rows in the main data table).

select_vars

Character vector of variable names to retain from the main data table. Although this input is optional, it is recommended to limit the final format to necessary variables for computational efficiency. IMPORTANT NOTE: if modeling multi-haul data, be sure to include the lagged zone ID (previous location) in this vector.

*IMPORTANT NOTE*: for expected profit models, the price and actual catch variables must be included here.

aux_data

Name of the auxiliary data table to join. Use list_tables function to view the table name.

aux_key

Variable name used to join the main data table with the auxiliary data.

gridded_data

Name of the gridded data table to join. Use list_tables function to view the table name.

expectations

Character vector containing the names of expected catch or revenue matrices to merge into the dataset.

distance

Logical. If 'TRUE', calculates and merges a distance matrix between observations and zones. Defaults to 'TRUE'.

distance_units

String representing the units of measurement for distance ("km" or "mi").

impute

Method for imputing missing values (NAs). Options are '"mean"', '"median"', '"mode"', or '"remove"'. '"remove"' will completely remove zones from the dataset that contain any NAs in corresponding data. If NULL, the function stops if NAs are detected.

crs

Coordinate reference system. Only used if 'distance = TRUE' and spatial calculations are required.

overwrite

Logical. Defaults to FALSE. If TRUE, overwrites an existing formatted dataset with the same name.

Details

The resulting formatted data is serialized and stored in the FishSET project database within a table named '[project_name]LongFormatData'.

Value

A list containing the formatted data frame and the input settings. The list is saved to the project database.

Examples

## Not run: 
  # Basic usage: Formatting data with simple mean imputation for missing values
  format_model_data(
    project = "NewEnglandCod",
    name = "ModelData_Run1",
    alt_name = "altname_1",
    zone_id = "zone_code",
    unique_obs_id = "trip_id",
    select_vars = c("vessel_length", "month", "permit_type"),
    impute = "mean"
  )

  # Advanced usage: Including distance calculations, auxiliary economic data, 
  # and revenue expectations
  format_model_data(
    project = "WestCoastGroundfish",
    name = "ModelData_Spatial",
    alt_name = "altname_1",
    zone_id = "grid_id",
    unique_obs_id = "haul_id",
    select_vars = c("vessel_len", "month", "fuel_price"),
    aux_data = "FuelCostIndex",
    aux_key = "year",
    expectations = c("ExpectedRevenue_2023"),
    distance = TRUE,
    crs = 4326,
    impute = "remove"
  )

## End(Not run)

Display summary of function calls

Description

Display summary of function calls

Usage

function_summary(project, date = NULL, type = "dat_load", show = "all")

Arguments

project

Project name.

date

Character string; the date of the log file (" retrieve. If NULL the most recent log is pulled.

type

The type of function to display. "dat_load", "dat_quality", "dat_create", "dat_exploration", "fleet", and "model".

show

Whether to display "all" calls, the "last" (most recent) call, or the "first" (oldest) function call from the log file.

Details

Displays a list of functions by type and their arguments from a log file. If no date is entered the most recent log file is pulled.

See Also

filter_summary

Examples

## Not run: 
function_summary("pollock")

## End(Not run)

Retrieve closure scenario by project

Description

Retrieve closure scenario by project

Usage

get_closure_scenario(project)

Arguments

project

Name of project.

Examples

## Not run: 
get_closure_scenario("pollock")

## End(Not run)

Return cached confidentiality tables

Description

This function lists the confidentiality "check" tables used to suppress values.

Usage

get_confid_cache(project, show = "all")

Arguments

project

Name of project

show

Output "all" tables, "last" table, or "first" table.

Value

A list of tables containing suppression conditions.

See Also

reset_confid_cache


Return the confidentiality settings

Description

This function returns the confidentiality settings from project settings file.

Usage

get_confid_check(project)

Arguments

project

Name of project

Value

A list containing the confidentiality parameters: check, v_id, rule, and value.

See Also

set_confid_check get_proj_settings


Retrieve grid log file

Description

Retrieves the grid log file for a project. The grid log shows which grid files are currently saved to the project data folder.

Usage

get_grid_log(project)

Arguments

project

Name of project.

Details

The grid log is a list containing information about the grid files currently saved to the project data folder. Each grid entry contains three fields: grid_name, closure_name, and combined_areas. grid_name is the name of the original grid object. If the other two fields are empty, this means that the grid file has not been altered and is the same as the original. closure_name is the name of a second grid file containing closure areas that were combined with grid_name. combined_areas are the names/IDs of the closures areas from the closure grid file that were combined with grid_name.

Examples

## Not run: 
get_grid_log("pollock")

## End(Not run)

Pull data from latest project file

Description

Pull data from latest project file

Usage

get_latest_projectfile(project, mod.name)

Arguments

project

Project name

mod.name

Model name

Examples

## Not run: 
get_latest_projectfile("pollock", "logit_mod1")

## End(Not run)

Retrieve project settings

Description

Retrieve project settings

Usage

get_proj_settings(project, format = FALSE)

Arguments

project

Name of project.

format

Logical, output project settings using pander. Useful for markdown documents.

Details

The project settings file includes confidentiality settings, the user output folder directory, and the default plot saving size.


Calculate and view Getis-Ord statistic

Description

Wrapper function to calculate global and local Getis-Ord by discrete area

Usage

getis_ord_stats(
  dat,
  project,
  varofint,
  zoneid,
  spat,
  cat,
  lon.dat = NULL,
  lat.dat = NULL,
  lon.spat = NULL,
  lat.spat = NULL
)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

String, name of project.

varofint

Numeric variable in dat to test for spatial high/low clustering.

zoneid

Variable in dat that identifies the individual zones or areas. Define if exists in dat and is not named 'ZoneID'. Defaults to NULL.

spat

Spatial data containing information on fishery management or regulatory zones. See load_spatial.

cat

Variable in spat defining the individual areas or zones.

lon.dat

Longitude variable in dat.Require if zoneid is not defined.

lat.dat

Latitude variable in dat. Require if zoneid is not defined.

lon.spat

Variable or list from spat containing longitude data. Required for csv files. Leave as NULL if spat is a shape or json file.

lat.spat

Variable or list from spat containing latitude data. Required for csv files. Leave as NULL if spat is a shape or json file.

Details

Calculates the degree, within each zone, that high or low values of the varofint cluster in space. Function utilizes the localG and knearneigh functions from the spdep package. The spatial input is a row-standardized spatial weights matrix for computed nearest neighbor matrix, which is the null setting for the nb2listw function. Requires a data frame with area as a factor, the lon/lat centroid for each area, the lat/lon outlining each area, and the variable of interest (varofint) or a map file with lat/lon defining boundaries of area/zones and variable of interest for weighting. Also required is the lat/lon defining the center of a zone/area. If the centroid is not included in the map file, then find_centroid can be called to calculate the centroid of each zone. If the variable of interest is not associated with an area/zone then the assignment_column function can be used to assign each observation to a zone. Arguments to identify centroid and assign variable of interest to area/zone are optional and default to NULL.

Value

Returns a plot and table. Both are saved to the output folder.

Examples

## Not run: 
getis_ord_stats(pcodMainDataTable, project = 'pcod', varofint = 'OFFICIAL_MT_TONS',
  spat = spatdat, lon.dat = 'LonLat_START_LON', lat.dat = 'LonLat_START_LAT', cat = 'NMFS_AREA')

## End(Not run)

Create a within-group running sum variable

Description

Create a within-group running sum variable

Usage

group_cumsum(
  dat,
  project,
  group,
  sort_by,
  value,
  name = "group_cumsum",
  include_total_col = FALSE
)

Arguments

dat

Primary data frame over which to apply function. Table in FishSET database should contain the string 'MainDataTable'.

project

String, project name.

group

String, the grouping variable(s) to sum value by. Used to create the "group_total" variable.

sort_by

String, a date variable (or variables) to order 'MainDataTable' by.

value

String, the value variable used to calculate cumulative sum. Must be numeric.

name

String, the name for the new variable. Defaults to "group_cumsum".

include_total_col

Logical, whether to remove the "group_total" variable created to calculate percentage. Defaults to FALSE.

Details

group_cumsum sums value by group, then cumulatively sums within groups. For example, a running sum by trip variable can be made by entering variables that identify unique vessels and trips into group and a numeric variable (such as catch or # of hauls) into value. Each vessel's trip total is calculated then cumulatively summed. The "group_total" variable gives the total value by group and can be dropped by setting include_total_col = TRUE.

Examples

## Not run: 
group_cumsum(pollockMainDataTable, "pollock", group = c("PERMIT", "TRIP_ID"),
             sort_by = "HAUL_DATE", value = "OFFICIAL_TOTAL_CATCH")

## End(Not run)

Create a within-group lagged difference variable

Description

Create a within-group lagged difference variable

Usage

group_diff(
  dat,
  project,
  group,
  sort_by,
  value,
  name = "group_diff",
  lag = 1,
  include_total_col = FALSE
)

Arguments

dat

Primary data frame over which to apply function. Table in FishSET database should contain the string 'MainDataTable'.

project

String, project name.

group

String, the grouping variable(s) to sum value by. Used to create the "group_total" variable.

sort_by

String, a date variable to order 'MainDataTable' by.

value

String, the value variable used to calculate lagged difference. Must be numeric.

name

String, the name for the new variable. Defaults to "group_diff".

lag

Integer, adjusts lag length. Defaults to 1.

include_total_col

Logical, whether to remove the "group_total" variable created to calculate percentage. Defaults to FALSE.

Details

group_diff creates a grouped lagged difference variable. value is first summed by the variable(s) in group, then the difference within-group is calculated. The "group_total" variable gives the total value by group and can be dropped by setting include_total_col = FALSE.

Examples

## Not run: 
group_diff(pollockMainDataTable, "pollock", group = c("PERMIT", "TRIP_ID"),
           sort_by = "HAUL_DATE", value = "HAUL")

## End(Not run)

Create a within-group percentage variable

Description

Create a within-group percentage variable

Usage

group_perc(
  dat,
  project,
  group = NULL,
  value,
  name = "group_perc",
  include_total_col = FALSE
)

Arguments

dat

Primary data frame over which to apply function. Table in FishSET database should contain the string 'MainDataTable'.

project

String, project name.

group

String, primary grouping variable(s). Used to create the "total_value" variable which sums value by group.

value

String, the value variable used to calculate percentage. Must be numeric.

name

String, the name for the new variable. Defaults to "group_perc".

include_total_col

Logical, whether to remove the "total_value" variables created to calculate percentage. Defaults to FALSE.

Details

group_perc creates a within-group percentage variable using a primary group (group). The total value of group is stored in the "total_value" variable, and the within-group total stored in "group_total". The group percentage is calculated using these two function-created variables. "total_value" can be dropped by setting include_total_col = TRUE.

Examples

## Not run: 
group_perc(pollockMainDataTable, "pollock", group = "PERMIT",
           value = "OFFICIAL_TOTAL_CATCH_MT")
           
group_perc(pollockMainDataTable, "pollock", group = "DISEMBARKED_PORT", value = "HAUL")

## End(Not run)

Collapse a data frame from haul-level to trip-level

Description

Aggregates a data frame from individual observations (hauls) to a summary level (trips). It provides flexible methods for collapsing numeric, character, and temporal data.

Usage

haul_to_trip(
  dat,
  project,
  trip_id,
  zoneID_dat,
  zone_fun = "mode",
  date_fun = "min",
  num_fun = "mean",
  char_fun = "mode",
  haul_count = TRUE,
  log_fun = TRUE
)

Arguments

dat

String, the name of the main data containing information on hauls or trips. Note that this is the project 'MainDataTable' in the FishSET database.

project

String, name of project.

trip_id

String. Column name that represents the unique trip identifier in dat.

zoneID_dat

String, the name of the column identifying fishing zones. This column is handled separately from other columns.

zone_fun

String, method for collapsing the 'zoneID_dat' variable. Options are "first", "last", or "mode".

date_fun

String, method for collapsing temporal columns. Options are "mean", "median", "min", or "max".

num_fun

String, method for collapsing numeric columns. Options are "mean", "median", "mode", "min", "max", or "sum".

char_fun

String, method for collapsing character or factor columns. Options are "first", "last", "paste", or "mode".

haul_count

Logical, If TRUE, a column name "haul_count" is added, showing the number of hauls (rows) per trip in the original data.

log_fun

Logical, If TRUE, the function call is logged for tracking.

Details

The function aggregates columns based on their data type per unique trip ID. For columns that are not numeric, date, or character, the function defaults to taking the first observation for each trip.

Value

A data frame where each row represents a single trip, aggregated according to the specified methods.

Examples

## Not run: 
# Collapse the data from haul to trip level
trip_data <- haul_to_trip(
  dat = "pollockMainDataTable",
  project = "pollock",
  trip_id = "tripID",
  zone_col = "ZONE",
  zone_fun = "mode", # Use the most common zone for the trip
  date_fun = "min",   # Use the earliest haul date as the trip date
  num_fun = "sum",    # Sum the fish weight for the trip
  char_fun = "first", # Use the first vessel name recorded
  haul_count = TRUE
)

## End(Not run)

Create ID variable

Description

Create ID variable from one or more variables

Usage

ID_var(
  dat,
  project,
  vars,
  name = NULL,
  type = "string",
  drop = FALSE,
  sep = "_",
  log_fun = TRUE
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Project name.

vars

Character string, additional column(s) in dat that define unique observations.

name

String, name of new ID column.

type

String, the class type of the new ID column. Choices are 'string“ or 'integar'. 'string' returns a character vector where each column in vars is combined and separated by sep. 'integer' returns an integer vector where each value corresponds to a unique group in vars.

drop

Logical, whether to drop columns in vars.

sep

Symbol used to combined variables.

log_fun

Logical, whether to log function call (for internal use).

Details

ID variable can be based on a single or multiple variables. Use sep = TRUE if dropping variables that create the ID variable.

Value

Returns the 'MainDataTable' with the ID variable included.

Examples

## Not run: 
pcodMainDataTable <- ID_var(pcodMainDataTable, "pcod", name = "PermitID", 
        vars = c("GEAR_TYPE", "TRIP_SEQ"), type = 'integar')
pcodMainDataTable <- ID_var(pcodMainDataTable, "pcod", name = "PermitID", 
        vars = c("GEAR_TYPE", "TRIP_SEQ"), type = 'string', sep="_")

## End(Not run)

Insert plot from user folder

Description

Insert plot from user folder

Usage

insert_plot(out, project)

Arguments

out

String, plot file name.

project

Name of project.

Examples

## Not run: 
insert_plot("pollock_plot.png")

## End(Not run)

Insert table from user folder

Description

Insert table from user folder

Usage

insert_table(out, project)

Arguments

out

String, table file name.

project

Name of project.

Examples

## Not run: 
insert_table("pollock_table.csv")

## End(Not run)

Jitter longitude and latitude variables

Description

Jitter longitude and latitude variables

Usage

jitter_lonlat(dat, project, lon, lat, factor = 1, amount = NULL)

Arguments

dat

Primary data frame over which to apply function. Table in FishSET database should contain the string 'MainDataTable'.

project

Project name.

lon

String, variable name containing longitude.

lat

String, variable name containing latitude.

factor

Numeric, see jitter for details.

amount

Numeric, see jitter for details. Default (NULL): factor * d/5 where d is about the smallest difference between x values.

Details

This is one of the FishSET confidentiality functions. It "jitters" longitude and latitude using the base R function jitter.

Examples

## Not run: 
jitter_lonlat(pollockMainDataTable, "pollock",
              lon = "LonLat_START_LON", lat = "LonLat_START_LAT")

## End(Not run)

Lag zone variable

Description

Creates a lagged zone ID variable for haul data, where the lagged location of the first haul is filled in with the port of departure. This variable is required for data with multiple sets or hauls in a single trip and for the full information model with Dahl's correction (logit_correction).

Usage

lag_zone(
  dat,
  project,
  spat = NULL,
  port,
  port_name,
  port_lon,
  port_lat,
  trip_id,
  haul_order,
  starting_port,
  zoneID_dat,
  zoneID_spat = NULL,
  name = "startingloc",
  bufferval = 100,
  db_save = FALSE
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Name of project

spat

Spatial data. Required if ZoneID does not exists in dat. Shape, json, geojson, and csv formats are supported.

port

Port data. Contains columns: Port_Name, Port_Long, Port_Lat. Table is generated using the load_port and saved in the FishSET database as the project and port table, for example 'pollockPortTable'.

port_name

String indicating the column in port table that contains the port name

port_lon

String indication the column in port table that contains port longitude

port_lat

String indication the column in port table that contains port latitude

trip_id

Variable in dat that identifies unique trips.

haul_order

Variable in dat containing information on the order that hauls occur within a trip. Can be time, coded variable, etc.

starting_port

Variable in dat to identify port at start of trip.

zoneID_dat

Variable in dat that identifies the individual zones or areas.

zoneID_spat

Variable in spat that identifies the individual zones or areas.

name

String, name of created variable. Defaults to name of the function if not defined.

bufferval

Maximum buffer distance, in meters, for assigning observations to the closest zone polygon.

db_save

Default = FALSE and only returns the dataframe without saving the the database. db_save = TRUE will save the dataframe with the lagged variable to the database.

Details

Function creates the startloc vector that is required for multihaul data and the full information model with Dahl's correction logit_correction. The vector is the lagged zone ID - zone when the decision of where to fish next was made. Generally, the first zone of a trip is the port of departure. The assignment_column function is called to assign starting port locations and haul locations to zones. If ZoneID exists in dat, assignment_column is not called and the following arguments are not required: spat, zoneID_spat.

Value

Primary data set with starting location variable added.

Examples

## Not run: 
pcodMainDataTable <- lag_zone(pcodMainDataTable, 'pcod',
    map2, "pcodPortTable", "TRIP_SEQ", "HAUL_SEQ", "DISEMBARKED_PORT", 
 "START_LON", "START_LAT", "NMFS_AREA", "STARTING_LOC"
)

## End(Not run)

View list of all log files

Description

View list of all log files

Usage

list_logs(project = NULL, chron = FALSE, modified = FALSE)

Arguments

project

Project name. Displays all logs if NULL.

chron

Logical, whether to display logs in chronological order (TRUE) or reverse chronological order (FALSE).

modified

Logical, whether to include date modified.


Display FishSET database tables by type

Description

Show project table names by table type. To see all tables for all projects in the FishSETFolder, use fishset_tables.

Usage

list_tables(project, type = "main")

Arguments

project

A project name to show main tables by.

type

the type of fishset_db table to search for. Options include "main" (MainDataTable), "port" (PortTable), "spat" (SpatTable), "grid" (GridTable), "aux" (AuxTable) "ec" (ExpectedCatch), "altc" (AltMatrix), "info" (MainDataTableInfo), "gc" (ldglobalcheck), "fleet" (FleetTable), "filter" (FilterTable), "centroid" (Centroid or FishCentroid), "model" (ModelOut), "model data" (ModelInputData), "outsample" (OutSampleDataTable).

Examples

## Not run: 
list_tables("pollock", type = "main")
list_tables("pollock", "ec")

## End(Not run)

Import, parse, and save auxiliary data to FishSET database

Description

Auxiliary data is additional data that connects the primary dataset. Function pulls the data, parses it, and then and saves the data to the FishSET database. A project must exist before running load_aux(). See load_maindata to create a new project.

Usage

load_aux(dat, aux, name, over_write = TRUE, project = NULL)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

aux

File name, including path of auxiliary data.

name

Name auxiliary data should be saved as in FishSET database.

over_write

Logical, If TRUE, saves data over previously saved data table in the FishSET database.

project

String, name of project.

Details

Auxiliary data is any additional data beyond the primary data and the port data. Auxiliary data can be any data that can be merged with the primary dataset (ex. prices by date, vessel characteristics, or fishery season). The auxiliary data does not have to be at a haul or trip level but must contain a variable to connect the auxiliary data to the primary dataset. The function checks that at least one column name of the auxiliary data matches a column name in the primary dataset. The function checks that each row is unique, that no variables are empty, and that column names are case-insensitive unique. There data issues are resolved before the data is saved to the database. The data is saved in the FishSET database as the raw data and the working data. The naming convention for auxiliary tables is "projectNameAuxTable". Date is also added to the name for the raw data. See table_view to view/load auxiliary tables into the working environment.

See Also

table_view, load_maindata, write_dat

Examples

## Not run: 
load_aux(pcodMainDataTable, name = 'FisherySeason', over_write = TRUE, 
         project = 'pcod')

## End(Not run)

Load data from FishSET database into the R environment

Description

Load data from FishSET database into the R environment

Usage

load_data(project, name = NULL)

Arguments

project

String, name of project.

name

Optional, name of table in FishSET database. Use this argument if pulling raw or dated table (not the working table).

Details

Pulls the primary data table from the FishSET database and loads it into the working environment as the project and MainDataTable. For example, if the project was pollock, then data would be saved to the working environment as 'pollockMainDataTable'.

Value

Data loaded to working environment as the project and ‘MainDataTable’.

Examples

## Not run: 
load_data('pollock')

load_data('pollock', 'pollockMainDataTable20190101')

## End(Not run)

Import, parse, and save gridded data to FishSET database

Description

Gridded data is data that varies by two dimensions. Column names must be zone names. Load, parse, and save gridded data to FishSET database. A project must exist before running load_grid(). See load_maindata to create a new project.

Usage

load_grid(grid, name, project, over_write = TRUE)

Arguments

grid

File name, including path, of gridded data.

name

Name gridded data should be saved as in FishSET database.

project

String, name of project.

over_write

Logical, If TRUE, saves dat over previously saved data table in the FishSET database.

Details

Grid data is an optional data frame that contains a variable that varies by the map grid (ex. sea surface temperature, wind speed). Data can also vary by a second dimension (e.g., date/time). Both dimensions in the gridded data file need to be variables included in the primary data set. The grid locations (zones) must define the columns and the optional second dimension defines the rows. The row variable must have the exact name as the variable in the primary data frame that it will be linked to. The function DOES NOT check that column and row variables match a variable in the primary data set. The function checks that each row is unique, that no variables are empty, and that column names are case-insensitive unique. These data issues are resolved before the data is saved to the database. The data is saved in the FishSET database as the raw data and the working data. In both cases, the table name is the project and the file name x. Date is attached to the name for the raw data. The naming convention for gridded tables is "projectNameGridTable". See table_view to view/load gridded tables into the working environment.

See Also

table_view, load_maindata, write_dat

Examples

## Not run: 
load_grid(dat = 'pcodMainDataTable', name = 'SeaSurfaceTemp', 
          over_write = TRUE, project = 'pcod')

## End(Not run)

load_gui_variables

Description

A helper function to load selected variables from an RDS file in the FishSET GUI.

Usage

load_gui_variables(project_name, folderpath)

Arguments

project_name

The name of the current project.

folderpath

The file path to the project's root folder.

Value

A list of saved variables if the file exists, otherwise returns NULL.


Import, parse, and save data to the FishSET Database

Description

load_maindata() saves the primary dataset to the FishSET Database (located in the FishSETFolder) and is a required step. The primary data will also be loaded into the working environment as a dataframe named "projectMainDataTable". Running load_maindata() creates a new project directory in the FishSETFolder. To see a list of existing projects run projects() or open the FishSETFolder.

Usage

load_maindata(dat, project, over_write = FALSE, compare = FALSE, y = NULL)

Arguments

dat

Primary data containing information on hauls or trips. This can be the full path to the file, the name of a main table in the FishSET database, or a dataframe object in the working environment. Main tables in the FishSET database contain the string 'MainDataTable'. A complete list of FishSET tables can be display by running fishset_tables().

project

String, name of project. Cannot contain spaces.

over_write

Logical, If TRUE, saves data over previously saved data table in the FishSET database. Defaults to FALSE.

compare

Logical, whether to compare new dataframe to previously saved dataframe y. See fishset_compare.

y

Name of previously saved table in FishSET Database. y must be defined if compare = TRUE.

Details

The dataset is saved in the FishSET database as raw and working tables. The table name is the project and the table type, 'MainDataTable'. The raw table is the original, unedited table. The working table contains any changes made to the table after uploading. An eight digit date string is included in the name of the raw table (e.g. "pollockMainDataTable20220210"). The primary data is loaded into the working environment as ‘projectMainDataTable’. The fishset_compare argument compares dat to an existing FishSET table in y and returns a message noting basic differences between the two. The column names are checked for case-insensitivity and uniqueness.

See Also

save_dat, write_dat, load_data, fishset_tables

Examples

## Not run: 
# upload data from filepath
load_maindata(dat = "PATH/TO/DATA", project = "pollock")

# upload from dataframe in working environment
load_maindata(dat = Mydata, project = 'pollock', over_write = TRUE, 
              compare = TRUE, y = 'MainDataTable01012011')
              
# upload from an exisitng FishSET primary data table
looad_maindata(dat = "pollockMainDataTable", project = "pollock2020")

## End(Not run)

Import, parse, and save out-of-sample data to FishSET database

Description

load_outsample() saves out-of-sample dataset to the FishSET Database (located in the FishSETFolder) and the structure must match the primary dataset. A project must exist before running load_outsample(). See load_maindata to create a new project. Note: if the data are out-of-sample temporally then upload a new datafile, if the data are only out-of-sample spatially then upload the primary data file in this function.

Usage

load_outsample(dat, project, over_write = FALSE, compare = FALSE, y = NULL)

Arguments

dat

Out-of-sample data containing information on hauls or trips with same structure as the primary data table. This can be the full path to the file, the name of a out-of-sample table in the FishSET database, or a dataframe object in the working environment. Out-of-sample tables in the FishSET database contain the string 'OutSampleDataTable'. A complete list of FishSET tables can be viewed by running fishset_tables().

project

String, name of project.

over_write

Logical, If TRUE, saves data over previously saved data table in the FishSET database. Defaults to FALSE.

compare

Logical, whether to compare new dataframe to previously saved dataframe y. See fishset_compare.

y

Name of previously saved table in FishSET Database. y must be defined if compare = TRUE.

Details

The out-of-sample dataset is saved in the FishSET database as raw and working tables. The table name is the project and the table type, 'OutSampleDataTable'. The raw table is the original, unedited table. The working table contains any changes made to the table after uploading. An eight digit date string is included in the name of the raw table (e.g. "pollockOutSampleDataTable20220210"). The out-of-sample data is loaded into the working environment as ‘projectOutSampleDataTable’. The fishset_compare argument compares dat to an existing FishSET table in y and returns a message noting basic differences between the two. The column names are checked for case-insensitivity and uniqueness.

See Also

load_maindata, save_dat, write_dat, load_data, fishset_tables

Examples

## Not run: 
# upload data from filepath
load_outsample(dat = "PATH/TO/DATA", project = "pollock")

# upload from dataframe in working environment
load_outsample(dat = MyData, project = 'pollock', over_write = TRUE, 
              compare = TRUE, y = 'OutSampleDataTable01012011')
              
# upload from an exisitng FishSET out-of-sample data table
load_outsample(dat = "pollockOutSampleDataTable", project = "pollock")

## End(Not run)

Import, parse, and save port data to FishSET database

Description

A project must exist before running load_port(). See load_maindata to create a new project.

Usage

load_port(
  dat,
  port_name,
  project,
  over_write = TRUE,
  compare = FALSE,
  y = NULL
)

Arguments

dat

Dataset containing port data. At a minimum, must include three columns, the port names, and the latitude and longitude of ports. dat can be a filepath, a existing FishSET table, or a dataframe in the working environment.

port_name

Variable containing port names. Names should match port names in primary dataset.

project

String, name of project.

over_write

Logical, if TRUE, saves over data table previously saved in the FishSET database.

compare

Logical, should new data be compared to previously saved dataframe y.

y

Name of previously saved table in FishSET database. y must be defined if compare is TRUE.

Details

Runs a series of checks on the port data. The function checks that each row is unique, that no variables are empty, and that column names are case-insensitive unique. There data issues are resolved before the data is saved to the database. If checks pass, runs the fishset_compare function and saves the new data frame to the FishSET database. The data is saved in the FishSET database as the raw data and the working data. The naming convention for port tables is "projectPortTable". Date is also attached to the name for the raw data. See table_view to view/load port tables into the working environment.

See Also

table_view, load_maindata, write_dat

Examples

## Not run: 
load_port(PortTable, over_write = TRUE, project  ='pollock',
          compare = TRUE, y = 'pollockPortTable01012011')

## End(Not run)

Import, parse, and save spatial data

Description

Saves a spatial table to the FishSETFolder as a geojson file. A project must exist before running load_spatial(). See load_maindata to create a new project.

Usage

load_spatial(
  spat,
  name = NULL,
  over_write = TRUE,
  project,
  data.type = NULL,
  lon = NULL,
  lat = NULL,
  id = NULL,
  ...
)

Arguments

spat

File name, including path, of spatial data.

name

Name spatial data should be saved as in FishSET project folder. Cannot be empty or contain spaces.

over_write

Logical, If TRUE, saves spat over previously saved data table in the FishSET project folder.

project

String, name of project.

data.type

Data type argument passed to read_dat. If reading from a shape folder use data.type = "shape".

lon

Variable or list from spat containing longitude data. Required for csv files. Leave as NULL if spat is a shape or json file.

lat

Variable or list from spat containing latitude data. Required for csv files. Leave as NULL if spat is a shape or json file

id

Polygon ID column. Required for csv files. Leave as NULL if spat is a shape or json file.

...

Additional argument passed to read_dat.

Details

Function to import, parse, and saved project folder in 'FishSETFolder' directory. To export as shape file, use write_dat specifying ‘type=’shp''. load_spatial() performs basic quality check before saving spatial tables to the project data folder as a geojson file. To be saved, the spatial must pass the checks in check_spatdat. The spatial table is converted to an sf object, and checked for unique rows and empty columns. The naming convention for spatial tables is "projectNameSpatTable". See table_view to view/load spatial tables into the working environment.

See Also

table_view, load_maindata, write_dat

Examples

## Not run: 
# upload from filepath
load_spatial(spat = "FILE/PATH/TO/SPAT", name = 'tenMinSqr', 
             over_write = TRUE, project = 'pcod')

# upload from object in working environment
load_spatial(spat = NMFSAreas, name = "NMFS", project = "pcod")

# upload from an existing FishSET spatial table
load_spatial(spat = "pcodNMFSSpatTable", name = "NMFS", project = "pcod2020")

## End(Not run)

Log user-created functions or models

Description

Log user-created functions or models

Usage

log_func_model(x, project)

Arguments

x

Name of function.

project

Project name.

Details

Logs function name, arguments, and, call. Use this function to log user-defined likelihood functions.

Examples

## Not run: 
my_func <- function(a, b) {
  a + b
}
log_func_model(my_func)

## End(Not run)

Console function for rerunning project log

Description

Console function for rerunning project log

Usage

log_rerun(
  log_file,
  dat = NULL,
  portTable = NULL,
  aux = NULL,
  gridfile = NULL,
  spat = NULL,
  ind = NULL,
  run = FALSE
)

Arguments

log_file

String, name of the log file starting with the date (YYYY-MM-DD) and ending in ".json".

dat

String, new primary data table to rerun log

portTable

String, name of port table. Defualts to NULL.

aux

String, name of auxiliary table. Defaults to NULL.

gridfile

String, name of gridded data table. Defaults to NULL.

spat

String, name of spatial data table. Defaults to NULL.

ind

Numeric, indices of function calls to rerun.

run

Logical, whether to run the logged function calls (TRUE) or simply list all function calls (FALSE).

See Also

log_rerun_gui

Examples

## Not run: 
log_rerun("pollock_2020-10-23.json", run = TRUE) # reruns entire log with original data table
# runs log with new data table
log_rerun("pollock_2020-10-23.json", dat = "pollockMainDataTable", run = TRUE) 

## End(Not run)

Interactive function for rerunning project log

Description

Interactive function for rerunning project log

Usage

log_rerun_gui()

See Also

log_rerun

Examples

## Not run: 
log_rerun_gui()

## End(Not run)

Reset log file

Description

Reset log file

Usage

log_reset(project, over_write = FALSE)

Arguments

project

Project name.

over_write

Logical, whether to over write an existing log file. This only applies if a log was created and reset in the same day for the same project. See "Details".

Details

Logs are saved by project name and date (date created, not date modified). For example, "pollock_2021-05-12.json". Calls to log functions are automatically appended to the existing project log file. Resetting the log file will create a new project log file with the current date. A log will not be reset if log_reset() is run the same day the log was created (or if the log is reset two or more times in a single day), unless over_write = TRUE. This will replace that day's log file.

See Also

list_logs project_logs

Examples

## Not run: 
log_reset("pollock")

## End(Not run)

Full information model with Dahl's correction function

Description

Full information model with Dahl's correction function

Usage

logit_correction(starts3, dat, otherdat, alts, project, expname, mod.name)

Arguments

starts3

Starting values as a vector (num). For this likelihood, the order takes: c([marginal utility from catch], [catch-function parameters], [polynomial starting parameters], [travel-distance parameters], [catch sigma]).

The number of polynomial interaction terms is currently set to 2, so given the chosen degree 'polyn' there should be (((polyn+1)*2) + 2)*(k) polynomial starting parameters, where (k) equals the number of alternatives. The marginal utility from catch and catch sigma are of length equal to unity respectively. The catch-function and travel-distance parameters are of length (# of catch variables)*(k) and (# of cost variables) respectively.

dat

Data matrix, see output from shift_sort_xcpp, alternatives with distance.

otherdat

Other data used in model (as a list containing objects 'griddat', 'intdat', 'startloc', 'polyn', and 'distance').

For catch-function variables ('griddat') alternative-invariant variables that are interacted with zonal constants to form the catch portion of the likelihood. Each variable name therefore corresponds to data with dimensions (number of observations) by (unity), and returns (k) parameters where (k) equals the number of alternatives. For travel-distance variables alternative-invariant variables that are interacted with travel distance to form the cost portion of the likelihood. Each variable name therefore corresponds to data with dimensions (number of observations) by (unity), and returns a single parameter. Any number of catch-function and travel-distance variables are allowed, as a list of matrices. Note the variables (each as a matrix) within 'griddat' and 'intdat' have no naming restrictions.

Catch-function variables may correspond to variables that affect catches across locations, or travel-distance variables may be vessel characteristics that affect how much disutility is suffered by traveling a greater distance. Note in this likelihood the catch-function variables vary across observations but not for each location: they are allowed to affect catches across locations due to the location-specific coefficients. If there are no other data, the user can set catch-function variables as ones with dimension (number of observations) by (number of alternatives) and travel-distance variables as ones with dimension (number of observations) by (unity).

The variable startloc is a matrix of dimension (number of observations) by (unity), that corresponds to the starting location when the agent decides between alternatives.

The variable polyn is a vector of length equal to unity corresponding to the chosen polynomial degree.

The variable distance is a matrix of dimension (number of observations) by (number of alternatives) corresponding to the distance to each alternative.

alts

Number of alternative choices in model as length equal to unity (as a numeric vector).

project

Name of project

expname

Expected catch table

mod.name

Name of model run for model result output table

Value

ld: negative log likelihood

Graphical examples

Figure: logit\_correction\_grid.png
Figure: logit\_correction\_travel.png
Figure: logit\_correction\_poly.png

Examples

## Not run: 
data(zi)
data(catch)
data(choice)
data(distance)
data(si)
data(startloc)

optimOpt <- c(1000,1.00000000000000e-08,1,0)

methodname <- 'BFGS'

polyn <- 3
kk <- 4

si2 <- sample(1:5,dim(si)[1],replace=TRUE)
zi2 <- sample(1:10,dim(zi)[1],replace=TRUE)

otherdat <- list(griddat=list(si=as.matrix(si),si2=as.matrix(si2)),
    intdat=list(zi=as.matrix(zi),zi2=as.matrix(zi2)),
    startloc=as.matrix(startloc),polyn=polyn,
    distance=as.matrix(distance))

initparams <- c(3, 0.5, 0.4, 0.3, 0.2, 0.55, 0.45, 0.35, 0.25,
    rep(0, (((polyn+1)*2) + 2)*kk), -0.3,-0.4, 3)

func <- logit_correction

results <- discretefish_subroutine(catch,choice,distance,otherdat,
    initparams,optimOpt,func,methodname)

## End(Not run)

Assign longitude and latitude points to zonal centroid

Description

Assign longitude and latitude points to zonal centroid

Usage

lonlat_to_centroid(dat, project, lon, lat, spat, zone)

Arguments

dat

Primary data frame over which to apply function. Table in FishSET database should contain the string 'MainDataTable'.

project

Project name.

lon

String, variable name containing longitude.

lat

String, variable name containing latitude.

spat

Spatial data table containing regulatory zones. This can be a "spatial feature" or sf object.

zone

String, column name contain the assigned zone. Must be the same for both the spatial data table and MainDataTable.

Details

This is one of the FishSET confidentiality functions. It replaces the selected longitude and latitude columns with the zonal centroid derived from a spatial data table.

Examples

## Not run: 
lonlat_to_centroid(pollockMainDataTable, "pollock", spatdat, 
                  lon = "LonLat_START_LON", lat = "LonLat_START_LAT",
                  zone = "NMFS_AREA")

## End(Not run)

View list of MainDataTables in FishSET database

Description

View list of MainDataTables in FishSET database

Usage

main_tables(project, show_all = TRUE)

Arguments

project

A project name to filter main tables by.

show_all

Logical, whether to show all main tables (including raw and final tables) or just editable tables.

Examples

## Not run: 
main_tables("pollock")

## End(Not run)

Add an area/polygon to spatial data

Description

Add an area/polygon to spatial data

Usage

make_spat_area(spat, project, coord, spat.id, new.id, combine)

Arguments

spat

Spatial dataset to add polygon too.

project

Name of project.

coord

Longitude and latitude coordinates forming a polygon. Can be a numeric vector of even length or a numeric matrix with two columns.

spat.id

The ID column in spat

new.id

The ID for new polygon.

combine

Whether to use combine_zone. This will turn the intersections between poly and spat into new polygons. Note that the new polygon IDs will be derived from spat and new.id will not be used.

Details

Adds an area/polygone to a spatial area

See Also

make_polygon add_polygon


Kernel density (hotspot) plot

Description

Kernel density (hotspot) plot

Usage

map_kernel(
  dat,
  project,
  latlon,
  type = "contours",
  group = NULL,
  facet = FALSE,
  date = NULL,
  filter_date = NULL,
  filter_value = NULL,
  minmax = NULL
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

latlon

Character string, specified as latitude then longitude, in decimal degrees.

type

String, plot type. Choices are "point", "contours", or "gradient". Note if you have a group, you must facet when choosing "gradient" (cannot overlap polygons clearly).

group

Optional group argument. Should be a factor with length of (# of observations), where each observation corresponds to the latlon coordinate of the same index. Recall that the legend will output the names of factor levels as you have named them (see ?factor).

facet

Optional facet parameter. TRUE if mapping each group as a separate facet. Defaults to FALSE.

date

Optional date variable to filter data by.

filter_date

Whether to filter data table by "year", "month", or "year-month". date and filter_value must be provided. Defaults to NULL.

filter_value

Integer (4 digits if year, 1-2 if month). The year, month, or year-month to filter data table by. Use a list if using "year-month", with the format: list(year(s), month(s)). For example, list(2011:2013, 5:7) will filter the data table from May to July, 2011-2013.

minmax

Optional map extent argument, a vector (num) of length 4 corresponding to c(minlat, maxlat, minlon, maxlon).

Value

Returns ggplot2 object. Map plot saved to Output folder.

Examples

## Not run: 
map_kernel(pollockMainDataTable, project = 'pollock', type = 'contours',
latlon = c('LonLat_START_LAT', 'LonLat_START_LON'), group = 'PORT_CODE',
facet = TRUE, minmax = NULL, date = 'FISHING_START_DATE',
filter_date = 'year-month', filter_value = list(2011, 2:4))

## End(Not run)

Map observed vessel locations

Description

Plot observed locations on a map. For large datasets, it is best to plot a subset of points. Use percshown to randomly subset the number of points. If the predefined map extent needs adjusting, use minmax.

Usage

map_plot(dat, project, lat, lon, minmax = NULL, percshown = NULL)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, project name.

lat

Variable in dat that defines latitude, in decimal degrees.

lon

Variable in dat that defines longitude, in decimal degrees.

minmax

Optional map extent argument, a vector (num) of length four corresponding to c(minlat, maxlat, minlon, maxlon).

percshown

Whole number, percent of points to show. Use this option if there are a lot of data points.

Value

mapout: ggplot2 object

Examples

## Not run: 
map_plot(pollockMainDataTable, 'pollock', 'LonLat_START_LAT', 'LonLat_START_LON', percshown=10)

## End(Not run)

Map FishSET Predicted Probabilities

Description

Maps the predicted probabilities from of a FishSET model using either a static ggplot map or an interactive leaflet map. Integrates directly with FishSET project databases for data loading and saving.

Usage

map_predicted_probs(
  project,
  fit_name,
  spat,
  zone_spat,
  obs_index = NULL,
  dat_center = TRUE,
  plot_type = "dynamic",
  output = "plot"
)

Arguments

project

Character string. Name of the project.

fit_name

Character string. Name of the specific model fit saved in the project's 'ModelFit' table.

spat

A spatial data file name or object containing information on fishery management or regulatory zones boundaries.

zone_spat

Name of zone ID column in 'spat'. Must match the zone IDs modeled in the probabilities.

obs_index

Integer (Optional). If provided, maps the predicted probabilities for a specific observation (row). If 'NULL' (default), maps the average predicted probability for each zone across all observations.

dat_center

Logical, whether the static plot should center on the zones present in the model predictions ('TRUE') or the entire spatial dataset ('FALSE').

plot_type

Type of plot output; dynamic ('"dynamic"') using leaflet or static ('"static"') using ggplot2.

output

Output a '"plot"', '"table"', or both ('"tab_plot"'). Defaults to '"plot"'.


Merge data tables using a left join

Description

Merge data tables using a left join

Usage

merge_dat(
  dat,
  other,
  project,
  main_key,
  other_key,
  other_type,
  merge_type = "left"
)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

other

A second data table to join to MainDataTable. Use string if referencing a table saved in the FishSET database.

project

Project name.

main_key

String, name of column(s) in MainDataTable to join by. The number of columns must match other_key.

other_key

String, name of column(s) in the other table to join by. The number of columns must match main_key.

other_type

String, the type of secondary data being merged. Options include "aux" (auxiliary), "grid" (gridded), "spat" (spatial), and "port".

merge_type

String, the type of merge to perform. "left" keeps all rows from dat and merges shared rows from other. "full" keeps all rows from each table.

Details

This function merges two datasets using a left join: all columns and rows from the MainDataTable are kept while only matching columns and rows from the secondary table are joined.

Examples

## Not run: 
 pollockMainDataTable <- 
    merge_dat("pollockMainDataTable", "pollockPortTable", "pollock", 
              main_key = "PORT_CODE", other_key = "PORT_CODE")

## End(Not run)

Merge expected catch

Description

Merge expected catch matrices to the primary dataset.

Usage

merge_expected_catch(
  dat,
  project,
  zoneID,
  date,
  exp.name,
  new.name = NULL,
  ec.table = NULL,
  log_fun = TRUE
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

zoneID

zone ID Variable in dat that identifies the individual zones or areas.

date

Date variable used to create the expected catch matrix.

exp.name

Name(s) of expected catch matrix to merge into dat.

new.name

Optional, new name for exp.name. These should be in the same order as exp.name.

ec.table

Optional, the name of a specific expected catch table to use. Defaults to projectnameExpectedCatch.

log_fun

For internal use. Whether to log the function call.

Value

Merges an expected catch matrix created using create_expectations() to the primary dataset, dat.


Print meta tables by project and/or type

Description

Print meta tables by project and/or type

Usage

meta_tables(project, tab.type = NULL)

Arguments

project

Name of project.

tab.type

String, table type. Optional, used to filter output. Options include "main", "spat" (spatial), "port", "grid" (gridded), and "aux" (auxiliary).


Get Model Design List

Description

Returns the Model Design list from the FishSET database.

Usage

model_design_list(project, name = NULL)

Arguments

project

Name of project.

name

Name of Model Design list in the FishSET database. The table name will contain the string "ModelInputData". If NULL, the default table is returned. Use tables_database to see a list of FishSET database tables by project.


Load model comparison metrics to console for the defined project

Description

Load model comparison metrics to console. Metrics are displayed for each model that was fun. Metrics produced by discretefish_subroutine.

Usage

model_fit(project, CV = FALSE)

Arguments

project

String, name of project.

CV

Logical, CV = TRUE to get model fit for training data in k-fold cross validation routine.

Examples

## Not run: 
model_fit('pollock')

## End(Not run)

Load discrete choice model output to console for the defined project

Description

Returns output from running the discretefish_subroutine function. The table parameter must be the full name of the table name in the FishSET database.

Usage

model_out_view(project, CV = FALSE)

Arguments

project

Name of project

CV

Logical, CV = TRUE when viewing model output from training data in k-fold cross validation

Details

Returns output from running discretefish_subroutine. The table argument must be the full name of the table name in the FishSET database. Output includes information on model convergence, standard errors, t-stats, etc.

Examples

## Not run: 
model_out_view('pcod')

## End(Not run)

Load model parameter estimates, standard errors, and t-statistic to console for the defined project

Description

Returns parameter estimates, standard errors, and t-statistic from running the discretefish_subroutine function. The table parameter must be the full name of the table name in the FishSET database.

Usage

model_params(project, output = "list")

Arguments

project

Name of project

output

Options include list, table, or print.

Details

Returns parameter estimates from running discretefish_subroutine. The table argument must be the full name of the table name in the FishSET database.

Examples

## Not run: 
model_params('pcod')

## End(Not run)

Check Spatial Autocorrelation of Model Residuals

Description

Calculates zone-level residuals from a fitted FishSET model, tests for spatial autocorrelation using Moran's I, and generates a spatial plot of the residuals.

Usage

model_resid_corr(
  project,
  model_name,
  spat,
  spat_id,
  fit_name = NULL,
  distribution = NULL
)

Arguments

project

Character string. Name of the project.

model_name

Character string. Name of the specific model design used.

spat

Character string of spatial table name in project database OR sf polygon object containing the spatial boundaries of the fishing zones.

spat_id

Character string. The name of the column in spat that matches the zone identifiers used in the model design.

fit_name

Character string (Optional). Name of the model fit object. Defaults to paste0(model_name, "_fit").

distribution

Character string (Optional). Distribution for the continuous catch component in EPMs. Required if evaluating an EPM.

Value

A list of class "fishset_spatial_resid" containing:

moran_test

The results of the Moran's I test from spdep.

residual_map

A ggplot2 object mapping the spatial residuals.

zonal_residuals

A dataframe of the calculated mean residuals per zone.

spatial_data

The merged sf object containing geometries and residuals.


Calculate Moran's I statistic

Description

This wrapper function calculates global and local Moran's I statistics to measure spatial autocorrelation by discrete area.

Usage

moran_stats(dat, var, dat_zone, spat, spat_zone, project)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MaindataTable'.

var

Numeric variable from dat to test for spatial autocorrelation.

dat_zone

Variable in dat that identifies the individual zones or areas. Define if exists in dat and is not named 'ZoneID'. Defaults to NULL.

spat

Spatial data containing information on fishery management or regulatory zones. Shape, json, geojson, and csv formats are supported.

spat_zone

Variable or list in spat that identifies the individual areas or zones. If spat is class sf, cat should be name of list containing information on zones.

project

Name of the project

Details

The function measures the degree of spatial autocorrelation and utilizes functions from the 'spdep' package. The function requires a spatial file with latitude and longitude coordinates defining the boundaries of areas or zones and a variable of interest ('var') to test for spatial autocorrelation.

Value

Returns a list with (1) global Moran's I stats, (2) Moran lagged plot, (3) LISA cluster map summarizing local Moran's values

Examples

## Not run: 
moran_stats(pcodMainDataTable, var='OFFICIAL_MT_TONS', dat_zone='zoneID',
  spat=spatdat, spat_zone='NMFS_AREA', project = 'pcod')

## End(Not run)

Identify, remove, or replace NAs and NaNs

Description

Replaces NAs and NaNs in the primary data with the chosen value or removes rows containing NAs and NaNs.

Usage

na_filter(
  dat,
  project,
  x = NULL,
  replace = FALSE,
  remove = FALSE,
  rep.value = "mean",
  over_write = FALSE
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Project name.

x

Character string. Column(s) in dat in which to remove or replace NAs.

replace

Logical, if TRUE, replaces NAs in a vector with rep.value. Defaults to FALSE.

remove

Logical, if TRUE removes the entire row of the dat where NA is present in a dat. Defaults to FALSE.

rep.value

Value to replace all NAs in a numeric column. Defaults to the mean value of the column. Other options include "median" or a numeric value, e.g. rep.value = 0.

over_write

Logical, If TRUE, saves data over previously saved data table in the FishSET database.

Details

To check for NAs across dat run the function specifying only dat (na_filter(dataset, project)). The function will return a statement of which variables, if any, contain NAs. To remove NAs, use remove = TRUE. All rows containing NAs in x will be removed from dat. To replace NAs, use replace = TRUE. If replace = FALSE and rep.value is not defined, then NAs are replaced with mean value. The modified dataset will be returned if replace = TRUE or remove = TRUE. If both replace and remove are TRUE then replace is used. Save the modified data table to the FishSET database by setting over_write = TRUE).

Value

If replace and remove are FALSE then a statement of whether NAs are found is returned. If either replace or remove is TRUE the modified primary dataset is returned.

Examples

## Not run: 
na_filter(pcodMainDataTable, 'pcod', 'OFFICIAL_TOTAL_CATCH_MT')

mod.dat <- na_filter(pcodMainDataTable, 'pcod', 'OFFICIAL_TOTAL_CATCH_MT', 
                     replace = TRUE)
                     
mod.dat <- na_filter(pcodMainDataTable,'pcod', 'OFFICIAL_TOTAL_CATCH_MT',
                     replace = TRUE, rep.value = 0)
                     
mod.dat <- na_filter(pcodMainDataTable, 'pcod',
                     c('OFFICIAL_TOTAL_CATCH_MT', 'CATCH_VALUE'), 
                     remove = TRUE)

## End(Not run)

Check for unique and syntatcic column names

Description

Used for creating new columns.

Usage

name_check(dat, names, repair = FALSE)

Arguments

dat

Dataset that will contain new columns.

names

New names to be added to dataset.

repair

Logical, whether to return repaired column names (repair = TRUE) or just check for unique column names (repair = FALSE).

Details

name_check() first checks to see if new column names are unique and returns an error if not. When repair = TRUE, name_check() will check for unique column names and returns new column names that are unique and syntactic (see vec_as_names for details).


Identify, remove, or replace NaNs

Description

Replaces NaNs in the primary data with the chosen value or removes rows containing NaNs

Usage

nan_filter(
  dat,
  project,
  x = NULL,
  replace = FALSE,
  remove = FALSE,
  rep.value = "mean",
  over_write = FALSE
)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

Project name.

x

Character string of variables to remove or replace NaNs.

replace

Logical, If TRUE, NaNs are replaced. Defaults to FALSE.

remove

Logical, if TRUE, removes the entire row of the dataset where NaN is present. Defaults to FALSE.

rep.value

Value to replace all NaNs in a numeric column. Defaults to the mean value of the column. Other options include "median" or a numeric value, e.g. rep.value = 0.

over_write

Logical, If TRUE, saves data over previously saved data table in the FishSET database. Defaults to FALSE.

Details

To check for NaNs across dat run the function specifying only dat (nan_filter(dataset, project)). The function will return a statement of which variables, if any, contain NaNs. To remove NaNs, use remove = TRUE. All rows containing NaNs in x will be removed from dat. To replace NaNs, use replace = TRUE. If both replace and remove are TRUE then replace is used. If replace is FALSE and rep.value is not defined, then NaNs are replaced with mean value. The modified dataset will be returned if replace = TRUE or remove = TRUE. Save the modified data table to the FishSET database by setting over_write = TRUE).

Value

If replace and remove are FALSE then a statement of whether NaNs are found is returned. If either replace or remove is TRUE the modified primary dataset is returned.

Examples

## Not run: 
nan_filter(pcodMainDataTable, 'pcod', 'OFFICIAL_TOTAL_CATCH_MT')

mod.dat <- nan_filter(pcodMainDataTable, 'pcod', 'OFFICIAL_TOTAL_CATCH_MT', 
                      replace = TRUE)
                      
mod.dat <- nan_filter(pcodMainDataTable, 'pcod', 'OFFICIAL_TOTAL_CATCH_MT',
                      replace = TRUE, rep.value = 0)
                      
mod.dat <- nan_filter(pcodMainDataTable, 'pcod', 'OFFICIAL_TOTAL_CATCH_MT', 
                      remove = TRUE)

## End(Not run)

Identify NaNs and NAs

Description

Check whether any columns in the primary dataset contain NAs or NaNs. Returns column names containing NAs or NaNs.

Usage

nan_identify(dat, project)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

Project name.

Details

Check whether any columns in the primary dataset contain NAs or NaNs.

Value

Returns names of columns containing NAs or NaNs, if any.

See Also

na_filter and nan_filter

Examples

## Not run: 
nan_identify(pcodMainDataTable, "pcod")

## End(Not run)

Create one or more binned frequency tables

Description

Create one or more binned frequency, relative frequency, or density table.

Usage

nfreq_table(
  dataset,
  var,
  group = NULL,
  bins = 30,
  type = "dens",
  v_id = NULL,
  format_lab = "decimal",
  format_tab = "wide"
)

Arguments

dataset

Primary data containing information on hauls or trips. Table in FishSET database should contain the string 'MainDataTable'.

var

String, name of numeric variable to bin.

group

String, name of variable(s) to group var by.

bins

Integer, the number of bins to create.

type

String, the type of binned frequency table to create. "freq" creates a frequency table, "perc" creates a relative frequency table, and "dens" creates a density table.

v_id

String, name of vessel ID column (used to detect confidential information).

format_lab

Formatting option for bin labels. Options include "decimal" or "scientific".

format_tab

Format table "wide" or "long"


Shape file for NMFS fishing zones

Description

Simple feature collection with 25 features and 2 fields

Format

shape file


Boxplot to assess outliers

Description

Boxplot to assess outliers

Usage

outlier_boxplot(dat, project, x = NULL)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

Project name.

x

Variables in dat to check for outliers. Leave as x = NULL to plot all numeric variables. To specify multiple variables use c('var1', 'var2')

Details

Creates a visual representation of five summary statistics: median, two hinges (first and third quartiles), two whiskers (extends to 1.5*IQR where IQR is the distance between the first and third quartiles. "Outlying" points, those beyond the two whiskers (1.5*IQR) are shown individually.

Value

Box and whisker plot for all numeric variables. Saved to 'output' folder.


Evaluate outliers in plot format

Description

Visualize spread of data and measures to identify outliers.

Usage

outlier_plot(
  dat,
  project,
  x,
  dat.remove = "none",
  sd_val = NULL,
  x.dist = "normal",
  date = NULL,
  group = NULL,
  pages = "single",
  output.screen = FALSE,
  log_fun = TRUE
)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

String, name of project.

x

Variable in dat to check for outliers.

dat.remove

Outlier measure. Values outside the measure are removed. Users can use the predefined values (see below) or user-defined distance from the mean. For user-defined values, dat.remove should be a numeric value. For example, dat.remove = 6 would would result in value outside 6SD from the mean being class as outliers. User-defined standard deviations from the mean can also be applied using sd_val. Pre-defined choices: "none", "5_95_quant", "25_75_quant", "mean_2SD", "median_2SD", "mean_3SD", "median_3SD". See the Details section for more information.

sd_val

Optional. Number of standard deviations from mean defining outliers. Example, sd_val = 6 would mean values outside +/- 6 SD from the mean would be outliers.

x.dist

Distribution of the data. Choices include: "normal", "lognormal", "exponential", "Weibull", "Poisson", "negative binomial".

date

(Optional) date variable to group the histogram by year.

group

(Optional) additional variable to group the histogram by.

pages

Whether to output plots on a single page ("single", the default) or multiple pages ("multi").

output.screen

Logical, if true, return plots to the screen. If FALSE, returns plot to the 'output' folder as a png file.

log_fun

Logical, whether to log function call (for internal use).

Details

The function returns three plots: the data, a probability plot, and a Q-Q plot. The data plot returns x against row number. Red points are data points that would be removed based on dat.remove. Blue points are data points within the bounds of dat.remove. If dat.remove is "none", then only blue points will be shown. The probability plot is a histogram of the data, after applying dat.remove, with the fitted probability distribution based on x.dist. group groups the histogram by a variable from dat, date groups the histogram by year. The Q-Q plot plots are sampled quantiles against theoretical quantiles, after applying dat.remove.

The dat.remove choices are:

  • numeric value: Remove data points outside +/- 'x'SD of the mean

  • none: No data points are removed

  • 5_95_quant: Removes data points outside the 5th and 95th quantiles

  • 25_75_quant: Removes data points outside the 25th and 75th quantiles

  • mean_2SD: Removes data points outside +/- 2SD of the mean

  • median_2SD: Removes data points outside +/- 2SD of the median

  • mean_3SD: Removes data points outside +/- 3SD of the mean

  • median_3SD: Removes data points outside +/- 3SD of the median

The distribution choices are:

  • normal

  • lognormal

  • exponential

  • Weibull

  • Poisson

  • negative binomial

Value

Plot of the data

Examples

## Not run: 

outlier_plot(pollockMainDataTable, 'pollock', x = 'Haul', dat.remove = 'mean_2SD', 
             x.dist = 'normal', output.screen = TRUE)
# user-defined outlier        
outlier_plot(pollockMainDataTable, 'pollock', x = 'Haul', dat.remove = 6, 
             x.dist = 'lognormal', output.screen = TRUE)

## End(Not run)

Remove outliers from data table

Description

Remove outliers based on outlier measure.

Usage

outlier_remove(
  dat,
  project,
  x,
  dat.remove = "none",
  sd_val = NULL,
  over_write = FALSE
)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

Project name.

x

Variable in dat containing potential outliers.

dat.remove

Defines measure to subset the data. Users can use the predefined values (see below) or user-defined standard deviations from the mean. For user-defined values, dat.remove should be a numeric value. For example, dat.remove=6 would would result in value outside 6SD from the mean being class as outliers. User-defined standard deviations from the mean can also be applied using sd_val. Predefined choices: "none", "5_95_quant", "25_75_quant", "mean_2SD", "median_2SD", "mean_3SD", "median_3SD".

sd_val

Optional. Number of standard deviations from mean defining outliers. For example, sd_val=6 would mean values outside +/- 6 SD from the mean would be outliers.

over_write

Logical, If TRUE, saves data over previously saved data table in the FishSET database.

Details

The dat.remove choices are:

  • numeric value: Remove data points outside +/- 'x'SD of the mean

  • none: No data points are removed

  • 5_95_quant: Removes data points outside the 5th and 95th quantiles

  • 25_75_quant: Removes data points outside the 25th and 75th quantiles

  • mean_2SD: Removes data points outside +/- 2SD of the mean

  • median_2SD: Removes data points outside +/- 2SD of the median

  • mean_3SD: Removes data points outside +/- 3SD of the mean

  • median_3SD: Removes data points outside +/- 3SD of the median

Value

Returns the modified primary dataset. Modified dataset will be saved to the FishSET database.

Examples

## Not run: 
pollockMainDataTable <- outlier_remove(pollockMainDataTable, 'pollock', 'dist', 
   dat.remove = 'mean_2SD', save.output = TRUE)

## End(Not run)

Evaluate outliers in Data

Description

outlier_table() returns a summary table which shows summary statistics of a variable after applying several outlier filters.

Usage

outlier_table(dat, project, x, sd_val = NULL, log_fun = TRUE)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

String, name of project.

x

Variable or column number in dat to check for outliers.

sd_val

Optional. Number of standard deviations from mean defining outliers. For example, sd_val = 4 would mean values outside +/- 4 SD from the mean would be outliers.

log_fun

Logical, whether to log function call (for internal use).

Details

Returns a table of summary statistics (mean, median, standard deviation, minimum, maximum, number of NAs, and skew of the data) for x after values outside the outlier measure have been removed. Outlier measures include 5-95% quantiles, 25-75% quantiles, mean +/-2SD, mean +/-3SD, median +/-2SD, and median +/-3SD. Only one variable can be checked at a time. Table is saved to the Output folder.

Value

Table for evaluating whether outliers may exist in the selected data column.

Examples

## Not run: 
outlier_table(pollockMainDataTable, 'pollock', x = 'HAUL')

## End(Not run)

Selectively display a note section

Description

Selectively display a note section

Usage

parse_notes(project, date = NULL, section, output = "print")

Arguments

project

The project name.

date

Date to pull notes from. If NULL then the most recent version of notes from the project are retrieved.

section

The note section to display. Options include "upload" for Upload data, "quality" for Data quality evaluation, "explore" for Data exploration, "fleet" for Fleet functions, "analysis" for Simple analysis, "new_variable" for Create new variable, "alt_choice" for Alternative choice, "models", and "bookmark".

output

Output type. "print" returns formatted notes. "string" returns a character vector of the notes. "print" is recommended for displaying notes in a report.

Examples

## Not run: 
parse_notes("pollock", type = "explore")

## End(Not run)

Plot spatial dataset

Description

Simple plotting function for viewing spatial data.

Usage

plot_spat(spat)

Arguments

spat

Spatial dataset to view. Must be an object of class sf or sfc.

Examples

## Not run: 
plot_spat(pollockNMFSSpatTable)

## End(Not run)

Plot FishSET Spatial Residuals

Description

Plot FishSET Spatial Residuals

Usage

## S3 method for class 'fishset_spatial_resid'
plot(x, ...)

Arguments

x

A fishset_spatial_resid object.

...

Additional arguments.


Format numbers in table

Description

Format numeric columns.

Usage

pretty_lab(tab, cols = "all", type = "pretty", ignore = NULL)

Arguments

tab

Table to format.

cols

Character string of columns to format. defaults to "all" which will include all numeric variables in tab. If ignore = TRUE then the columns listed in cols will be not be formatted and all other columns in tab will be formatted.

type

The type of formatting to apply. "pretty" uses prettyNum which uses commas (",") to mark big intervals. "scientific" uses scientific notation. "decimal" simply rounds to two decimal places.

ignore

Logical, whether to exclude the columns listed in cols and apply formatting to all other columns in tab.


Format table for R Markdown

Description

Format table for R Markdown

Usage

pretty_tab(tab, full_width = FALSE)

Arguments

tab

Table to format.

full_width

Logical, whether table should fill out entire width of the page.


Scroll box for R Markdown table

Description

Allows tables to become scrollable. Useful for large tables.

Usage

pretty_tab_sb(tab, width = "100%", height = "500px", full_width = FALSE)

Arguments

tab

Table to format.

width

A character string indicating the width of the box. Can be in pixels (e.g. "50px") or as a percentage (e.g. "50%").

height

A character string indicating the height of the box. Can be in pixels (e.g. "50px") or as a percentage (e.g. "50%").

full_width

Logical, whether table should fill out entire width of the page.


Create Previous Location/Area Variable

Description

Creates a variable of the previous port/zone (previous area) or the previous longitude/latitude for a vessel.

Usage

previous_loc(
  dat,
  spat,
  project,
  starting_port,
  v_id,
  tripID,
  haulID,
  zoneID = NULL,
  spatID = NULL,
  date = NULL,
  lon = NULL,
  lat = NULL
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

spat

A spatial data file containing information on fishery management or regulatory zones boundaries. sf objects are recommended, but sp objects can be used as well. See dat_to_sf() to convert a spatial table read from a csv file to an sf object. To upload your spatial data to the FishSETFolder see load_spatial().

project

String, name of project.

starting_port

The name of the starting (or disembarking) port in dat.

v_id

The name of the variable in dat that uniquely identifies vessels.

tripID

Variable name in dat that uniquely identifies trips.

haulID

Variable name in dat that uniquely identifies hauls.

zoneID

Name of zone ID column in dat. Used to identify the previous area. Required for previous area variable.

spatID

Name of zone ID column in spat. spat is used to assign ports to spatial areas. Required for previous area variable.

date

Optional, a date variable to order hauls by.

lon

Longitude variable from dat. Required for previous location variable.

lat

Latitude variable from dat. Required for previous location variable.

Details

previous_loc() can create a previous area or location variable. "Previous area" is defined as the port or zone the vessel last visited. The first area for each trip is the disembarking port (starting_port). If a port is within a zone, the zone is returned. If a port is not within a zone, the name of the port is returned. "Previous location" is defined as the previous longitude and latitude of the vessel. The first set of coordinates is the location of the port. Users must have a port table saved to the FishSET database to use this function (see load_port()). This variable can be used to define the distance matrix (see create_alternative_choice()).


Print FishSET Cross Validation Results

Description

Formats and prints the output of a FishSET cross-validation run. Displays the average out-of-sample performance, a detailed table of metrics for each individual fold, and the estimated coefficients across folds.

Usage

## S3 method for class 'fishset_cv'
print(x, digits = 4, ...)

Arguments

x

A fishset_cv object returned by fishset_cv.

digits

Integer. The number of significant digits to use when printing numeric values. Default is 4.

...

Additional arguments passed to the print method.


Print FishSET Model Fit Results

Description

Formats and prints the output of a FishSET discrete choice model fit. Displays the model formula (if available), coefficients table with significance stars, and key goodness-of-fit statistics (Log-Likelihood, AIC, BIC, Pseudo-R2, Accuracy).

Usage

## S3 method for class 'fishset_fit'
print(x, digits = 4, ...)

Arguments

x

A fishset_fit object returned by fishset_fit.

digits

Integer. The number of significant digits to use when printing numeric values. Default is 4.

...

Additional arguments passed to printCoefmat.


Print Hausman-McFadden IIA Test Results

Description

Formats and prints the output of a Hausman-McFadden test for the Independence of Irrelevant Alternatives (IIA) assumption within the FishSET framework. Displays the omitted alternatives, the Chi-squared test statistic, degrees of freedom, the calculated p-value, and a clear text interpretation of the result (including custom diagnostic interpretations for Expected Profit Models).

Usage

## S3 method for class 'fishset_iia'
print(x, ...)

Arguments

x

A fishset_iia object returned by fishset_iia_test.

...

Additional arguments passed to other methods (currently ignored).


Print FishSET Spatial Residuals

Description

Print FishSET Spatial Residuals

Usage

## S3 method for class 'fishset_spatial_resid'
print(x, ...)

Arguments

x

A fishset_spatial_resid object.

...

Additional arguments.


Check if option file exists for a project

Description

Check if option file exists for a project

Usage

proj_settings_exists(project)

Arguments

project

Project name.

Value

TRUE if project options file exists, FALSE if not.


List output files by project name

Description

List output files by project name

Usage

project_files(project)

Arguments

project

Project name

Examples

## Not run: 
project_files("pollock")

## End(Not run)

List logs by project

Description

List logs by project

Usage

project_logs(project, modified = FALSE)

Arguments

project

Name of project.

modified

Logical, whether to show modification date. Returns a data frame.


Display database table names by project

Description

Display database table names by project

Usage

project_tables(project, ...)

Arguments

project

Name of project.

...

String, additional characters to match by.

See Also

list_tables, fishset_tables

Examples

## Not run: 
project_tables("pollock")
project_tables("pollock", "main")

## End(Not run)

Display projects names

Description

Display projects names

Usage

projects()

Details

Lists the unique project names currently in the FishSET Database.

Examples

## Not run: 
projects()

## End(Not run)

Retrieve/display meta data by project

Description

Retrieve/display meta data by project

Usage

pull_meta(project, tab.name = NULL, tab.type = NULL, format = FALSE)

Arguments

project

Project name.

tab.name

String, table name. Optional, used to filter output to a specific table.

tab.type

String, table type. Optional, used to filter output. Options include "main", "spat" (spatial), "port", "grid" (gridded), and "aux" (auxiliary).

format

Logical, whether to format output using pander. Useful for displaying in reports.


Pull notes from output folder

Description

Pull notes from output folder

Usage

pull_notes(project, date = NULL, output = "print")

Arguments

project

String, the project name.

date

String, date to pull notes from. If NULL, most recent note file is retrieved.

output

Output type. "print" returns formatted notes. "string" returns a character vector of the notes. "print" is recommended for displaying notes in a report.

Details

Notes are saved to the output folder by project name and date. If date is not specified then the most recent notes file with the project name is pulled. Notes are are also saved by FishSET app session; if more than one session occurred in the same day, each session's notes are pulled and listed in chronological order.


Retrieve output file name by project, function, and type

Description

Retrieve output file name by project, function, and type

Usage

pull_output(project, fun = NULL, date = NULL, type = "plot", conf = TRUE)

Arguments

project

Name of project

fun

Name of function.

date

Output file date in " the most recent output file is pulled.

type

Whether to return the "plot" (.png), "table" (.csv), "notes" (.txt) or "all" files matching the project name, function, and date.

conf

Logical, whether to return suppressed confidential data. Unsuppressed output will be pulled if suppressed output is not available.

Examples

## Not run: 
pull_output("pollock", "species_catch", type = "plot")

## End(Not run)

Import and format plots to notebook file

Description

Import and format plots to notebook file

Usage

pull_plot(project, fun, date = NULL, conf = TRUE)

Arguments

project

Project name.

fun

String, the name of the function that created the plot.

date

the date the plot was created. If NULL, then the most recent version is retrieved.

conf

Logical, whether to return suppressed confidential data. Unsuppressed output will be pulled if suppressed output is not available.

Examples

## Not run: 
pull_plot("pollock", "density_plot")

## End(Not run)

Import and format table to notebook file

Description

Import and format table to notebook file

Usage

pull_table(project, fun, date = NULL, conf = TRUE)

Arguments

project

Project name.

fun

String, the name of the function that created the table.

date

the date the table was created. If NULL, then the most recent version is retrieved.

conf

Logical, whether to return suppressed confidential data. Unsuppressed output will be pulled if suppressed output is not available.

Examples

## Not run: 
pull_table("pollock", "vessel_count")

## End(Not run)

Randomize latitude and longitude points by zone

Description

Randomize latitude and longitude points by zone

Usage

randomize_lonlat_zone(dat, project, spat, lon, lat, zone)

Arguments

dat

Primary data frame over which to apply function. Table in FishSET database should contain the string 'MainDataTable'.

project

Project name.

spat

Spatial data table containing regulatory zones. This can be a "spatial feature" or sf object.

lon

String, variable name containing longitude.

lat

String, variable name containing latitude.

zone

String, column name contain the assigned zone. Must be the same for both the spatial data table and MainDataTable.

Details

This is one of the FishSET confidentiality functions. It replaces longitude and latitude values with randomly sampled coordinates from the regulatory zone the observation occurred in.

Examples

## Not run: 
randomize_lonlat_zone(pollockMainDataTable, "pollock", spatdat, 
                   lon = "LonLat_START_LON", lat = "LonLat_START_LAT",
                   zone = "NMFS_AREA")

## End(Not run)

Randomize variable value by percentage range

Description

Randomize variable value by percentage range

Usage

randomize_value_range(dat, project, value, perc = NULL)

Arguments

dat

Primary data frame over which to apply function. Table in FishSET database should contain the string 'MainDataTable'.

project

Project name.

value

String, name of variable to jitter.

perc

Numeric, a vector of percentages to randomly adjust a column of values by. Defaults to a range of 0.05 - 0.15 (i.e. 5-15 percent of original value).

Details

This is one of the FishSET confidentiality functions. It adjusts a value by adding or substracting (chosen at random for each value) a percentage of the value. The percentage is randomly sampled from a range of percentages provided in the "perc" argument.

Examples

## Not run: 
randomize_value_range(pollockMainDataTable, "pollock", "LBS_270_POLLOCK_LBS")

## End(Not run)

Randomize value between rows

Description

Randomize value between rows

Usage

randomize_value_row(dat, project, value)

Arguments

dat

Primary data frame over which to apply function. Table in FishSET database should contain the string 'MainDataTable'.

project

Project name.

value

String, variable name to be randomly distributed between rows.

Details

This is one of the FishSET confidentiality functions. It is useful for randomly assigning ID values between observations.

Examples

## Not run: 
randomize_value_row(pollockMainDataTable, "pollock", "PERMIT")

## End(Not run)

Import data from local file directory or webpage into the R environment

Description

Import data from local file directory or webpage into the R environment

Usage

read_dat(
  x,
  data.type = NULL,
  is.map = FALSE,
  drv = NULL,
  dbname = NULL,
  user = NULL,
  password = NULL,
  ...
)

Arguments

x

Name and path of dataset to be read in. To load data directly from a webpage, x should be the web address.

data.type

Optional. Data type can be defined by user or based on the file extension. If undefined, data.type is the string after the last period or equal sign. data.type must be defined if x is the path to a shape folder, if the file is a Google spreadsheet use data.type = 'google', or if the correct extension cannot be derived from x. R, comma-delimited, tab-delimited, excel, Matlab, json, geojson, sas, spss, stata, and html, and XML data extensions do not have to be specified.

is.map

logical, for .json file extension, set is.map = TRUE if data is a spatial file. Spatial files ending in .json will not be read in properly unless is.map = TRUE.

drv

Use with sql files. Database driver.

dbname

Use with sql files. If required, database name.

user

Use with sql files. If required, user name for SQL database.

password

Use with sql files. If required, SQL database password.

...

Optional arguments

Details

Uses the appropriate function to read in data based on data type. Use write_dat to save data to the data folder in the project directory. Supported data types include shape, csv, json, matlab, R, spss, and stata files. Use data.type = 'shape' if x is the path to a shape folder. Use data.type = 'google' if the file is a Google spreadsheet.

For sql files, use data.type = 'sql'. The function will connect to the specified DBI and pull the table. Users must specify the DBI driver (drv), for example: RSQLite::SQLite(), RPostgreSQL::PostgreSQL(), odbc::odbc(). Further arguments may be required, including database name (dbname), user id (user), and password (password).

Additional arguments can be added, such as skip lines skip = 2 and header header = FALSE. To specify the separator argument for a delimited file, include tab-delimited, specify data.type = 'delim'.

For more details, see load for loading R objects, read_csv for reading in comma separated value files, read_tsv for reading in tab separated value files, read_delim for reading in delimited files, read_excel for reading in excel files (xls, xlsx), st_read for reading in geojson , GeoPackage files, and shape files, readMat for reading in matlab data files, read_dta for reading in stata data files, read_spss for reading in spss data files, read_sas for reading in sas data files, and fromJSON for reading in json files. read_xml for reading in XML files. Further processing may be required. read_html for reading in html tables. See read_sheet in range_read for reading in google spreadsheets. Google spreadsheets require data.type be specified. Use data.type = 'google'. read_ods for reading in open document spreadsheets.

Examples

## Not run: 
# Read in shape file
dat <- read_dat('C:/data/nmfs_manage_simple', data.type = 'shape')

# Read in spatial data file in json format
dat <- read_dat('C:/data/nmfs_manage_simple.json', is.map = TRUE)

# read in data directly from web page
dat <- read_dat("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/test.txt", 
                data.type = 'delim', sep = '', header = FALSE)

## End(Not run)

Remove a model design from list in ModelInputData table

Description

Remove a model design from list in ModelInputData table

Usage

remove_model_design(project, names)

Arguments

project

Name of project.

names

Names of model designs to be deleted from the table


Replace suppression code

Description

This function replaces the default suppression code in a table.

Usage

replace_sup_code(output, code = NA)

Arguments

output

Table containing suppressed values.

code

The replacement suppression code. code = NA by default; this is ideal for plotting as ggplot automatically removes NAs.

Details

Suppressed values are represented as ‘-999' by default. This isn’t ideal for plotting. NAs – the default in 'replace_sup_code()'– are a better alternative for plots as they can easily be removed.

Examples

## Not run: 
summary_tab <- replace_sup_code(summary_tab, code = NA)

## End(Not run)

Reset confidentiality cache tables

Description

This function deletes all confidentiality check tables stored in the "confid_cache.json" file located in the project output folder. Resetting this cache is recommended after a long period of use as check tables can accumulate over time.

Usage

reset_confid_cache(project)

Arguments

project

Project name

See Also

get_confid_cache


Apply moving average function to catch

Description

Apply moving average function to catch

Usage

roll_catch(
  dat,
  project,
  catch,
  date,
  group = NULL,
  combine = FALSE,
  k = 10,
  fun = "mean",
  filter_date = NULL,
  date_value = NULL,
  filter_by = NULL,
  filter_value = NULL,
  filter_expr = NULL,
  facet_by = NULL,
  scale = "fixed",
  align = "center",
  conv = "none",
  tran = "identity",
  format_lab = "decimal",
  output = "tab_plot",
  ...
)

Arguments

dat

Primary data frame over which to apply function. Table in FishSET database should contain the string 'MainDataTable'.

project

Name of project.

catch

Variable name or names containing catch data. Multiple variables can entered as a vector.

date

Date variable to aggregate by.

group

Variable name or names to group by. Plot will display up to two grouping variables.

combine

Whether to combine variables listed in group. This is passed to the "fill" or "color" aesthetic for plots.

k

The width of the window.

fun

The function to be applied to window. Defaults to mean.

filter_date

The type of filter to apply to table. The "date_range" option will subset the data by two date values entered in filter_val. Other options include "year-day", "year-week", "year-month", "year", "month", "week", or "day". The argument filter_value must be provided.

date_value

String containing a start and end date if using filter_date = "date_range", e.g. c("2011-01-01", "2011-03-15"). If filter_date = "period" or "year-period", use integers (4 digits if year, 1-2 if day, month, or week). Use a list if using a two-part filter, e.g. "year-week", with the format list(year, period) or a vector if using a single period, c(period). For example, list(2011:2013, 5:7) will filter the data table from weeks 5 through 7 for years 2011-2013 if filter_date = "year-week".c(2:5) will filter the data February through May when filter_date = "month".

filter_by

String, variable name to filter by.

filter_value

A vector of values to filter 'MainDataTable' by using the variable in filter_by.

filter_expr

String, a valid R expression to filter 'MainDataTable' by using the variable in filter_by.

facet_by

Variable name to facet by. This can be a variable that exists in the dataset, or a variable created by roll_catch() such as "year", "month", or "species" if more than one variable is entered in catch.

scale

Scale argument passed to facet_grid. Options include "free", "free_x", "free_y". Defaults to "fixed".

align

Indicates whether results of window should be left-aligned ("left"), right-aligned ("right"), or centered ("center"). Defaults to "center".

conv

Convert catch variable to "tons", "metric_tons", or by using a function. Defaults to FALSE.

tran

A function to transform the y-axis. Options include log, log2, log10, sqrt.

format_lab

Formatting option for y-axis labels. Options include "decimal" or "scientific".

output

Whether to display "plot", "table", or both. Defaults to both ("tab_plot").

...

Additional arguments passed to rollapply

Examples

## Not run: 
roll_catch(pollockMainDataTable, project = "pollock", catch = "LBS_270_POLLOCK_LBS",
  date = "FISHING_START_DATE", group = "GEAR_TYPE", k = 15
)

roll_catch(pollockMainDataTable, project = "pollock", catch = c("LBS_270_POLLOCK_LBS", 
 "LBS_110_PACIFIC_COD_LBS"), date = "FISHING_START_DATE", group = "GEAR_TYPE", k = 5, 
 filter_date = "month", date_value = 4:6, facet_by = "month", conv = "tons"
)

## End(Not run)

Guided user interface for FishSET functions

Description

Runs functions associated with loading data, exploring data, checking for data quality issues, generating new variables, and basic data analysis function.

Usage

run_fishset_gui()

Details

Opens an interactive page that allows users to select which functions to run by clicking check boxes. Data can be modified and saved. Plot and table output are saved to the output folder. Functions calls are logged in the log file.

Examples

## Not run: 
run_fishset_gui()

## End(Not run)

Run Policy and Data Simulations

Description

Simulates the impact of policy changes (e.g., area closures) or changes in exogenous variables (e.g., fuel prices, expected catch) on redistributed fishing effort and economic welfare.

Usage

run_simulation(
  project,
  mod_name,
  closures = NULL,
  data_modifiers = NULL,
  betadraws = 500,
  marg_util_income = NULL,
  income_cost = FALSE
)

Arguments

project

Character. Name of the project.

mod_name

Character. Name of the fitted model to use for the simulation.

closures

List (Optional). A list of closure scenarios generated by zone_closure().

data_modifiers

List (Optional). A named list of new data vectors to inject into the model. Names must match variables in the model's design matrix or price vector.

betadraws

Integer. Number of multivariate normal draws for the simulation. Default is 1000.

marg_util_income

Character. For standard/zonal logit models: Name of the coefficient to use as the marginal utility of income to calculate welfare in dollars. Note this input will be ignored for EPMs.

income_cost

Logical. Default FALSE. Set to TRUE if the 'marg_util_income' parameter represents a cost (flips the sign of the coefficient). Note this input will be ignored for EPMs.

Details

How to use data_modifiers: To create counterfactual data vectors, be sure to include your unique observation ID (e.g., trip or haul ID), date, and zone ID variables when formatting your data with format_model_data(). To guarantee the math aligns perfectly with the simulation matrices:

  1. Load your saved long-format dataset.

  2. Apply your mathematical transformation or merge in your external counterfactual data.

  3. Pass the raw, modified vector into the data_modifiers argument as a named list where the name exactly matches the column in the model (e.g., list(distance = new_dist)). For EPM price modifications, strictly use the name "price".

Analyzing results: Once your simulations have successfully run, use the companion summary functions to extract cleaned data frames and ggplot2 visualizations:

  • summarize_policy_welfare: Extracts compensating variation (economic impact) metrics and generates bar and density plots.

  • summarize_policy_effort: Extracts spatial effort redistribution and generates absolute, percentage, and spillover scatter plots.

Value

An object of class 'fishset_policy' containing simulation results saved in the project database with the name [project]PolicySimulations.

See Also

summarize_policy_welfare, summarize_policy_effort

Examples

## Not run: 
# -----------------------------------------------------------------------
# Example 1: Standard Logit - Baseline & Closures
# -----------------------------------------------------------------------
# Run a baseline simulation (no closures)
run_simulation(
  project = "MyProject",
  mod_name = "clogit_model1",
  marg_util_income = "expected_catch"
)

# Run simulations for two separate spatial closures defined in your YAML
run_simulation(
  project = "MyProject",
  mod_name = "clogit_model1",
  closures = c("closure_1", "closure_2"),
  marg_util_income = "expected_catch"
)

# -----------------------------------------------------------------------
# Example 2: Standard Logit - Data Modifiers (e.g., Fuel Spike)
# -----------------------------------------------------------------------
library(data.table)

# 1. Load the formatted data to ensure perfect matrix alignment
# Note that this saves as a qs2 file if the package is available, and as a .rds file if not.
lf_data <- 
  qs2::qs_read("MyProject/Models/FormattedData/MyProjectLongFormatData.rds")[["format1"]]
# OR #
lf_data <- 
  readRDS("MyProject/Models/FormattedData/MyProjectLongFormatData.rds")[["format1"]]
new_distance <- as.numeric(lf_data$distance) * 1.5

# 2. Run the simulation
run_simulation(
  project = "MyProject",
  mod_name = "clogit_model1",
  data_modifiers = list(distance = new_distance), 
  marg_util_income = "expected_catch"
)

# -----------------------------------------------------------------------
# Example 3: Expected Profit Model - Closures AND Price Drop Combined
# -----------------------------------------------------------------------
# EPMs automatically calculate marginal utility of income, so it is omitted.
# The keyword "price" must be used for EPM revenue modifications.

new_price <- as.numeric(lf_data$price) * 0.80 # 20% price drop

run_simulation(
  project = "MyProject",
  mod_name = "epm_model1",
  closures = c("closure_1"),
  data_modifiers = list(price = new_price),
  betadraws = 1000
)

## End(Not run)

Save modified primary data table to FishSET database

Description

Save modified primary data table to FishSET database

Usage

save_dat(dat, project)

Arguments

dat

Name of data frame in working environment to save to FishSET database.

project

String, name of project.

Details

Use function to save modified data to the FishSET database. The primary data is only saved automatically in data upload and data check functions. It is therefore advisable to save the modified data to the database before moving on to modeling functions. Users should use primary data in the working environment for assessing data quality issues, modifying the data, and generating new variables. Pulling the primary data from the FishSET database on each function without manually saving will result in a loss of changes.

Examples

## Not run: 
save_dat(pollockMainDataTable, 'pollock')

## End(Not run)

Northeast Scallop Data

Description

A subset of anonymized scallop data

Usage

scallop

Format

'scallop' A data.frame with 10,000 rows and 19 columns:

TRIPID

Randomly assigned trip ID number.

DATE_TRIP

Date of landing.

PERMIT.y

Randomly assigned six-digit vessel fishing permit number.

TRIP_LENGTH

Days calculated from the elapsed time between the date-time sailed and date-time landed; this is a measure of days absent.

GEARCODE

Fishing gear used on the trip.

port_lat

Latitude of the geoid.

port_lon

longitude of the geoid.

previous_port_lat

Previous latitude of geoid.

previous_port_lon

Previous longitude of geoid.

Plan Code

Portion of the VMS declaration code that identifies the fishery being declared into for the trip.

Program Code

Portion of the VMS declaration code that identifies the program within the declared fishery. For scallops, the program code delineates LA and LAGC trips, as well as access area trips from other trips.

TRIP_COST_WINSOR_2020_DOL

The estimated or real composite trip cost for the VTR trip record generated using the methods described in the Commercial Trip Cost Estimation 2007-2019 PDF file. However, these values have been Winsorized by gear type as a method of avoiding unreasonably high or low trip costs, replacing any value within each gear-group that is less than the 1st percentile or greater than the 99th percentile with the 1st and 99th percentile value, respectively.

DDLAT

The latitude reported on a VTR (Vessel Trip Reports).

DDLON

The longitude reported on a VTR (Vessel Trip Reports).

ZoneID

FishSET's version of a ten minute square.

LANDED_OBSCURED

Landed pounds from the dealer report (jittered/obscured).

DOLLAR_OBSCURED

The value of catch paid by the dealer, from the dealer report (jittered/obscured).

DOLLAR_2020_OBSCURED

The value of catch paid by the dealer, from the dealer report (in 2020 dollars, jittered/obscured).

DOLLAR_ALL_SP_2020_OBSCURED

The value of catch for all species caught (in 2020 dollars, jittered/obscured).

Source

Add source here


Ports from the NE scallop fishery

Description

A dataset containing the names and lat/lon coordinates of ports used in the US northeast scallop fishery.

Usage

scallop_ports

Format

A data frame (tibble) with 40 observations and 3 variables.

[,1] Port names
[,2] Longitude
[,3] Latitude

Source

NEED TO ADD SOURCE DESCRIPTION


Create single binary fishery season identifier variable

Description

Create single binary fishery season identifier variable

Usage

seasonalID(
  dat,
  project,
  seasonal.dat = NULL,
  start,
  end,
  overlap = FALSE,
  name = NULL
)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

Project name.

seasonal.dat

Data table containing date of fishery season(s). Data table can be pulled from the FishSET database. Leave seasonal.dat as NULL if supplying start and end dates with start and end arguments.

start

Date, supplied as a string (example: start='2011/04/22', start='04222011'), or variable in seasonal.dat which identifies start date of fishery season

end

DDate, supplied as a string (example: start='2011/04/22', start='04222011'), or variable in seasonal.dat which identifies end date of fishery season

overlap

Logical. Should trip or haul dates that start before or end after the fishery season date but starts or ends within the fishery season dates be included? FALSE indicates to inlude only hauls/trips that fall completely within the bounds of a fishery season date. Defaults to FALSE.

name

String Seasonal identifier name

Details

Uses a supplied dates or a table of fishery season dates to create fishery season identifier variables. Output is a binary variable called name or 'SeasonID' if name is not supplied.

For each row dat, the function matches fishery season dates provided in seasonal.dat to the earliest date variable in dat.

Value

Returns a binary variable of within (1) or outside (0) the fishery season.

Examples

## Not run: 
#Example using a table stored in the FishSET database
pcodMainDataTable <- season_ID("pcodMainDataTable", 'pcod', seasonal_dat='seasonTable', 
     start='SeasonStart', end='SeasonEnd', name='2001A')
#Example using manually entered dates
pcodMainDataTable <- season_ID("pcodMainDataTable", 'pcod', seasonal.dat=NULL, 
    start='04152011', end='06302011', name='2001A')

## End(Not run)

Set confidentiality parameters

Description

This function specifics whether to check for confidentiality and which rule should be applied.

Usage

set_confid_check(project, check = TRUE, v_id = NULL, rule = "n", value = NULL)

Arguments

project

Name of project.

check

Logical, whether to check for confidentiality.

v_id

String, the column name containing the vessel identifier.

rule

String, the confidentiality rule to apply. See "Details" below. rule = "n" suppresses values containing fewer than n vessels. rule = "k" suppresses values where a single vessel contains k percent or more of the total catch.

value

The threshold for confidentiality. for rule = "n" must be an integer of at least 2. For rule = "k" any numeric value from 0 to 100.

Details

rule = "n" counts the number of vessel in each strata and suppresses values where fewer than n vessels are present. For rule = "k", or the "Majority allocation rule", each vessel's share of catch is calculated by strata. If any vessel's total catch share is greater than or equal to k percent the value is suppressed.

Examples

## Not run: 
set_confid_check("pollock", check = TRUE, v_id = "PERMIT", rule = "n", value = 3L)

## End(Not run)

Create factor variable from quantiles

Description

Create a factor variable from numeric data. Numeric variable is split into categories based on quantile categories.

Usage

set_quants(
  dat,
  project,
  x,
  quant_cat = 0.25,
  custom_quant = NULL,
  name = "set_quants"
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Project name.

x

Variable to transform into quantiles.

quant_cat

Quantile options: 0.1 0.2, 0.25, 0.33, and 0.4

  • 0.1: (0%, 10%, 20%, 30%, 40%, 50%, 60%, 70%, 80%, 90%, 100%)

  • 0.2: (0%, 20%, 40%, 60%, 80%, 100%)

  • 0.25: (0%, 25%, 50%, 75%, 100%)

  • 0.33: (0%, 33%, 66%, 100%)

  • 0.4: (0%, 10%, 50%, 90%, 100%)

custom_quant

Vector, user defined quantiles (between 0-1)

name

String, name of created vector. Defaults to name of the function if not defined.

Value

Primary dataset with quantile variable added.

Examples

## Not run: 
pollockMainDataTable <- set_quants(pollockMainDataTable, 'pollock', 'HAUL', 
   quant_cat=.2, 'haul.quant')

## End(Not run)

Set user folder directory

Description

Set user folder directory

Usage

set_user_locoutput(loc_dir, project)

Arguments

loc_dir

Local user directory

project

Name of project.

Details

This function saves the local user directory to the project settings file with a valid folder directory. This directory path is used for inserting plots and tables from a folder outside the FishSET package into the FishSET RMarkdown Template.

See Also

insert_plot insert_table get_proj_settings


Evaluate sparsity in data over time in table format

Description

Create table of data sparsity by predefined time periods.

Usage

sparsetable(dat, project, timevar, zonevar, var)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

String, name of project.

timevar

Variable in dat containing temporal data

zonevar

Variable in dat containing zone observation assigned to

var

Variable in dat containing catch data


Evaluate sparsity in data over time in plot format

Description

Evaluate sparsity in data over time in plot format

Usage

sparsplot(project, x = NULL)

Arguments

project

String, name of project.

x

Output from sparsetable. If x is null, the sparsity table will be pulled from the output folder if it exists.

Details

Returns a plot of sparsity values over time. Requires sparsity table generated by sparsetable.


GUI for spatial data checks

Description

Runs the spatial checks performed by spatial_qaqc in a shiny application.

Usage

spat_qaqc_gui(dataset, project, spatdat, checks = NULL)

Arguments

dataset

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Name of project.

spatdat

Spatial data containing information on fishery management or regulatory zones. See read_dat for details on importing spatial data.

checks

(Optional) A list of spatial data quality checks outputted by spatial_qaqc.

See Also

spatial_qaqc


Histogram of latitude and longitude by grouping variable

Description

Histogram of latitude and longitude by grouping variable

Usage

spatial_hist(dat, project, group = NULL)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

group

Column in dat containing grouping categories.

Details

Returns a histogram of observed lat/lon split by grouping variable. Output printed to console and saved to Output folder. Function is used to assess spatial variance/clumping of selected grouping variable.

Value

Returns histogram of latitude and longitude by grouping variable. Output returned to console and saved to Output folder.

Examples

## Not run: 
spatial_hist(pollockMainDataTable, 'pollock', 'GEAR_TYPE')

## End(Not run)

Spatial data quality checks

Description

This function performs spatial quality checks and outputs summary tables and plots. Checks include percent of observations on land, outside regulatory zone (spat), and on a zone boundary. If any observation occurs outside the regulatory zones then summary information on distance from nearest zone is provided. spatial_qaqc can filter out observations that are not within the distance specified in filter_dist.

Usage

spatial_qaqc(
  dat,
  project,
  spat,
  lon.dat,
  lat.dat,
  lon.spat = NULL,
  lat.spat = NULL,
  id.spat = NULL,
  epsg = NULL,
  date = NULL,
  group = NULL,
  filter_dist = NULL
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Name of project.

spat

Spatial data containing information on fishery management or regulatory zones. sf objects are recommended, but sp objects can be used as well. If using a spatial table read from a csv file, then arguments lon.spat and lat.spat are required. To upload your spatial data to the FishSETFolder see load_spatial.

lon.dat

Longitude variable in dat.

lat.dat

Latitude variable in dat.

lon.spat

Variable or list from spat containing longitude data. Required for spatial tables read from csv files. Leave as NULL if spat is an sf or sp object.

lat.spat

Variable or list from spat containing latitude data. Required for spatial tables read from csv files. Leave as NULL if spat is an sf or sp object.

id.spat

Polygon ID column. Required for spatial tables read from csv files. Leave as NULL if spat is an sf or sp object.

epsg

EPSG number. Manually set the epsg code, which will be applied to spat and dat. If epsg is not specified but is defined for spat, then the spat epsg will be applied to dat. In addition, if epsg is not specified and epsg is not defined for spat, then a default epsg value will be applied to spat and dat (epsg = 4326). See http://spatialreference.org/ to help identify optimal epsg number.

date

String, name of date variable. Used to summarize over year. If NULL the first date column will be used. Returns an error if no date columns can be found.

group

String, optional. Name of variable to group spatial summary by.

filter_dist

(Optional) Numeric, distance value to filter primary data by (in meters). Rows containing distance values greater than or equal to filter_dist will be removed from the data. This action will be saved to the filter table.

Value

A list of plots and/or dataframes depending on whether spatial data quality issues are detected. The list includes:

dataset

Primary data. Up to five logical columns will be added if spatial issues are found: "ON_LAND" (if obs fall on land), "OUTSIDE_ZONE" (if obs occur at sea but outside zone), "ON_ZONE_BOUNDARY" (if obs occurs on zone boundary), "EXPECTED_LOC" (whether obs occurs at sea, within a zone, and not on zone boundary), and "NEAREST_ZONE_DIST_M" (distance in meters from nearest zone. Applies only to obs outside zone or on land).

spatial_summary

Dataframe containing the percentage of observations that occur at sea and within zones, on land, outside zones but at sea, or on zone boundary by year and/or group. The total number of observations by year/group are in the "N" column.

outside_plot

Plot of observations outside regulatory zones.

land_plot

Plot of observations that fall on land.

land_out_plot

Plot of observations that occur on land and are outside the regulatory zones (combines outside_plot and land_plot if both occur).

boundary_plot

Plot of observations that fall on zone boundary.

expected_plot

Plot of observations that occur at sea and within zones.

distance_plot

Histogram of distance form nearest zone (meters) by year for observations that are outside regulatory grid.

distance_freq

Binned frequency table of distance values.

distance_summary

Dataframe containing the minimum, 1st quartile, median, mean, 3rd quartile, and maximum distance values by year and/or group.

Examples

## Not run: 
# run spatial checks
spatial_qaqc("pollockMainDataTable", "pollock", spat = NMFS_AREAS, 
             lon.dat = "LonLat_START_LON", lat.dat = "LonLat_START_LAT")
             
# filter obs by distance
spat_out <- 
     spatial_qaqc(pollockMainDataTable, "pollock", spat = NMFS_AREAS,
                  lon.dat = "LonLat_START_LON", lat.dat = "LonLat_START_LAT",
                  filter_dist = 100)
mod.dat <- spat_out$dataset

## End(Not run)

Summarize variable over data and time

Description

View summary and exploratory statistics of selected variable by date and zone.

Usage

spatial_summary(
  dat,
  project,
  stat.var = c("length", "no_unique_obs", "perc_total", "mean", "median", "min", "max",
    "sum"),
  variable,
  spat,
  lon.spat = NULL,
  lat.spat = NULL,
  lon.dat = NULL,
  lat.dat = NULL,
  cat
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

stat.var

Options are "length", "no_unique_obs", "perc_total", "mean", "median", "min", "max", and "sum".

variable

Variable in dat to summarize over date and zone.

spat

Spatial data containing information on fishery management or regulatory zones. Shape, json, geojson, and csv formats are supported. Leave as NULL if the variable ‘ZoneID’ assigning observations to zones exists in dat.

lon.spat

Variable or list from spat containing longitude data. Required for csv files. Leave as NULL if spat is a shape or json file or if the variable ‘ZoneID’ exists in dat.

lat.spat

Variable or list from spat containing latitude data. Required for csv files. Leave as NULL if spat is a shape or json file, or if the variable ‘ZoneID’ exists in dat.

lon.dat

Longitude variable in dat. Leave as NULL if the variable ‘ZoneID’ (zonal assignment) exists in dat.

lat.dat

Latitude variable in dat. Leave as NULL if the variable ‘ZoneID’ (zonal assignments) exists in dat.

cat

Variable or list in spat that identifies the individual areas or zones. If spat is class sf, cat should be name of list containing information on zones. Leave as NULL if the variable ‘ZoneID’ exists in dat.

Details

stat.var details:

length: Number of observations
no_unique_obs: Number of unique observations
perc_total: Percent of total observations
mean: Mean
median: Median
min: Minimum
max: Maximum
sum: Sum

Value

Returns two plots, the variable aggregated by stat.var plotted against date and against zone.

Examples

## Not run: 
Example where ZoneID exists in dataset
   spatial_summary(pcodMainDataTable, project = 'pcod', 
      stat.var = "no_unique_obs", variable = 'HAUL')

Example where obs. have not been assigned to zones
    spatial_summary(pcodMainDataTable, project = 'pcod', stat.var = "no_unique_obs",
       variable = 'HAUL', spat = spatdat, lon.dat = 'MidLat', lat.dat = 'MidLat',
       cat = 'NMFS_AREA')

## End(Not run)

Summarize species catch

Description

species_catch summarizes catch (or other numeric variables) in the main table. It can summarize by period if date is provided, grouping variables, and filter by period or value. There are several options for customizing the table and plot output.

Usage

species_catch(
  dat,
  project,
  species,
  date = NULL,
  period = NULL,
  fun = "sum",
  group = NULL,
  sub_date = NULL,
  filter_date = NULL,
  date_value = NULL,
  filter_by = NULL,
  filter_value = NULL,
  filter_expr = NULL,
  facet_by = NULL,
  type = "bar",
  conv = "none",
  tran = "identity",
  format_lab = "decimal",
  value = "count",
  position = "stack",
  combine = FALSE,
  scale = "fixed",
  output = "tab_plot",
  format_tab = "wide"
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

species

Variable in dat containing the species catch or a vector of species variables (in pounds).

date

Variable in dat containing dates to aggregate by.

period

Time period to count by. Options include 'year', 'month', 'week' (week of the year), 'weekday', 'day' (day of the month), and 'day_of_year'. date is required.

fun

String, name of function to aggregate by. Defaults to sum.

group

Grouping variable name(s). Up to two grouping variables are available for line plots and one for bar plots. For bar plots, if only one species is entered the first group variable is passed to 'fill'. If multiple species are entered, species is passed to "fill" and the grouping variable is dropped. An exception occurs when facetting by species, then the grouping variable is passed to "fill". For line plots, the first grouping variable is passed to "fill" and the second to "linetype" if a single species column is entered or if facetting by species. Otherwise species is passed to "fill", the first group variable to "linetype", and second is dropped.

sub_date

Date variable used for subsetting, grouping, or splitting by date.

filter_date

The type of filter to apply to 'MainDataTable'. To filter by a range of dates, use filter_date = "date_range". To filter by a given period, use "year-day", "year-week", "year-month", "year", "month", "week", or "day". The argument date_value must be provided.

date_value

This argument is paired with filter_date. To filter by date range, set filter_date = "date_range" and enter a start- and end-date into date_value as a string: date_value = c("2011-01-01", "2011-03-15").

To filter by period (e.g. "year", "year-month"), use integers (4 digits if year, 1-2 digits if referencing a day, month, or week). Use a vector if filtering by a single period: date_filter = "month" and date_value = c(1, 3, 5). This would filter the data to January, March, and May.

Use a list if using a year-period type filter, e.g. "year-week", with the format: list(year, period). For example, filter_date = "year-month" and date_value = list(2011:2013, 5:7) will filter the data table from May through July for years 2011-2013.

filter_by

String, variable name to filter 'MainDataTable' by. the argument filter_value must be provided.

filter_value

A vector of values to filter 'MainDataTable' by using the variable in filter_by. For example, if filter_by = "GEAR_TYPE", filter_value = 1 will include only observations with a gear type of 1.

filter_expr

String, a valid R expression to filter 'MainDataTable' by using the variable in filter_by.

facet_by

Variable name to facet by. Accepts up to two variables. These can be variables that exist in the dat, or a variable created by species_catch() such as "year", "month", or "week" if a date variable is added to sub_date. Facetting by "species" is available if multiple catch columns are included in "species". The first variable is facetted by row and the second by column.

type

Plot type, options include "bar" (the default) and "line".

conv

Convert catch variable to "tons", "metric_tons", or by using a function entered as a string. Defaults to "none" for no conversion.

tran

A function to transform the y-axis. Options include log, log2, log10, sqrt.

format_lab

Formatting option for y-axis labels. Options include "decimal" or "scientific".

value

Whether to calculate raw "count" or "percent" of total catch.

position

Positioning of bar plot. Options include 'stack', 'dodge', and 'fill'.

combine

Whether to combine variables listed in group. This is passed to the "fill" or "color" aesthetic for plots.

scale

Scale argument passed to facet_grid. Defaults to "fixed".

output

Output a "plot" or "table". Defaults to both ("tab_plot").

format_tab

How table output should be formatted. Options include 'wide' (the default) and 'long'.

Value

species_catch() aggregates catch using one or more columns of catch data. Users can aggregate by time period, by group, or by both. When multiple catch variables are entered, a new column "species" is created and used to group values in plots. The "species" column can also be used to split (or facet) the plot. For table output, the "species" column will be kept if format_tab = "long", i.e. a column of species names ("species") and a column containing catch ("catch"). When format_tab = "wide", each species is given its own column of catch.

The data can be filtered by date and/or by a variable. filter_date specifies the type of date filter to apply–by date-range or by period. date_value should contain the values to filter the data by. To filter by a variable, enter its name as a string in filter_by and include the values to filter by in filter_value.

Plots can handle Up to two grouping variables, there is no limit for tables. Grouping variables can be merged into one variable using combine; in this case any number of variables can be joined, but no more than three is recommended.

For faceting, any variable (including ones listed in group) can be used, but "year", "month", "week" are also available provided a date variable is added to sub_date. Currently, combined variables cannot be faceted. A list containing a table and plot are printed to the console and viewer by default.

Examples

## Not run: 
# summarizing one catch column by one group variable
species_catch(pollockMainDataTable, species = "OFFICIAL_TOTAL_CATCH_MT",
              group = "GEAR_TYPE", ouput = "tab_plot")

# summarizing three catch columns by month
species_catch('pollockMainDataTable', 
              species = c('HAUL_LBS_270_POLLOCK_LBS', 
                          'HAUL_LBS_110_PACIFIC_COD_LBS', 
                          'HAUL_LBS_OTHER_LBS'), 
              date = 'HAUL_DATE', period = 'month_num', output = 'plot', 
              conv = 'tons')

# filtering by variable
species_catch(pollockMainDataTable, species = "OFFICAL_TOTAL_CATCH_MT",
              group = "GEAR_TYPE", filter_by = "PORT_CODE", 
              filter_value = "Dutch Harbor")
              
 # filtering by date
 species_catch(pollockMainDataTable, species = "OFFICAL_TOTAL_CATCH_MT",
               sub_date = "HAUL_DATE", filter_date = "month", date_value = 7:10)

## End(Not run)

Separate secondary data table from MainDataTable

Description

Separate secondary data table from MainDataTable

Usage

split_dat(dat, aux = NULL, project, split_by = NULL, key, output = "main")

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

aux

Auxiliary data table in fishset_db or environment. Use string if referencing a table saved in the FishSET database. The column names from "aux" will be used to find and separate the auxiliary table from the MainDataTable.

project

String, name of project.

split_by

String, columns in MainDataTable to split by. These columns will be separated from MainDataTable. Must contain values from "key".

key

String, the column(s) that link the main and auxiliary data tables. If using "aux" method, "key" must match a column in both MainDataTable and "aux" data table. If using "split_by", "key" must match a column in "MainDataTable and also be included in "split_by".

output

String, return either the "main" data table, "aux" data table, or "both" main and aux data tables in a list.

Details

This function separates auxiliary data (or gridded and port data) from the MainDatatable. Users can either input the secondary data table (from environment or fishset_db) to determine which columns to remove or by passing a string of columns names to "split_by". Use either the "aux" or the "split_by" method. Defaults to "aux" method if both arguments are used.

Examples

## Not run: 
split_dat("pollockMainDataTable", "pollock", aux = "pollockPortTable", key = "PORT_CODE")

## End(Not run)

Summarize and Plot Policy Effort Redistribution

Description

Extracts and visualizes the spatial redistribution of fishing effort.

Usage

summarize_policy_effort(
  project,
  spat,
  zone_spat,
  output_type = "static",
  plot_scenarios = NULL,
  plot_models = NULL,
  plotly_source = "effort_scatter"
)

Arguments

project

Character. Name of the project.

spat

Character. Name of the spatial dataset containing the fishing zones.

zone_spat

Character. The ID column in the spatial data matching simulation zone IDs.

output_type

Character. Dictates the rendering engine: "static" (ggplot2) for standard R plots, or "dynamic" (leaflet/plotly) for interactive HTML widgets. Default is "static".

plot_scenarios

Character vector (optional). Filters plots for specific scenarios using partial string matching (grep).

plot_models

Character vector (optional). Filters plots for specific models using exact matching.

plotly_source

Character. A unique identifier string utilized by the crosstalk package to enable cross-widget interactivity (e.g., capturing a click on the scatter plot to highlight a zone on the map). The default is "effort_scatter". Note: There is no strict menu of alternative options; rather, you can provide any custom string here to match the namespace or SharedData group ID defined within your specific Shiny app or RMarkdown document.

Details

What does "Effort" represent?

In this simulation framework, "effort" represents the expected number of fishing choice occasions (e.g., trips or hauls) allocated to each spatial zone.

Value

A named list containing four main elements:

  • summary_data: A data frame of all raw metrics for the requested simulations.

  • plots_absolute_map: A list of map objects showing raw net change in effort.

  • plots_percent_map: A list of map objects showing percentage change.

  • plots_scatter: A list of 1:1 scatter plot objects comparing baseline vs. counterfactual effort.


Summarize and Plot Policy Welfare Impacts

Description

Extracts and visualizes the expected economic welfare changes across all simulated policy scenarios, including statistical uncertainty.

Usage

summarize_policy_welfare(project, plot_scenarios = NULL, plot_models = NULL)

Arguments

project

Character. Name of the project.

plot_scenarios

Character vector (optional). Specific Scenario or Simulation names to include in the plots. If NULL (the default), scenarios are not filtered.

plot_models

Character vector (optional). Specific Model names to include in the plots (e.g., "zonal_logit", "clogit"). If NULL (the default), models are not filtered.

Details

What does "Welfare Change" represent?

In this simulation framework, "welfare change" represents the Compensating Variation (CV) resulting from a policy shock, expressed in real-world monetary units (e.g., dollars).

It is calculated using the log-sum difference formula from random utility theory. This metric captures not just the direct penalty of a closure or a drop in catch, but also the mitigating effect of spatial substitution—how fishers adapt by reallocating their effort to the next-best available fishing grounds.

Value

A list containing three elements: summary_data (a data frame of welfare impacts), plot_bar (mean per-trip changes), and plot_density (distribution of uncertainty).


View summary statistics

Description

View summary statistics in table format for entire dataset or for a specific variable.

Usage

summary_stats(dat, project, x = NULL, log_fun = TRUE)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Name of project

x

Optional. Variable in dat to view summary statistics for. If not defined, summary stats are displayed for all columns in the dataset.

log_fun

Logical, whether to log function call (for internal use).

Details

Prints summary statistics for each variable in the data set. If x is specified, summary stats will be returned only for that variable. Numeric variables are summarized by minimum, median, mean, maximum, and the number of NA's, unique values, and zeros. Non-numeric variables are summarized by first value and the number of NA's, unique values, and empty values. Function is called in the data_check function.

Examples

## Not run: 
summary_stats(pcodMainDataTable, project = "pcod")

summary_stats(pcodMainDataTable, project = "pcod", x = "HAUL")

## End(Not run)

Check if table exists in the FishSET database for the defined project

Description

Wrapper for dbExistsTable. Check if a table exists in the FishSET database.

Usage

table_exists(table, project)

Arguments

table

Name of table in FishSET database.Table name must be in quotes.

project

Name of project

Value

Returns a logical statement of table existence.

Examples

## Not run: 
table_exists('pollockMainDataTable', 'pollock')

## End(Not run)

Lists fields for FishSET database table

Description

Wrapper for dbListFields. View fields of selected table.

Usage

table_fields(table, project)

Arguments

table

String, name of table in FishSET database. Table name must be in quotes.

project

Project name

Examples

## Not run: 
table_fields('pollockMainDataTable', 'pollock')

## End(Not run)

Remove table from FishSET database

Description

Wrapper for dbRemoveTable. Remove a table from the FishSET database.

Usage

table_remove(table, project)

Arguments

table

String, name of table in FishSET database. Table name must be in quotes.

project

Name of project

Details

Function utilizes sql functions to remove tables from the FishSET database.

Examples

## Not run: 
table_remove('pollockMainDataTable', 'pollock')

## End(Not run)

Save an existing FishSET DB table

Description

table_save() updates existing FishSET DB tables. If the table doesn't exist, the user is reminded to use the appropriate load_ function.

Usage

table_save(table, project, type, name = NULL)

Arguments

table

A dataframe to save to the FishSET Database.

project

Name of project.

type

The table type. Options include, "main" for primary data tables, "port" for port tables, "grid" for gridded tables, "aux" for auxiliary tables.

name

String, table name. Applicable only for gridded, auxiliary, and spatial tables.


View FishSET Database table

Description

Wrapper for dbGetQuery. View or call the selected table from the FishSET database.

Usage

table_view(table, project)

Arguments

table

String, name of table in FishSET database. Table name must be in quotes.

project

Name of project.

Details

table_view() returns a table from a project's FishSET Database.

See Also

list_tables to show existing tables by project and type. fishset_tables to show all tables in the FishSETFolder.

Examples

## Not run: 
head(table_view('pollockMainDataTable', project = 'pollock'))

## End(Not run)

View names of project tables

Description

Wrapper for dbListTables. View names of tables in a project's FishSET database.

Usage

tables_database(project)

Arguments

project

Project name

Examples

## Not run: 
tables_database('pollock')

## End(Not run)

Number of observations by temporal unit

Description

View the number of observations by year, month, and zone in table format

Usage

temp_obs_table(
  dat,
  project,
  x,
  zoneid = NULL,
  spat = NULL,
  lon.dat = NULL,
  lat.dat = NULL,
  cat = NULL,
  lon.spat = NULL,
  lat.spat = NULL
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

x

Variable in dat containing date variable.

zoneid

Variable in dat that identifies the individual zones or areas. Defaults to NULL. Define if the name of the zone identifier variable is not 'ZoneID'.

spat

Spatial data containing information on fishery management or regulatory zones. Shape, json, geojson, and csv formats are supported. Required if zoneid does not exist in dat.

lon.dat

Longitude variable in dat. Required if zoneid does not exist in dat.

lat.dat

Latitude variable in dat. Required if zoneid does not exist in dat.

cat

Variable or list in spat that identifies the individual areas or zones. If spat is class sf, cat should be name of list containing information on zones. Required if zoneid does not exist in dat.

lon.spat

Variable or list from spat containing longitude data. Required if zoneid does not exist in dat and spat is a csv file. Leave as NULL if spat is a shape or json file.

lat.spat

Variable or list from spat containing latitude data. Required if zoneid does not exist in dat and spat is a csv file. Leave as NULL if spat is a shape or json file.

Details

Prints tables displaying the number of observations by year, month, and zone. assignment_column is called to assign observations to zones if zoneid does not exist in dat. Output is not saved.

Examples

## Not run: 
temp_obs_table(pollockMainDataTable, spat = map2, x = "DATE_FISHING_BEGAN",
  lon.dat = "LonLat_START_LON", lat.dat = "LonLat_START_LAT", cat = "NMFS_AREA",
  lon.spat = "", lat.spat = ""
  )

## End(Not run)

Transform units of date variables

Description

Creates a new temporal variable by extracting temporal unit, such as year, month, or day from a date variable.

Usage

temporal_mod(
  dat,
  project,
  x,
  define_format = NULL,
  timezone = NULL,
  name = NULL,
  log_fun = TRUE,
  ...
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

Project name.

x

Time variable to modify from dat.

define_format

Format of temporal data. define_format should be NULL if converting timezone for x but not changing format. Format can be user-defined or from pre-defined choices. Format follows as.Date format. See Details for more information.

timezone

String, defaults to NULL. Returns the date-time in the specified time zone. Must be a recognizable timezone, such as "UTC", "America/New_York", "Europe/Amsterdam".

name

String, name of created variables. Defaults to 'TempMod'.

log_fun

Logical, whether to log function call (for internal use).

...

Additional arguments. Use tz='' to specify time zone.

Details

Converts a date variable to desired timezone or units using as.Date. date_parser is also called to ensure the date variable is in an acceptable format for as.Date. define_format defines the format that the variable should take on. Examples include "%Y%m%d", "%Y-%m-%d %H:%M:%S". Users can define their own format or use one of the predefined ones. Hours is 0-23. To return a list of time-zone name in the Olson/IANA database paste OlsonNames() to the console.

Predefined formats:

  • year: Takes on the format "%Y" and returns the year.

  • month: Takes on the format "%Y/%m" and returns the year and month.

  • day: Takes on the format "%Y/%m/%d" and returns the year, month, and day.

  • hour: Takes on the format "%Y/%m/%d %H" and returns the year, month, day and hour.

  • minute: Takes on the format "%Y/%m/%d %H:%M" and returns the year, month, day, hour, and minute.

For more information on formats, see https://www.stat.berkeley.edu/~s133/dates.html.

Value

Primary data set with new variable added.

Examples

## Not run: 
pcodMainDataTable <- temporal_mod(pcodMainDataTable, "pcod", 
   "DATE_LANDED", define_format = "%Y%m%d")
pcodMainDataTable <- temporal_mod(pcodMainDataTable, "pcod", 
   "DATE_LANDED", define_format = "year")

## End(Not run)


# Change to Year, month, day, minutes

Northeast Ten Minute Squares

Description

Northeast Ten Minute Squares

Usage

tenMNSQR

Format

'tenMNSQR' A simple feature COLLECTION with 5267 features and 9 fields:

AREA
PERIMETER
TEN_
TEN_ID
LL
LAT
LON
TEMP
LOC

Check rows are unique

Description

Check for and remove non-unique rows from primary dataset.

Usage

unique_filter(dat, project, remove = FALSE)

Arguments

dat

Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.

project

String, name of project.

remove

Logical, if TRUE removes non-unique rows. Defaults to FALSE.

Details

Output is determined by remove. If remove = TRUE then non-unique rows are removed. If remove = FALSE then only a statement is returned regarding the number of rows that are not unique.

Value

Returns the modified primary dataset with non-unique rows removed if remove = TRUE.

Examples

## Not run: 
# check for unique rows
unique_filter(pollockMainDataTable)

# remove non-unique rows from dataset
mod.dat <- unique_filter(pollockMainDataTable, remove = TRUE)

## End(Not run)

Summarize active vessels

Description

vessel_count counts the number of active vessels in the main table. It can summarize by period if date is provided, group by any number of grouping variables, and filter by period or value. There are several options for customizing table/plot output.

Usage

vessel_count(
  dat,
  project,
  v_id,
  date = NULL,
  period = NULL,
  group = NULL,
  sub_date = NULL,
  filter_date = NULL,
  date_value = NULL,
  filter_by = NULL,
  filter_value = NULL,
  filter_expr = NULL,
  facet_by = NULL,
  combine = FALSE,
  position = "stack",
  tran = "identity",
  format_lab = "decimal",
  value = "count",
  type = "bar",
  scale = "fixed",
  output = "tab_plot"
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

v_id

Variable in dat containing vessel identifier to count.

date

Date variable to aggregate by.

period

Time period to aggregate by. Options include "year", "month", "week" (weeks in the year), "weekday", "weekday_abv", "day_of_month", "day_of_year", and "cal_date" (calender date).

group

Names of grouping variables. For line plots (type = "line") two grouping variables can be entered, the first is passed to "color" and second to "linetype". Only one grouping variable can be used for barplots (type = "bar"), which is passed to "fill". When combine = TRUE all variables in group will be joined. Grouping by "year", "month", and "week" are available if a date variable is added to sub_date.

sub_date

Date variable used for subsetting, grouping, or splitting by date.

filter_date

The type of filter to apply to 'MainDataTable'. To filter by a range of dates, use filter_date = "date_range". To filter by a given period, use "year-day", "year-week", "year-month", "year", "month", "week", or "day". The argument date_value must be provided.

date_value

This argument is paired with filter_date. To filter by date range, set filter_date = "date_range" and enter a start- and end-date into date_value as a string: date_value = c("2011-01-01", "2011-03-15").

To filter by period (e.g. "year", "year-month"), use integers (4 digits if year, 1-2 digits if referencing a day, month, or week). Use a vector if filtering by a single period: date_filter = "month" and date_value = c(1, 3, 5). This would filter the data to January, March, and May.

Use a list if using a year-period type filter, e.g. "year-week", with the format: list(year, period). For example, filter_date = "year-month" and date_value = list(2011:2013, 5:7) will filter the data table from May through July for years 2011-2013.

filter_by

String, variable name to filter 'MainDataTable' by. the argument filter_value must be provided.

filter_value

A vector of values to filter 'MainDataTable' by using the variable in filter_by. For example, if filter_by = "GEAR_TYPE", filter_value = 1 will include only observations with a gear type of 1.

filter_expr

String, a valid R expression to filter 'MainDataTable' by using the variable in filter_by.

facet_by

Variable name to facet by. Accepts up to two variables. These can be variables that exist in dat, or a variable created by vessel_count() such as "year", "month", or "week" if a date variable is added to sub_date. The first variable is facetted by row and the second by column.

combine

Whether to combine variables listed in group. This is passed to the "fill" or "color" aesthetic for plots.

position

Positioning of bar plot. Options include 'stack', 'dodge', and 'fill'.

tran

A function to transform the y-axis. Options include log, log2, log10, and sqrt.

format_lab

decimal or scientific

value

Whether to return "count" or "percent" of active vessels. Defaults to "count".

type

Plot type, options include "bar" (the default) and "line".

scale

Scale argument passed to facet_grid. Options include "free", "free_x", "free_y". Defaults to "fixed".

output

Whether to display "plot", "table". Defaults to both ("tab_plot").

Details

vessel_count gives the number (or percent) of active vessels using a column of unique vessel IDs. The data can be filtered by date and/or by a variable. (console users may want to use a separate filtering function, like dplyr::filter, before running vessel_count: note that this is okay but will lead to different output if using log_rerun). filter_date specifies the type of date filter to apply–by date-range or by period. date_value should contain the values to filter the data by. To filter by a variable, enter its name as a string in filter_by and include the values to filter by in filter_value.

Up to two grouping variables can be entered. Grouping variables can be merged into one variable using combine = TRUE; in this case any number of variables can be joined, but no more than three is recommended.

For faceting, any variable (including ones listed in group) can be used, but "year", "month", "week" are also available provided a date variable is added to sub_date. Currently, combined variables cannot be faceted.

Value

When output = "tab_plot" a list containing a table and plot are returned. If output = "table" only the summary table is returned, if output = "plot" only the plot.

Examples

## Not run: 
# grouping by two variables
vessel_count(pollockMainDataTable, v_id = "VESSEL_ID", 
             group = c("GEAR_TYPE", "IFQ"))
             
# filter by variable
vessel_count(pollockMainDataTable, v_id = "VESSEL_ID", group = "GEAR_TYPE",
             filter_by = "IFQ", filter_value = "Y")
             
# filter by month
vessel_count(pollockMainDataTable, v_id = "VESSEL_ID", group = "GEAR_TYPE",
             sub_date = "HAUL_DATE", date_filter = "month", date_value = 1:5)
             
#' # filter by date
vessel_count(pollockMainDataTable, v_id = "VESSEL_ID", group = "GEAR_TYPE",
             sub_date = "HAUL_DATE", date_filter = "date_range", 
             date_value = c("2011-01-01", "2011-02-05"))

# summarize by month
vessel_count(pollockMainDataTable, v_id = 'VESSEL_ID', date = 'DATE_FISHING_BEGAN', 
             period = 'month', group = 'DISEMBARKED_PORT', position = 'dodge', 
             output = 'plot')

## End(Not run)

View the most recent fleet table by project

Description

View the most recent fleet table by project

Usage

view_fleet_table(project)

Arguments

project

The name of project.

Examples

## Not run: 
view_fleet_table("pollock")

## End(Not run)

Visualize gridded data on a map

Description

Visualize gridded data on a map

Usage

view_grid_dat(
  grid,
  project,
  lon,
  lat,
  value,
  split_by = NULL,
  group = NULL,
  agg_fun = "mean"
)

Arguments

grid

Gridded data table to visualize. Use string if visualizing a gridded data table in the FishSET Database.

project

String, project name.

lon

String, variable name containing longitude.

lat

String, variable name containing latitude.

value

String, variable name containing gridded values, e.g. sea surface temperature, wind speed, etc.

split_by

String, variable in gridded data table to split by.

group

String, variable in gridded data table to group value by. In addition to the variable(s) in group, value is also aggregated by each longitude-latitude pair. The string "lonlat" is a shortcut for group = c("lon", "lat") which aggregates the value for each longitude-latitude pair across the entire dataset.

agg_fun

Aggregating function applied to group. Defaults to mean.


View

Description

View

Usage

view_lon_lat(dat, lon, lat, id = NULL, crs = 4326)

Arguments

dat

Data containing lon and lat columns.

lon

Name of Longitude column.

lat

Name of Lattitude column.

id

Optional, name of an ID variable that is paired with lon and lat columns

crs

Optional, coordinate reference system to use. Defaults to EPSG code 4326 (WGS 84).


View model design file in database

Description

View model design file in database

Usage

view_model_design(project, date = NULL)

Arguments

project

Project name.

date

String, date model design file was created.


View interactive map of spatial data

Description

View interactive map of spatial data

Usage

view_spat(spat, id = NULL, type = "polygon")

Arguments

spat

Spatial dataset to view. Must be an object of class sf or sfc.

id

Optional, name of spatial ID column to view with spatial data.

type

Can be "polygon", "line", or "point".

Examples

## Not run: 
view_spat(pollockNMFSSpatTable, id = "NMFS_AREA")

## End(Not run)

Summarize weekly catch

Description

weekly_catch summarizes catch (or other numeric variables) in the main table by week. It can summarize by grouping variables and filter by period or value. There are several options for customizing the table and plot output.

Usage

weekly_catch(
  dat,
  project,
  species,
  date,
  fun = "sum",
  group = NULL,
  sub_date = NULL,
  filter_date = NULL,
  date_value = NULL,
  filter_by = NULL,
  filter_value = NULL,
  filter_expr = NULL,
  facet_by = NULL,
  type = "bar",
  conv = "none",
  tran = "identity",
  format_lab = "decimal",
  value = "count",
  position = "stack",
  combine = FALSE,
  scale = "fixed",
  output = "tab_plot",
  format_tab = "wide"
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

species

A variable in dat containing the species catch or a vector of species variables.

date

Variable in dat containing dates to aggregate by.

fun

Name of function to aggregate by. Defaults to sum.

group

Grouping variable names(s). Up to two grouping variables are available for line plots and one for bar plots. For bar plots, if only one species is entered the first group variable is passed to "fill". If multiple species are entered, species is passed to "fill" and the grouping variable is dropped. An exception occurs when faceting by species, then the grouping variable is passed to "fill". For line plots, the first grouping variable is passed to "fill" and the second to "linetype" if a single species column is entered or if faceting by species. Otherwise, species is passed to "fill", the first group variable to "linetype", and second is dropped.

sub_date

Date variable used for subsetting, grouping, or splitting by date.

filter_date

The type of filter to apply to 'MainDataTable'. To filter by a range of dates, use filter_date = "date_range". To filter by a given period, use "year-day", "year-week", "year-month", "year", "month", "week", or "day". The argument date_value must be provided.

date_value

This argument is paired with filter_date. To filter by date range, set filter_date = "date_range" and enter a start- and end-date into date_value as a string: date_value = c("2011-01-01", "2011-03-15").

To filter by period (e.g. "year", "year-month"), use integers (4 digits if year, 1-2 digits if referencing a day, month, or week). Use a vector if filtering by a single period: date_filter = "month" and date_value = c(1, 3, 5). This would filter the data to January, March, and May.

Use a list if using a year-period type filter, e.g. "year-week", with the format: list(year, period). For example, filter_date = "year-month" and date_value = list(2011:2013, 5:7) will filter the data table from May through July for years 2011-2013.

filter_by

String, variable name to filter 'MainDataTable' by. the argument filter_value must be provided.

filter_value

A vector of values to filter 'MainDataTable' by using the variable in filter_by. For example, if filter_by = "GEAR_TYPE", filter_value = 1 will include only observations with a gear type of 1.

filter_expr

String, a valid R expression to filter 'MainDataTable' by using the variable in filter_by.

facet_by

Variable name to facet by. Accepts up to two variables. These can be variables that exist in the dataset, or a variable created by species_catch() such as "year", "month", or "week" if a date variable is added to sub_date. Facetting by "species" is available if multiple catch columns are included in "species". The first variable is facetted by row and the second by column.

type

Plot type, options include "bar" (the default) and "line".

conv

Convert catch variable to "tons", "metric_tons", or by using a function entered as a string. Defaults to "none" for no conversion.

tran

A function to transform the y-axis. Options include log, log2, log10, sqrt.

format_lab

Formatting option for y-axis labels. Options include "decimal" or "scientific".

value

Whether to calculate raw "count" or "percent" of total catch.

position

Positioning of bar plot. Options include 'stack', 'dodge', and 'fill'.

combine

Whether to combine variables listed in group. This is passed to the "fill" or "color" aesthetic for plots.

scale

Scale argument passed to facet_grid. Defaults to "fixed".

output

Return output as "plot", "table", or both "tab_plot". Defaults to both ("tab_plot").

format_tab

How table output should be formatted. Options include 'wide' (the default) and 'long'.

Value

weekly_catch() aggregates catch by week using one or more columns of catch data. When multiple catch variables are entered, a new column "species" is created and used to group values in plots. The "species" column can also be used to split (or facet) the plot. For table output, the "species" column will be kept if format_tab = "long", i.e. a column of species names ("species") and a column containing catch ("catch"). When format_tab = "wide", each species is given its own column of catch. The data can be filtered by date and/or by a variable. filter_date specifies the type of date filter to apply–by date-range or by period. date_value should contain the values to filter the data by. To filter by a variable, enter its name as a string in filter_by and include the values to filter by in filter_value. Up to two grouping variables can be entered. Grouping variables can be merged into one variable using combine; in this case any number of variables can be joined, but no more than three is recommended. For faceting, any variable (including ones listed in group) can be used, but "year", "month", "week" are also available provided a date variable is added to sub_date. Currently, combined variables cannot be faceted. A list containing a table and plot are printed to the console and viewer by default.

Examples

## Not run: 
weekly_catch(pollockMainDataTable,
  species = c(
    "HAUL_LBS_270_POLLOCK_LBS",
    "HAUL_LBS_110_PACIFIC_COD_LBS",  "HAUL_LBS_OTHER_LBS"
  ), date = "DATE_FISHING_BEGAN",
  conv = "tons", year = 2011, output = "plot"
)

## End(Not run)

Summarize average CPUE by week

Description

weekly_effort summarizes CPUE (or other numeric variables) in the main table by week. It can summarize by grouping variables and filter by period or value. There are several options for customizing the table and plot output.

Usage

weekly_effort(
  dat,
  project,
  cpue,
  date,
  group = NULL,
  sub_date = NULL,
  filter_date = NULL,
  date_value = NULL,
  filter_by = NULL,
  filter_value = NULL,
  filter_expr = NULL,
  facet_by = NULL,
  conv = "none",
  tran = "identity",
  format_lab = "decimal",
  combine = FALSE,
  scale = "fixed",
  output = "tab_plot",
  format_tab = "wide"
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

project

String, name of project.

cpue

Variable(s) in dat containing catch per unit effort.

date

A variable in dat containing dates to aggregate by.

group

Grouping variable name(s). Up to two grouping variables are available. For plotting, if a single CPUE column is entered the first grouping variable is passed to the "color" aesthetic and the second to "linetype". If multiple CPUE columns are entered, a new variable named "species" is created and passed to "fill", the first group variable to "linetype", and second is dropped.

sub_date

Date variable used for subsetting, grouping, or splitting by date.

filter_date

The type of filter to apply to 'MainDataTable'. To filter by a range of dates, use filter_date = "date_range". To filter by a given period, use "year-day", "year-week", "year-month", "year", "month", "week", or "day". The argument date_value must be provided.

date_value

This argument is paired with filter_date. To filter by date range, set filter_date = "date_range" and enter a start- and end-date into date_value as a string: date_value = c("2011-01-01", "2011-03-15").

To filter by period (e.g. "year", "year-month"), use integers (4 digits if year, 1-2 digits if referencing a day, month, or week). Use a vector if filtering by a single period: date_filter = "month" and date_value = c(1, 3, 5). This would filter the data to January, March, and May.

Use a list if using a year-period type filter, e.g. "year-week", with the format: list(year, period). For example, filter_date = "year-month" and date_value = list(2011:2013, 5:7) will filter the data table from May through July for years 2011-2013.

filter_by

String, variable name to filter 'MainDataTable' by. the argument filter_value must be provided.

filter_value

A vector of values to filter 'MainDataTable' by using the variable in filter_by. For example, if filter_by = "GEAR_TYPE", filter_value = 1 will include only observations with a gear type of 1.

filter_expr

String, a valid R expression to filter 'MainDataTable' by using the variable in filter_by.

facet_by

Variable name to facet by. Accepts up to two variables. Facetting by "year" is available if a date variable is added to sub_date. Facetting by "species" is available if multiple cpue columns are included in "cpue". The first variable is facetted by row and the second by column.

conv

Convert catch variable to "tons", "metric_tons", or by using a function entered as a string. Defaults to "none" for no conversion.

tran

A function to transform the y-axis. Options include log, log2, log10, sqrt.

format_lab

Formatting option for y-axis labels. Options include "decimal" or "scientific".

combine

Whether to combine variables listed in group. This is passed to the "color" aesthetic for plots.

scale

Scale argument passed to facet_grid. Defaults to "fixed".

output

Whether to display "plot", "table". Defaults to both ("tab_plot").

format_tab

How table output should be formatted. Options include 'wide' (the default) and 'long'.

Value

weekly_effort() calculates mean CPUE by week. This function doesn't calculate CPUE; the CPUE variable must be created in advance (see cpue). When multiple CPUE variables are entered, a new column named "species" is created and used to group values in plots. The "species" column can also be used to split (or facet) the plot. For table output, the "species" column will be kept if format_tab = "long", i.e. a column of species names ("species") and a column containing the mean CPUE ("mean_cpue"). When format_tab = "wide", each CPUE variable is given its own value column. The data can be filtered by date and/or by a variable. filter_date specifies the type of date filter to apply–by date-range or by period. date_value should contain the values to filter the data by. To filter by a variable, enter its name as a string in filter_by and include the values to filter by in filter_value. Up to two grouping variables can be entered. Grouping variables can be merged into one variable using combine; in this case any number of variables can be joined, but no more than three is recommended. For faceting, any variable (including ones listed in group) can be used, but "year" and "species" are also available. Facetting by "year" requires a date variable be added to sub_date. Currently, combined variables cannot be faceted. A list containing a table and plot are printed to the console and viewer by default.

Examples

## Not run: 
weekly_effort(pollockMainDataTable, "CPUE", "DATE_FISHING_BEGAN", filter_date = "year", 
              date_value = 2011, output = "table")

## End(Not run)

Northeast wind closure areas

Description

Northeast wind closure areas

Usage

windLease

Format

'windLease' Simple features collection with 32 features and 1 field:

NAME

Name of wind lease.


Write a data table to local file directory

Description

Write a data table to local file directory

Usage

write_dat(dat, project, path = NULL, file_type = "csv", ...)

Arguments

dat

Name of data frame in working environment to save to file.

project

String, project name.

path

String, path or connection to write to. If left empty, the file will be written to the dat folder in the project directory.

file_type

String, the type of file to write to. Options include "csv", "txt" (tab-separated text file), "xlsx" (excel), "rdata", "json", "stata", "spss", "sas", and "matlab".

...

Additional arguments passed to writing function. See "details" for the list of functions.

Details

Leave path = NULL to save dat to the data folder in the project directory See write.table for csv and tab-separated files, save for R data files, write.xlsx, read_json for json files, st_write for geojson files, read_dta for Stata files, read_spss for SPSS files, read_sas for SAS files, and writeMat for Matlab files, and st_write for shape files.

Examples

## Not run: 
# Save to the default data folder in project directory
write_dat(pollockMainDataTable, type = "csv", "pollock")

# Save to defined directory location
write_dat(pollockMainDataTable, path = "C://data/pollock_dataset.csv", 
          type = "csv", "pollock")
          
# Save shape file
write_dat(ST6, path = "C://data//ST6.shp", type = "shp", project = 'Pollock')

## End(Not run)

Define zone closure scenarios

Description

Define zone closure scenarios

Usage

zone_closure(
  project,
  spatname,
  zone_spat,
  lon_spat = NULL,
  lat_spat = NULL,
  epsg = NULL
)

Arguments

project

Required, name of project.

spatname

Required, data file or character. spat is a spatial data file containing information on fishery management or regulatory zone boundaries. Shape, json, geojson, and csv formats are supported. geojson is the preferred format. json files must be converted into geojson. This is done automatically when the file is loaded with read_dat with is.map set to true. spat cannot, at this time, be loaded from the FishSET database.

zone_spat

Variable in spat that identifies the individual areas or zones.

lon_spat

Required for csv files. Variable or list from spat containing longitude data. Leave as NULL if spat is a shape or json file.

lat_spat

Required for csv files. Variable or list from spat containing latitude data. Leave as NULL if spat is a shape or json file.

epsg

EPSG number. Set the epsg to ensure that spat has the correct projections. If epsg is not specified but is defined for spat. See http://spatialreference.org/ to help identify the optimal epsg number.

Details

Define zone closure scenarios via an interactive app. Users can define scenarios by clicking on one or more zones on the map, adjusting the allowable TAC percentages in the table, and entering a unique scenario name. Clicking 'Add closure' instantly saves the scenario to the project database. These saved choices are later called in the policy scenario function.

Value

Returns a yaml file to the project output folder.


Summarize zones, closure areas

Description

'zone_summary' counts observations and aggregates values in 'dat' by regulatory zone or closure area.

Usage

zone_summary(
  dat,
  spat,
  project,
  zone.dat,
  zone.spat,
  dat_lon,
  dat_lat,
  count = TRUE,
  var = NULL,
  group = NULL,
  fun = NULL,
  na.rm = TRUE,
  dat_center = TRUE,
  plot_type = "dynamic",
  output = "plot"
)

Arguments

dat

Primary data containing information on hauls or trips. Table in FishSET database contains the string 'MainDataTable'.

spat

A spatial data file containing information on fishery management or regulatory zones boundaries. 'sf' objects are recommended, but 'sp' objects can be used as well. See [dat_to_sf()] to convert a spatial table read from a csv file to an 'sf' object. To upload your spatial data to the FishSETFolder see [load_spatial()].

project

Name of project.

zone.dat

Name of zone ID column in 'dat'.

zone.spat

Name of zone ID column in 'spat'.

dat_lon

Name of longitude column in 'dat'.

dat_lat

Name of latitude column in 'dat'.

count

Logical. if 'TRUE', then the number observations per zone will be returned. Can be paired with 'fun = "percent"' and 'group'. 'zone_summary' will return an error if 'var' is include and 'count = TRUE'.

var

Optional, name of numeric variable to aggregate by zone/closure area.

group

Name of grouping variable to aggregate by zone/closure area. Only one variable is allowed.

fun

Function name (string) to aggregate by. '"percent"' the percentage of observations in a given zone. Other options include "sum", "mean", "median", "min", and "max".

na.rm

Logical, whether to remove zones with zero counts.

dat_center

Logical, whether the plot should center on 'dat' ('TRUE') or 'spat' ('FALSE'). Recommend 'dat_center = TRUE' when aggregating by regulatory zone and 'dat_center = FALSE' when aggregating by closure area.

plot_type

Type of plot output; dynamic ('leaflet' object) or static ('ggplot' object)

output

Output a '"plot"', '"table"', or both ('"tab_plot"'). Defaults to '"plot"'.

Details

Observations in 'dat' must be assigned to regulatory zones to use this function. See [assignment_column()] for details. 'zone_summary' can return: the number of observations per zone ('count = TRUE', 'fun = NULL', 'group = NULL'), the percentage of observations by zone ('count = TRUE', 'fun = "percent"', 'group = NULL'), the percentage of observations by zone and group ('count = TRUE', 'fun = "percent"', 'group = "group"'), summary of a numeric variable by zone ('count = FALSE', 'var = "var"', 'fun = "sum"', 'group = NULL'), summary of a numeric variable by zone and group ('count = FALSE', 'var = "var"', 'fun = "sum"', 'group = "group"'), share (percentage) of a numeric variable by zone ('count = FALSE', 'var = "var"', 'fun = "percent"', 'group = NULL'), share (percentage) of a numeric variable by zone and group ('count = FALSE', 'var = "var"', 'fun = "percent"', 'group = "group"').

Examples

## Not run: 

# count # of obs
zone_summary(pollockMainTable, spat = nmfs_area, zone.dat = "ZoneID", 
            zone.spat = "NMFS_AREA")
            
# percent of obs
zone_summary(pollockMainTable, spat = nmfs_area, zone.dat = "ZoneID", 
            zone.spat = "NMFS_AREA", count = TRUE, fun = "percent")

# count by group
zone_summary(pollockMainTable, spat = nmfs_area, zone.dat = "ZoneID", 
            zone.spat = "NMFS_AREA", group = "GEAR_TYPE")   

# total catch by zone           
zone_summary(pollockMainTable, spat = nmfs_area, zone.dat = "ZoneID", 
            zone.spat = "NMFS_AREA", var = "OFFICIAL_TOTAL_CATCH_MT",
            count = FALSE, fun = "sum")  

# percent of catch by zone           
zone_summary(pollockMainTable, spat = nmfs_area, zone.dat = "ZoneID", 
            zone.spat = "NMFS_AREA", var = "OFFICIAL_TOTAL_CATCH_MT",
            count = FALSE, fun = "percent")         
            

## End(Not run)