-
Notifications
You must be signed in to change notification settings - Fork 0
/
vivj_matrix.R
153 lines (119 loc) · 4.98 KB
/
vivj_matrix.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Select any two species pair [i,j] for a copula of location loc
# This function gives you a matrix with vi and vj as two columns
# Input :
# d_allsp : dataset in data[[loc]][[sp]] format
# loc : location index
# i,j : sp-pair indices
# level : significance level for BiCopIndepTest p-value
# ploton : (optional) logical, if T gives copula plot without transforming j-th variable to it's -ve value
# onbounds : a logical tag (default=FALSE) to get info about the points exactly lying on bounds, if set to TRUE
# then the arguments lb and ub must be numeric
# lb : numeric value between [0,1] for lower bound (default =NA)
# ub : numeric value between [0,1] for upper bound (default =NA), ub should be greater than lb
# Output :
# A list of 4 elements:
# mat : a matrix : copula of (vi,vj) with transforming j-th variable to it's -ve value for -ve corr.
# corval : Spearman's correlation
# pval : pvalue of Kendall's cor.test
# IndepTestRes : BiCopIndepTest p-value
# and an optional plot of the copula
library(VineCopula)
vivj_matrix<-function(d_allsp,loc,i,j,level=0.05,ploton,onbounds=F,lb=NA,ub=NA){
ds1<-d_allsp[[loc]][[i]]
ds2<-d_allsp[[loc]][[j]]
#----------------------------
colnames(ds1)<-c("Year","Dat") # ensuring column names
colnames(ds2)<-c("Year","Dat")
a1<-ds1$Year[1]
a2<-ds2$Year[1]
a3<-ds1$Year[dim(ds1)[1]]
a4<-ds2$Year[dim(ds2)[1]]
year_s<-max(a1,a2)
year_e<-min(a3,a4)
ind_s1<-which(ds1$Year==year_s)
ind_s2<-which(ds2$Year==year_s)
ind_e1<-which(ds1$Year==year_e)
ind_e2<-which(ds2$Year==year_e)
ds1<-ds1[ind_s1:ind_e1,]
ds2<-ds2[ind_s2:ind_e2,]
# Omitting the years and data containing NA in either d1 or d2
#from both d1 and d2
if(anyNA(ds1$Dat)==T | anyNA(ds2$Dat)==T){
ind_na1<-which(is.na(ds1$Dat))
ind_na2<-c(ind_na1,which(is.na(ds2$Dat)))
ind_na<-unique(ind_na2)
d1Dat<-ds1$Dat[-ind_na]
d2Dat<-ds2$Dat[-ind_na]
Years<-ds1$Year[-ind_na]
d1<-data.frame(Year=Years,Dat=d1Dat)
d2<-data.frame(Year=Years,Dat=d2Dat)
}else{
d1<-ds1
d2<-ds2
}
colnames(d1)[2]<-"Dat" # ensuring column names
colnames(d2)[2]<-"Dat"
#get ranks modified now
vi<-VineCopula::pobs(d1$Dat)
vj<-VineCopula::pobs(d2$Dat)
IndepTestRes<-VineCopula::BiCopIndTest(vi,vj)$p.value
ct<-cor.test(vi,vj,alternative = "two.sided",method="spearman",exact=F)
corval<-unname(ct$estimate)
pval<-ct$p.value
if(ploton==T){
if(IndepTestRes<level && corval>0){ # for significant positive correlation
plot(vi,vj,type='p',col=rgb(0,0,0,0.3),pch=19,xlim=c(0,1),ylim=c(0,1),
xlab=names(d_allsp[[loc]])[i],ylab=names(d_allsp[[loc]])[j],cex.lab=1.5)
if(j>i){
if(onbounds==T & identical(vi,vj)==F){
ind_lb<-which(vi+vj==(2*lb))
ind_ub<-which(vi+vj==(2*ub))
onlb<-length(ind_lb)
onub<-length(ind_ub)
if(onlb!=0 | onub!=0){
mtext(paste0("onbs = (",onlb," , ",onub,")"),
side = 4, line=0.15, adj=0.5, col="red")
}
}
}
}else if(IndepTestRes<level && corval<0){ # for significant negative correlation
plot(vi,vj,type='p',col=rgb(0,1,0,0.3),pch=19,xlim=c(0,1),ylim=c(0,1),
xlab=names(d_allsp[[loc]])[i],ylab=names(d_allsp[[loc]])[j],cex.lab=1.5)
if(j>i){
if(onbounds==T & identical(vi,vj)==F){
vneg<-VineCopula::pobs(-(d2$Dat)) # see when we count points on bounds we took reverse of second variable
ind_lb<-which(vi+vneg==(2*lb))
ind_ub<-which(vi+vneg==(2*ub))
#vneg<-VineCopula::pobs(-(d1$Dat)) # NOTE : onbs will not be same if we consider first variable to be reversed
#ind_lb<-which(vj+vneg==(2*lb))
#ind_ub<-which(vj+vneg==(2*ub))
onlb<-length(ind_lb)
onub<-length(ind_ub)
if(onlb!=0 | onub!=0){
mtext(paste0("onbs = (",onlb," , ",onub,")"),
side = 4, line=0.15, adj=0.5, col="red")
}
}
}
}else{ # independent case
plot(-1,0,xlim=c(0,1),ylim=c(0,1),xlab=names(d_allsp[[loc]])[i],ylab=names(d_allsp[[loc]])[j],cex.lab=1.5)
text(0.5,0.5,"Indep.",adj=c(0.5,.5),cex=2)
}
mtext(paste0("(sp_x, sp_y) = (",i," , ",j,")"),
side = 3, line=0.15, adj=0.5, col="black")
}
if(IndepTestRes<level && corval<0){
vj<-VineCopula::pobs(-(d2$Dat))
}
Years<-d1$Year
#-------------------------
#n_datapt<-length(vi)
#--------------------
#plot(vi,vj,type="p")
#-------------------------
mat<-as.matrix(cbind(vi,vj))
return(list(mat=mat, # return reversed mat so that if you plot this mat you get +ve correlation
corval=corval, # but return the actual -ve corr. value
pval=pval,
IndepTestRes=IndepTestRes))
}