--- title: "JDS20112_Downey et al. 2021_Continuous duration results and conversion to second-by-second data" author: "Blair C. Downey" date: "4/28/2021" output: pdf_document: default html_document: default word_document: default --- ```{r setup, include=FALSE} knitr::opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE) ``` ## Validation of 1-0 and instantaneous sampling for quantifying oral behaviors in milk-fed dairy calves This file is #1 in a series. This file obtains true continuous durations of behaviors from BORIS aggregated events outputs, plots average bout length for each behavior from continuous data, expands true continuous durations (start/stop times reported) to second-by-second data, and creates a plot representing differences in continuous, instantaneous, and 1-0 sampling, as presented in the methods section of this paper. Please click the "show document outline" button if viewing in R in the top righthand corner; this will show the contents of the file so you can easily click and jump to specific sections of code For reference, the other RMarkdown files in the series cover the following: 2. "JDS20112_Downey et al. 2021_Converting second-by-second data to instantaneous sampling and regression.Rmd"; Scores expanded second-by-second continuous data with instantaneous sampling at 5, 10, 30, and 60-s intervals using 4 random start times for each interval; the results of these durations vs. continuous durations are then plotted 3. "JDS20112_Downey et al. 2021_Converting continuous point behaviors to 1-0 sampling.Rmd"; Formats 1-0 sampling from Animal Behaviour Pro app exports; we used the Focal Animal sampling scheme in the app paired with an external Pebble watch that continuously counted down 5s intervals. The exports that are loaded here initially resemble continuous point sampling, and the code breaks this into 5s bins to calculate 1-0 proportions. The 5s intervals are then extrapolated to obtain 1-0 sampling proportions at 10, 30, and 60-s intervals 4. "JDS20112_Downey et al. 2021_1-0 vs. continuous regression.Rmd"; Plots 1-0 sampling proportions against continuous values Please note that in these files, tongue flicks were originally scored with the name "tongue play." Sections of code that directly refer to raw data output thus say "tongue play;" this is the same as tongue flicks # Continuous data Load libraries ```{r,warning=FALSE,message=FALSE} library(readxl) library(dplyr) library(plyr) library(hms) library(ggplot2) library(doBy) library(car) library(egg) library(ggpubr) library(tidyr) ``` Load data from calves continuously scored in BORIS and save each sheet as a separate data frame. ```{r, tidy=TRUE} sheets <- readxl::excel_sheets("Downeyetal_2021_All calf continuous BORIS exports.xlsx") calf <- lapply(sheets, function(sheet) readxl::read_excel("Downeyetal_2021_All calf continuous BORIS exports.xlsx", sheet = sheet) ) names(calf) <- sheets calf3000<-calf$`3000` calf3005<-calf$`3005` calf3007<-calf$`3007` calf3008<-calf$`3008` calf3009<-calf$`3009` calf3010<-calf$`3010` calf3012<-calf$`3012` calf3013<-calf$`3013` calf3014<-calf$`3014` calf3015<-calf$`3015` calf3016<-calf$`3016` ``` #Continuous durations Combine all calves into one data frame, then cut the data frame to leave only the ID, behavior, and duration columns ```{r} allCalves<-rbind(calf3000,calf3005,calf3007,calf3008,calf3009,calf3010,calf3012,calf3013,calf3014,calf3015,calf3016) colnames(allCalves) bouts<-allCalves[c("Observation id","Behavior","Duration (s)")] names(bouts)[1:3]<-c("Calf","Behavior","Duration") head(bouts) ``` Separate bouts into categories. The following code places the durations into bins; e.g. if duration length is between 0-5s long, label a new column "Bin" with a 5 for those behaviors. This is required to create the average bout length figure. ```{r} bouts$Bin <- ifelse(bouts$Duration >= 0 & bouts$Duration < 5, 5, ifelse(bouts$Duration >=5 & bouts$Duration <10, 10, ifelse(bouts$Duration >=10 & bouts$Duration <15,15, ifelse(bouts$Duration >=15 & bouts$Duration <20, 20, ifelse(bouts$Duration >=20 & bouts$Duration <25, 25, ifelse(bouts$Duration >=25 & bouts$Duration <30, 30, ifelse(bouts$Duration >=30 & bouts$Duration <35, 35, ifelse(bouts$Duration >=35 & bouts$Duration <40, 40, ifelse(bouts$Duration >=40 & bouts$Duration <45, 45, ifelse(bouts$Duration >=45 & bouts$Duration <50, 50, ifelse(bouts$Duration >=50 & bouts$Duration <55, 55, ifelse(bouts$Duration >=55 & bouts$Duration <60, 60, ifelse(bouts$Duration >=60, 65, NA))))))))))))) ``` Summarize behaviors by calf; the "Duration.sum" column gives us the total amount of time performing the behavior, which was copied into the "Downeyetal_2021_Continuous proportions + SE.xlsx", "Downeyetal_2021_Instantaneous proportions + error calculations.xlsx", and "Downeyetal_2021_1-0 proportions + error calculations.xlsx" files. These durations were divided by the total video duration in seconds, found in "Downeyetal_2021_Start and stop for all continuous videos.xlsx" and copied into the instantaneous and 1-0 proportion Excel files, to obtain continuous proportions. The "Downeyetal_2021_Continuous proportions + SE.xlsx" also reports overall averages, SE, and average duration+SE for rare behaviors (drinking water, eating grain, ruminating), which weren't included in our analyses since all calves did not perform them. The latter 2 files are used in "JDS20112_Downey et al. 2021_Converting second-by-second data to instantaneous sampling and regression.Rmd" and "JDS20112_Downey et al. 2021_1-0 vs. continuous regression.Rmd" ```{r} calfSum<-summaryBy(Duration~Calf+Behavior,data=bouts, FUN=c(sum,mean,sd)) ``` #Bout length and plot Calculate average bout lengths for all behaviors: ```{r} grain<-subset(calfSum, Behavior == "eating") grainSum<-summaryBy(Duration.mean~Behavior, data=grain, FUN=c(length, mean, sd)) water<-subset(calfSum, Behavior == "drinking water") waterSum<-summaryBy(Duration.mean~Behavior, data=water, FUN=c(length, mean, sd)) rumin<-subset(calfSum, Behavior == "ruminating") ruminSum<-summaryBy(Duration.mean~Behavior, data=rumin, FUN=c(length, mean, sd)) sl<-subset(calfSum, Behavior == "sucking liquid") slSum<-summaryBy(Duration.mean~Behavior, data=sl, FUN=c(length, mean, sd)) groom<-subset(calfSum, Behavior == "grooming") groomSum<-summaryBy(Duration.mean~Behavior, data=groom, FUN=c(length, mean, sd)) tp<-subset(calfSum, Behavior == "tongue play") tpSum<-summaryBy(Duration.mean~Behavior, data=tp, FUN=c(length, mean, sd)) nnom<-subset(calfSum, Behavior == "other oral manipulation") nnomSUM<-summaryBy(Duration.mean~Behavior, data=nnom, FUN=c(length, mean, sd)) ``` Summarize data by bin for the behaviors all calves performed; calculating the % based on the count of bouts per bin / total bouts of behavior performed across all calves ```{r} boutSum<-summaryBy(Duration~Bin+Behavior,data=bouts, FUN=c(length,mean,sd)) tpBout<-subset(boutSum, Behavior == "tongue play") tpBout$Percentage<-(tpBout$Duration.length/sum(tpBout$Duration.length))*100 tpBout<-tpBout[c("Bin","Percentage")] x <- data.frame("Bin" = c(25,30,35,40,45,50,55,60,65), "Percentage" = 0) tpBout<-rbind(tpBout,x) tpBout <- tpBout[order(tpBout$Bin),] gBout<-subset(boutSum, Behavior == "grooming") gBout$Percentage<-(gBout$Duration.length/sum(gBout$Duration.length))*100 gBout<-gBout[c("Bin","Percentage")] x <- data.frame("Bin" = c(35,45,55,60,65), "Percentage" = 0) gBout<-rbind(gBout,x) gBout <- gBout[order(gBout$Bin),] omBout<-subset(boutSum, Behavior == "other oral manipulation") omBout$Percentage<-(omBout$Duration.length/sum(omBout$Duration.length))*100 omBout<-omBout[c("Bin","Percentage")] slBout<-subset(boutSum, Behavior == "sucking liquid") slBout$Percentage<-(slBout$Duration.length/sum(slBout$Duration.length))*100 slBout<-slBout[c("Bin","Percentage")] x <- data.frame("Bin" = c(10,15,20,25,30,40,45,50,55,60), "Percentage" = 0) slBout<-rbind(slBout,x) slBout <- slBout[order(slBout$Bin),] rBout<-subset(boutSum, Behavior == "ruminating") rBout$Percentage<-(rBout$Duration.length/sum(rBout$Duration.length))*100 rBout<-rBout[c("Bin","Percentage")] x <- data.frame("Bin" = c(15,30,35,40,45,50,55,60), "Percentage" = 0) rBout<-rbind(rBout,x) rBout <- rBout[order(rBout$Bin),] egBout<-subset(boutSum, Behavior == "eating") egBout$Percentage<-(egBout$Duration.length/sum(egBout$Duration.length))*100 egBout<-egBout[c("Bin","Percentage")] x <- data.frame("Bin" = c(55,60,65), "Percentage" = 0) egBout<-rbind(egBout,x) egBout <- egBout[order(egBout$Bin),] wBout<-subset(boutSum, Behavior == "drinking water") wBout$Percentage<-(wBout$Duration.length/sum(wBout$Duration.length))*100 wBout<-wBout[c("Bin","Percentage")] x <- data.frame("Bin" = c(30,35,40,45,50,55,60,65), "Percentage" = 0) wBout<-rbind(wBout,x) wBout <- wBout[order(wBout$Bin),] ``` Plot behaviors that were performed by all calves (sucking liquid, grooming, tongue flicks, other oral manipulation); change the margins to plot 2x2 and to have extra margin space for labels ```{r, echo=FALSE,warning=FALSE,message=FALSE} par(mfrow=c(2,2)) par(oma = c(4, 4, 0, 0)) par(mar = c(2, 2, 4, 1)) barplot(slBout$Percentage, xlab="", ylim=c(0,100), main="a) Sucking milk", adj=0, las=2,line=0.3, ylab="", xaxt="n") barplot(gBout$Percentage, xlab="", ylim=c(0,100), main="b) Grooming", adj=0, las=2,line=0.3, ylab="", xaxt="n") par(mar = c(4, 2, 1, 1)) #bottom, l, top, r barplot(tpBout$Percentage,xlab="", ylim=c(0,100), main="c) Tongue flicks", adj=0, las=2,line=0.3, ylab="", names.arg=c("<5 s","5 to <10 s","10 to <15 s","15 to <20 s","20 to <25 s", "25 to <30 s", "30 to <35 s", "35 to <40 s", "40 to <45 s", "45 to <50 s", "50 to <55 s", "55 to <60 s", ">60 s")) barplot(omBout$Percentage, names.arg=c("<5 s","5 to <10 s","10 to <15 s","15 to <20 s","20 to <25 s", "25 to <30 s", "30 to <35 s", "35 to <40 s", "40 to <45 s", "45 to <50 s", "50 to <55 s", "55 to <60 s", ">60 s"),xlab="", ylim=c(0,100), main="d) Other oral manipulation", adj=0,line=0.3,las=2, ylab="") mtext('Bout duration', side = 1, outer = TRUE, line = 2) #overall x axis mtext('Percentage of bouts', side = 2, outer = TRUE, line = 2) #overall y axis ``` Return margins and window to default: ```{r} par(mar=c(5.1, 4.1, 4.1, 2.1)) par(mfrow=c(1,1)) ``` ## Continuous data to second-by-second data Our data in this section uses the BORIS aggregated events output. This code will work for any data that has a start and stop time recorded. Going one calf at a time, we need to edit BORIS output, which lists start/stop times for all behaviors, so we have one row for every second from the start of the video until the end with all behaviors as columns. We'll put a 1 if the behavior occurred in that second and a 0 if not. ## >Calf 3000 Convert Start (s) and Stop (s) columns from BORIS, which represent the true continuous start and stop time of each behavior in seconds, to hms class. ```{r} calf3000$secStart<-hms(seconds=calf3000$`Start (s)`) calf3000$secStop<-hms(seconds=calf3000$`Stop (s)`) ``` Truncate seconds without rounding so we just have seconds without miliseconds, which BORIS automatically includes. For example, 00:03:04.92 is 3 minutes and 4.92 seconds, and we'll convert it to 00:03:04, or 203 seconds, instead. ```{r} calf3000$secStart <- trunc(calf3000$secStart*1)/1 calf3000$secStop <- trunc(calf3000$secStop*1)/1 head(calf3000) ``` Remove extraneous columns from the data frame ```{r} calf3000<-dplyr::select(calf3000, "Behavior","secStart","secStop") head(calf3000) ``` Calves should only be scored for 30 min before bottle delivery and 30 min after the bottle is finished and removed. First, create a data frame with all possible seconds based on the start and stop times for each calf in the "Downeyetal_2021_Start and stop times for all continuous videos.xlsx" file. For example, calf 3000's recording should start at 195s and end at 4079s (3885s total, since this R code counts the first and last second) ```{r} calf3000Times<-lubridate::seconds(seq(195,4079, 1)) ``` Now convert these values from seconds to hms class and convert to a data frame so it can merge/compare with our original calf3000 data frame. We'll rename the column to "times" as well. ```{r} calf3000Times<-hms(seconds=calf3000Times) calf3000Times<-as.data.frame(calf3000Times) head(calf3000Times) names(calf3000Times)[names(calf3000Times)=="calf3000Times"]<-"times" ``` Now we need to merge data frames to get second-by-second data from the original BORIS output for each behavior of interest. This function is matching time values for each behavior from the calf3000 data frame, which has start/stop times for each behavior from the original BORIS output, that fall between the range of start/stop values (i.e. the entire "calf3000Times" data frame), then adding a column to the calf3000Times data frame with 1s if the behavior was occurring at that second and 0 if not; still need to remove milliseconds because it seems to be rounding. For example, "eating" is the first behavior in the BORIS output and occurs from 203 - 207 seconds. After this function, the calf3000Times dataframe has a 1 in the new "eating" column from 3:23-3:27. ```{r} calf3000Times$eating<-ifelse(sapply(calf3000Times$times,function(p) any(calf3000$Behavior=='eating' & calf3000$secStart<=p &calf3000$secStop>=p)),1,0) ``` Repeat this function for the rest of the behaviors. ```{r} calf3000Times$otheroralmanipulation<-ifelse(sapply(calf3000Times$times,function(p) any(calf3000$Behavior=='other oral manipulation' & calf3000$secStart<=p &calf3000$secStop>=p)),1,0) calf3000Times$grooming<-ifelse(sapply(calf3000Times$times,function(p) any(calf3000$Behavior=='grooming' & calf3000$secStart<=p &calf3000$secStop>=p)),1,0) calf3000Times$tongueplay<-ifelse(sapply(calf3000Times$times,function(p) any(calf3000$Behavior=='tongue play' & calf3000$secStart<=p &calf3000$secStop>=p)),1,0) calf3000Times$sucking<-ifelse(sapply(calf3000Times$times,function(p) any(calf3000$Behavior=='sucking liquid' & calf3000$secStart<=p &calf3000$secStop>=p)),1,0) calf3000Times$rumination<-ifelse(sapply(calf3000Times$times,function(p) any(calf3000$Behavior=='ruminating' & calf3000$secStart<=p &calf3000$secStop>=p)),1,0) calf3000Times$water<-ifelse(sapply(calf3000Times$times,function(p) any(calf3000$Behavior=='drinking water' & calf3000$secStart<=p &calf3000$secStop>=p)),1,0) head(calf3000Times) ``` Now have a data frame of all seconds for calf 3000 for 30min before bottle delivery up to 30min after bottles were removed. This wide data frame has a 1 if the behavior occurred at that second and a 0 if it didn't. We'll repeat this process for the rest of the 10 calves, who all have different start and stop times due to variance in when bottles were delivered. ## >Calf 3005 This code is the exact same as for 3000 above, but the start and stop times are different (1622-5441s) ```{r} calf3005$secStart<-hms(seconds=calf3005$`Start (s)`) calf3005$secStop<-hms(seconds=calf3005$`Stop (s)`) calf3005$secStart <- trunc(calf3005$secStart*1)/1 calf3005$secStop <- trunc(calf3005$secStop*1)/1 head(calf3005) calf3005<-dplyr::select(calf3005, "Behavior","secStart","secStop") head(calf3005) calf3005Times<-lubridate::seconds(seq(1622,5441, 1)) calf3005Times<-hms(seconds=calf3005Times) calf3005Times<-as.data.frame(calf3005Times) head(calf3005Times) names(calf3005Times)[names(calf3005Times)=="calf3005Times"]<-"times" calf3005Times$eating<-ifelse(sapply(calf3005Times$times,function(p) any(calf3005$Behavior=='eating' & calf3005$secStart<=p &calf3005$secStop>=p)),1,0) calf3005Times$otheroralmanipulation<-ifelse(sapply(calf3005Times$times,function(p) any(calf3005$Behavior=='other oral manipulation' & calf3005$secStart<=p &calf3005$secStop>=p)),1,0) calf3005Times$grooming<-ifelse(sapply(calf3005Times$times,function(p) any(calf3005$Behavior=='grooming' & calf3005$secStart<=p &calf3005$secStop>=p)),1,0) calf3005Times$tongueplay<-ifelse(sapply(calf3005Times$times,function(p) any(calf3005$Behavior=='tongue play' & calf3005$secStart<=p &calf3005$secStop>=p)),1,0) calf3005Times$sucking<-ifelse(sapply(calf3005Times$times,function(p) any(calf3005$Behavior=='sucking liquid' & calf3005$secStart<=p &calf3005$secStop>=p)),1,0) calf3005Times$rumination<-ifelse(sapply(calf3005Times$times,function(p) any(calf3005$Behavior=='ruminating' & calf3005$secStart<=p &calf3005$secStop>=p)),1,0) calf3005Times$water<-ifelse(sapply(calf3005Times$times,function(p) any(calf3005$Behavior=='drinking water' & calf3005$secStart<=p &calf3005$secStop>=p)),1,0) head(calf3005Times) ``` ## >Calf 3007 ```{r} calf3007$secStart<-hms(seconds=calf3007$`Start (s)`) calf3007$secStop<-hms(seconds=calf3007$`Stop (s)`) calf3007$secStart <- trunc(calf3007$secStart*1)/1 calf3007$secStop <- trunc(calf3007$secStop*1)/1 head(calf3007) calf3007<-dplyr::select(calf3007, "Behavior","secStart","secStop") head(calf3007) calf3007Times<-lubridate::seconds(seq(1106,5035, 1)) calf3007Times<-hms(seconds=calf3007Times) calf3007Times<-as.data.frame(calf3007Times) head(calf3007Times) names(calf3007Times)[names(calf3007Times)=="calf3007Times"]<-"times" calf3007Times$eating<-ifelse(sapply(calf3007Times$times,function(p) any(calf3007$Behavior=='eating' & calf3007$secStart<=p &calf3007$secStop>=p)),1,0) calf3007Times$otheroralmanipulation<-ifelse(sapply(calf3007Times$times,function(p) any(calf3007$Behavior=='other oral manipulation' & calf3007$secStart<=p &calf3007$secStop>=p)),1,0) calf3007Times$grooming<-ifelse(sapply(calf3007Times$times,function(p) any(calf3007$Behavior=='grooming' & calf3007$secStart<=p &calf3007$secStop>=p)),1,0) calf3007Times$tongueplay<-ifelse(sapply(calf3007Times$times,function(p) any(calf3007$Behavior=='tongue play' & calf3007$secStart<=p &calf3007$secStop>=p)),1,0) calf3007Times$sucking<-ifelse(sapply(calf3007Times$times,function(p) any(calf3007$Behavior=='sucking liquid' & calf3007$secStart<=p &calf3007$secStop>=p)),1,0) calf3007Times$rumination<-ifelse(sapply(calf3007Times$times,function(p) any(calf3007$Behavior=='ruminating' & calf3007$secStart<=p &calf3007$secStop>=p)),1,0) calf3007Times$water<-ifelse(sapply(calf3007Times$times,function(p) any(calf3007$Behavior=='drinking water' & calf3007$secStart<=p &calf3007$secStop>=p)),1,0) head(calf3007Times) ``` ## >Calf 3008 ```{r} calf3008$secStart<-hms(seconds=calf3008$`Start (s)`) calf3008$secStop<-hms(seconds=calf3008$`Stop (s)`) calf3008$secStart <- trunc(calf3008$secStart*1)/1 calf3008$secStop <- trunc(calf3008$secStop*1)/1 head(calf3008) calf3008<-dplyr::select(calf3008, "Behavior","secStart","secStop") head(calf3008) calf3008Times<-lubridate::seconds(seq(858,4712, 1)) calf3008Times<-hms(seconds=calf3008Times) calf3008Times<-as.data.frame(calf3008Times) head(calf3008Times) names(calf3008Times)[names(calf3008Times)=="calf3008Times"]<-"times" calf3008Times$eating<-ifelse(sapply(calf3008Times$times,function(p) any(calf3008$Behavior=='eating' & calf3008$secStart<=p &calf3008$secStop>=p)),1,0) calf3008Times$otheroralmanipulation<-ifelse(sapply(calf3008Times$times,function(p) any(calf3008$Behavior=='other oral manipulation' & calf3008$secStart<=p &calf3008$secStop>=p)),1,0) calf3008Times$grooming<-ifelse(sapply(calf3008Times$times,function(p) any(calf3008$Behavior=='grooming' & calf3008$secStart<=p &calf3008$secStop>=p)),1,0) calf3008Times$tongueplay<-ifelse(sapply(calf3008Times$times,function(p) any(calf3008$Behavior=='tongue play' & calf3008$secStart<=p &calf3008$secStop>=p)),1,0) calf3008Times$sucking<-ifelse(sapply(calf3008Times$times,function(p) any(calf3008$Behavior=='sucking liquid' & calf3008$secStart<=p &calf3008$secStop>=p)),1,0) calf3008Times$rumination<-ifelse(sapply(calf3008Times$times,function(p) any(calf3008$Behavior=='ruminating' & calf3008$secStart<=p &calf3008$secStop>=p)),1,0) calf3008Times$water<-ifelse(sapply(calf3008Times$times,function(p) any(calf3008$Behavior=='drinking water' & calf3008$secStart<=p &calf3008$secStop>=p)),1,0) head(calf3008Times) ``` ## >Calf 3009 ```{r} calf3009$secStart<-hms(seconds=calf3009$`Start (s)`) calf3009$secStop<-hms(seconds=calf3009$`Stop (s)`) calf3009$secStart <- trunc(calf3009$secStart*1)/1 calf3009$secStop <- trunc(calf3009$secStop*1)/1 head(calf3009) calf3009<-dplyr::select(calf3009, "Behavior","secStart","secStop") head(calf3009) calf3009Times<-lubridate::seconds(seq(808,4622, 1)) calf3009Times<-hms(seconds=calf3009Times) calf3009Times<-as.data.frame(calf3009Times) head(calf3009Times) names(calf3009Times)[names(calf3009Times)=="calf3009Times"]<-"times" calf3009Times$eating<-ifelse(sapply(calf3009Times$times,function(p) any(calf3009$Behavior=='eating' & calf3009$secStart<=p &calf3009$secStop>=p)),1,0) calf3009Times$otheroralmanipulation<-ifelse(sapply(calf3009Times$times,function(p) any(calf3009$Behavior=='other oral manipulation' & calf3009$secStart<=p &calf3009$secStop>=p)),1,0) calf3009Times$grooming<-ifelse(sapply(calf3009Times$times,function(p) any(calf3009$Behavior=='grooming' & calf3009$secStart<=p &calf3009$secStop>=p)),1,0) calf3009Times$tongueplay<-ifelse(sapply(calf3009Times$times,function(p) any(calf3009$Behavior=='tongue play' & calf3009$secStart<=p &calf3009$secStop>=p)),1,0) calf3009Times$sucking<-ifelse(sapply(calf3009Times$times,function(p) any(calf3009$Behavior=='sucking liquid' & calf3009$secStart<=p &calf3009$secStop>=p)),1,0) calf3009Times$rumination<-ifelse(sapply(calf3009Times$times,function(p) any(calf3009$Behavior=='ruminating' & calf3009$secStart<=p &calf3009$secStop>=p)),1,0) calf3009Times$water<-ifelse(sapply(calf3009Times$times,function(p) any(calf3009$Behavior=='drinking water' & calf3009$secStart<=p &calf3009$secStop>=p)),1,0) head(calf3009Times) ``` ## >Calf 3010 ```{r} calf3010$secStart<-hms(seconds=calf3010$`Start (s)`) calf3010$secStop<-hms(seconds=calf3010$`Stop (s)`) calf3010$secStart <- trunc(calf3010$secStart*1)/1 calf3010$secStop <- trunc(calf3010$secStop*1)/1 head(calf3010) calf3010<-dplyr::select(calf3010, "Behavior","secStart","secStop") head(calf3010) calf3010Times<-lubridate::seconds(seq(1164,5203, 1)) calf3010Times<-hms(seconds=calf3010Times) calf3010Times<-as.data.frame(calf3010Times) head(calf3010Times) names(calf3010Times)[names(calf3010Times)=="calf3010Times"]<-"times" calf3010Times$eating<-ifelse(sapply(calf3010Times$times,function(p) any(calf3010$Behavior=='eating' & calf3010$secStart<=p &calf3010$secStop>=p)),1,0) calf3010Times$otheroralmanipulation<-ifelse(sapply(calf3010Times$times,function(p) any(calf3010$Behavior=='other oral manipulation' & calf3010$secStart<=p &calf3010$secStop>=p)),1,0) calf3010Times$grooming<-ifelse(sapply(calf3010Times$times,function(p) any(calf3010$Behavior=='grooming' & calf3010$secStart<=p &calf3010$secStop>=p)),1,0) calf3010Times$tongueplay<-ifelse(sapply(calf3010Times$times,function(p) any(calf3010$Behavior=='tongue play' & calf3010$secStart<=p &calf3010$secStop>=p)),1,0) calf3010Times$sucking<-ifelse(sapply(calf3010Times$times,function(p) any(calf3010$Behavior=='sucking liquid' & calf3010$secStart<=p &calf3010$secStop>=p)),1,0) calf3010Times$rumination<-ifelse(sapply(calf3010Times$times,function(p) any(calf3010$Behavior=='ruminating' & calf3010$secStart<=p &calf3010$secStop>=p)),1,0) calf3010Times$water<-ifelse(sapply(calf3010Times$times,function(p) any(calf3010$Behavior=='drinking water' & calf3010$secStart<=p &calf3010$secStop>=p)),1,0) head(calf3010Times) ``` ## >Calf 3012 ```{r} calf3012$secStart<-hms(seconds=calf3012$`Start (s)`) calf3012$secStop<-hms(seconds=calf3012$`Stop (s)`) calf3012$secStart <- trunc(calf3012$secStart*1)/1 calf3012$secStop <- trunc(calf3012$secStop*1)/1 head(calf3012) calf3012<-dplyr::select(calf3012, "Behavior","secStart","secStop") head(calf3012) calf3012Times<-lubridate::seconds(seq(710,4574, 1)) calf3012Times<-hms(seconds=calf3012Times) calf3012Times<-as.data.frame(calf3012Times) head(calf3012Times) names(calf3012Times)[names(calf3012Times)=="calf3012Times"]<-"times" calf3012Times$eating<-ifelse(sapply(calf3012Times$times,function(p) any(calf3012$Behavior=='eating' & calf3012$secStart<=p &calf3012$secStop>=p)),1,0) calf3012Times$otheroralmanipulation<-ifelse(sapply(calf3012Times$times,function(p) any(calf3012$Behavior=='other oral manipulation' & calf3012$secStart<=p &calf3012$secStop>=p)),1,0) calf3012Times$grooming<-ifelse(sapply(calf3012Times$times,function(p) any(calf3012$Behavior=='grooming' & calf3012$secStart<=p &calf3012$secStop>=p)),1,0) calf3012Times$tongueplay<-ifelse(sapply(calf3012Times$times,function(p) any(calf3012$Behavior=='tongue play' & calf3012$secStart<=p &calf3012$secStop>=p)),1,0) calf3012Times$sucking<-ifelse(sapply(calf3012Times$times,function(p) any(calf3012$Behavior=='sucking liquid' & calf3012$secStart<=p &calf3012$secStop>=p)),1,0) calf3012Times$rumination<-ifelse(sapply(calf3012Times$times,function(p) any(calf3012$Behavior=='ruminating' & calf3012$secStart<=p &calf3012$secStop>=p)),1,0) calf3012Times$water<-ifelse(sapply(calf3012Times$times,function(p) any(calf3012$Behavior=='drinking water' & calf3012$secStart<=p &calf3012$secStop>=p)),1,0) head(calf3012Times) ``` ## >Calf 3013 ```{r} calf3013$secStart<-hms(seconds=calf3013$`Start (s)`) calf3013$secStop<-hms(seconds=calf3013$`Stop (s)`) calf3013$secStart <- trunc(calf3013$secStart*1)/1 calf3013$secStop <- trunc(calf3013$secStop*1)/1 head(calf3013) calf3013<-dplyr::select(calf3013, "Behavior","secStart","secStop") head(calf3013) calf3013Times<-lubridate::seconds(seq(704,4503, 1)) calf3013Times<-hms(seconds=calf3013Times) calf3013Times<-as.data.frame(calf3013Times) head(calf3013Times) names(calf3013Times)[names(calf3013Times)=="calf3013Times"]<-"times" calf3013Times$eating<-ifelse(sapply(calf3013Times$times,function(p) any(calf3013$Behavior=='eating' & calf3013$secStart<=p &calf3013$secStop>=p)),1,0) calf3013Times$otheroralmanipulation<-ifelse(sapply(calf3013Times$times,function(p) any(calf3013$Behavior=='other oral manipulation' & calf3013$secStart<=p &calf3013$secStop>=p)),1,0) calf3013Times$grooming<-ifelse(sapply(calf3013Times$times,function(p) any(calf3013$Behavior=='grooming' & calf3013$secStart<=p &calf3013$secStop>=p)),1,0) calf3013Times$tongueplay<-ifelse(sapply(calf3013Times$times,function(p) any(calf3013$Behavior=='tongue play' & calf3013$secStart<=p &calf3013$secStop>=p)),1,0) calf3013Times$sucking<-ifelse(sapply(calf3013Times$times,function(p) any(calf3013$Behavior=='sucking liquid' & calf3013$secStart<=p &calf3013$secStop>=p)),1,0) calf3013Times$rumination<-ifelse(sapply(calf3013Times$times,function(p) any(calf3013$Behavior=='ruminating' & calf3013$secStart<=p &calf3013$secStop>=p)),1,0) calf3013Times$water<-ifelse(sapply(calf3013Times$times,function(p) any(calf3013$Behavior=='drinking water' & calf3013$secStart<=p &calf3013$secStop>=p)),1,0) head(calf3013Times) ``` ## >Calf 3014 ```{r} calf3014$secStart<-hms(seconds=calf3014$`Start (s)`) calf3014$secStop<-hms(seconds=calf3014$`Stop (s)`) calf3014$secStart <- trunc(calf3014$secStart*1)/1 calf3014$secStop <- trunc(calf3014$secStop*1)/1 head(calf3014) calf3014<-dplyr::select(calf3014, "Behavior","secStart","secStop") head(calf3014) calf3014Times<-lubridate::seconds(seq(1290,5209, 1)) calf3014Times<-hms(seconds=calf3014Times) calf3014Times<-as.data.frame(calf3014Times) head(calf3014Times) names(calf3014Times)[names(calf3014Times)=="calf3014Times"]<-"times" calf3014Times$eating<-ifelse(sapply(calf3014Times$times,function(p) any(calf3014$Behavior=='eating' & calf3014$secStart<=p &calf3014$secStop>=p)),1,0) calf3014Times$otheroralmanipulation<-ifelse(sapply(calf3014Times$times,function(p) any(calf3014$Behavior=='other oral manipulation' & calf3014$secStart<=p &calf3014$secStop>=p)),1,0) calf3014Times$grooming<-ifelse(sapply(calf3014Times$times,function(p) any(calf3014$Behavior=='grooming' & calf3014$secStart<=p &calf3014$secStop>=p)),1,0) calf3014Times$tongueplay<-ifelse(sapply(calf3014Times$times,function(p) any(calf3014$Behavior=='tongue play' & calf3014$secStart<=p &calf3014$secStop>=p)),1,0) calf3014Times$sucking<-ifelse(sapply(calf3014Times$times,function(p) any(calf3014$Behavior=='sucking liquid' & calf3014$secStart<=p &calf3014$secStop>=p)),1,0) calf3014Times$rumination<-ifelse(sapply(calf3014Times$times,function(p) any(calf3014$Behavior=='ruminating' & calf3014$secStart<=p &calf3014$secStop>=p)),1,0) calf3014Times$water<-ifelse(sapply(calf3014Times$times,function(p) any(calf3014$Behavior=='drinking water' & calf3014$secStart<=p &calf3014$secStop>=p)),1,0) head(calf3014Times) ``` ## >Calf 3015 ```{r} calf3015$secStart<-hms(seconds=calf3015$`Start (s)`) calf3015$secStop<-hms(seconds=calf3015$`Stop (s)`) calf3015$secStart <- trunc(calf3015$secStart*1)/1 calf3015$secStop <- trunc(calf3015$secStop*1)/1 head(calf3015) calf3015<-dplyr::select(calf3015, "Behavior","secStart","secStop") head(calf3015) calf3015Times<-lubridate::seconds(seq(411,4320, 1)) calf3015Times<-hms(seconds=calf3015Times) calf3015Times<-as.data.frame(calf3015Times) head(calf3015Times) names(calf3015Times)[names(calf3015Times)=="calf3015Times"]<-"times" calf3015Times$eating<-ifelse(sapply(calf3015Times$times,function(p) any(calf3015$Behavior=='eating' & calf3015$secStart<=p &calf3015$secStop>=p)),1,0) calf3015Times$otheroralmanipulation<-ifelse(sapply(calf3015Times$times,function(p) any(calf3015$Behavior=='other oral manipulation' & calf3015$secStart<=p &calf3015$secStop>=p)),1,0) calf3015Times$grooming<-ifelse(sapply(calf3015Times$times,function(p) any(calf3015$Behavior=='grooming' & calf3015$secStart<=p &calf3015$secStop>=p)),1,0) calf3015Times$tongueplay<-ifelse(sapply(calf3015Times$times,function(p) any(calf3015$Behavior=='tongue play' & calf3015$secStart<=p &calf3015$secStop>=p)),1,0) calf3015Times$sucking<-ifelse(sapply(calf3015Times$times,function(p) any(calf3015$Behavior=='sucking liquid' & calf3015$secStart<=p &calf3015$secStop>=p)),1,0) calf3015Times$rumination<-ifelse(sapply(calf3015Times$times,function(p) any(calf3015$Behavior=='ruminating' & calf3015$secStart<=p &calf3015$secStop>=p)),1,0) calf3015Times$water<-ifelse(sapply(calf3015Times$times,function(p) any(calf3015$Behavior=='drinking water' & calf3015$secStart<=p &calf3015$secStop>=p)),1,0) head(calf3015Times) ``` ## >Calf 3016 ```{r} calf3016$secStart<-hms(seconds=calf3016$`Start (s)`) calf3016$secStop<-hms(seconds=calf3016$`Stop (s)`) calf3016$secStart <- trunc(calf3016$secStart*1)/1 calf3016$secStop <- trunc(calf3016$secStop*1)/1 head(calf3016) calf3016<-dplyr::select(calf3016, "Behavior","secStart","secStop") head(calf3016) calf3016Times<-lubridate::seconds(seq(685,4604, 1)) calf3016Times<-hms(seconds=calf3016Times) calf3016Times<-as.data.frame(calf3016Times) head(calf3016Times) names(calf3016Times)[names(calf3016Times)=="calf3016Times"]<-"times" calf3016Times$eating<-ifelse(sapply(calf3016Times$times,function(p) any(calf3016$Behavior=='eating' & calf3016$secStart<=p &calf3016$secStop>=p)),1,0) calf3016Times$otheroralmanipulation<-ifelse(sapply(calf3016Times$times,function(p) any(calf3016$Behavior=='other oral manipulation' & calf3016$secStart<=p &calf3016$secStop>=p)),1,0) calf3016Times$grooming<-ifelse(sapply(calf3016Times$times,function(p) any(calf3016$Behavior=='grooming' & calf3016$secStart<=p &calf3016$secStop>=p)),1,0) calf3016Times$tongueplay<-ifelse(sapply(calf3016Times$times,function(p) any(calf3016$Behavior=='tongue play' & calf3016$secStart<=p &calf3016$secStop>=p)),1,0) calf3016Times$sucking<-ifelse(sapply(calf3016Times$times,function(p) any(calf3016$Behavior=='sucking liquid' & calf3016$secStart<=p &calf3016$secStop>=p)),1,0) calf3016Times$rumination<-ifelse(sapply(calf3016Times$times,function(p) any(calf3016$Behavior=='ruminating' & calf3016$secStart<=p &calf3016$secStop>=p)),1,0) calf3016Times$water<-ifelse(sapply(calf3016Times$times,function(p) any(calf3016$Behavior=='drinking water' & calf3016$secStart<=p &calf3016$secStop>=p)),1,0) head(calf3016Times) ``` This data can be exported into one Excel file with one calf/sheet; first labeling each individual df with the calf's ID, the stitching and exporting ```{r} calf3000Times$calf<-3000 calf3005Times$calf<-3005 calf3007Times$calf<-3007 calf3008Times$calf<-3008 calf3009Times$calf<-3009 calf3010Times$calf<-3010 calf3012Times$calf<-3012 calf3013Times$calf<-3013 calf3014Times$calf<-3014 calf3015Times$calf<-3015 calf3016Times$calf<-3016 allData<-rbind(calf3000Times,calf3005Times,calf3007Times,calf3008Times,calf3009Times,calf3010Times,calf3012Times,calf3013Times,calf3014Times,calf3015Times,calf3016Times) list_df<-split(allData, allData$calf) #to save each calf as a separate tab openxlsx::write.xlsx(list_df, file="Downeyetal_2021_Continuous BORIS data in second format.xlsx", row.names=F, append=T) ``` ## Creating plot to represent differences in continuous, instantaneous, and 1-0 sampling Cut a 5 minute sample of time from 34:30 - 39:30 for calf 3014. ```{r} sample <- calf3014Times %>% slice(781:1081) class(sample$times) ``` Convert "time" column to a time format that can be cut for 1-0 and instantaneous comparisons. strptime will convert this to a date/time variable and will automatically use the computer's "system date" ```{r} sample$times<-strptime(sample$times, format = '%H:%M:%S') class(sample$times) ``` Create the minimum and maximum times for this 5 minute subset; using "Sys.Date()" to apply the current computer date. This is necessary so the min/max times properly cut the sample$times column. Cut the sample data frame between the min and max times by 10 sec periods. ```{r} minTime <- paste(Sys.Date(), "00:34:30") maxTime <- paste(Sys.Date(), "00:39:40") sample$by10 <- cut(sample$times, seq(as.POSIXct(minTime),as.POSIXct(maxTime),by="10 sec")) ``` We only want to plot tongue flicks (recorded in the raw output as tongue play), so we'll cut the data frame. ```{r} tongueOnly<-dplyr::select(sample, "times","tongueplay","by10") ``` Convert continuous values to 1-0 based on 10 sec intervals; do this by aggregating and summarizing the # of 1s/0s that occur in each interval, then convert any #s >1 to 1. ```{r} onezero<-aggregate(tongueOnly$tongueplay,by=list(tongueOnly$by10),FUN=sum,drop=F) head(onezero) onezero$x[onezero$x>1] = 1 ``` Now convert the same section of continuous data to instantaneous. Take every 10th row from the tongueOnly data frame, which is equivalent to every 10th second ```{r} instantaneous <- tongueOnly[seq(1, nrow(tongueOnly), 10), ] head(instantaneous) ``` Now plot all 3 data frames (tongueOnly, onezero, and instantaneous). To do this, we'll convert any zeroes to NA and omit them ```{r} tongueOnly$tongueplay[tongueOnly$tongueplay==0]<-NA onezero$x[onezero$x==0]<-NA instantaneous$tongueplay[instantaneous$tongueplay==0]<-NA ``` Change 1s to different numbers so plot will be spaced out better; add values to 1-0 so points will be offset in the middle of the interval, rather than on the exact 10th second like instantaneous - this first requires changing onezero's times back to strptime format Format data to prepare to plot. Convert 1s to other numbers (in this case 0.55, 0.5, and 0.45) just to aid in spacing out on the plot. Add a new column for "type" labeling the sampling method. Reorder data frames if needed (e.g. for instantaneous) to make sure the columns are in the same order so they can be combined into one data frame. For onezero, add 5s to the times column so these points will fall in the middle of the 10s intervals once you plot ```{r} tongueOnly$tongueplay[tongueOnly$tongueplay == 1 ] = 0.55 head(tongueOnly) continuous<-tongueOnly[-c(3)] continuous$type<-"continuous" instantaneous$tongueplay[instantaneous$tongueplay == 1 ] = 0.5 head(instantaneous) instantaneous2<-instantaneous[,2:3] names(instantaneous2)[2]<-c("times") instantaneous2$type<-"instantaneous" instantaneous2 <- instantaneous2[c(2,1,3)] onezero$x[onezero$x == 1 ] = 0.45 onezero$Group.1 <- as.character(onezero$Group.1) onezero$Group.1<-strptime(onezero$Group.1, format = '%Y-%m-%d %H:%M:%S') names(onezero)[1:2]<-c("times","tongueplay") onezero$times<-onezero$times+5 onezero$type<-"1-0" ``` Combine the 3 data frames to plot ```{r} new<-rbind(continuous,instantaneous2,onezero) new$type<-factor(new$type) head(new) new$times<-as.POSIXct(new$times) ``` Plot with ggplot ```{r,echo=FALSE,warning=FALSE,message=FALSE} ggplot(new, aes(x=times, y=tongueplay, colour=type))+geom_point(size=4, aes(shape=type))+ scale_shape_manual(values=c(4,15,17))+ scale_color_manual(values=c("gray60","black","gray45"))+ theme_classic()+ theme(axis.text.y=element_blank(), axis.text.x=element_text(size=8,angle=90), axis.title=element_blank(), legend.title = element_blank(), legend.text=element_text(size = 15), legend.position = "top")+ scale_x_datetime(date_breaks = "10 sec", date_labels = "%M:%S")+ scale_y_continuous(limits=c(0.4,0.6,expand=c(0,2.5)))+ geom_vline(xintercept = new$times[c(1,11,21,31,41,51,61,71,81,91,101,111,121,131,141,151,161,171,181,191,201,211,221,231,241,251,261,271,281,291,301,311)], linetype="longdash",color="grey69", size=0.2) ```