Load libraries

library(tidyverse)
library(lubridate)
library(reshape2)

Import data

df<-read_csv("processed_data/nsd_db100_recurse.csv")

Extra data cleaning

df$year<-year(df$timestamp)
df$juliand<-yday(df$timestamp)
df<-df%>%filter(id!="3A  (Barb Wire)")
df<-df%>%filter(id!='0M (N of Mud Lake)')
df<-df%>%filter(!(id=="7E (Nora Township colt)"&year==2015&juliand==205))
df<-df%>%filter(!(id=="7E (Nora Township colt)"&year==2015&juliand==206)) 
df<-df%>%filter(!(id=="7E (Nora Township colt)"&year==2018&juliand==114))

Pull out the mean and median residence time by individual and year

df2<-df%>%
  group_by(id, year)%>%
  mutate(mean.dur=mean(res_time),
         med.dur=median(res_time))

Cluster-level bootstrap

set.seed(1234)
uid<-unique(df2$id)
ncranes<-length(uid)
res<-NULL
nboots<-1000
for(i in 1:nboots){
  # create bootstrap
  bootIDs <- data.frame(id = sample(x = uid, size = ncranes, replace = TRUE))
  bootDat <- left_join(bootIDs, df2)

  tmp<-bootDat%>%
    group_by(age_yr)%>%
    summarise(age.means=mean(mean.dur),
           age.meds=mean(med.dur))
  res<-rbind(res, tmp)
}

Summarize standard errors

std.err<-function(x){
  sd(x)/sqrt(sum(!is.na(x)))
}
bc<-res%>%
  group_by(age_yr)%>%
  summarize(pop.mean.dur=mean(age.means),
            pop.median.dur=median(age.meds),
            pop.mean.se=std.err(age.means),
            pop.median.se=std.err(age.meds))

Rearrange for ggplot

bc1<-melt(bc)
bc1$CI.lower<-NA
bc1$CI.upper<-NA


i<-NULL
for(i in 1:10){
  bc1[i,4]<-bc1[i,3]-1.96*bc1[i+10,3]
  bc1[i,5]<-bc1[i,3]+1.96*bc1[i+10,3]
}
bc1<-bc1[-(11:20),]

bc1<-bc1%>%
  mutate(age_yr=case_when(
    age_yr=='hatch-yr'~'Hatch-Year',
    age_yr=='second-yr'~'Second-Year',
    age_yr=='third-yr'~'Third-Year',
    age_yr=='fourth-yr'~'Fourth-Year',
    age_yr=='adult'~'Adult'
  ))
bc1$age_yr<-factor(bc1$age_yr, levels=c('Hatch-Year', 'Second-Year', 'Third-Year', 'Fourth-Year', 'Adult'))

# Just retain mean values
bc.means<-bc1[1:5,]

# Take out hatch-year data
bc.nohy<-bc.means[bc.means$age_yr!="Hatch-Year",]

Plot

ggplot(bc.nohy, aes(x=age_yr, y=value))+geom_point()+
  geom_errorbar(width=.1, aes(ymin=CI.lower, ymax=CI.upper))+
  ylab('Residence Time (in hours)\n')+xlab("\nAge Category")+
  theme_bw()

sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18362)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] reshape2_1.4.3  lubridate_1.7.4 forcats_0.4.0   stringr_1.4.0  
##  [5] dplyr_0.8.3     purrr_0.3.3     readr_1.3.1     tidyr_1.0.0    
##  [9] tibble_2.1.3    ggplot2_3.2.1   tidyverse_1.2.1
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.2       plyr_1.8.4       cellranger_1.1.0 pillar_1.4.2    
##  [5] compiler_3.6.0   tools_3.6.0      zeallot_0.1.0    digest_0.6.22   
##  [9] jsonlite_1.6     evaluate_0.14    lifecycle_0.1.0  nlme_3.1-139    
## [13] gtable_0.3.0     lattice_0.20-38  pkgconfig_2.0.3  rlang_0.4.0     
## [17] cli_1.1.0        rstudioapi_0.10  yaml_2.2.0       haven_2.1.1     
## [21] xfun_0.10        withr_2.1.2      xml2_1.2.2       httr_1.4.1      
## [25] knitr_1.25       hms_0.5.1        generics_0.0.2   vctrs_0.2.0     
## [29] grid_3.6.0       tidyselect_0.2.5 glue_1.3.1       R6_2.4.0        
## [33] readxl_1.3.1     rmarkdown_1.16   modelr_0.1.5     magrittr_1.5    
## [37] backports_1.1.5  scales_1.0.0     htmltools_0.4.0  rvest_0.3.4     
## [41] assertthat_0.2.1 colorspace_1.4-1 labeling_0.3     stringi_1.4.3   
## [45] lazyeval_0.2.2   munsell_0.5.0    broom_0.5.2      crayon_1.3.4