-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathechinoderm_clus_trial.R
More file actions
95 lines (72 loc) · 2.66 KB
/
Copy pathechinoderm_clus_trial.R
File metadata and controls
95 lines (72 loc) · 2.66 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Coral trial
library(cluster)
library(fpc)
library(ggplot2)
library(dendextend)
library(dplyr)
library(reshape2)
library(gridExtra)
library(readxl)
# source clVal function
source('C:/coral_fish/scripts/coral_fish/clVal.R')
dat<-read.csv('C:/coral_fish/data/Traits/echino_traitDB_jpn.csv')
summary(dat) # 23 missing vals in max depth and reporduction
str(dat)
dat[dat$Spines=='No ',]$Spines<-'No'
dat$Spines<-factor(dat$Spines)
table(dat$Spines)
table(dat$Max_Length)
table(dat$Depth_Range)
table(dat$Aggregation)
table(dat$Tidal_zone)
table(dat$Exposure)
table(dat$habitat_KMC)
table(dat$Diet)
table(dat$Mating_System_KMC)
#make ordered factor
dat$Exposure<-factor(dat$Exposure,
levels=c("Protected", "both","Exposed"), ordered = T,
exclude='NA')
str(dat)
table(dat$Spines)
table(dat$Max_Length)
table(dat$Depth_Range)
table(dat$Aggregation_KMC)
table(dat$Tidal_zone)
table(dat$Exposure)
table(dat$habitat_KMC)
table(dat$Diet)
table(dat$Mating_System_KMC)
crl_out<-clVal(data=dat[,c('Spines', 'Max_Length','Depth_Range',
'Aggregation_KMC','Tidal_zone',
'Exposure','habitat_KMC',
'Diet', 'Mating_System_KMC')],
daisytypelist = list(symm=c(1,4,9)),
runs=500, min_cl=2, max_cl=20, subs_perc=0.95,
fast.k.h = 0.1, calc_wigl = F, daisyweights=rep(1, 9))
a_melt<-melt(crl_out$stats, id.vars=c( 'k', 'runs'))
a_sum<-a_melt%>%group_by(k, variable)%>%
summarise(mean=mean(value), median=median(value))
a_melt<-filter(a_melt, variable!='wig')
a_sum<-filter(a_sum, variable!='wig')
ggplot()+
geom_violin(data=a_melt, aes(x=k, y=value, group=k))+
geom_point(data=a_sum, aes(x=k, y=mean), color='red', shape=1)+
geom_line(data=a_sum, aes(x=k, y=mean), color='red')+
geom_point(data=a_sum, aes(x=k, y=median), color='green', shape=1)+
geom_line(data=a_sum, aes(x=k, y=median), color='green')+
scale_x_continuous(breaks=2:20)+
facet_wrap(~variable, scales='free_y')+
geom_vline(xintercept = 8, color='cyan')+
geom_vline(xintercept = 12, color='cyan')
# write clusters out
hc<-hclust(daisy(dat[,c('Spines', 'Max_Length','Depth_Range',
'Aggregation_KMC','Tidal_zone',
'Exposure','habitat_KMC',
'Diet', 'Mating_System_KMC')],
metric='gower',
type = list(symm=c(1,4,9)),stand = FALSE), method='average')
plot(hc); rect.hclust(hc, k=8);rect.hclust(hc, k=12, border=4)
dat$groupk8<-cutree(hc, k=8)
dat$groupk12<-cutree(hc, k=12)
write.csv(dat, 'C:/coral_fish/outputs/echinoderm_clust_jpn.csv', quote=F, row.names=F)