Skip to content

Commit

Permalink
update after adding example dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
rosieluain committed Jun 27, 2023
1 parent d68f6a4 commit 2f7cd00
Show file tree
Hide file tree
Showing 24 changed files with 634 additions and 305 deletions.
24 changes: 24 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,27 @@ NULL
#'@usage data(seasons)
#'@format A data frame with 5 rows (years) and 2 variables.
NULL

#'Example of new acoustic telemetry detection data
#'
#'These are fabricated detection data to demonstrate the use of the review
#'function.
#'
#'The variables are as follows:
#'
#'\itemize{
#'\item ResidenceStart. The start time of the residence events, in POSIXct.
#'\item Station.Name. Name of the receiver location.
#'\item ID. Unique ID of the fish.
#'\item ResidenceEnd. The end time of the residence events, in POSIXct.
#'\item ResidenceLength.days. The duration of the residence events. The units are
#'days, as indicated by the variable name (which is automatically generated
#'by mort::residences())
#'}
#'
#'@docType data
#'@keywords datasets
#'@name new.data
#'@usage data(new.data)
#'@format A data frame with 35 rows (observations) and 3 variables.
NULL
82 changes: 42 additions & 40 deletions R/mort.R
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ morts<-function(data,type="mort",ID,station,res.start="auto",res.end="auto",
morts[nrow(morts)+1,]<-res.temp[j[k],]
}
}
setTxtProgressBar(pb,i)
}
setTxtProgressBar(pb,i)
}
}

Expand All @@ -367,47 +367,49 @@ morts<-function(data,type="mort",ID,station,res.start="auto",res.end="auto",
for (i in 1:nrow(sc2)){
res.temp<-data.morts[data.morts[[ID]]==sc2[[ID]][i],]
res.temp<-res.temp[order(res.temp[[res.start]]),]
repeat {
if (res.temp[[station]][nrow(res.temp)]=="Break"){
res.temp<-res.temp[-nrow(res.temp),]
}
else {break}
}
if (any(res.temp[[station]]=="Break")){
# Need two values, and determine which is lower
# Value 1: difftime between end of most recent residence and station change (sc2)
dt1<-difftime(res.temp[[res.end]][nrow(res.temp)],
sc2[[res.start]][i],
units=units)
# Value 2: difftime between end of most recent residence and most recent break
k<-(which(res.temp[[res.start]]==max(res.temp[[res.start]][res.temp[[station]]=="Break"])))+1
dt2<-difftime(res.temp[[res.end]][nrow(res.temp)],
res.temp[[res.start]][k],
units=units)
comp<-min(dt1,dt2)
}
else {
comp<-difftime(res.temp[[res.end]][nrow(res.temp)],
sc2[[res.start]][i],
units=units)
}
# If the cumulative residence at the most recent station is longer than the
# threshold of max.rescml
if (comp>max.rescml){
# If the ID is already in morts
if (sc2[[ID]][i] %in% morts[[ID]]){
# Identify which row
j<-which(morts[[ID]]==sc2[[ID]][i])
# If the residence currently in res.morts ocurred later
# than the cumulative residence period
if (morts[[res.start]][j]>sc2[[res.start]][i]){
# Adjust the start time to the earlier date
morts[j,]<-sc2[i,]
if (nrow(res.temp)>0){
repeat {
if (res.temp[[station]][nrow(res.temp)]=="Break"){
res.temp<-res.temp[-nrow(res.temp),]
}
else {break}
}
if (any(res.temp[[station]]=="Break")){
# Need two values, and determine which is lower
# Value 1: difftime between end of most recent residence and station change (sc2)
dt1<-difftime(res.temp[[res.end]][nrow(res.temp)],
sc2[[res.start]][i],
units=units)
# Value 2: difftime between end of most recent residence and most recent break
k<-(which(res.temp[[res.start]]==max(res.temp[[res.start]][res.temp[[station]]=="Break"])))+1
dt2<-difftime(res.temp[[res.end]][nrow(res.temp)],
res.temp[[res.start]][k],
units=units)
comp<-min(dt1,dt2)
}
# If the ID is not yet in morts
else {
morts[nrow(morts)+1,]<-sc2[i,]
comp<-difftime(res.temp[[res.end]][nrow(res.temp)],
sc2[[res.start]][i],
units=units)
}
# If the cumulative residence at the most recent station is longer than the
# threshold of max.rescml
if (comp>max.rescml){
# If the ID is already in morts
if (sc2[[ID]][i] %in% morts[[ID]]){
# Identify which row
j<-which(morts[[ID]]==sc2[[ID]][i])
# If the residence currently in res.morts ocurred later
# than the cumulative residence period
if (morts[[res.start]][j]>sc2[[res.start]][i]){
# Adjust the start time to the earlier date
morts[j,]<-sc2[i,]
}
}
# If the ID is not yet in morts
else {
morts[nrow(morts)+1,]<-sc2[i,]
}
}
}
setTxtProgressBar(pb,i)
Expand Down Expand Up @@ -679,7 +681,7 @@ infrequent<-function(data,type="mort",ID,station,res.start="auto",
}
}
else {
inf.morts[nrow(inf.morts)+1,]<-res.temp[1,]
inf.morts[nrow(inf.morts)+1,]<-res.temp[j,]
}
}
}
Expand Down
20 changes: 14 additions & 6 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ drift<-function(data,type,ID,station,res.start="auto",res.end="auto",
else {
for (k in 1:length(j)){
if ((res.temp[[station]][j[k]+1] %in%
ddd[[to.station]][ddd[[from.station]]==res.temp[[station]][j[k]]])&
ddd[[to.station]][ddd[[from.station]]==res.temp[[station]][[j[k]]][length(res.temp[[station]][[j[k]]])]])&
difftime(res.temp[[res.start]][j[k]+1],
res.temp[[res.end]][j[k]],
units=cutoff.units)<cutoff){
res.temp[[res.start]][j[k]+1]<-res.temp[[res.start]][j[k]]
res.temp[[station]][j[k]+1]<-res.temp[[station]][j[k]]
res.temp[[station]][[j[k]+1]]<-append(res.temp[[station]][[j[k]]],res.temp[[station]][[j[k]+1]])
del<-c(del,j[k])
}
}
Expand Down Expand Up @@ -286,6 +286,8 @@ drift<-function(data,type,ID,station,res.start="auto",res.end="auto",
#' residence events that is within the period of interest will be retained,
#' and `residences` will be recalculated, using specified `units`.
#' Default is `TRUE`.
#' @param progressbar option to display progress bar as function is run. Default
#' is TRUE.
#'
#' @return a dataframe in the same format as the input data, with residence
#' events limited to the period(s) of interest.
Expand All @@ -297,7 +299,7 @@ drift<-function(data,type,ID,station,res.start="auto",res.end="auto",
#' season.end="31-10")}
season<-function(data,type="mort",ID,station,res.start="auto",res.end="auto",
residences="auto",units="auto",season.start,
season.end,overlap=TRUE){
season.end,overlap=TRUE,progressbar=TRUE){

if (type %in% c("actel","vtrack")){
data<-extractres(data=data,type=type)
Expand Down Expand Up @@ -400,9 +402,13 @@ season<-function(data,type="mort",ID,station,res.start="auto",res.end="auto",
data.season<-data[0,]

for (i in 1:length(season.start)){
print(paste("season/period",i,"of",length(season.start)))
if (progressbar==TRUE){
print(paste("season/period",i,"of",length(season.start)))
}
if (length(tag)>1){
pb<-txtProgressBar(1,length(tag),style=3)
if (progressbar==TRUE){
pb<-txtProgressBar(1,length(tag),style=3)
}
}
for (j in 1:length(tag)){
data.temp<-data[0,]
Expand Down Expand Up @@ -444,7 +450,9 @@ season<-function(data,type="mort",ID,station,res.start="auto",res.end="auto",
}
data.season<-rbind(data.season,data.temp)
if (length(tag)>1){
setTxtProgressBar(pb,j)
if (progressbar==TRUE){
setTxtProgressBar(pb,j)
}
}
}
}
Expand Down
41 changes: 35 additions & 6 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#' `facet.by="year"`.
#' @param facet.by option to facet by "season" (as defined with `season.start`
#' and `season.end`) or "year". Default is "season".
#' @param progressbar option to display progress bar as function is run. Default
#' is TRUE.
#'
#' @return a ggplot2 plot. Additional arguments (e.g., formatting axes,
#' legend, aes, manual colour scales) can be added as for any ggplot2 plot.
Expand All @@ -60,7 +62,8 @@ mortsplot<-function(data,type,ID,station,res.start="auto",res.end="auto",
morts=NULL,singles=TRUE,interactive=FALSE,residences=NULL,
units=NULL,
season.start=NULL,season.end=NULL,facet=FALSE,
facet.axis="x",facet.by="season"){
facet.axis="x",facet.by="season",
progressbar=TRUE){

if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("Package \"ggplot2\" must be installed to use this function.",
Expand Down Expand Up @@ -134,8 +137,26 @@ mortsplot<-function(data,type,ID,station,res.start="auto",res.end="auto",
}
}

if (is(data[[station]],"list")){
data$station2<-as.character(NA)
for (i in 1:nrow(data)){
if (length(data[[station]][[i]])==1){
data$station2[i]<-data[[station]][[i]][1]
}
else {
stn.temp<-unique(data[[station]][[i]])
stn.temp<-stn.temp[order(stn.temp)]
stn.temp<-paste("Drift",paste(stn.temp,collapse=" "))
stn.temp<-gsub(" ","-",stn.temp)
data$station2[i]<-stn.temp
}
}
station<-"station2"
}

if (!is.null(season.start)|
!is.null(season.end)){
!is.null(season.end)|
facet.by=="year"){
if (is.null(residences)){
residences<-autofield(type=type,field="residences",data=data)
}
Expand All @@ -148,10 +169,16 @@ mortsplot<-function(data,type,ID,station,res.start="auto",res.end="auto",
if (units=="auto"){
units<-autofield(type=type,field="units",data=data)
}
print("Extracting data from the period/season(s) of interest")
if (progressbar==TRUE){
print("Extracting data from the period/season(s) of interest")
}
if (is.null(season.start)&is.null(season.end)&facet.by=="year"){
season.start<-"01-01"
season.end<-"31-12"
}
data<-season(data=data,type=type,ID=ID,station=station,res.start=res.start,res.end=res.end,
residences=residences,units=units,season.start=season.start,
season.end=season.end,overlap=FALSE)
season.end=season.end,overlap=FALSE,progressbar=FALSE)
data<-data[data[[station]]!="Break",]
if (!is(season.start,"POSIXt")){
try(season.start<-as.POSIXct(season.start,tz="UTC"),silent=TRUE)
Expand Down Expand Up @@ -349,11 +376,13 @@ mortsplot<-function(data,type,ID,station,res.start="auto",res.end="auto",
if (facet==TRUE){
if (!is.null(data$Year)&facet.axis=="y"){
plot<-plot+
ggplot2::facet_grid(Year~.,scales="free",space="free_y")
ggplot2::facet_grid(Year~.,scales="free",space="free_y")+
scale_x_datetime(date_labels = "%b %d")
}
else if (!is.null(data$Year)&facet.axis=="x"){
plot<-plot+
ggplot2::facet_grid(.~Year,scales="free",space="free_x")
ggplot2::facet_grid(.~Year,scales="free",space="free_x")+
scale_x_datetime(date_labels = "%b %d")
}
else if (!is.null(data$Season)){
plot<-plot+
Expand Down
6 changes: 4 additions & 2 deletions R/review.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ review<-function(morts,new.data,old.data=NULL,type,ID,station,res.start="auto",
stop("ID and station must be specified (i.e., cannot be 'auto') for format='mort'")
}

if (!(type %in% c("manual","actel"))&units!="auto"){
unitcheck(type=type,units=units,data=data)
if (!is.null(ddd)){
if (!(type %in% c("manual","actel"))&units!="auto"){
unitcheck(type=type,units=units,data=data)
}
}

# Fill in auto fields
Expand Down
12 changes: 9 additions & 3 deletions R/stationchange.R
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ resmax<-function(data,ID,station,res.start,
#' @param units units of the duration of the residence events in `data`.
#' @param stnchange a dataframe with the start time and location of the most
#' recent station or location change. Must use the same column names as `data`.
#' @param progressbar option to display progress bar as function is run. Default
#' is TRUE.
#'
#' @return a dataframe with the cumulative residence information for each
#' period where an animal was consecutively detected at a single station/location.
Expand All @@ -300,10 +302,12 @@ resmax<-function(data,ID,station,res.start,
#' res.start="StartUTC",res.end="EndUTC",residences="ResidencesLength.days",
#' units="days",stnchange=station.change)}
resmaxcml<-function(data,ID,station,res.start,res.end,
residences,units,stnchange){
residences,units,stnchange,progressbar=TRUE){
res.maxcml<-data[0,]

pb<-txtProgressBar(1,nrow(stnchange),style=3)
if (progressbar==TRUE){
pb<-txtProgressBar(1,nrow(stnchange),style=3)
}
for (i in 1:nrow(stnchange)){
# Subset residences for ID i, where res.start < res.start of stnchange
res.temp<-data[data[[ID]]==stnchange[[ID]][i]&
Expand Down Expand Up @@ -388,7 +392,9 @@ resmaxcml<-function(data,ID,station,res.start,res.end,
else {break}
}
}
setTxtProgressBar(pb,i)
if (progressbar==TRUE){
setTxtProgressBar(pb,i)
}
}

res.maxcml[[residences]]<-difftime(res.maxcml[[res.end]],
Expand Down
26 changes: 0 additions & 26 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ devtools::install_github("rosieluain/mort")
### Package contents
Please see the package vignettes for more details, as well as guidelines and tips for the following functions.

**Note:** vignettes are under development! We are currently developing a sample dataset, so there are some obvious gaps where there should be figures, tables, etc. These will be completed as soon as the sample dataset is done.

#### Data preparation and visualization

`residences` condenses detection records into residence events, with a start time, end time, and duration. Residence events are used as the input for all other mort functions.
Expand Down Expand Up @@ -71,27 +69,3 @@ These are functions that may be called by `morts` and/or `infrequent`, depending
### Disclaimer
mort is brand new. Although it is has been tested extensively on a complex dataset, we expect that issues will arise as mort is applied to other datasets and systems. If you run into any issues or have any suggestions for improvements, please post an issue, and we'll see what we can do!

<!-- ## Example -->

<!-- This is a basic example which shows you how to solve a common problem: -->

<!-- ```{r example} -->
<!-- library(mort) -->
<!-- ## basic example code -->
<!-- ``` -->

<!-- What is special about using `README.Rmd` instead of just `README.md`? You can include R chunks like so: -->

<!-- ```{r cars} -->
<!-- summary(cars) -->
<!-- ``` -->

<!-- You'll still need to render `README.Rmd` regularly, to keep `README.md` up-to-date. `devtools::build_readme()` is handy for this. You could also use GitHub Actions to re-render `README.Rmd` every time you push. An example workflow can be found here: <https://github.com/r-lib/actions/tree/v1/examples>. -->

<!-- You can also embed plots, for example: -->

<!-- ```{r pressure, echo = FALSE} -->
<!-- plot(pressure) -->
<!-- ``` -->

<!-- In that case, don't forget to commit and push the resulting figure files, so they display on GitHub and CRAN. -->
22 changes: 0 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ devtools::install_github("rosieluain/mort")
Please see the package vignettes for more details, as well as guidelines
and tips for the following functions.

**Note:** vignettes are under development! We are currently developing a
sample dataset, so there are some obvious gaps where there should be
figures, tables, etc. These will be completed as soon as the sample
dataset is done.

#### Data preparation and visualization

`residences` condenses detection records into residence events, with a
Expand Down Expand Up @@ -108,20 +103,3 @@ complex dataset, we expect that issues will arise as mort is applied to
other datasets and systems. If you run into any issues or have any
suggestions for improvements, please post an issue, and we’ll see what
we can do!

<!-- ## Example -->
<!-- This is a basic example which shows you how to solve a common problem: -->
<!-- ```{r example} -->
<!-- library(mort) -->
<!-- ## basic example code -->
<!-- ``` -->
<!-- What is special about using `README.Rmd` instead of just `README.md`? You can include R chunks like so: -->
<!-- ```{r cars} -->
<!-- summary(cars) -->
<!-- ``` -->
<!-- You'll still need to render `README.Rmd` regularly, to keep `README.md` up-to-date. `devtools::build_readme()` is handy for this. You could also use GitHub Actions to re-render `README.Rmd` every time you push. An example workflow can be found here: <https://github.com/r-lib/actions/tree/v1/examples>. -->
<!-- You can also embed plots, for example: -->
<!-- ```{r pressure, echo = FALSE} -->
<!-- plot(pressure) -->
<!-- ``` -->
<!-- In that case, don't forget to commit and push the resulting figure files, so they display on GitHub and CRAN. -->
Binary file added data/new.data.rda
Binary file not shown.
Binary file added docs/interactive_plot_ex.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2f7cd00

Please sign in to comment.