Loading 'brms' package (version 2.21.0). Useful instructions
can be found by typing help('brms'). A more detailed introduction
to the package is available through vignette('brms_overview').
Attaching package: 'brms'
The following object is masked from 'package:stats':
ar
Fitting a multivariate model in brms involves several new consideration above those for fitting univariate models. First, we need to create two models/objects with the function bf fitting the desired univariate model structure for each response variable (here bwt and tarsus). It is the equivalent of writing mvbf(bwt, tarsus), but the advantage to create two distinct model is to specific different model structure (fixed or random effect) for each response variable.
Then, the two objects/models are added into a third model to quantify all the estimates in addition to their covariance. Contrary to MCMCglmm or asreml-R, brms directly estimate the covariance and the correlation in its outputs. Our most basic model can be specified as:
bf_bwt<-bf(bwt~1+(1|a|gr(animal, cov =Amat)))bf_tarsus<-bf(tarsus~1+(1|a|gr(animal, cov =Amat)))brms_m2.1<-brm(bf_bwt+bf_tarsus+set_rescor(TRUE), data =gryphon, data2 =list(Amat =Amat), chains =2, cores =2, iter =1000)save(brms_m2.1, file ="r-obj/brms_m2_1.rda")
Again we have provided the data from one such run. It can be accessed using the code:
Warning: Parts of the model have not converged (some Rhats are > 1.05). Be
careful when analysing the results! We recommend running more iterations and/or
setting stronger priors.
Family: MV(gaussian, gaussian)
Links: mu = identity; sigma = identity
mu = identity; sigma = identity
Formula: bwt ~ 1 + (1 | p | gr(animal, cov = Amat))
tarsus ~ 1 + (1 | p | gr(animal, cov = Amat))
Data: gryphon (Number of observations: 683)
Draws: 2 chains, each with iter = 1000; warmup = 500; thin = 1;
total post-warmup draws = 1000
Multilevel Hyperparameters:
~animal (Number of levels: 683)
Estimate Est.Error l-95% CI u-95% CI Rhat
sd(bwt_Intercept) 1.81 0.21 1.41 2.20 1.06
sd(tarsus_Intercept) 3.44 0.43 2.49 4.25 1.05
cor(bwt_Intercept,tarsus_Intercept) 0.38 0.14 0.08 0.62 1.02
Bulk_ESS Tail_ESS
sd(bwt_Intercept) 31 192
sd(tarsus_Intercept) 61 173
cor(bwt_Intercept,tarsus_Intercept) 101 232
Regression Coefficients:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
bwt_Intercept 7.49 0.16 7.20 7.79 1.00 608 839
tarsus_Intercept 20.47 0.30 19.92 21.03 1.00 868 803
Further Distributional Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma_bwt 1.97 0.16 1.66 2.28 1.06 27 172
sigma_tarsus 4.24 0.30 3.63 4.82 1.04 72 162
Residual Correlations:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
rescor(bwt,tarsus) 0.39 0.09 0.21 0.55 1.02 95 179
Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.457051 0.090878 0.002874 0.011675
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
0.2878 0.3926 0.4596 0.5254 0.6297
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.397237 0.087350 0.002762 0.009971
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
0.2174 0.3390 0.3982 0.4553 0.5682
It is also possible to extract the correlation. Just to remember it is an example, the correlation distribution is skewed to 1 due to a weak prior and model parameters. Note, since
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.381406 0.138001 0.004364 0.014946
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
0.07581 0.30354 0.39041 0.47497 0.62090
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.388875 0.085105 0.002691 0.009215
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
0.2128 0.3319 0.3913 0.4484 0.5527
Here, some simple code to plot the genetic correlation.
plot(tarsus_Intercept~bwt_Intercept, bl_m2.1, xlab ="", ylab ="", xlim =c(min(bl_m2.1$bwt_Intercept_lo), max(bl_m2.1$bwt_Intercept_up)), ylim =c(min(bl_m2.1$tarsus_Intercept_lo), max(bl_m2.1$tarsus_Intercept_up)), las =1.2, type ="n")with(bl_m2.1,segments( x0 =bwt_Intercept, y0 =tarsus_Intercept_lo, x1 =bwt_Intercept, y1 =tarsus_Intercept_up, col ="black"))with(bl_m2.1, segments( x0 =bwt_Intercept_lo, y0 =tarsus_Intercept, x1 =bwt_Intercept_up, y1 =tarsus_Intercept, col ="black"))points(tarsus_Intercept~bwt_Intercept, bl_m2.1, pch =16, col ="red", cex =1.5)points(tarsus_Intercept~bwt_Intercept, bl_m2.1, pch =1, col =rgb(0, 0, 0, 0.3), cex =c(1.5))mtext("btw (BV±CI)", side =1, line =2.4)mtext("tarsus (BV±CI)", side =2, line =2, las =3)
7.0.2 Adding fixed and random effects
Fixed and random effects can be added just as for the univariate case. Given that our full model of bwt from tutorial 1 had sex as a fixed effect as well as random effects of byear and mother, we could specify a bivariate formulation of this using the following code (including a line to save the output):
bf_bwt_2<-bf(bwt~1+sex+(1|a|gr(animal, cov =Amat))+(1|b|byear)+(1|c|mother))bf_tarsus_2<-bf(tarsus~1+sex+(1|a|gr(animal, cov =Amat))+(1|b|byear)+(1|c|mother))brms_m2.2<-brm(bf_bwt_2+bf_tarsus_2+set_rescor(TRUE), data =gryphon, data2 =list(Amat =Amat), chains =2, cores =2, iter =1000)save(brms_m2.2, file ="r-obj/brms_m2_2.rda")
Again we have provided the data from one such run. It can be accessed using the code:
Warning: Parts of the model have not converged (some Rhats are > 1.05). Be
careful when analysing the results! We recommend running more iterations and/or
setting stronger priors.
Warning: There were 4 divergent transitions after warmup. Increasing
adapt_delta above 0.8 may help. See
http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
Family: MV(gaussian, gaussian)
Links: mu = identity; sigma = identity
mu = identity; sigma = identity
Formula: bwt ~ 1 + sex + (1 | a | gr(animal, cov = Amat)) + (1 | b | byear) + (1 | c | mother)
tarsus ~ 1 + sex + (1 | a | gr(animal, cov = Amat)) + (1 | b | byear) + (1 | c | mother)
Data: gryphon (Number of observations: 683)
Draws: 2 chains, each with iter = 1000; warmup = 500; thin = 1;
total post-warmup draws = 1000
Multilevel Hyperparameters:
~animal (Number of levels: 683)
Estimate Est.Error l-95% CI u-95% CI Rhat
sd(bwt_Intercept) 1.31 0.21 0.86 1.69 1.06
sd(tarsus_Intercept) 2.88 0.47 1.88 3.70 1.01
cor(bwt_Intercept,tarsus_Intercept) 0.60 0.17 0.18 0.89 1.09
Bulk_ESS Tail_ESS
sd(bwt_Intercept) 54 58
sd(tarsus_Intercept) 52 162
cor(bwt_Intercept,tarsus_Intercept) 25 30
~byear (Number of levels: 34)
Estimate Est.Error l-95% CI u-95% CI Rhat
sd(bwt_Intercept) 0.99 0.17 0.71 1.39 1.00
sd(tarsus_Intercept) 2.02 0.34 1.45 2.78 1.00
cor(bwt_Intercept,tarsus_Intercept) 0.01 0.22 -0.44 0.44 1.01
Bulk_ESS Tail_ESS
sd(bwt_Intercept) 424 509
sd(tarsus_Intercept) 525 699
cor(bwt_Intercept,tarsus_Intercept) 478 492
~mother (Number of levels: 352)
Estimate Est.Error l-95% CI u-95% CI Rhat
sd(bwt_Intercept) 1.14 0.12 0.90 1.36 1.01
sd(tarsus_Intercept) 2.09 0.29 1.54 2.67 1.01
cor(bwt_Intercept,tarsus_Intercept) -0.64 0.20 -0.97 -0.24 1.02
Bulk_ESS Tail_ESS
sd(bwt_Intercept) 370 764
sd(tarsus_Intercept) 134 420
cor(bwt_Intercept,tarsus_Intercept) 84 147
Regression Coefficients:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
bwt_Intercept 6.28 0.24 5.80 6.73 1.00 453 703
tarsus_Intercept 20.39 0.52 19.45 21.39 1.00 755 760
bwt_sex2 2.05 0.17 1.71 2.37 1.00 1097 715
tarsus_sex2 0.11 0.42 -0.67 0.90 1.00 780 578
Further Distributional Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma_bwt 1.40 0.16 1.05 1.68 1.04 59 60
sigma_tarsus 3.73 0.32 3.14 4.32 1.00 55 176
Residual Correlations:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
rescor(bwt,tarsus) 0.89 0.07 0.72 0.98 1.50 4 24
Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
Evaluation of the statistical support for these genetic and maternal correlations is straightforward. Because we imposed no constraint on their estimation, we can evaluate the extent to which the posterior distributions overlap zero:
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.596151 0.172794 0.005464 0.028178
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
0.1850 0.5065 0.6117 0.7064 0.8930
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
-0.641333 0.196810 0.006224 0.021355
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
-0.9746 -0.7933 -0.6507 -0.5027 -0.2367
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.013514 0.220619 0.006977 0.009633
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
-0.43678 -0.13505 0.02408 0.16947 0.44123
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.892512 0.067229 0.002126 0.026496
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
0.7168 0.8620 0.9069 0.9425 0.9775
Note, brms estimates the correlation and also the covariance. We can also recalculate the correlation directly from the covariance.To facilitate the extraction of the different parameter, we can the fucntion as_draws_df
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.596151 0.172794 0.005464 0.028178
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
0.1850 0.5065 0.6117 0.7064 0.8930
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.013514 0.220619 0.006977 0.009633
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
-0.43678 -0.13505 0.02408 0.16947 0.44123
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
1.01862 0.46337 0.01465 0.06126
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
0.1514 0.6969 1.0310 1.3241 2.0032
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.892512 0.067229 0.002126 0.026496
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
0.7168 0.8620 0.9069 0.9425 0.9775
Warning: Parts of the model have not converged (some Rhats are > 1.05). Be
careful when analysing the results! We recommend running more iterations and/or
setting stronger priors.
Warning: There were 6 divergent transitions after warmup. Increasing
adapt_delta above 0.8 may help. See
http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
Family: MV(gaussian, gaussian)
Links: mu = identity; sigma = identity
mu = identity; sigma = identity
Formula: bwt ~ 1 + sex + ((1 | a | gr(animal, cov = Amat, by = sex))) + (1 | b | byear) + (1 | c | mother)
tarsus ~ 1 + sex + (1 | a | gr(animal, cov = Amat, by = sex)) + (1 | b | byear) + (1 | c | mother)
Data: gryphon (Number of observations: 683)
Draws: 2 chains, each with iter = 1000; warmup = 500; thin = 1;
total post-warmup draws = 1000
Multilevel Hyperparameters:
~animal (Number of levels: 683)
Estimate Est.Error l-95% CI
sd(bwt_Intercept:sex1) 1.05 0.28 0.48
sd(tarsus_Intercept:sex1) 1.57 0.78 0.14
sd(bwt_Intercept:sex2) 1.23 0.24 0.71
sd(tarsus_Intercept:sex2) 3.24 0.48 2.17
cor(bwt_Intercept:sex1,tarsus_Intercept:sex1) 0.32 0.43 -0.78
cor(bwt_Intercept:sex2,tarsus_Intercept:sex2) 0.70 0.12 0.45
u-95% CI Rhat Bulk_ESS Tail_ESS
sd(bwt_Intercept:sex1) 1.58 1.27 6 39
sd(tarsus_Intercept:sex1) 3.03 1.08 24 117
sd(bwt_Intercept:sex2) 1.65 1.24 7 46
sd(tarsus_Intercept:sex2) 4.13 1.05 37 79
cor(bwt_Intercept:sex1,tarsus_Intercept:sex1) 0.90 1.07 25 202
cor(bwt_Intercept:sex2,tarsus_Intercept:sex2) 0.90 1.09 28 44
~byear (Number of levels: 34)
Estimate Est.Error l-95% CI u-95% CI Rhat
sd(bwt_Intercept) 0.97 0.14 0.70 1.26 1.01
sd(tarsus_Intercept) 2.03 0.34 1.47 2.80 1.00
cor(bwt_Intercept,tarsus_Intercept) 0.01 0.21 -0.41 0.41 1.01
Bulk_ESS Tail_ESS
sd(bwt_Intercept) 282 292
sd(tarsus_Intercept) 324 361
cor(bwt_Intercept,tarsus_Intercept) 183 393
~mother (Number of levels: 352)
Estimate Est.Error l-95% CI u-95% CI Rhat
sd(bwt_Intercept) 1.18 0.11 0.96 1.39 1.02
sd(tarsus_Intercept) 2.04 0.34 1.33 2.65 1.09
cor(bwt_Intercept,tarsus_Intercept) -0.63 0.21 -0.97 -0.23 1.08
Bulk_ESS Tail_ESS
sd(bwt_Intercept) 170 352
sd(tarsus_Intercept) 24 60
cor(bwt_Intercept,tarsus_Intercept) 22 100
Regression Coefficients:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
bwt_Intercept 6.27 0.22 5.82 6.68 1.00 195 123
tarsus_Intercept 20.35 0.48 19.35 21.21 1.00 494 578
bwt_sex2 2.04 0.17 1.71 2.36 1.01 265 384
tarsus_sex2 0.14 0.39 -0.60 0.88 1.00 691 621
Further Distributional Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma_bwt 1.49 0.15 1.16 1.74 1.33 5 81
sigma_tarsus 3.88 0.27 3.27 4.35 1.04 42 85
Residual Correlations:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
rescor(bwt,tarsus) 0.87 0.05 0.78 0.94 1.04 22 94
Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
However, this model is lacking an important and essentiel group-specific partitionning (we do with the asreml-R and MCMCglmm). We need to partition the residual variance (or sigma) as well. Doing so, we will use the argument `sigma`` to parititon the model by sex. To avoid an estimation of the difference between sexes, we need to remove the estimate of the intercept at the sigma level.
bf_bwt_4<-bf(bwt~1+sex+((1|a|gr(animal, cov =Amat, by =sex)))+(1|b|byear)+(1|c|mother), sigma~sex-1)bf_tarsus_4<-bf(tarsus~1+sex+(1|a|gr(animal, cov =Amat, by =sex))+(1|b|byear)+(1|c|mother), sigma~sex-1)brms_m2.4<-brm(bf_bwt_4+bf_tarsus_4+set_rescor(TRUE), data =gryphon, data2 =list(Amat =Amat), chains =2, cores =2, iter =1000)save(brms_m2.4, file ="r-obj/brms_m2_4.rda")
Again we have provided the data from one such run. It can be accessed using the code:
Warning: Parts of the model have not converged (some Rhats are > 1.05). Be
careful when analysing the results! We recommend running more iterations and/or
setting stronger priors.
Warning: There were 6 divergent transitions after warmup. Increasing
adapt_delta above 0.8 may help. See
http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
Family: MV(gaussian, gaussian)
Links: mu = identity; sigma = log
mu = identity; sigma = log
Formula: bwt ~ 1 + sex + ((1 | a | gr(animal, cov = Amat, by = sex))) + (1 | b | byear) + (1 | c | mother)
sigma ~ sex - 1
tarsus ~ 1 + sex + (1 | a | gr(animal, cov = Amat, by = sex)) + (1 | b | byear) + (1 | c | mother)
sigma ~ sex - 1
Data: gryphon (Number of observations: 683)
Draws: 2 chains, each with iter = 1000; warmup = 500; thin = 1;
total post-warmup draws = 1000
Multilevel Hyperparameters:
~animal (Number of levels: 683)
Estimate Est.Error l-95% CI
sd(bwt_Intercept:sex1) 0.86 0.35 0.14
sd(tarsus_Intercept:sex1) 1.40 0.82 0.10
sd(bwt_Intercept:sex2) 1.40 0.23 0.94
sd(tarsus_Intercept:sex2) 3.49 0.72 1.96
cor(bwt_Intercept:sex1,tarsus_Intercept:sex1) 0.28 0.51 -0.85
cor(bwt_Intercept:sex2,tarsus_Intercept:sex2) 0.78 0.10 0.56
u-95% CI Rhat Bulk_ESS Tail_ESS
sd(bwt_Intercept:sex1) 1.48 1.00 59 101
sd(tarsus_Intercept:sex1) 3.00 1.07 45 111
sd(bwt_Intercept:sex2) 1.83 1.04 42 40
sd(tarsus_Intercept:sex2) 4.62 1.22 8 50
cor(bwt_Intercept:sex1,tarsus_Intercept:sex1) 0.94 1.05 60 287
cor(bwt_Intercept:sex2,tarsus_Intercept:sex2) 0.97 1.09 30 94
~byear (Number of levels: 34)
Estimate Est.Error l-95% CI u-95% CI Rhat
sd(bwt_Intercept) 0.97 0.15 0.73 1.30 1.01
sd(tarsus_Intercept) 2.01 0.33 1.41 2.70 1.00
cor(bwt_Intercept,tarsus_Intercept) -0.00 0.22 -0.42 0.45 1.02
Bulk_ESS Tail_ESS
sd(bwt_Intercept) 283 412
sd(tarsus_Intercept) 349 554
cor(bwt_Intercept,tarsus_Intercept) 225 256
~mother (Number of levels: 352)
Estimate Est.Error l-95% CI u-95% CI Rhat
sd(bwt_Intercept) 1.19 0.11 0.98 1.42 1.00
sd(tarsus_Intercept) 2.14 0.30 1.57 2.71 1.05
cor(bwt_Intercept,tarsus_Intercept) -0.55 0.21 -0.96 -0.16 1.04
Bulk_ESS Tail_ESS
sd(bwt_Intercept) 279 434
sd(tarsus_Intercept) 46 227
cor(bwt_Intercept,tarsus_Intercept) 46 122
Regression Coefficients:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
bwt_Intercept 6.27 0.23 5.82 6.73 1.01 336 556
tarsus_Intercept 20.39 0.49 19.42 21.37 1.00 384 509
bwt_sex2 2.04 0.17 1.70 2.38 1.00 483 507
sigma_bwt_sex1 0.45 0.12 0.18 0.63 1.00 68 128
sigma_bwt_sex2 0.26 0.17 -0.17 0.52 1.06 38 33
tarsus_sex2 0.10 0.41 -0.69 0.92 1.00 658 659
sigma_tarsus_sex1 1.37 0.08 1.19 1.50 1.04 65 215
sigma_tarsus_sex2 1.24 0.16 0.91 1.50 1.22 7 67
Residual Correlations:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
rescor(bwt,tarsus) 0.84 0.05 0.73 0.93 1.15 11 29
Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
Evaluation of the statistical support for these sex-specific correlations is straightforward. Because we imposed no constraint on their estimation, we can evaluate the extent to which the posterior distributions overlap zero or overlap each other:
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.27734 0.51195 0.01619 0.05981
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
-0.84800 -0.02476 0.43226 0.67124 0.93982
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.778166 0.098502 0.003115 0.013827
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
0.5640 0.7240 0.7805 0.8424 0.9700
#par(mfrow =c(1, 2))plot(tarsus_Intercept~bwt_Intercept, FEM, xlab ="", ylab ="", xlim =c(min(FEM$bwt_Intercept_lo), max(FEM$bwt_Intercept_up)), ylim =c(min(FEM$tarsus_Intercept_lo), max(FEM$tarsus_Intercept_up)), las =1.2, type ="n")segments( x0 =FEM$bwt_Intercept, y0 =FEM$tarsus_Intercept_lo, x1 =FEM$bwt_Intercept, y1 =FEM$tarsus_Intercept_up, col ="black")segments( x0 =FEM$bwt_Intercept_lo, y0 =FEM$tarsus_Intercept, x1 =FEM$bwt_Intercept_up, y1 =FEM$tarsus_Intercept, col ="black")points(tarsus_Intercept~bwt_Intercept, FEM, pch =16, col ="red", cex =1.5)points(tarsus_Intercept~bwt_Intercept, FEM, pch =1, col =rgb(0, 0, 0, 0.3), cex =c(1.5))mtext("btw (BV±CI)", side =1, line =2.4)mtext("tarsus (BV±CI)", side =2, line =2, las =3)#plot(tarsus_Intercept~bwt_Intercept, MAL, xlab ="", ylab ="", xlim =c(min(MAL$bwt_Intercept_lo), max(MAL$bwt_Intercept_up)), ylim =c(min(MAL$tarsus_Intercept_lo), max(MAL$tarsus_Intercept_up)), las =1.2, type ="n")segments( x0 =MAL$bwt_Intercept, y0 =MAL$tarsus_Intercept_lo, x1 =MAL$bwt_Intercept, y1 =MAL$tarsus_Intercept_up, col ="black")segments( x0 =MAL$bwt_Intercept_lo, y0 =MAL$tarsus_Intercept, x1 =MAL$bwt_Intercept_up, y1 =MAL$tarsus_Intercept, col ="black")points(tarsus_Intercept~bwt_Intercept, MAL, pch =17, col ="blue", cex =1.5)points(tarsus_Intercept~bwt_Intercept, MAL, pch =1, col =rgb(0, 0, 0, 0.3), cex =c(1.5))mtext("btw (BV±CI)", side =1, line =2.4)mtext("tarsus (BV±CI)", side =2, line =2, las =3)
7.0.4 Between groups (co)variances and the B-matrix
Animal models are amazing model. With different group within a population, it is also possible to estimate how much the different groups shared the same genetic via the cross-group genetic covariance. This covariance is essential to understand ontogenic or sexual conflict, which can constraint or enhanced response to evolution. As an example, we estimate the cross-sex genetic correlation r_{fm}
It is important to keep in mind the covariance matrix at the residual level is zero and it is important to avoid estimating the cross-sex residual covariance because no individual switched sex during the experiment.
Note: the way of partitionning variance per sex is a bit different then the previous code “,by=sex”.
This code is faster and also easier to understand. Note, it is possible to play with the | or || to estimate or not covariance between sexes.
bf_bwt_5<-bf(bwt~1+sex+(0+sex|a|gr(animal, cov =Amat))+(0+sex|b|mother)+(0+sex|c|byear),sigma~sex-1)bf_tarsus_5<-bf(tarsus~1+sex+(0+sex|a|gr(animal, cov =Amat))+(0+sex|b|mother)+(0+sex|c|byear),sigma~sex-1)brms_m2.5<-brm(bf_bwt_5+bf_tarsus_5+set_rescor(TRUE), data =gryphon, data2 =list(Amat =Amat), chains =2, cores =2, iter =1000)save(brms_m2.5, file ="r-obj/brms_m2_5.rda")
Again we have provided the data from one such run. It can be accessed using the code:
Warning: Parts of the model have not converged (some Rhats are > 1.05). Be
careful when analysing the results! We recommend running more iterations and/or
setting stronger priors.
Warning: There were 45 divergent transitions after warmup. Increasing
adapt_delta above 0.8 may help. See
http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
The cross-sex genetic correlation can estimate form the output of the model. For tarsus length at fledging, sexes shared a lot of genetic variance which is commun for a trait with low sexual dimorphism. If the selection is antagonistic between males and females, sexes can not evolve freely from the other sexes and a intralocus sexual conflict can appeared.
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.482346 0.287205 0.009082 0.032430
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
-0.2354 0.3433 0.5365 0.6861 0.8699
Iterations = 1:1000
Thinning interval = 1
Number of chains = 1
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
0.440143 0.292918 0.009263 0.048863
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
-0.2962 0.2950 0.4846 0.6419 0.8720
Here, some simple code to plot the cross-sex genetic correlation.
par(mfrow =c(1, 2))plot(bwt_sex2~bwt_sex1, bl_m2.5, xlab ="", ylab ="", las =1.2, type ="n", xlim =c(min(bl_m2.5$bwt_sex1_lo), max(bl_m2.5$bwt_sex1_up)), ylim =c(min(bl_m2.5$bwt_sex2_lo), max(bl_m2.5$bwt_sex2_up)))with(bl_m2.5, segments(x0 =bwt_sex1, y0 =bwt_sex2_lo, x1 =bwt_sex1, y1 =bwt_sex2_up, col ="black"))with(bl_m2.5, segments(x0 =bwt_sex1_lo, y0 =bwt_sex2, x1 =bwt_sex1_up, y1 =bwt_sex2, col ="black"))points(bwt_sex2~bwt_sex1, bl_m2.5, pch =16, col ="red", cex =1.5)points(bwt_sex2~bwt_sex1, bl_m2.5, pch =1, col =rgb(0, 0, 0, 0.3), cex =c(1.5))mtext("bwt male (BV±CI)", side =1, line =2.4)mtext("bwt female (BV±CI)", side =2, line =2, las =3)plot(tarsus_sex2~tarsus_sex1, bl_m2.5, xlab ="", ylab ="", las =1.2, type ="n", xlim =c(min(bl_m2.5$tarsus_sex1_lo), max(bl_m2.5$tarsus_sex1_up)), ylim =c(min(bl_m2.5$tarsus_sex2_lo), max(bl_m2.5$tarsus_sex2_up)))with(bl_m2.5, segments(x0 =tarsus_sex1, y0 =tarsus_sex2_lo, x1 =tarsus_sex1, y1 =tarsus_sex2_up, col ="black"))with(bl_m2.5, segments(x0 =tarsus_sex1_lo, y0 =tarsus_sex2, x1 =tarsus_sex1_up, y1 =tarsus_sex2, col ="black"))points(tarsus_sex2~tarsus_sex1, bl_m2.5, pch =16, col ="red", cex =1.5)points(tarsus_sex2~tarsus_sex1, bl_m2.5, pch =1, col =rgb(0, 0, 0, 0.3), cex =c(1.5))mtext("tarsus male (BV±CI)", side =1, line =2.4)mtext("tarsus female (BV±CI)", side =2, line =2, las =3)
Within this model, we also have acces to the rest of the B-matrix. Note, the cross-sex genetic correlation is just the diagonal of the B matrix. For now on, you can explore this matrix and estimate the cross-sex-cross-trait genetic correlation.
Wilson, A. J., D. Réale, M. N. Clements, M. M. Morrissey, E. Postma, C. A. Walling, L. E. B. Kruuk, and D. H. Nussey. 2010. An ecologist’s guide to the animal model. Journal of Animal Ecology 79:13–26.