-
Notifications
You must be signed in to change notification settings - Fork 0
/
phenoselection_FUNCTIONS.R
251 lines (193 loc) · 6.25 KB
/
phenoselection_FUNCTIONS.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#### master function
PHENOSELECTION<-function(Var1,Var2,Fitness,Gmatrix=NULL){
library(boot)
# prepare data function
preparedata<-function(Fitness,Var1,Var2){
data<-data.frame(Fitness=as.numeric(Fitness),
Var1=as.numeric(Var1),
Var2=as.numeric(Var2) )
data<-na.omit(data)
d1<-(data.frame(w=data$Fitness/mean(data$Fitness),
Var1=scale(data$Var1),
Var2=scale(data$Var2)))
d1$w
d1$Var1<-scale(d1$Var1)
d1$Var2<-scale(d1$Var2)
return(d1)
}
# functions from Lande & Arnold 1983 with bootstrap significance
gradientlinear<- function(data, indices) {
d1 <- data[indices,]
P<-cov(d1[,2:3])
Pinv<-solve(cov(d1[,2:3]))
s<- cov(d1)[2:3,1]
B<-Pinv%*%s
return(B)
}
gradientquadratic<- function(data, indices) {
d1 <- data[indices,]
P<-cov(d1[,2:3])
Pinv<-solve(cov(d1[,2:3]))
new<-d1[,2:3]^2
z12<-d1[,2]*d1[,3]
new<-data.frame(new,z12)
c<-matrix(ncol=2,nrow=2)
rawcov<-cov(d1$w,new)
c<-diag(cov(d1$w,new)[1:2])
c[1,2]<-rawcov[3]
c[2,1]<-rawcov[3]
gamma = Pinv%*% c %*%Pinv
return(gamma)
}
coeflinear<- function(data, indices) {
d1 <- data[indices,]
P<-cov(d1[,2:3])
Pinv<-solve(cov(d1[,2:3]))
s<- cov(d1)[2:3,1]
B<-Pinv%*%s
return(s)
}
coefquadratic<- function(data, indices) {
d1 <- data[indices,]
P<-cov(d1[,2:3])
Pinv<-solve(cov(d1[,2:3]))
new<-d1[,2:3]^2
z12<-d1[,2]*d1[,3]
new<-data.frame(new,z12)
c<-matrix(ncol=2,nrow=2)
rawcov<-cov(d1$w,new)
c<-diag(cov(d1$w,new)[1:2])
c[1,2]<-rawcov[3]
c[2,1]<-rawcov[3]
gamma = Pinv%*% c %*%Pinv
return(c)
}
responselinear<- function(data, indices) {
d1 <- data[indices,] # allows boot to select sample
P<-cov(d1[,2:3])
Pinv<-solve(cov(d1[,2:3]))
s<- cov(d1)[2:3,1]
B<-Pinv%*%s
deltaZ<-Gmatrix%*%B
deltaZ
newZ<-deltaZ
meanchange<-newZ
return(meanchange)
}
responsequadratic<- function(data, indices) {
d1 <- data[indices,]
P<-cov(d1[,2:3])
Pinv<-solve(cov(d1[,2:3]))
d1 <- data[indices,]
P<-cov(d1[,2:3])
Pinv<-solve(cov(d1[,2:3]))
s<- cov(d1)[2:3,1]
B<-Pinv%*%s
new<-d1[,2:3]^2
z12<-d1[,2]*d1[,3]
new<-data.frame(new,z12)
c<-matrix(ncol=2,nrow=2)
rawcov<-cov(d1$w,new)
c<-diag(cov(d1$w,new)[1:2])
c[1,2]<-rawcov[3]
c[2,1]<-rawcov[3]
gamma = Pinv%*% c %*%Pinv
deltaP<- (P*gamma*P) - s%*%t(s)
deltaG<-Gmatrix%*%(gamma-(B%*%t(B)))%*%Gmatrix
deltaG+Gmatrix
return(deltaP)
}
responsequadratic_gmatrix<- function(data, indices) {
d1 <- data[indices,]
P<-cov(d1[,2:3])
Pinv<-solve(cov(d1[,2:3]))
s<- cov(d1)[2:3,1]
B<-Pinv%*%s
new<-d1[,2:3]^2
z12<-d1[,2]*d1[,3]
new<-data.frame(new,z12)
c<-matrix(ncol=2,nrow=2)
rawcov<-cov(d1$w,new)
c<-diag(cov(d1$w,new)[1:2])
c[1,2]<-rawcov[3]
c[2,1]<-rawcov[3]
gamma = Pinv%*% c %*%Pinv
return(gamma)
deltaP<- (P*gamma*P) - s%*%t(s) # why variance is not 1??
deltaP
newP<-deltaP+P
covariancechange<-deltaP
deltaG<-Gmatrix%*%(gamma-(B%*%t(B)))%*%Gmatrix
deltaG+Gmatrix # the change in heritability and genetic correlation!
return(deltaG)
}
#### function to produce nice bootstrap output #####
extractbootstrap<-function(bootstrapresults){
treatbootstrap<-function(x){
five<-quantile(x,p=c(0.05,0.95) )
one<-quantile(x,p=c(0.01,0.99) )
zeroone<-quantile(x,p=c(0.001,0.999) )
tempsign<-(five[1]/five[2]) / abs(five[1]/five[2])
tempsign<-c(tempsign,(one[1]/one[2]) / abs(one[1]/one[2]))
tempsign<-c(tempsign,(zeroone[1]/zeroone[2]) / abs(zeroone[1]/zeroone[2]))
tempsign[tempsign==1]<-"*"
tempsign[tempsign==-1]<-""
tempsign[tempsign==NA]<-""
tempsign<-paste(as.character(tempsign)[1],as.character(tempsign)[2],as.character(tempsign)[3],sep="")
as.character(tempsign)
se<-round(sd(x),digits = 3)
media<-round(mean(x),digits = 3)
pasted<-paste(media," (",se, ")",tempsign,sep="")
return(pasted)
}
extracted<-apply(bootstrapresults$t,2,treatbootstrap)
return(extracted)
}
extractbootstrap_numeric<-function(bootstrapresults){
treatbootstrap<-function(x){
media<-round(mean(x),digits = 3)
return(media)
}
extracted<-apply(bootstrapresults$t,2,treatbootstrap)
return(extracted)
}
#@ start LITTLE BIT THAT ACTUALLY DO ANALYSES @#
d1<-preparedata(Fitness,Var1,Var2)
if (is.null(Gmatrix)==T){
print ("Heritabilities not provided, only selection analyses reported" )
result_gradient_linear<- boot(data=d1, statistic=gradientlinear, R=1000)
result_gradient_quadratic<- boot(data=d1, statistic=gradientquadratic, R=1000)
result_coefficient_linear<- boot(data=d1, statistic=coeflinear, R=1000)
result_coefficient_quadratic<- boot(data=d1, statistic=coefquadratic, R=1000)
resa<-extractbootstrap(result_gradient_linear)
resb<-extractbootstrap(result_gradient_quadratic)
resc<-extractbootstrap(result_coefficient_linear)
resd<-extractbootstrap(result_coefficient_quadratic)
}
if (is.null(Gmatrix)==F){
print (" Heritabilities provided, selection gradients and response to selection analyses reported" )
result_gradient_linear<- boot(data=d1, statistic=gradientlinear, R=1000)
result_gradient_quadratic<- boot(data=d1, statistic=gradientquadratic, R=1000)
result_coefficient_linear<- boot(data=d1, statistic=coeflinear, R=1000)
result_coefficient_quadratic<- boot(data=d1, statistic=coefquadratic, R=1000)
resa<-extractbootstrap(result_gradient_linear)
resb<-extractbootstrap(result_gradient_quadratic)
resc<-extractbootstrap(result_coefficient_linear)
resd<-extractbootstrap(result_coefficient_quadratic)
result_response_linear<- boot(data=d1, statistic=responselinear, R=1000)
result_response_quadratic<- boot(data=d1, statistic=responsequadratic, R=1000)
result_response_quadratic_gmatrix<- boot(data=d1, statistic=responsequadratic_gmatrix, R=1000)
rese<-extractbootstrap(result_response_linear)
resf<-extractbootstrap(result_response_quadratic)
resg<-extractbootstrap(result_response_quadratic_gmatrix)
analysislist<-list(gradient_linear=resa,gradient_quadratic=resb,
coefficient_linear=resc,coefficient_quadratic=resd,
response_linear=rese,
response_quadratic_Vpheno=resf,
response_quadratic_Vaddit=resg)
}
#@ end LITTLE BIT THAT ACTUALLY DO ANALYSES @#
analysislist<-list(gradient_linear=resa,gradient_quadratic=resb,
coefficient_linear=resc,coefficient_quadratic=resd)
return(analysislist)
} # end phenoselection