-
Notifications
You must be signed in to change notification settings - Fork 2
/
LookAtData.R
206 lines (154 loc) · 8.89 KB
/
LookAtData.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
setwd("C:/Users/Thimothee Admin/Dropbox/Decomposing pop dynamics/Dataset.R/")
source("data.R")
LeprechaunSimul(InitialPopSize = 100,MeanRepro = 2,StudyLength = 50,SurvivalPenaltyForRepro = 0.2)
popfile<-read.table(file="data/simple/TheDataSetV1.csv",header=T)
popfile2<-read.table(file="data/simple/TheDataSetV2.csv",header=T)
plot(tapply(X = popfile$C[which(popfile$age>1)],INDEX = popfile$t[which(popfile$age>1)],mean),type="l")
plot(popfile$C,x=popfile$t)
popsize<-table(popfile$t)
plot(popsize)
plot(table(popfile$age))
round(table(popfile$age)/(sum(table(popfile$age))),digits = 3)
popsize2<-table(popfile2$t)
plot(popsize2)
plot(table(popfile2$age))
round(table(popfile2$age)/(sum(table(popfile2$age))),digits = 3)
plot(tapply(X = popfile2$C[which(popfile2$age>1)],INDEX = popfile2$t[which(popfile2$age>1)],mean),type="l")
summary(glm(phi~1+age+I(age^2)+C,data=popfile))
var(popfile$bvs[which(popfile$age==0 & popfile$t==2)])
var(popfile$z[which(popfile$age==0 & popfile$t==2)])
plot(popfile$z,x=popfile$age)
plot(popfile$z,x=popfile$C)#size depending on how many Camemberts you (leprechaun) get, or the other way around rather
library(ggplot2)
qplot(x=bvs,y=z,colour=t,data=popfile)#visualize heritability and response to selection (temporal change in breeding values)
plot(popfile$z,x=popfile$t)
plot(popfile$bvs,x=popfile$t)
plot(popfile$hunting,x=popfile$t)
plot(popfile$bvh,x=popfile$t)
mean(popfile$phi[which(popfile$t==8)])
library(lme4)
mm0<-lmer(z~1+age+t+s+(1|ID),data=popfile)
summary(mm0)
table(popfile$C)
Camemberts<-tapply(X=popfile$C,INDEX=popfile$t,FUN=sum)#retrieve the yearly food abundance
popSize<-tapply(X=popfile$ID[which(popfile$phi==1)],INDEX=popfile$t[which(popfile$phi==1)],FUN=length)#retrieve the end of year pop sizes
tapply(X=popfile$z[which(popfile$phi==1)],INDEX=popfile$t[which(popfile$phi==1)],FUN=mean)#retrieve the end of year pop sizes
Z<-tapply(X=popfile$z,INDEX=popfile$t,FUN=mean)#retrieve the end of year pop sizes
if(length(popSize)+1==length(Camemberts)){popSize<-c(popSize,0)}
plot(Camemberts,popSize)
plot(Z,popSize)
summary(lm(popSize~1+Camemberts)
summary(lm(popSize~1+Z))
summary(lm(popSize~1+Z*Camemberts))
summary(lm(popSize~1+Z+Camemberts))
####Ellner approach####
####We must look only at adult females because only them have Annual Reproductive Success values.
females<-popfile[which(popfile$s=="F" & popfile$age>0),]
plot(density(log(females$ARS)))#very grossly gaussian distribution
#### log(ARS) ~ C+z (Hairston)
evol<-vector(length=max(females$t)-1)
ecol<-vector(length=max(females$t)-1)
for (tempus in 2:max(females$t))
{
mARS0<-glm(ARS~1+C+z,data=females[which(females$t==(tempus-1) | females$t==tempus),],family=poisson)
intercept<-mARS0$coefficients[1]
dXdz<-mARS0$coefficients["z"]
dXdk<-mARS0$coefficients["C"]
Zt0<-mean(females$z[which(females$t==(tempus-1))])
kt0<-mean(females$C[which(females$t==(tempus-1))])
Zt1<-mean(females$z[which(females$t==tempus)])
kt1<-mean(females$C[which(females$t==tempus)])
Xtt<-mean(females$ARS[which(females$t==(tempus-1))]) # not exactly exp(intercept+Zt0*dXdz+kt0*dXdk) I do not want to account for the interaction (??) as it is problematic for XtT and XTt
XTT<-mean(females$ARS[which(females$t==tempus)])# not exactly exp(intercept+Zt1*dXdz+kt1*dXdk) I do not want to account for the interaction (??) as it is problematic for XtT and XTt
XtT<-exp(intercept+Zt0*dXdz+kt1*dXdk)#this is done with population mean assumption. Would be impossible on an individual basis
XTt<-exp(intercept+Zt1*dXdz+kt0*dXdk)#this is done with population mean assumption. Would be impossible on an individual basis
AX<-as.data.frame(matrix(data=c(Xtt,XTt,XtT,XTT,0,1,0,1,0,0,1,1),ncol=3))
m1X<-(lm(V1~V2+V3,data=AX))
evol[tempus-1]<-0.5*(XTT-XtT + XTt-Xtt)
ecol[tempus-1]<-0.5*(XTT-XTt + XtT-Xtt)
}
plot(log(tapply(X=females$ARS,INDEX=females$t,FUN=mean)),ylim=c(-5,6),type="b")#log to see better what is going on at g13
points(evol/sd(evol),x=2:30-0.5,type="l",col="red")
points(ecol/sd(ecol),x=2:30-0.5,type="l",col="blue")
points(abs(evol)/abs(ecol),x=2:30-0.5,type="l",col="green")
legend(x="bottomright",legend=c("log(Noff)","Evol","Ecol","Abs(evol/ecol)"),col=c("black","red","blue","green"),lwd=2)
mean(abs(evol)/abs(ecol))
plot(ecol)
plot(abs(evol)/(abs(ecol)+abs(evol)))
mean(abs(evol)/(abs(ecol)+abs(evol)))
mean(abs(evol)/(abs(ecol)))
###############################################
########## log(ARS) ~ C+zE+zG (Ellner)#########
evol<-vector(length=max(females$t)-1)
plas<-vector(length=max(females$t)-1)
ecol<-vector(length=max(females$t)-1)
for (tempus in 2:max(females$t))
{
mARS1<-glm(ARS~1+C+z+bvs,data=females[which(females$t==(tempus-1) | females$t==tempus),],family=poisson)
intercept<-mARS0$coefficients[1]
dXdz<-mARS1$coefficients["z"]
dXdk<-mARS1$coefficients["C"]
dXdg<-mARS1$coefficients["bvs"]
Zt0<-mean(females$z[which(females$t==(tempus-1))])
kt0<-mean(females$C[which(females$t==(tempus-1))])
gt0<-mean(females$bvs[which(females$t==(tempus-1))])
Zt1<-mean(females$z[which(females$t==tempus)])
kt1<-mean(females$C[which(females$t==tempus)])
gt1<-mean(females$bvs[which(females$t==tempus)])
Xttt<-mean(females$ARS[which(females$t==(tempus-1))]) # not exactly exp(intercept+Zt0*dXdz+kt0*dXdk) I do not want to account for the interaction (??) as it is problematic for XtT and XTt
XTTT<-mean(females$ARS[which(females$t==tempus)])# not exactly exp(intercept+Zt1*dXdz+kt1*dXdk) I do not want to account for the interaction (??) as it is problematic for XtT and XTt
XttT<-exp(intercept+Zt0*dXdz+gt0*dXdg+kt1*dXdk)#this is done with population mean assumption. Would be impossible on an individual basis
XtTt<-exp(intercept+Zt0*dXdz+gt1*dXdg+kt0*dXdk)#this is done with population mean assumption. Would be impossible on an individual basis
XtTT<-exp(intercept+Zt0*dXdz+gt1*dXdg+kt1*dXdk)#this is done with population mean assumption. Would be impossible on an individual basis
XTtt<-exp(intercept+Zt1*dXdz+gt0*dXdg+kt0*dXdk)#this is done with population mean assumption. Would be impossible on an individual basis
XTtT<-exp(intercept+Zt1*dXdz+gt0*dXdg+kt1*dXdk)#this is done with population mean assumption. Would be impossible on an individual basis
XTTt<-exp(intercept+Zt1*dXdz+gt1*dXdg+kt0*dXdk)#this is done with population mean assumption. Would be impossible on an individual basis
AX<-data.frame(c(Xttt,XttT,XtTt,XtTT,XTtt,XTtT,XTTt,XTTT),c(0,0,0,0,1,1,1,1),c(0,0,1,1,0,0,1,1),c(0,1,0,1,0,1,0,1))
names(AX)<-c("X","Z","G","K")
m1X<-(lm(X~Z+G+K,data=AX))
evol[tempus-1]<-m1X$coefficients["G"]
plas[tempus-1]<-m1X$coefficients["Z"]
ecol[tempus-1]<-m1X$coefficients["K"]
}
plot(log(tapply(X=females$ARS,INDEX=females$t,FUN=mean)),ylim=c(-5,6),type="b")#log to see better what is going on at g13
points(evol,x=2:30-0.5,type="l",col="red")
points(plas,x=2:30-0.5,type="l",col="purple")
points(ecol,x=2:30-0.5,type="l",col="blue")
points(abs(evol)/(abs(ecol)+abs(plas)),x=2:30-0.5,type="l",col="green")
legend(x="bottomright",legend=c("log(Noff)","Evol","Ecol","Abs(evol/ecol)"),col=c("black","red","blue","green"),lwd=2)
mean(abs(evol)/abs(ecol))
mean(abs(evol)/(abs(ecol)+abs(plas)))
plot(abs(evol)/(abs(ecol)+abs(plas)+abs(evol)),x=2:30-0.5,type="l",col="red",ylim=c(0,1))
points(abs(plas)/(abs(ecol)+abs(plas)+abs(evol)),x=2:30-0.5,type="l",col="purple")
points(abs(ecol)/(abs(ecol)+abs(plas)+abs(evol)),x=2:30-0.5,type="l",col="blue")
plot(evol)
popfile[which(popfile$t==13),]
#Conclusion: it is amazing how many fancy plots and numbers you can get out of nonsensical simulations and methods.
#quantitative genetics
library("MCMCglmm")
library("MasterBayes")
library("pedantics")
ped<-popfile[,c("ID","p1","p2")]
ped<-unique(ped[,c("ID","p1","p2")])
ped<-orderPed(ped)
names(ped)=c("id","dam","sire")
popfile$animal<-popfile$ID
#pedMass<-prunePed(ped,keep=c(levels(AllM$id[which(is.na(AllM$Weight)==F)]),levels(indinfo$id[which(indinfo$Ghost==T)])),make.base=T)
prior1 <- list(G = list(G1 = list(V = 1, nu = 0.002),G2 = list(V = 1, nu = 0.002)), R = list(V = 1,nu = 0.002))
AM0 <- MCMCglmm(z~ 1+age, random = ~animal+ID, data = popfile,pedigree=ped,prior = prior1, verbose = TRUE ,nitt = 13000, thin = 10, burnin = 3000)
summary(AM0)
plot(AM0)
autocorr(AM0$VCV)
posterior.heritability1<-AM0$VCV[,"animal"]/(AM0$VCV[,"animal"]+AM0$VCV[,"units"]+AM0$VCV[,"ID"])
plot(posterior.heritability1)
HPDinterval(posterior.heritability1,0.95)
posterior.mode(posterior.heritability1)
#just for the fun of pointing out the absurdity of our simulations:
AM1 <- MCMCglmm(z~ 1, random = ~animal+ID, data = popfile[which(popfile$age==0),],pedigree=ped,prior = prior1, verbose = TRUE ,nitt = 13000, thin = 10, burnin = 3000)
summary(AM1)
plot(AM1)
autocorr(AM1$VCV)
posterior.heritability2<-AM1$VCV[,"animal"]/(AM1$VCV[,"animal"]+AM1$VCV[,"units"]+AM1$VCV[,"ID"])
plot(posterior.heritability2)
HPDinterval(posterior.heritability2,0.95)
posterior.mode(posterior.heritability2)# of course almost 1 for new born. We should make this trait a bit more complex!