-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinCatchAnalysis_3_CreateAnalysis.qmd
176 lines (117 loc) · 7.8 KB
/
FinCatchAnalysis_3_CreateAnalysis.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Create Analysis
### General
Analysis function calls are prefixed with "fca\_" and object names are prefixed with "fc\_"
Make sure to test each analysis function for:
- Data selected by surveys only
- Data selected by samples only
- Data selected by both surveys and samples
- Filters that return NO data
- Species in processing list and not in data
- Species not in processing list and in data
- Works for both grouped by survey and ungrouped analysis, if not grouped by survey in fc_data\$groupSurveys, surveyUid is set to "-1" during data download by the data object
### Flow and Critical Issues
- Import Data
- Make sure to use remove_nonCPUE_data as appropiate in get_data\_ functions
::: callout-important
use remove_nonCPUE_data=FALSE to get all data if appropriate, specify remove_nonCPUE_data=TRUE for readability (is default if not specified)
:::
- Check For Empty/Missing Stations using ??? function
- Conduct Analysis
- Expand to account for missing 0's using AddZerosForMissingSpecies() function
- Finish Analysis
- Label survey UIDs, sample UIDs, species, and methods
- ::: callout-note
Useful Functions:
fc_labeler_Survey
fc_labeler_fishSample
fc_labeler_wqSample
fc_matchCodes
fc_createCodeLabel
fc_createCodeLabelReversed
:::
### Steps
1. Create new r file names fc_analysisName.R (Easiest To Copy Existing Analysis and Modify)
2. No library statements should be included in R file. Instead, they need to be included in the package DESCRIPTION file.
3. Create/Modify the roxygen comments for procedure
4. Name/Rename function. Analysis functions are prefixed with "fca\_" and the same base name as the "fc\_" file.
5. All function calls require a FinCatch Data Object (fc_data) to be passed to an arguement named "myData"
6. Check that fish (or Wq) samples exist in the current dataset.
7. Set grouping variables.
- To group analysis calculations during analysis, use the dplyr verbs
``` r
group_by(across(all_of(myGroups))
```
- add addition fields as necessary "group_by(across(all_of(myGroups), anotherFieldHere)"
8. Write analysis code
- Always include Standard Error and Sample size (if appropriate), this allow users to calculate difference confidence intervals post hoc
- When including confidence intervals, include 95% and 80%
- Make sure to account for missing data in any input
9. Label values like survey, sample, species, waterbody, etc.
- Labelers exist for samples and surveys (make sure surveyUid's are set to -1 if not grouping by survey
- Helper functions are available for coded values
10. If analysis is calculated (as opposed to raw data summations), screen out species NOT in the processing list. This is necessisary because species NOT in the list do not carry the assumption that all specimans for that species were processed.
11. Attach all results to an analysis object (either base or custom). Example:
``` r
#create return object at beginning of function
(this allows errors and warnings to be added throughout survey)
op <- fc_meanWeight$new()
op$analysisTitle = "Mean Weight (Weighted)"
op$exportName = "MeanWeight"
op$descriptionText = "These results display weighted mean
weight. Calculations are weighted as
only a non-proportional (i.e. first 10 fish
per 10mm length group) number of fish
are subsampled."
op$tableTitle <- list("Weighted Mean Weight")
op$groupByVars <- "speciesCode"
op$groupSurveys <- myData$groupSurveys
#during function, add warnings and errors to return object
op$errors <- rlist::list.append(op$errors, "Error1 Message", "Error2 Message")
op$warnings <- rlist::list.append(op$warnings, "Warning1")
#at end of function, add results to return object
op$results <- list(data.frame(d))
op$plots <- myPlots
```
12. Add the function to the list of analysis functions available in the package (found in inst folder). This list is used to populate the FinCatchRA UI and to feed the fc_getAvailableAnalysis function.
### Create Custom Analysis R6 Object
All analysis results are returned using R6 objects. This allows for consistent use and implementation of different analysis functions by parent applications and code. A base R6 object, "fc_base", provides all the basic functions and structure for FinCatch analysis objects. Custom objects can be created in the analysis files to allow customized output tables and plots and MUST inherit from the fc_base object.
Column names will often need to be altered to provided user friendly text in the outputs. In addition, sometimes valuable columns are dropped for display purposes. Both of these should be done in the analysis output object createTable functions, NOT in the analysis function itself or in the "results" property of the output object. This is to provide for consistency between analyses and is important for the download function of FinCatchRA.
Every effort is made to ensure tables produced by analysis objects work in both HTML (which allows more formatting options and used by FinCatchRA) and in LaTex (for PDF report generation). Basic table creation should happen by providing a "CreateTable" function. Any additional work needed for specific HTML or LaTex output should be included in overridden "CreateTableHtml" or "CreateTableLatex" functions....which otherwise just call and return the "CreateTable" function by default.
``` r
fc_counts <- R6Class("fc_counts",
inherit = fc_base,
public = list(
createTable = function(mySurveyUid, myTableNumber) {
#if data was selected by samples only…all surveyUids will be blank
op <- NA
thisSurveyLabel <- (self$results[[myTableNumber]] %>% filter(surveyUid == mySurveyUid) %>%
pull(surveyLabel))[[1]]
op <- self$results[[myTableNumber]] %>%
filter(surveyUid == mySurveyUid) %>%
group_by(countSpeciesLabel, sampleLabel) %>%
summarise(FishCount = sum(FishCount, na.rm = TRUE)) %>%
mutate(countGenderLabel = "Total") %>%
bind_rows(self$results[[myTableNumber]] %>%
filter(surveyUid == mySurveyUid)) %>%
arrange(countSpeciesLabel, sampleLabel, countGenderLabel) %>%
select(-countSpeciesCode,-countGenderCode,-sampleUid,-surveyLabel,-surveyUid) %>%
spread(countGenderLabel, FishCount, fill = 0) %>%
relocate("Total", .after = last_col()) %>%
gt(rowname_col = colnames(self$results[[1]])[8]) %>%
tab_options(
row_group.background.color = self$groupHeaderBackgroundColor,
summary_row.background.color = self$groupSummaryBackgroundColor
) %>%
tab_header(title = md(self$tableTitle),
subtitle = thisSurveyLabel) %>%
summary_rows(groups = TRUE,
columns = everything(),
fns = list("Total" = "sum"),
formatter = fmt_integer,
use_seps = TRUE,
missing_text = "") %>%
self$gtTheme()
return(op)
}
))
```