#************************************************************************************************ #Create capture histories from Manomet data #The code contained in this script produces capture histories for analysis with both Cormack-Jolly-Seber and Multi-state mark-recapture models. The first section takes input data and formats it so that it can be easily converted into matrices. The input data contains a unique record for each bird ,with columns for band number, date, time of capture, and weight. The second portion takes this formatted data and produces CJS capture histories. #Code written by N.N.Dorian #Last revised: January 22nd, 2019 #************************************************************************************************ ################################## #####Format data from Manomet##### ################################## rm(list = ls()) #clear environment library(lubridate) #load this package to make working with dates easier setwd() #change this directory to reflect the location of your dataset #pick which species you want to create matrix for #you will likely have to change the code in this section to match your column names df <-read.csv("VEER.csv") #format csv to analyze df$Date<-as.Date(df$Date, format = "%m/%d/%Y") #format dates df$Time<-hm(df$Time) #extract time of captures df$Year<-year(df$Date) #extract Year df$DOY<-yday(df$Date)#create new column for day of year df$Hour<-hour(df$Time)#create new column for hour of day df$Weight<-as.numeric(as.character(df$Weight))#make weight continuous #check to make sure none of the bands are weird #if they are, use the commented-out line zeros<-which(df$Band==0) #df<-df[-c(zeros),] #create unique identifiers that account for individuals caught across multiple years df$yearband<- paste0(df$Band, df$Year)#new column band-yearID df$dyband <-paste0(df$Band,df$Year, df$DOY)#new column with band-day-yearID #subset data for recaptures capturefreq<-as.data.frame((table(df$yearband)))#freq of band captures recap<-as.vector(capturefreq$Var1[which(capturefreq$Freq >1)])#recap band nums dfrecap<- subset(df, yearband %in% recap)#data for recaptures from t0-t1 #dfrecap<-dfrecap[-c(which(table(dfrecap$yearband) ==1)),] #subset data for birds captured only once singlecap<-as.vector(capturefreq$Var1[which(capturefreq$Freq == 1)]) dfsinglecap<-subset(df, yearband %in% singlecap) #dataframe of birds captured more than once in a day multday<-as.data.frame(table(dfrecap$dyband)) multdaybands<-as.vector(multday$Var1[which(multday$Freq >1)]) dfmultday<-subset(dfrecap, dyband %in% multdaybands) #subset by banding season data.spring <-df[df$DOY >= 105 & df$DOY <= 166,] #pull out spring banded birds April 15 to Jun 15 data.fall <-df[df$DOY >= 227 & df$DOY <= 319,] #pull out fall banded birds, Aug 15 to Nov 15 ####DO UP TO HERE EACH TIME REGARDLESS OF METHOD###### ######################################################## #####Cormack-Jolly-Seber analysis capture histories##### ######################################################## #This function *makech_cjs* takes Manomet banding data and converts it into capture histories that can be analyzed using CJS models in a Bayesian context. It produces matrices for each time period and season (as specified) where each row corresponds to the sequence of captures (captured/non-captured = 1/0) over the course of each banding season. These capture histories need to be modified further in order to be analyzed using ProgramMARK. makech_cjs<-function(data, startdoy, enddoy, startyear, endyear){ doy<-seq(startdoy, enddoy, by =1)#sequence of dates when banding happened doysub<-data[data$DOY >=startdoy & data$DOY <= enddoy,] #subset data by day of year range years<-seq(startyear, endyear, by = 1) #sequence of years you are interested in yearsub<-doysub[doysub$Year >=startyear & doysub$Year <= endyear,] #subset range by years too yearsub$bandyearID <-paste0(yearsub$Band,yearsub$Year)#combine band and year into a single ID bands<-unique(yearsub$bandyearID)#pull out unique band nums bandyears<-substr(bands, nchar(bands)-3, nchar(bands)) #lets us use year as a covariate recaptures<-array(0, dim = c(length(bands), length(doy)+2)) #create blank matrix to populate with capture histories recaptures[,1]<-bands #fill first column with band numbers recaptures[,2]<-bandyears #fill second column with year colnames(recaptures)<-c("band", "year", doy) #name the columns #loop over all band numbers for(j in 1:length(bands)){ subset<-yearsub[yearsub$bandyearID == bands[j],]#subset data by band number position<-as.vector(match(subset$DOY,colnames(recaptures)[3:ncol(recaptures)])) #get the day of year when the bird was captured/recaptured recaptures[j,position] <- 1 #assign "1" to the matrix on those days } assign("matrix.surv",recaptures, envir = .GlobalEnv) } #Produce and export CJS capture histories for each season and period combination #SPRING mawach.spring.7015<-makech_cjs(df, 105, 166, 1970, 2015) write.csv(mawach.spring.7015, "veerch.spring.7015.csv") #FALL mawach.fall.7015<-makech_cjs(df, 227, 319, 1970, 2015) write.csv(mawach.fall.7015, "veerch.fall.7015.csv")