|
| 1 | +#------------------------------------------------------------------------------ |
| 2 | +# Data manipulation of banding data to create m-arrays for band recovery models |
| 3 | +#------------------------------------------------------------------------------ |
| 4 | +rm(list=ls()) |
| 5 | +# Read in dataset |
| 6 | +#raw.data<-read.csv(file.choose()) |
| 7 | +#raw<-read.csv("~/Google Drive/AMWO IPM/Datasets/M array table AMWO CSV.csv") |
| 8 | +raw<-read.csv("AMWO recoveries.csv") #reading in CSV from GitHub-linked timberdoodle folder |
| 9 | + |
| 10 | +######################################################################## |
| 11 | +#cleaning data |
| 12 | +######################################################################## |
| 13 | +#SS added following conditions to subset raw data: |
| 14 | + |
| 15 | +#only use status 3 birds |
| 16 | +raw<-subset(raw,Status==3) |
| 17 | + |
| 18 | +#only use how obtained category 1 (shot) |
| 19 | +raw<-subset(raw,How.Obt==1) |
| 20 | + |
| 21 | +#only use B.Year from 1963 onwards |
| 22 | +raw<-subset(raw,B.Year>=1963) |
| 23 | + |
| 24 | +#bring in B.month, convert to season |
| 25 | +clean<-matrix(NA,nrow=length(raw$B.Month),ncol=1) |
| 26 | +clean<-data.frame(clean) |
| 27 | +clean[raw$B.Month<=6,]<-1 #shouldn't we change this to between 4 and 6? |
| 28 | +clean[raw$B.Month>6,]<-2 #shouldn't we change this to between 7 and 9? |
| 29 | + |
| 30 | +#Bring in B.year |
| 31 | +clean[,2]<-raw$B.Year #but we only want to start with 1963! |
| 32 | + |
| 33 | +#bring in recovery year and account for recoveries occurring in Jan-March |
| 34 | +clean[,3]<-NA |
| 35 | +clean[raw$R.Month>=4,3]<-raw[raw$R.Month>=4,"R.Year"] |
| 36 | +#adjust if you don't want to include birds recovered in March |
| 37 | +clean[raw$R.Month<4,3]<-raw[raw$R.Month<4,"R.Year"]-1 |
| 38 | + |
| 39 | +#bring in B region |
| 40 | +clean[,4]<-0 |
| 41 | +clean[raw$B.Flyway==1,4]<-1 |
| 42 | +clean[raw$B.Flyway%in%2:3,4]<-2 |
| 43 | +clean[raw$B.Flyway==6&raw$BRegion..STA%in%c("QC","NS","NB","PE","NF","PQ"),4]<-1 |
| 44 | +clean[raw$B.Flyway==6&raw$BRegion..STA%in%c("ONT"),4]<-2 |
| 45 | + |
| 46 | +#bring in R region, this is only to exlude region crossers |
| 47 | +clean[,5]<-0 #specify different number from previous step to flag it in the next step |
| 48 | +clean[raw$B.Flyway==1,5]<-1 |
| 49 | +clean[raw$B.Flyway%in%2:3,5]<-2 |
| 50 | +clean[raw$B.Flyway==6&raw$BRegion..STA%in%c("QC","NS","NB","PE","NF","PQ"),5]<-1 |
| 51 | +clean[raw$B.Flyway==6&raw$BRegion..STA%in%c("ONT"),5]<-2 |
| 52 | + |
| 53 | +# pull out places you don't care about |
| 54 | +raw<-raw[clean$V4!=0|clean$V5!=0,] |
| 55 | +clean<-clean[clean$V4!=0|clean$V5!=0,] |
| 56 | +clean<-clean[,1:4] #remove R.state becuase it is redundant |
| 57 | + |
| 58 | +#bring in age |
| 59 | +# local = 1, hatch year = 2, adult = 3 |
| 60 | +clean[,5]<-NA |
| 61 | +clean[raw$Age..VAGE=="After Hatch Year",5]<-3 |
| 62 | +clean[raw$Age..VAGE=="After Second Year",5]<-3 |
| 63 | +clean[raw$Age..VAGE=="After Third Year",5]<-3 |
| 64 | +clean[raw$Age..VAGE=="Second Year",5]<-3 |
| 65 | +clean[raw$Age..VAGE=="Unknown",5]<-NA |
| 66 | +clean[raw$Age..VAGE=="Hatch Year",5]<-2 |
| 67 | +clean[raw$Age..VAGE=="Local",5]<-1 |
| 68 | +#remove unknowns |
| 69 | +raw<-raw[!is.na(clean[,5]),] |
| 70 | +clean<-clean[!is.na(clean[,5]),] |
| 71 | + |
| 72 | +# get rid of hatch years in months 5 and 6 |
| 73 | +clean <- clean[!(raw$Age..VAGE=="Hatch Year"&raw$B.Month%in%c(5:6)),] |
| 74 | +raw <- raw[!(raw$Age..VAGE=="Hatch Year"&raw$B.Month%in%c(5:6)),] |
| 75 | + |
| 76 | +#bring in sex and convert to age class |
| 77 | +# 1=local, 2=juv, 3=male, 4=female |
| 78 | +clean[,6]<-NA |
| 79 | +clean[clean[,5]%in%1:2,6]<-clean[clean[,5]%in%1:2,5] |
| 80 | +clean[raw$Sex..VSEX%in%c("Male","Male; from subsequent encounter")&clean[,5]==3,6]<-3 |
| 81 | +clean[raw$Sex..VSEX%in%c("Female","Female; from subsequent encounter")&clean[,5]==3,6]<-4 |
| 82 | + |
| 83 | +#remove unknown adults for now? Can treat unknowns via mixtures acc. to Todd |
| 84 | +raw<-raw[!(is.na(clean[,6])&clean[,5]==3),] |
| 85 | +clean<-clean[!(is.na(clean[,6])&clean[,5]==3),] |
| 86 | + |
| 87 | +# remove unwanted banding periods (Oct-Dec) for now |
| 88 | +clean<-clean[!raw$B.Month%in%c(10:12,1:3),] |
| 89 | +raw<-raw[!raw$B.Month%in%c(10:12,1:3),] |
| 90 | + |
| 91 | + |
| 92 | +clean[,7]<-1 |
| 93 | +colnames(clean)<-c("bSeason","bYear","rYear","region","age","class","dummy") |
| 94 | +######################################################################## |
| 95 | +#create the marray |
| 96 | +######################################################################## |
| 97 | +Year<-unique(clean$bYear) |
| 98 | +Year<-sort(Year) #SS added because needs to be chronological or else will have values below diagnol? |
| 99 | +NbYear<-length(Year) |
| 100 | +Season<-unique(clean$bSeason) |
| 101 | +NSeason<-length(Season) |
| 102 | +Class<-unique(clean$class) |
| 103 | +Class<-sort(Class) #SS sorted |
| 104 | +NClass<-length(Class) |
| 105 | +Region<-unique(clean$region) |
| 106 | +Region<-sort(Region) #SS sorted |
| 107 | +NRegion<-length(Region) |
| 108 | + |
| 109 | +awc<-array(NA,dim=c(NbYear,NbYear,NSeason,NClass,NRegion)) |
| 110 | +for (s in 1:NSeason){ |
| 111 | + for (cc in 1:NClass){ |
| 112 | + for (i in 1:NRegion){ |
| 113 | + for (b in 1:NbYear){ |
| 114 | + for (r in 1:NbYear){ |
| 115 | + awc[b,r,s,cc,i]<-sum(clean[clean$bYear==Year[b]&clean$rYear==Year[r]&clean$class==Class[cc]&clean$region==Region[i],7]) |
| 116 | + }}}}} |
| 117 | + |
| 118 | +#take a look at subset of giant marray |
| 119 | +awc[1:20,1:20,1,1,1] |
| 120 | + |
| 121 | +#are these the correct dimensions that we want?? or do we want to merge all age classes together into same marray? 2 separate |
| 122 | +#marrays for the 2 seasons, right? and regions, right? |
0 commit comments