Skip to content

Commit 64fed81

Browse files
committed
first commit.
0 parents  commit 64fed81

30 files changed

+1305
-0
lines changed

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata

Calibr.Rproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: knitr
13+
LaTeX: pdfLaTeX
14+
15+
AutoAppendNewline: Yes
16+
StripTrailingWhitespace: Yes
17+
18+
BuildType: Package
19+
PackageUseDevtools: Yes
20+
PackageInstallArgs: --no-multiarch --with-keep.source
21+
PackageRoxygenize: rd,collate,namespace

DESCRIPTION

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Package: Calibr
2+
Type: Package
3+
Title: Standardization of population abundance datasets
4+
Version: 0.0.10
5+
Author: Eric Fletcher, Marc Nadon
6+
Maintainer: The package maintainer <[email protected]>
7+
Description: More about what it does (maybe more than one line)
8+
Use four spaces when indenting paragraphs within the Description.
9+
License: file LICENSE
10+
Encoding: UTF-8
11+
LazyData: true
12+
Depends:
13+
R (>= 3.0.0),
14+
Imports:
15+
MASS,
16+
methods,
17+
utils,
18+
plyr,
19+
dplyr,
20+
data.table,
21+
boot,
22+
glmmTMB,
23+
pbapply,
24+
parallel
25+
RoxygenNote: 6.0.1

LICENSE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Software code created by U.S. Government employees is not subject to copyright in the United States (17 U.S.C.
2+
§105). The United States/Department of Commerce reserve all rights to seek and obtain copyright protection in
3+
countries other than the United States for Software authored in its entirety by the Department of Commerce. To
4+
this end, the Department of Commerce hereby grants to Recipient a royalty-free, nonexclusive license to use,
5+
copy, and create derivative works of the Software outside of the United States.

NAMESPACE

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(calibrate_dataset)
4+
export(export_results)
5+
export(gcf_glm)
6+
export(gcf_glm_apply)
7+
export(gcf_glmm)
8+
export(load_dataset)
9+
export(rep_summary)
10+
export(run_calibr)
11+
import(data.table)
12+
import(methods)
13+
import(stats)
14+
importFrom(MASS,mvrnorm)
15+
importFrom(boot,inv.logit)
16+
importFrom(boot,logit)
17+
importFrom(dplyr,arrange_)
18+
importFrom(glmmTMB,glmmTMB)
19+
importFrom(glmmTMB,ranef)
20+
importFrom(parallel,clusterEvalQ)
21+
importFrom(parallel,detectCores)
22+
importFrom(parallel,makeCluster)
23+
importFrom(parallel,parLapply)
24+
importFrom(parallel,stopCluster)
25+
importFrom(pbapply,pblapply)
26+
importFrom(pbapply,pboptions)
27+
importFrom(plyr,.)
28+
importFrom(plyr,daply)
29+
importFrom(plyr,ddply)
30+
importFrom(utils,read.csv)
31+
importFrom(utils,write.csv)

R/calibr.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#' Calibr: A tool to standardize species population aboundance.
2+
#'
3+
#' Calibr is a R-Package designed to process highly diverse coral reef species datasets and to
4+
#' analyse with Generalized Linear Models (GLM) to obtian standarization factors.
5+
#'
6+
#' @import methods
7+
#' @importFrom utils read.csv
8+
#'
9+
#' @docType package
10+
#' @name Calibr
11+
NULL

R/calibrate_dataset.R

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#Bindings for "global variables" used in calibrate_dataset
2+
if(getRversion() >= "2.15.1") {
3+
utils::globalVariables(c("GCF.SOURCE",
4+
"GROUP",
5+
"OPUE",
6+
"OPUE.CAL",
7+
"POS",
8+
"POS.CAL",
9+
"POS.GCF",
10+
"PRES",
11+
"PRES.CAL",
12+
"PRES.GCF"))
13+
}
14+
15+
#' Convert original dataset using the calculated GCFs
16+
#'
17+
#' @param ORIG Original Survey Dataset
18+
#' @param GCFs Gear calibration factors obtained from gcf_glmm function.
19+
#' @param std_method Standard gear METHOD
20+
#'
21+
#' @import data.table
22+
#' @importFrom boot inv.logit
23+
#'
24+
#' @export
25+
calibrate_dataset <- function (ORIG,std_method, GCFs) {
26+
27+
D <- data.table(ORIG)
28+
GCFs <- data.table(GCFs,keep.rownames=T)
29+
colnames(GCFs) <- c("GROUP","PRES.GCF","POS.GCF")
30+
31+
S.pres <- D[,list(PRES=mean(PRESENCE)),by=list(GROUP,METHOD,BLOCK)]
32+
S.pres <- S.pres[,list(PRES=mean(PRES)),by=list(GROUP,METHOD)]
33+
34+
S.pos <- D[PRESENCE>0,list(POS=mean(DENSITY)),by=list(GROUP,METHOD,BLOCK)]
35+
S.pos <- S.pos[,list(POS=mean(POS)),by=list(GROUP,METHOD)]
36+
37+
S <- merge(S.pres,S.pos)
38+
S$OPUE <- S$PRES*S$POS
39+
40+
S$PRES.CAL <- S$PRES
41+
S$POS.CAL <- S$POS
42+
S$OPUE.CAL <- S$OPUE
43+
44+
S <- merge(S,GCFs,by="GROUP",all.x=T)
45+
S <- data.table(S)
46+
47+
# Fill GROUPs with no GCF with the Global ones
48+
S$GCF.SOURCE <-"Group-specific"
49+
S[is.na(PRES.GCF)]$GCF.SOURCE <- "Global"
50+
S[is.na(PRES.GCF)]$PRES.GCF <- GCFs[1,]$PRES.GCF
51+
S[is.na(POS.GCF)]$POS.GCF <- GCFs[1,]$POS.GCF
52+
53+
S[METHOD!=std_method]$PRES.CAL <- inv.logit(S[METHOD!=std_method]$PRES.GCF+logit(S[METHOD!=std_method]$PRES ))
54+
S[METHOD!=std_method]$POS.CAL <- S[METHOD!=std_method]$POS/S[METHOD!=std_method]$POS.GCF
55+
S[METHOD!=std_method]$OPUE.CAL <- S[METHOD!=std_method]$PRES.CAL*S[METHOD!=std_method]$POS.CAL
56+
57+
# Re-organize
58+
S <- subset(S,select=c(GROUP,METHOD,GCF.SOURCE,PRES.GCF,POS.GCF,PRES,PRES.CAL,POS,POS.CAL,OPUE,OPUE.CAL))
59+
S[,4:ncol(S)] <- round(S[,4:ncol(S)],2)
60+
61+
return(S)
62+
63+
}

R/data.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
#'NOAA Pacific RAMP, Small Belt Transect, 2002-2010
3+
#'
4+
#'Dataset from Coral Reef Fish Surveys, conducted by the NOAA Pacific Reef
5+
#'Assessment and Monitoring Program (RAMP). The survey area includes the
6+
#'Main and Northwest Hawaiian Islands, the Mariana Archipelago, and various
7+
#'remote islands in the central pacific.
8+
#'
9+
#'@format A data.frame with 263580 rows, and 6 fields:
10+
#'\describe{
11+
#' \item{BLOCK}{Spacetime temporial strata.}
12+
#' \item{REP}{Replicate, survey site.}
13+
#' \item{GROUP}{Species or taxinominal grouping.}
14+
#' \item{METHOD}{Survey method used.}
15+
#' \item{DENSITY}{Group abundance of positive observations.}
16+
#' \item{PRESENCE}{Proportion of presence over absence of a group at a survey site.}
17+
#'}
18+
#'
19+
#'@source \url{https://www.pifsc.noaa.gov/library/pubs/admin/PIFSC_Admin_Rep_11-08.pdf}
20+
"SMALL_UNPAIR"
21+
22+
#'SIMDATA: Simulation data
23+
#'
24+
#'Description Of SIMDATA
25+
#'
26+
#'@format A data.frame with 4586 rows, and 6 fields:
27+
#'\describe{
28+
#' \item{GROUP}{Species or taxinominal grouping.}
29+
#' \item{BLOCK}{Spacetime temporial strata.}
30+
#' \item{REP}{Replicate, survey site.}
31+
#' \item{METHOD}{Survey method used.}
32+
#' \item{DENSITY}{Group abundance of positive observations.}
33+
#' \item{PRESENCE}{Proportion of presence over absence of a group at a survey site.}
34+
#'}
35+
#'
36+
#'@source \url{https://www.pifsc.noaa.gov/library/pubs/admin/PIFSC_Admin_Rep_11-08.pdf}
37+
"SIMDATA"

R/export_results.R

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
#' Export results to saved files.
3+
#'
4+
#' Option to export grouped survey datasets, the GCF/OPUE Summary Table, and REP summaries in
5+
#' seperate data files.
6+
#'
7+
#' @details These following datalist objects will be written to the location, defined by \code{outdir}.
8+
#' \itemize{
9+
#' \item The Grouped survey dataset object will be as saved as \code{reeffish_grouped.Rds}.
10+
#' \item The GCF and OPUE summaries will be saved as \code{summary_table.csv}.
11+
#' \item The \code{REP} count by species group summary will be saved as \code{REP_summary.csv}.
12+
#' }
13+
#'
14+
#' If the user doesn't specify a output directory, then a new directory will be generated from the user's HOME location.
15+
#'
16+
#' @param datalist List containing the Grouped dataset and summaries from \code{\link{load_dataset}}
17+
#' @param outdir Location to export data and summaries to. Default location is user's \code{HOME} directory
18+
#'
19+
#' @importFrom utils write.csv
20+
#'
21+
#' @export
22+
export_results <- function(datalist,outdir=Sys.getenv("HOME")){
23+
24+
tryCatch(
25+
{
26+
#If dir is default, create an output subdirectory from the user's HOME location.
27+
if(outdir == Sys.getenv("HOME")){
28+
calibr_home <- file.path(outdir, "Calibr")
29+
30+
if(!dir.exists(calibr_home)){
31+
message("Creating directory for Calibr runs: ", calibr_home ," ...")
32+
dir.create(calibr_home)
33+
}
34+
35+
outdir <- file.path(calibr_home,basename(tempfile(pattern="CalibrRun_")))
36+
message("Creating output directory: ", outdir ," ...")
37+
dir.create(outdir)
38+
}else{
39+
#check if outdir exists in user's system. If not return error message
40+
if(!(dir.exists(outdir))){
41+
stop("Location of output directory was not found or inaccessible : \n",outdir)
42+
}
43+
message("Output Directory: ", outdir)
44+
}
45+
},
46+
error=function(cond){
47+
message(cond)
48+
return(NULL)
49+
}
50+
)
51+
52+
message("Exporting grouped data and summary tables to output directory ...")
53+
54+
55+
56+
57+
58+
#Check datalist is a list
59+
if(class(datalist) != "list"){
60+
stop("Parameter datalist not a list object.")
61+
}
62+
63+
#Check datalist contents have the appropriate data structures.
64+
str.datalist <- list("list",
65+
c("data.table","data.frame"),
66+
"data.frame")
67+
if(!all(sapply(datalist,class) %in% str.datalist)){
68+
index_mismatch_classes <- !(sapply(datalist,class) %in% str.datalist)
69+
mismatched_classes <- sapply(datalist,class)[index_mismatch_classes]
70+
count_mismatches<- length(which(index_mismatch_classes))
71+
72+
mismatch_datalist_obj <- names(datalist)[index_mismatch_classes]
73+
stop("Datalist object(s) ", paste(mismatch_datalist_obj, collapse=", ")
74+
, "has an invalid object class:", paste(mismatched_classes,collapse=", "))
75+
76+
}
77+
78+
#Check if datalist has the names "LGROUP", "SUMMARY", and "REP_SUMMARY" in datalist
79+
if(!all(names(datalist) %in% c("LGROUP", "SUMMARY", "REP_SUMMARY" ))){
80+
missing_listNames <-names(datalist)[!(names(datalist) %in% c("LGROUP", "SUMMARY", "REP_SUMMARY" ))]
81+
stop("Invalid reef datalist fields: ", paste(missing_listNames,collapse = ", "))
82+
}
83+
84+
85+
#LGROUP
86+
message("Saving Grouped Coral Reef Data ...")
87+
reeffish_grouped_datalist <- datalist[["LGROUP"]]
88+
saveRDS(reeffish_grouped_datalist, file = file.path(outdir,"reeffish_grouped.Rds"))
89+
90+
#SUMMARY
91+
message("Saving GCF and OPUE Summaries to file ...")
92+
write.csv(datalist[["SUMMARY"]], file = file.path(outdir,"summary_table.csv"), row.names = FALSE)
93+
94+
#REP_SUMMARY
95+
message("Saving REP Summary Table to file ...")
96+
write.csv(datalist[["REP_SUMMARY"]], file = file.path(outdir,"REP_summary.csv"), row.names = FALSE)
97+
98+
message("Done.")
99+
message("Grouped data and summary tables have been to:\n", outdir)
100+
101+
}

0 commit comments

Comments
 (0)