Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
rosieluain committed Jun 13, 2023
1 parent b130897 commit 55aab31
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 97 deletions.
142 changes: 73 additions & 69 deletions R/mort.R
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,12 @@ infrequent<-function(data,type="mort",ID,station,res.start="auto",
if (any(data[[station]]=="Break")){
warning("Either a station name was 'Break' or data included seasonal
breaks. Breaks were removed.")
k<-which(data[[station]]=="Break")
if (length(k)>0){
data<-data[-k,]
}
}

k<-which(data[[station]]=="Break")
if (length(k)>0){
data<-data[-k,]
}
if (!is.null(ddd)){
if (is.null(drift.cutoff)){
print("Applying drift")
Expand Down Expand Up @@ -684,31 +684,74 @@ infrequent<-function(data,type="mort",ID,station,res.start="auto",
}
}
else {
res.temp.drift<-drift(data=res.temp[j:nrow(res.temp),],
type=type,ID=ID,station=station,
res.start=res.start,res.end=res.end,
residences=residences,units=units,ddd=ddd,
from.station=from.station,to.station=to.station,
progress.bar=FALSE)
if (nrow(res.temp.drift)>1){
# Then run backwards
rb<-backwards(data=res.temp.drift,morts=res.temp.drift[nrow(res.temp.drift),],
ID=ID,station=station,res.start=res.start)
k<-which(res.temp.drift[[res.start]]==rb[[res.start]])
if (length(k)>1){
k<-which(res.temp.drift[[res.end]][k]==max(res.temp.drift[[res.end]][k]))
if (nrow(res.temp)>0&
sum(res.temp[[residences]])<threshold){
res.temp.drift<-drift(data=res.temp[j:nrow(res.temp),],
type=type,ID=ID,station=station,
res.start=res.start,res.end=res.end,
residences=residences,units=units,ddd=ddd,
from.station=from.station,to.station=to.station,
progress.bar=FALSE)
if (nrow(res.temp.drift)>1){
# Then run backwards
rb<-backwards(data=res.temp.drift,morts=res.temp.drift[nrow(res.temp.drift),],
ID=ID,station=station,res.start=res.start)
k<-which(res.temp.drift[[res.start]]==rb[[res.start]])
if (length(k)>1){
k<-which(res.temp.drift[[res.end]][k]==max(res.temp.drift[[res.end]][k]))
}
if (k==1){
if (backwards==TRUE&j>1){
# Run drift on whole dataset
res.temp.drift.full<-drift(data=res.temp,type=type,ID=ID,station=station,
res.start=res.start,res.end=res.end,
residences=residences,units=units,ddd=ddd,
from.station=from.station,to.station=to.station,
progress.bar=FALSE)
# Run backwards on dataset, starting with j
morts.temp<-backwards(data=res.temp.drift.full,
morts=res.temp[j,],
ID=ID,station=station,res.start=res.start)
if (tag[i] %in% inf.morts[[ID]]){
m<-which(inf.morts[[ID]]==tag[i])
if (morts.temp[[res.start]]<inf.morts[[res.start]][m]){
inf.morts[m,]<-morts.temp[1,]
if (!is.null(morts.prev)&backwards==TRUE){
new.morts<-c(new.morts,m)
}
}
}
else {
inf.morts[nrow(inf.morts)+1,]<-morts.temp
}
}
else {
if (tag[i] %in% inf.morts[[ID]]){
m<-which(inf.morts[[ID]]==tag[i])
if (res.temp.drift[[res.start]][1]<inf.morts[[res.start]][m]){
inf.morts[m,]<-res.temp.drift[1,]
if (!is.null(morts.prev)&backwards==TRUE){
new.morts<-c(new.morts,m)
}
}
}
else {
inf.morts[nrow(inf.morts)+1,]<-res.temp.drift[1,]
}
}
}
}
if (k==1){
else if (nrow(res.temp.drift)==1){
if (backwards==TRUE&j>1){
# Run drift on whole dataset
res.temp.drift.full<-drift(data=res.temp,ID=ID,station=station,
res.temp<-drift(data=res.temp,type=type,ID=ID,station=station,
res.start=res.start,res.end=res.end,
residences=residences,units=units,ddd=ddd,
from.station=from.station,to.station=to.station,
progress.bar=FALSE)
# Run backwards on dataset, starting with j
morts.temp<-backwards(data=res.temp.drift.full,
morts=res.temp[j,],
# Run backwards
morts.temp<-backwards(data=res.temp,
morts=res.temp[nrow(res.temp),],
ID=ID,station=station,res.start=res.start)
if (tag[i] %in% inf.morts[[ID]]){
m<-which(inf.morts[[ID]]==tag[i])
Expand All @@ -726,10 +769,12 @@ infrequent<-function(data,type="mort",ID,station,res.start="auto",
else {
if (tag[i] %in% inf.morts[[ID]]){
m<-which(inf.morts[[ID]]==tag[i])
if (res.temp.drift[[res.start]][1]<inf.morts[[res.start]][m]){
inf.morts[m,]<-res.temp.drift[1,]
if (!is.null(morts.prev)&backwards==TRUE){
new.morts<-c(new.morts,m)
if (length(m)>0){
if (res.temp.drift[[res.start]][1]<inf.morts[[res.start]][m]){
inf.morts[m,]<-res.temp.drift[1,]
if (!is.null(morts.prev)&backwards==TRUE){
new.morts<-c(new.morts,m)
}
}
}
}
Expand All @@ -739,48 +784,6 @@ infrequent<-function(data,type="mort",ID,station,res.start="auto",
}
}
}
else if (nrow(res.temp.drift)==1){
if (backwards==TRUE&j>1){
# Run drift on whole dataset
res.temp<-drift(data=res.temp,type=type,ID=ID,station=station,
res.start=res.start,res.end=res.end,
residences=residences,units=units,ddd=ddd,
from.station=from.station,to.station=to.station,
progress.bar=FALSE)
# Run backwards
morts.temp<-backwards(data=res.temp,
morts=res.temp[nrow(res.temp),],
ID=ID,station=station,res.start=res.start)
if (tag[i] %in% inf.morts[[ID]]){
m<-which(inf.morts[[ID]]==tag[i])
if (morts.temp[[res.start]]<inf.morts[[res.start]][m]){
inf.morts[m,]<-morts.temp[1,]
if (!is.null(morts.prev)&backwards==TRUE){
new.morts<-c(new.morts,m)
}
}
}
else {
inf.morts[nrow(inf.morts)+1,]<-morts.temp
}
}
else {
if (tag[i] %in% inf.morts[[ID]]){
m<-which(inf.morts[[ID]]==tag[i])
if (length(m)>0){
if (res.temp.drift[[res.start]][1]<inf.morts[[res.start]][m]){
inf.morts[m,]<-res.temp.drift[1,]
if (!is.null(morts.prev)&backwards==TRUE){
new.morts<-c(new.morts,m)
}
}
}
}
else {
inf.morts[nrow(inf.morts)+1,]<-res.temp.drift[1,]
}
}
}
}
}
setTxtProgressBar(pb,i)
Expand Down Expand Up @@ -832,7 +835,8 @@ infrequent<-function(data,type="mort",ID,station,res.start="auto",
}
}
else {
if (nrow(res.temp)>0){
if (nrow(res.temp)>0&
sum(res.temp[[residences]])<threshold){
res.temp.drift<-drift(data=res.temp,type=type,ID=ID,station=station,
res.start=res.start,res.end=res.end,
residences=residences,units=units,ddd=ddd,
Expand Down
8 changes: 4 additions & 4 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ drift<-function(data,type,ID,station,res.start="auto",res.end="auto",
res.end<-autofield(type=type,field="res.end")
}
if (residences=="auto"){
residences<-autofield(type=type,field="residences")
residences<-autofield(type=type,field="residences",data=data)
}
if (units=="auto"){
units<-autofield(type=type,field="units")
units<-autofield(type=type,field="units",data=data)
}

# Could make this check be a function
Expand Down Expand Up @@ -331,10 +331,10 @@ season<-function(data,type="mort",ID,station,res.start="auto",res.end="auto",
res.end<-autofield(type=type,field="res.end")
}
if (residences=="auto"){
residences<-autofield(type=type,field="residences")
residences<-autofield(type=type,field="residences",data=data)
}
if (units=="auto"){
units<-autofield(type=type,field="units")
units<-autofield(type=type,field="units",data=data)
}

if (!is(data[[res.start]],"POSIXt")){
Expand Down
37 changes: 15 additions & 22 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,16 @@ mortsplot<-function(data,type,ID,station,res.start="auto",res.end="auto",

if (!is.null(season.start)|
!is.null(season.end)){
if (is.null(residences)|residences=="auto"){
if (is.null(residences)){
residences<-autofield(type=type,field="residences",data=data)
}
if (is.null(units)|units=="auto"){
if (residences=="auto"){
residences<-autofield(type=type,field="residences",data=data)
}
if (is.null(units)){
units<-autofield(type=type,field="units",data=data)
}
if (units=="auto"){
units<-autofield(type=type,field="units",data=data)
}
print("Extracting data from the period/season(s) of interest")
Expand Down Expand Up @@ -362,12 +368,13 @@ mortsplot<-function(data,type,ID,station,res.start="auto",res.end="auto",
}

if (interactive==TRUE){
plot<-plot+ggplot2::aes(text=paste("ID:",.data[[ID]],
"</br></br>Start:",ResStart,
"</br>End:",ResEnd,
"</br>Duration:",.data[[residences]],
"</br>Station:",.data[[station]]))
plot.int<-plotly::ggplotly(plot,tooltip="text")
# plot<-plot+ggplot2::aes(text=paste("ID:",.data[[ID]],
# "</br></br>Start:",ResStart,
# "</br>End:",ResEnd,
# "</br>Duration:",.data[[residences]],
# "</br>Station:",.data[[station]]))
# plot.int<-plotly::ggplotly(plot,tooltip="text")
plot.int<-ggplotly(plot)
plot.int
}
else {
Expand All @@ -376,17 +383,3 @@ mortsplot<-function(data,type,ID,station,res.start="auto",res.end="auto",

}

# For season, it should depend on if the seasons are portions of the same year
# Or different years



# And add in a facet (as an add-in)








2 changes: 1 addition & 1 deletion R/stationchange.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ stationchange<-function(data,type="mort",ID,station,res.start="auto",
#' @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 drift indicates if drift residence events should be included in
#' determining the the maximum residence duration
#' determining the maximum residence duration
#'
#' @return a dataframe with the residence information for the longest residence
#' for each tag ID that occurred before the most recent station/location change.
Expand Down
4 changes: 3 additions & 1 deletion vignettes/morts.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,12 @@ prev_ex<-morts(data=data,type="mort",ID="ID",station="Station.Name",morts.prev=r
```

### Look backwards
For both `morts()` and `infrequent()`, there is the option to look backwards (i.e., earlier) in the dataset. If the most recent station change occurred before the flagged mortality (i.e., the animal was detected earlier than the flagged mortality, at the same station, and with no detections elsewhere), the start date and time of the flagged mortality are shifted earlier. The default is `backwards=FALSE`; however, it is more conservative to set `backwards=TRUE`.
For both `morts()` and `infrequent()`, there is the option to look backwards (i.e., earlier) in the dataset. If the most recent station change occurred before the flagged mortality (i.e., the animal was detected earlier than the flagged mortality, at the same station, and with no detections elsewhere), the start dates and times of the flagged mortalities are shifted earlier. The default is `backwards=FALSE`; however, it is more conservative to set `backwards=TRUE`.

Note, when `method="cumulative"` and there is no seasonality, cumulative residence events will cover all consecutive detections at a given station, and `backwards` is unnecessary.

<!-- Should give example -->

### Drift and season
In some systems, an expelled tag or a tag from a dead animal may seem to move, due to currents or tides, or if the tag is located within range of two overlapping receivers. Both `morts()` and `infrequent()` include an option to specify stations or locations where drift may occur and to consider drift in identifying mortalities. For more information, see the "Drift" vignette.

Expand Down

0 comments on commit 55aab31

Please sign in to comment.