-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-animal.Rmd
247 lines (189 loc) · 6.94 KB
/
05-animal.Rmd
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
# Genetic effects {#animal}
```{r, echo=FALSE, message=FALSE,warning=FALSE}
rm(list=ls())
library(dplyr)
library(flair)
library(lme4)
library(MCMCglmm)
library(scales)
library(squidSim)
library(knitr)
set.seed(25)
```
This vignette assumes that you are generally happy with how the `sim_population()` function works.
## Additive genetics effects {#va}
In order to simulate breeding values (additive genetic effects), we can provide the `simulate_population()` function with the relatedness structure in the population. The simplest way to do this is providing a pedigree using the the `pedigree` argument (a genetic relatedness matrix could also be given to the `cov_str` argument). The input to this argument needs to be a list, and the name of the pedigree in the list links it with the item in the parameter list.
**NOTE** the `simulate_population` function has very little error checking of pedigree structure at the moment
When simulating breeding values, **all** individuals in pedigree need to be in the data_structure and *vice versa*. Having unsampled individuals (for example the base population) can be achieved in the sampling stage (not implemented yet).
Lets start by importing a pedigree
```{r, eval=TRUE, message=FALSE,warning=FALSE}
library(MCMCglmm)
data(BTped)
head(BTped)
```
We can use this pedigree as a data_structure
```{r, eval=TRUE, cache=TRUE}
squid_data <- simulate_population(
data_structure = BTped,
pedigree = list(animal=BTped),
parameters =list(
animal = list(
vcov = 0.2
),
residual = list(
vcov = 0.5
)
)
)
data <- get_population_data(squid_data)
head(data)
# Ainv<-inverseA(BTped)$Ainv
# mod <- MCMCglmm(y~1, random=~ animal,data=data,ginverse=list(animal=Ainv),verbose=FALSE)
# summary(mod)
```
We might want to simulate repeated measurements to allow estimation of permanent environment effects. This is where being able to have something in the parameter list with a different name to the grouping factor is useful. In this way permanent environmental and additive genetic effects can be simulated in different parts of the parameter list, and linked to the same part of the data_structure.
```{r, eval=TRUE, cache=TRUE}
## make data structure with two observations per individual
ds <- data.frame(individual=rep(BTped[,1], 2))
squid_data <- simulate_population(
data_structure = ds,
pedigree=list(animal=BTped),
parameters = list(
individual = list(
vcov = 0.3
),
animal = list(
group="individual",
vcov = 0.2
),
residual = list(
vcov = 0.5
)
)
)
data <- get_population_data(squid_data)
head(data)
# Ainv<-inverseA(BTped)$Ainv
# data$animal_id <- data$individual
# mod <- MCMCglmm(y~1, random=~ individual + animal_id,data=data,ginverse=list(animal_id=Ainv),verbose=FALSE)
# summary(mod)
```
## Multivariate genetic effects
We can simulate genetic effects affecting multiple phenotypes and the covariance between them, by specifying the number of response variables, and a covariance matrix, instead of only a variance.
```{r, eval=TRUE, cache=TRUE}
squid_data <- simulate_population(
data_structure = BTped,
pedigree = list(animal = BTped),
n_response=2,
parameters = list(
animal = list(
vcov = diag(2)
),
residual = list(
vcov = diag(2)
)
)
)
data <- get_population_data(squid_data)
head(data)
# Ainv<-inverseA(BTped)$Ainv
# mod <- MCMCglmm(cbind(y1,y2)~1,random=~us(trait):animal, rcov=~us(trait):units,data=data,family=rep("gaussian",2),verbose=FALSE,ginverse=list(animal=Ainv))
# summary(mod)
```
<br>
## Sex specific genetic variance and inter-sexual genetic correlations
```{r,fig.width=10,fig.height=6}
ds <- data.frame(animal=BTped[,"animal"],sex=sample(c("Female","Male"),nrow(BTped), replace=TRUE))
squid_data <- simulate_population(
parameters = list(
sex=list(
fixed=TRUE,
names=c("Female","Male"),
beta=c(-0.5,0.5)
),
animal= list(
names = c("G_female","G_male"),
vcov =matrix(c(0.1,-0.1,-0.1,0.4), nrow=2, ncol=2 ,byrow=TRUE)
),
residual = list(
names="residual",
vcov = 0.1
)
),
data_structure = ds,
pedigree = list(animal=BTped),
model = "y = Female + Male + I(Female)*G_female + I(Male)*G_male + residual"
)
data <- get_population_data(squid_data)
head(data)
par(mfrow=c(1,2))
boxplot(y~factor(sex),data)
plot(G_female~G_male,data)
```
<br>
## Indirect Genetic Effects {#IGE}
Indirect genetic effects are a bit more difficult to code. Lets take the example of maternal genetic effects. The maternal genetic effect that affects an individual's phenotype, is that of its mother, not itself. Here we can use `[]` to index the levels of the random effects within the formula. We need to be careful here as internally in `simulate_population()` the indexing of the factors in the data structure is done independently. We therefore need to generate a index for the mothers that links to the individual. We can do this using the `index_link` argument - in the code below we create a new factor to index with, called `dam_link`, that is the dam factor in our data structure, that has been indexed to match the animal factor.
Using this indexing trick, we can simulate the direct genetic and maternal genetic effects that an individual has (and the covariance between them), as well as generating an individual's phenotype from its own direct genetic effects, and its mother's maternal genetic effect.
```{r, eval=TRUE, cache=TRUE}
squid_data <- simulate_population(
parameters=list(
animal = list(
names=c("direct","maternal"),
vcov = matrix(c(1,0.3,0.3,0.5),2,2)
),
residual = list(
names="residual",
vcov = 0.5
)
),
data_structure=BTped,
pedigree=list(animal=BTped),
index_link=list(dam_link="dam-animal"),
model = "y = direct + maternal[dam_link] + residual"
)
data <- get_population_data(squid_data)
head(data)
```
## GxE
Coming soon...
<!--
I dont know why this doesnt work
squid_data <- simulate_population(
parameters = list(
animal = list(
names = c("G_int","G_slope"),
mean = c(0,0),
vcov = matrix(c(1,0.3,0.3,0.5),ncol=2,nrow=2,byrow=TRUE),
beta = c(1,0)
),
observation= list(
names = c("environment"),
vcov = c(0.2)
),
residual = list(
names = c("residual"),
vcov = c(0.5)
),
interactions=list(
names = "G_slope:environment",
beta = 1
)
),
data_structure=rbind(BTped,BTped,BTped,BTped,BTped),
pedigree = list(animal=BTped)
)
data <- get_population_data(squid_data)
library(lme4)
short_summary <- function(x) print(summary(x), correlation=FALSE, show.resids=FALSE, ranef.comp = c("Variance"))
short_summary(lmer(y ~ environment + (1+environment|animal),data))
-->
## Dominance
Coming soon...
<!--
Here we can make use of the dominance relatedness matrices that can be generated in the `nadiv` package
NOTE: not working fully yet!!!
-->
## Inbreeding depression
Coming soon...
## Genetic Groups
Coming soon...