Showing posts with label Timaliidae. Show all posts
Showing posts with label Timaliidae. Show all posts

Tuesday, July 15, 2014

This is from awhile ago--I found it in my drafts, but I just wanted to reopen this can of worms.
selecting priors:

1) select random node on the tree:
#randomly sample a node from a randomly trimmed tree
random.node<-sample(tree.trim$edge[1,], 1)
> random.node
[1] 75

2) open up mesquite and simulate random character in Brownian motion on the tree and look at before and after rates on node 75 in Mesquite using these instructions:
https://nunn.rc.fas.harvard.edu/groups/pica/revisions/7b932/24/
https://nunn.rc.fas.harvard.edu/groups/pica/wiki/f4cc5/43_Squared_Change_Parsimony.html

Take the rate at node 75 for sig1 1 prior: -0.86245
Pick random branch within this clade for sig2 prior (Pellorneum_pyrrogenys):1.1092
Alpha: random uniform distribution runif(1)
rand.shift = 0.05 --used what Revell, et al uses
use ngen=100,000
sample:100



Thursday, June 12, 2014

Downloading the files from CIPRES

View output for "babblers_8_newdate_#".

Download "bablers_8_5part_log" and "babblers_8_5part.trees" for all five runs.

All files are now being stored in "Timaliidae->Babblers_Adaptive Radiation->Final_BEAST
Only the first two runs converged well. Using the consensus tree from all five run yields and incorrect tree, especially species Dumetia. However, the branch length here virtually does not exist. Now I am going to make the consensus tree for the first two runs.

Dumetia is still wonky. Going to redo the partitions again...

Thursday, May 29, 2014

I am revisiting my babbler project.

Looking at the Tracer files again, I see that the "babblers_8_newdate" task provided the best convergence. I installed SumTrees to find the consensus tree.

#install Setuptools
wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
#enter password

#install SumTrees

sudo easy_install -U dendropy

Okay realized I have already made the consensus tree back in October!

See directory /Users/loloyohe/Documents/Timaliidae/Babblers Adaptive Radiation/trees/output_trees




Monday, September 2, 2013

babblersPCA.csv is the data to be used for analysis for publication

babblers.csv was the file used without any imputation or editing yet 

Friday, June 14, 2013

Finally have some meaningful(?) output. This is going to be lengthy but it will be really helpful in organizing my thought. Because I had been including morphological data for very "distantly-related babblers", all of the nodes for the rate shifts occurred, of course on the root node. I removed some more taxa to only include measurements for birds within the babbler family. Therefore, I reran the PCA. Still similar trends:

Count the number of species in my analysis and the number of specimens per species.
> babblers.species.count<-ddply(.data=new.babblers, .(Genus, Species), species.count=length(Wing.Length), summarize)
> babblers.species.count
             Genus         Species species.count
1       Actinodura        egertoni             6
2       Actinodura    morrisoniana             3
3       Actinodura      nipalensis             3
4       Actinodura         waldeni             5
5          Alcippe    brunneicauda             4
6          Alcippe          grotei            11
7          Alcippe      morrisonia             5
8          Alcippe      peracensis             2
9          Alcippe    poioicephala            11
10           Babax     lanceolatus             4
11           Cutia      nipalensis             7
12         Dumetia      hyperythra             6
13  Gampsorhynchus         rufulus             8
14        Garrulax         affinis            11
15        Garrulax     albogularis             5
16        Garrulax         canorus            14
17        Garrulax       chinensis            40
18        Garrulax      cineraceus             4
19        Garrulax erythrocephalus             1
20        Garrulax        formosus             4
21        Garrulax     leucolophus            41
22        Garrulax        lineatus             6
23        Garrulax           maesi             3
24        Garrulax       merulinus             1
25        Garrulax         milleti             1
26        Garrulax          milnei             4
27        Garrulax        mitratus             6
28        Garrulax       monileger             8
29        Garrulax        nuchalis             3
30        Garrulax       ocellatus             7
31        Garrulax       palliatus             4
32        Garrulax poecilorhynchus             4
33        Garrulax          sannio            12
34        Garrulax       squamatus             3
35        Garrulax        striatus             6
36        Garrulax     subunicolor             6
37        Garrulax      variegatum             3
38        Garrulax        virgatus             2
39    Heterophasia     auricularis             3
40    Heterophasia      capistrata             6
41    Heterophasia        gracilis             4
42    Heterophasia    maelanoleuca             9
43    Heterophasia       picaoides             7
44    Heterophasia       pulchella             3
45     Illadopsis       albipectus             4
46     Illadopsis         cleaveri             4
47     Illadopsis       fulvescens             8
48     Illadopsis           puveli             2
49     Illadopsis      pyrrhoptera             2
50     Illadopsis        rufescens             3
51     Illadopsis       rufipennis             6
52         Kenopia         striata             2
53       Leiothrix     argentauris            15
54       Leiothrix           lutea             4
55       Liocichla       phoenicea             1
56       Liocichla          steeri             3
57        Macronus         gularis            25
58        Macronus        ptilosus             3
59        Macronus     striaticeps            12
60    Malacocincla         abbotti             4
61    Malacocincla     cinereiceps             2
62    Malacocincla     malaccensis             6
63    Malacocincla        sepiaria             3
64    Malacopteron          affine             3
65    Malacopteron      albogulare             4
66    Malacopteron        cinereum             1
67    Malacopteron     magnirostre             7
68    Malacopteron          magnum             5
69    Malacopteron    palawanenese             3
70           Minla      ignotincta             6
71           Minla        strigula             7
72       Napothera    brevicaudata             4
73       Napothera          crassa             2
74       Napothera     crispifrons             7
75       Napothera     epilepidota            10
76      Pellorneum      albiventre             9
77      Pellorneum    capristratum            15
78      Pellorneum      pyrrogenys             6
79      Pellorneum        ruficeps             5
80      Pellorneum        tickelli             1
81     Phyllanthus      atripennis             6
82    Pomatorhinus    erythrogenys            18
83    Pomatorhinus    ferruginosus             4
84    Pomatorhinus      horsfieldi             6
85    Pomatorhinus      hypoleucos             4
86    Pomatorhinus        montanus             4
87    Pomatorhinus      ruficollis             9
88    Pomatorhinus        swinhoei             2
89     Ptilocichla         falcata             2
90     Ptilocichla   leucogrammica             3
91     Ptilocichla     mindanensis             3
92       Ptyrticus        turdinus             6
93    Rhopocichla         atriceps             4
94         Rimator    malacoptilus             3
95    Schoeniparus         brunnea            10
96    Schoeniparus     castaneceps             1
97    Schoeniparus         cinerea             3
98    Schoeniparus           dubia             6
99    Schoeniparus     rufogularis             9
100    Spelaeornis    chocolatinus             5
101    Spelaeornis   troglodytoide             3
102   Sphenocichla           humei             3
103      Stachyris         ambigua             4
104      Stachyris        chrysaea            11
105      Stachyris    erythroptera             8
106      Stachyris      grammiceps             2
107      Stachyris        leucotis             3
108      Stachyris        maculata             3
109      Stachyris    melanothorax             6
110      Stachyris       nigriceps             7
111      Stachyris     nigricollis             4
112      Stachyris           oglei             3
113      Stachyris    poliocephala             4
114      Stachyris        pyrrhops             3
115      Stachyris        ruficeps            10
116      Stachyris       rufifrons             3
117      Stachyris       striolata             5
118      Stachyris       thoracica             4
119        Timalia         pileata             8
120    Trichastoma         bicolor             4
121    Trichastoma       celebense            11
122    Trichastoma       rostratum             5
123      Turdoides         bicolor             3
124      Turdoides         gularis             4
125      Turdoides       jardineii            10
126      Turdoides        plebejus            16
127      Turdoides      reinwardii             3
128  Xiphirhynchus   superciliaris             3

I had to match up the names of my species with the names on the tips (huge pain) and I ended up doing it by hand--I could have probably scripted it but I think it would have taken me just as long. 

I collapsed the PCA values into means for each species but I believe I already posted the script for that. So now I have a .csv that contains all the mean PCA that corresponds to each species with the species name that matches the tip. YAY.

Now I finally have evol.rate.mcmc() running and I ran the output for the first four principle components:
#read in the PCA vector:
temp<-read.csv(file.choose())
x<-as.vector(temp$Tarsus) #vector of tarsus measurement traits

names(x)<-as.vector(temp$Species) #names the measurements
#i have to date the trees
undated.tree<-read.tree(file.choose())
#the tree was based on two calibration points (see Moyle, et al. 2012) and the root

tree<-chronopl(undated.tree, lambda=.5, age.min=c(8.4, 13, 27.1), age.max=c(11.4,17, 37.3), node = c(336, 297, 293)) 

#now time for the juicy part--using default parameters to start with as suggested by Revell, et al. 2011

result<-evol.rate.mcmc(tree,x,ngen=100000,control=list(sd2=2.0,sdlnr=3.0))
min_split<-minSplit(tree,result$mcmc[21:101,c("node","bp")]) 
mcmc<-posterior.evolrate(tree,min_split,result$mcmc[21:101,],result$tips[201:1001])
mcmc.data.1<-as.data.frame(mcmc)
#nicely summarize the mcmc for Tarsus
output.summary.1<-ddply(.data=mcmc.data.1, .(node), node.count=length(bp), mean.bp=mean(bp),mean.sig1=mean(sig1), mean.sig2=mean(sig2), summarize)

#let's visualize the nodes! first trim the tree to visualize only nodes that were used in analysis
trim.tree<-drop.tip(tree,tree$tip.label[-(match(names(x),tree$tip.label))]) 

Nodes 3 and 4 are probably root nodes and have high probability. Let's look at 186, 185, and 188.

#now extract the clades we want to look at

this.extract<-extract.clade(tree.trim, tree.trim$edge[186])
this.extract<-extract.clade(tree.trim, tree.trim$edge[58])
#perhaps the most interesting because it contains nearly the whole subfamily
this.extract<-extract.clade(tree.trim, tree.trim$edge[4])
Now time for the bill length:
#read in the PCA vector:
temp<-read.csv(file.choose())
x<-as.vector(temp$Bill.Length) #vector of bill length measurement traits

#you have to reset the names
names(x)<-as.vector(temp$Species) #names the measurements

#use the same dated tree as before
result<-evol.rate.mcmc(tree,x,ngen=100000,control=list(sd2=2.0,sdlnr=3.0))
min_split<-minSplit(tree,result$mcmc[21:101,c("node","bp")]) 
mcmc<-posterior.evolrate(tree,min_split,result$mcmc[21:101,],result$tips[201:1001])
mcmc.data.2<-as.data.frame(mcmc)
#nicely summarize the mcmc for Bill Length
output.summary.2<-ddply(.data=mcmc.data.2, .(node), node.count=length(bp), mean.bp=mean(bp),mean.sig1=mean(sig1), mean.sig2=mean(sig2), summarize)

Hmm...very strange. 250 and 251 are only tips of two sister taxa.
> this.extract<-extract.clade(tree.trim, tree.trim$edge[122])
> plot(this.extract)

Now for the Hallux:
x<-as.vector(temp$Hallux) #vector of hallux measurement traits

#you have to reset the names
names(x)<-as.vector(temp$Species) #names the measurements

#use the same dated tree as before
result<-evol.rate.mcmc(tree,x,ngen=100000,control=list(sd2=2.0,sdlnr=3.0))
min_split<-minSplit(tree,result$mcmc[21:101,c("node","bp")]) 
mcmc<-posterior.evolrate(tree,min_split,result$mcmc[21:101,],result$tips[201:1001])
mcmc.data.2<-as.data.frame(mcmc)
#nicely summarize the mcmc for hallux
output.summary.2<-ddply(.data=mcmc.data.3, .(node), node.count=length(bp), mean.bp=mean(bp),mean.sig1=mean(sig1), mean.sig2=mean(sig2), summarize)

Node 9 has high probability (low frequency):
> this.extract<-extract.clade(tree.trim, tree.trim$edge[9])
> plot(this.extract)

> this.extract<-extract.clade(tree.trim, tree.trim$edge[29])
> plot(this.extract)

> this.extract<-extract.clade(tree.trim, tree.trim$edge[235])
> plot(this.extract)

Enough of that, let's finally look at Tail Length and see if there is anything interesting:
x<-as.vector(temp$Tail.Length) #vector of tail measurement traits

#you have to reset the names
names(x)<-as.vector(temp$Species) #names the measurements

#use the same dated tree as before
result<-evol.rate.mcmc(tree,x,ngen=100000,control=list(sd2=2.0,sdlnr=3.0))
min_split<-minSplit(tree,result$mcmc[21:101,c("node","bp")]) 
mcmc<-posterior.evolrate(tree,min_split,result$mcmc[21:101,],result$tips[201:1001])
mcmc.data.2<-as.data.frame(mcmc)
#nicely summarize the mcmc for tail length
output.summary.4<-ddply(.data=mcmc.data.4, .(node), node.count=length(bp), mean.bp=mean(bp),mean.sig1=mean(sig1), mean.sig2=mean(sig2), summarize)



> this.extract<-extract.clade(tree.trim, tree.trim$edge[8])
> plot(this.extract)

Okay I am kind of petering out on uploading all of these. Gives me something to compare to though.

Important questions:
Next step is to start making my slides for my presentation and date random samples of trees and get feedback from adviser to make sure I am on the right track. 

Thursday, June 6, 2013

Okay checked all my data and things were looking fishy and realized there were still some typos (missing a decimal point, etc.). Everything was carefully reexamined and I think (fingers crossed) these are official PCA results.

There are 145 unique species of babblers to be used in my analysis.

> library(plyr)
> babblers.species.count<-ddply(.data=new.babblers, .(Genus, Species), species.count=length(Wing.Length), summarize)
> babblers.species.count

                   Genus         Species species.count
1             Actinodura        egertoni             6
2             Actinodura    morrisoniana             3
3             Actinodura      nipalensis             3
4             Actinodura         waldeni             5
5                Alcippe    brunneicauda             4
6                Alcippe          grotei            11
7                Alcippe      morrisonia             5
8                Alcippe      peracensis             2
9                Alcippe    poioicephala            11
10               Alcippe     rufogularis             5
11                 Babax      lanceolata             4
12             Chrysomma         sinense             6
13  Chrysomma (Moupinia)      poecilotis             4
14                 Cutia      nipalensis             7
15              Dumentia      hyperythra             6
16        Gampsorhynchus         rufulus             8
17              Garrulax         affinis            11
18              Garrulax     albogularis             5
19              Garrulax         canorus            14
20              Garrulax       chinensis            40
21              Garrulax      cineraceus             4
22              Garrulax erythrocephalus             1
23              Garrulax        formosus             4
24              Garrulax     leucolophus            41
25              Garrulax        lineatus             6
26              Garrulax           maesi             3
27              Garrulax       merulinus             1
28              Garrulax         milleti             1
29              Garrulax          milnei             4
30              Garrulax        mitratus             6
31              Garrulax       monileger             8
32              Garrulax        nuchalis             3
33              Garrulax       ocellatus             7
34              Garrulax       palliatus             4
35              Garrulax      pectoralis             1
36              Garrulax  perspicillatus            14
37              Garrulax poecilorhynchus             4
38              Garrulax          sannio            12
39              Garrulax         sinesis             9
40              Garrulax       squamatus             3
41              Garrulax        striatus             6
42              Garrulax     subunicolor             6
43              Garrulax      variegatum             3
44              Garrulax         vassali             7
45              Garrulax        virgatus             2
46         Heterophaesia       annectens             4
47         Heterophaesia     auricularis             3
48         Heterophaesia      capistrata             6
49         Heterophaesia      desgodinsi             1
50         Heterophaesia        gracilis             4
51         Heterophaesia    maelanoleuca             9
52         Heterophaesia       picaoides             7
53         Heterophaesia       pulchella             3
54          Heterophasia      desgodinsi             7
55           Illadopsis       albipectus             4
56           Illadopsis         cleaveri             4
57           Illadopsis       fulvescens             8
58           Illadopsis           puveli             2
59           Illadopsis      pyrrhoptera             2
60           Illadopsis        rufescens             3
61           Illadopsis       rufipennis             6
62               Kenopia         striata             2
63             Leiothrix     argentauris            15
64             Leiothrix           lutea             4
65            Leonardina           woodi             1
66             Liocichla       phoenicea             1
67             Liocichla         steerii             3
68              Macronus         gularis            25
69              Macronus        ptilosus             3
70              Macronus     striaticeps            12
71          Malacocincla         abbotti             4
72          Malacocincla     cinereiceps             2
73          Malacocincla     malaccensis             6
74          Malacocincla        sepiaria             3
75          Malacopteron          affine             3
76          Malacopteron      albogulare             4
77          Malacopteron        cinereum             1
78          Malacopteron     magnirostre             7
79          Malacopteron          magnum             5
80          Malacopteron    palawanenese             3
81                 Malia           grata             6
82         Micromacronus        sordidus             2
83                 Minla      ignotincta             6
84                 Minla        strigula             7
85             Napothera    brevicaudata             4
86             Napothera          crassa             2
87             Napothera     crispifrons             7
88             Napothera     epilepidota            10
89            Pellorneum      albiventre             9
90            Pellorneum    capristratum            15
91            Pellorneum      pyrrogenys             6
92            Pellorneum        ruficeps             5
93            Pellorneum        tickelli             1
94           Phyllanthus      atripennis             6
95              Pnoepyga         pusilla             1
96          Pomatorhinus    erythrogenys            18
97          Pomatorhinus    ferruginosus             4
98          Pomatorhinus      horsfieldi             6
99          Pomatorhinus      hypoleucos             4
100         Pomatorhinus        montanus             4
101         Pomatorhinus      ruficollis             9
102         Pomatorhinus        swinhoei             2
103          Ptilocichla         falcata             2
104          Ptilocichla   leucogrammica             3
105          Ptilocichla     mindanensis             3
106            Ptyrticus        turdinus             6
107         Rhopocichla         atriceps             4
108              Rimator    malacoptilus             3
109         Schoeniparus         brunnea            10
110         Schoeniparus     castaneceps             1
111         Schoeniparus         cinerea             3
112         Schoeniparus           dubia             6
113         Schoeniparus     rufogularis             4
114          Spelaeornis    chocolatinus             5
115          Spelaeornis        formosus             2
116          Spelaeornis   troglodytoide             3
117         Sphenocichla           humei             3
118            Stachyris         ambigua             4
119            Stachyris        chrysaea            11
120            Stachyris    erythroptera             8
121            Stachyris      grammiceps             2
122            Stachyris        leucotis             3
123            Stachyris        maculata             3
124            Stachyris    melanothorax             6
125            Stachyris       nigriceps             7
126            Stachyris     nigricollis             4
127            Stachyris           oglei             3
128            Stachyris    poliocephala             4
129            Stachyris        pyrrhops             3
130            Stachyris        ruficeps            10
131            Stachyris       rufifrons             3
132            Stachyris       striolata             5
133            Stachyris       thoracica             4
134              Timalia         pileata             8
135          Trichastoma         bicolor             4
136          Trichastoma       celebense            11
137          Trichastoma       rostratum             5
138          Trichastoma        tickelli            10
139          Trichastona        tickelli             2
140            Turdoides         bicolor             3
141            Turdoides         gularis             4
142            Turdoides       jardineii            10
143            Turdoides        plebejus            16
144            Turdoides      reinwardii             3
145        Xiphirhynchus   superciliaris             3

Had a minor panic attack because I realized my missing tarsus values were being imputed with mean of the bill length which of course would lead to extreme variance in the trait. Anyways, fixed it right up and still got somewhat similar results. Whew.
New PCA result:


Fixed script:
#impute missing values by taking mean of all other values of of other species ofthat character 

i<-1 #set the index
species.removed<-c()
#sift through all babbler species
while (i <= length(babblers$UniqueID_1)){
  #missing wing lengths; if the Wing Length is NA for that value
  if(is.na(babblers$Wing.Length[i]) == TRUE){
    #select the wing lengths of all birds which match this species with missing wing length
    wing.species.vector<-babblers$Wing.Length[which(babblers$Species[i] == babblers$Species)]
    #make a new vector without the missing value
    wing.species.filler<-wing.species.vector[which(is.na(wing.species.vector) == FALSE)]
    #calculate the mean
    wing.impute<-mean(wing.species.filler)
    #fill in missing value with the mean for that bird
    babblers$Wing.Length[i]<-wing.impute
  }
  
  #repeat the above method for all characters
  if(is.na(babblers$Tail.Length[i]) == TRUE){
    tail.species.vector<-babblers$Tail.Length[which(babblers$Species[i] == babblers$Species)]
    tail.species.filler<-tail.species.vector[which(is.na(tail.species.vector) == FALSE)]
    tail.impute<-mean(tail.species.filler)
    babblers$Tail.Length[i]<-tail.impute
  }
  
  
  
  if(is.na(babblers$Bill.Length[i]) == TRUE){
    BL.species.vector<-babblers$Bill.Length[which(babblers$Species[i] == babblers$Species)]
    BL.species.filler<-BL.species.vector[which(is.na(BL.species.vector) == FALSE)]
    BL.impute<-mean(BL.species.filler)
    babblers$Bill.Length[i]<-BL.impute
  }
  
  if(is.na(babblers$Bill.Width[i]) == TRUE){
    BW.species.vector<-babblers$Bill.Width[which(babblers$Species[i] == babblers$Species)]
    BW.species.filler<-BW.species.vector[which(is.na(BW.species.vector) == FALSE)]
    BW.impute<-mean(BW.species.filler)
    babblers$Bill.Width[i]<-BW.impute
  }
  
  if(is.na(babblers$Bill.Depth[i]) == TRUE){
    BD.species.vector<-babblers$Bill.Depth[which(babblers$Species[i] == babblers$Species)]
    BD.species.filler<-BD.species.vector[which(is.na(BD.species.vector) == FALSE)]
    BD.impute<-mean(BD.species.filler)
    babblers$Bill.Depth[i]<-BD.impute
  }
  
  if(is.na(babblers$Tarsus.Length[i]) == TRUE){
    TL.species.vector<-babblers$Tarsus.Length[which(babblers$Species[i] == babblers$Species)]
    TL.species.filler<-TL.species.vector[which(is.na(TL.species.vector) == FALSE)]
    TL.impute<-mean(TL.species.filler)
    babblers$Tarsus.Length[i]<-TL.impute
  }
  
  if(is.na(babblers$Hallux[i]) == TRUE){
    hallux.species.vector<-babblers$Hallux[which(babblers$Species[i] == babblers$Species)]
    hallux.species.filler<-hallux.species.vector[which(is.na(hallux.species.vector) == FALSE)]
    hallux.impute<-mean(hallux.species.filler)
    babblers$Hallux[i]<-hallux.impute
  }

  i<-i+1
}


PCAs and reweights of the values for each species is finally done and in a format that can be easily used to run the evol.rate.mcmc()


Script to impute data:
#impute missing values by taking mean of all other values of of other species ofthat character 

i<-1 #set the index
species.removed<-c()
#sift through all babbler species
while (i <= length(babblers$UniqueID_1)){
  #missing wing lengths; if the Wing Length is NA for that value
  if(is.na(babblers$Wing.Length[i]) == TRUE){
    #select the wing lengths of all birds which match this species with missing wing length
    wing.species.vector<-babblers$Wing.Length[which(babblers$Species[i] == babblers$Species)]
    #make a new vector without the missing value
    wing.species.filler<-wing.species.vector[which(is.na(wing.species.vector) == FALSE)]
    #calculate the mean
    wing.impute<-mean(wing.species.filler)
    #fill in missing value with the mean for that bird
    babblers$Wing.Length[i]<-wing.impute
  }
  
  #repeat the above method for all characters
  if(is.na(babblers$Tail.Length[i]) == TRUE){
    tail.species.vector<-babblers$Tail.Length[which(babblers$Species[i] == babblers$Species)]
    tail.species.filler<-tail.species.vector[which(is.na(tail.species.vector) == FALSE)]
    tail.impute<-mean(tail.species.filler)
    babblers$Tail.Length[i]<-tail.impute
  }
  
  
  
  if(is.na(babblers$Bill.Length[i]) == TRUE){
    BL.species.vector<-babblers$Bill.Length[which(babblers$Species[i] == babblers$Species)]
    BL.species.filler<-BL.species.vector[which(is.na(BL.species.vector) == FALSE)]
    BL.impute<-mean(BL.species.filler)
    babblers$Bill.Length[i]<-BL.impute
  }
  
  if(is.na(babblers$Bill.Width[i]) == TRUE){
    BW.species.vector<-babblers$Bill.Width[which(babblers$Species[i] == babblers$Species)]
    BW.species.filler<-BW.species.vector[which(is.na(BW.species.vector) == FALSE)]
    BW.impute<-mean(BW.species.filler)
    babblers$Bill.Width[i]<-BW.impute
  }
  
  if(is.na(babblers$Bill.Depth[i]) == TRUE){
    BD.species.vector<-babblers$Bill.Depth[which(babblers$Species[i] == babblers$Species)]
    BD.species.filler<-BD.species.vector[which(is.na(BD.species.vector) == FALSE)]
    BD.impute<-mean(BD.species.filler)
    babblers$Bill.Depth[i]<-BD.impute
  }
  
  if(is.na(babblers$Tarsus.Length[i]) == TRUE){
    TL.species.vector<-babblers$Tarsus.Length[which(babblers$Species[i] == babblers$Species)]
    TL.species.filler<-TL.species.vector[which(is.na(TL.species.vector) == FALSE)]
    TL.impute<-mean(BD.species.filler)
    babblers$Tarsus.Length[i]<-TL.impute
  }
  
  if(is.na(babblers$Hallux[i]) == TRUE){
    hallux.species.vector<-babblers$Hallux[which(babblers$Species[i] == babblers$Species)]
    hallux.species.filler<-hallux.species.vector[which(is.na(hallux.species.vector) == FALSE)]
    hallux.impute<-mean(hallux.species.filler)
    babblers$Hallux[i]<-hallux.impute
  }

  i<-i+1
}

Script to scale by geometric mean:
#make empty columns for geometric mean scale
babblers$gm.wing<-0
babblers$gm.tail<-0
babblers$gm.BL<-0
babblers$gm.BW<-0
babblers$gm.BD<-0
babblers$gm.tarsus<-0
babblers$gm.hallux<-0

#Size-adjust the raw data by dividing each individual by the geometric mean 

j<-1
could.not.scale<-c()
while(j<=length(babblers$UniqueID_1)){
  #grab all the data of that species and put in vector
  temp<-c(babblers$Wing.Length[j], babblers$Tail.Length[j], babblers$Bill.Length[j],
          babblers$Bill.Width[j], babblers$Bill.Depth[j], babblers$Tarsus.Length[j], 
          babblers$Hallux[j])
  temp<-na.omit(temp)
  #calculate the geometric mean of meausrements for that species
  geo.mean<-exp(mean(log(temp)))
  
  if(is.nan(geo.mean)==FALSE){
  #divide all values for that species by the geometric mean
  geo.wing<-(babblers$Wing.Length[j])/geo.mean
  babblers$gm.wing[j]<-geo.wing
  
  geo.tail<-babblers$Tail.Length[j]/geo.mean
  babblers$gm.tail[j]<-geo.tail
  
  geo.BL<-babblers$Bill.Length[j]/geo.mean
  babblers$gm.BL[j]<-geo.BL
  
  geo.BW<-babblers$Bill.Width[j]/geo.mean
  babblers$gm.BW[j]<-geo.BW
  
  geo.BD<-babblers$Bill.Depth[j]/geo.mean
  babblers$gm.BD[j]<-geo.BD
  
  geo.tarsus<-babblers$Tarsus.Length[j]/geo.mean
  babblers$gm.tarsus[j]<-geo.tarsus
  
  geo.hallux<-babblers$Hallux[j]/geo.mean
  babblers$gm.hallux[j]<-geo.hallux
 }
 else{
    append(could.not.scale, babblers$UniqueID_1[j])
  }
  #reset the temp
  temp<-c()
  
  #increment to next species
  j<-j+1
}

cat("The following species could not be scaled:", could.not.scale, "\n")

Script to do PCA with geometric mean values and put rescaled scores into new data frame:
#PCA for all measurements
pca.result <- prcomp(~babblers$gm.wing + babblers$gm.tail + babblers$gm.BL + babblers$gm.BW + babblers$gm.BD + 
       babblers$gm.tarsus + babblers$gm.hallux, data = babblers, retx = T)

#standard deviation of PCA
sd <- pca.result$sdev
#make vector of weights
loadings <- pca.result$rotation
#value of the rotated data (scores)
scores <- pca.result$x

#make a new data frame
new.babblers<-as.data.frame(babblers)

#make empty columns in the data frame
new.babblers$pca1<-0
new.babblers$pca2<-0
new.babblers$pca3<-0
new.babblers$pca4<-0
new.babblers$pca5<-0
new.babblers$pca6<-0
new.babblers$pca7<-0

#put PCA Scores into the data frame
new.babblers$pca1<-scores[,1]
new.babblers$pca2<-scores[,2]
new.babblers$pca3<-scores[,3]
new.babblers$pca4<-scores[,4]
new.babblers$pca5<-scores[,5]
new.babblers$pca6<-scores[,6]
new.babblers$pca7<-scores[,7]

new.babblers$pca# are the traits that will be used in the Bayesian analysis.
This site was really useful for understanding PCA:
http://strata.uga.edu/software/pdf/pcaTutorial.pdf