-
Notifications
You must be signed in to change notification settings - Fork 0
/
Run phyPCA.R
52 lines (40 loc) · 2.04 KB
/
Run phyPCA.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
library(phytools)
library(ggalt)
library(ggrepel)
#get residuals from pgls of head mass vs each ear measure
source("Extract pgls residuals.R")
#this creates a new dataframe with residuals attached in the last columns:
names(dfwithresids)
#isolate columns with residuals to be used in PCA
PCAset<-dfwithresids[,37:48]# data to be used in PCA
row.names(PCAset)<-dfwithresids$Binomial#species names need to be in row names for use of phy.PCA function
names(PCAset)
#remove air volume from PCA if desired (air volume has more missing values than other measures)
#PCAset<-PCAset[,-c(10)]
#remove NAs
naomit_PCA<-na.omit(PCAset)
#trim phylogeny to the species used in the PCA
phyPCA<-keep.tip(birdtreels,row.names(naomit_PCA))
plot(phyPCA)
################run phyPCA####################
pPCA<- phyl.pca(phyPCA,naomit_PCA,
method = "lambda",
mode = "corr")
#plot PCA#################
#par(mar = c(4,3,3,3))
#plot(pPCA)
#biplot(pPCA)
#put PCA values and loadings in dataframes
speciesPCAvalues<-as.data.frame(pPCA$S)#PCA score, with each
pPCAloadings<-as.data.frame(pPCA$L)
pPCAloadings
#Create column in the PCA dataset for
#species name, order, dive score, and ecological grouping (plunge-distinct)
speciesPCAvalues$Binomial<-as.factor(dfwithresids[match(row.names(speciesPCAvalues),
dfwithresids$Binomial),"Binomial"])#<-------------<-------
speciesPCAvalues$Order<-as.factor(dfwithresids[match(row.names(speciesPCAvalues),
dfwithresids$Binomial),"Order"])#<-------------<-------
speciesPCAvalues$divescore<-as.factor(dfwithresids[match(row.names(speciesPCAvalues),
dfwithresids$Binomial),"divescore"])#<-------------<-------
speciesPCAvalues$plungedistinct<-as.factor(dfwithresids[match(row.names(speciesPCAvalues),
dfwithresids$Binomial),"plungedistinct"])#<-------------<-------