3.4 Multivariate Random Slopes

Before reading this it is worth checking out how to simulate univariate random slopes in Section 2.4.

Here we have to think about the beta matrix. As we saw in an example above, in multivariate models beta can be thought of as switching on and off predictor variables for the response variables. We we can simulate 4 variables, an intercept and slope for each variable, and then use the beta matrix to tell simulate_population which response variable they link to

individual <- list(
  names = c("ind_int1","ind_slope1","ind_int2","ind_slope2"),
  vcov = matrix(c(
     1, 0.5, 0,  0, 
    0.5, 1,  0,  0,
     0,  0,  1, 0.2,
     0,  0, 0.2, 1
    ),nrow=4,ncol=4, byrow=TRUE),
  beta = matrix(c(
    1, 0,
    0, 0,
    0, 1,
    0, 0
    ),nrow = 4,ncol = 2, byrow=TRUE)

)

observation <- list(
  names="environment",
  beta=matrix(c(0.5,-0.3), ncol=2,byrow=TRUE)
)

residual <- list(
  vcov = matrix(c(
    1,0.5,
    0.5,1
    ),nrow = 2,ncol = 2,byrow=TRUE)
)

interactions <- list(
  names=c("ind_slope1:environment","ind_slope2:environment"),
  beta= matrix(c(
    1,0,
    0,1
    ), ncol=2,byrow=TRUE)
)

squid_data <- simulate_population(
  data_structure = make_structure(structure = "individual(100)",repeat_obs=20),
  n_response = 2,
  parameters=list(
    individual = individual, 
    observation = observation,
    residual = residual, 
    interactions = interactions
  )
)

data <- get_population_data(squid_data)
head(data,20)
##             y1         y2  ind_int1 ind_slope1 ind_int2 ind_slope2 environment
## 1  -1.07284118 -0.7063502 -1.379322  -1.675724 -2.07382 -0.6038441  0.27241358
## 2  -2.73615771 -3.8998721 -1.379322  -1.675724 -2.07382 -0.6038441  1.88695292
## 3   2.30907268 -0.1346602 -1.379322  -1.675724 -2.07382 -0.6038441 -2.48201511
## 4  -0.59162679 -1.9301263 -1.379322  -1.675724 -2.07382 -0.6038441 -0.52459897
## 5  -1.79334628 -1.7390556 -1.379322  -1.675724 -2.07382 -0.6038441  0.17781662
## 6   0.37926761 -1.1070232 -1.379322  -1.675724 -2.07382 -0.6038441 -0.62796804
## 7  -3.66798525 -3.1329732 -1.379322  -1.675724 -2.07382 -0.6038441  1.42127867
## 8  -1.60121532 -3.5016071 -1.379322  -1.675724 -2.07382 -0.6038441  0.73788631
## 9  -0.04876558 -0.9818298 -1.379322  -1.675724 -2.07382 -0.6038441  0.60326386
## 10  0.15490674 -1.6868303 -1.379322  -1.675724 -2.07382 -0.6038441 -0.42497852
## 11 -0.24363724  0.4803048 -1.379322  -1.675724 -2.07382 -0.6038441  0.20271543
## 12 -0.82656505  1.7013557 -1.379322  -1.675724 -2.07382 -0.6038441 -0.32382136
## 13  0.44331446 -0.4691145 -1.379322  -1.675724 -2.07382 -0.6038441 -1.15975731
## 14  1.87942657  0.4287303 -1.379322  -1.675724 -2.07382 -0.6038441 -1.76811131
## 15 -1.98410710 -2.4175230 -1.379322  -1.675724 -2.07382 -0.6038441  0.28958932
## 16 -1.12883705 -0.6458835 -1.379322  -1.675724 -2.07382 -0.6038441 -0.11048588
## 17 -2.10790515 -2.9632071 -1.379322  -1.675724 -2.07382 -0.6038441  1.15822172
## 18  1.16587270 -0.9111980 -1.379322  -1.675724 -2.07382 -0.6038441 -1.37427364
## 19 -0.87193964 -2.8776114 -1.379322  -1.675724 -2.07382 -0.6038441 -0.03008455
## 20 -0.21801425 -2.0496166 -1.379322  -1.675724 -2.07382 -0.6038441 -0.10592694
##     residual1    residual2 ind_slope1:environment ind_slope2:environment
## 1   0.6267639  1.613689536            -0.45648984            -0.16449532
## 2   0.8616993 -0.120540569            -3.16201141            -1.13942532
## 3   0.7702311 -0.304194525             4.15917112             1.49875008
## 4   0.1709119 -0.330461646             0.87908283             0.31677597
## 5  -0.2049611  0.495483184            -0.29797150            -0.10737351
## 6   1.0202728  0.399211998             1.05230082             0.37919477
## 7  -0.6176324  0.225461374            -2.38167012            -0.85823069
## 8   0.6456570 -0.760852634            -1.23649346            -0.44556827
## 9   2.0398280  1.637247029            -1.01090344            -0.36427730
## 10  1.0345715  0.002875761             0.71214650             0.25662075
## 11  1.3740221  2.737348294            -0.33969501            -0.12240851
## 12  0.1720326  3.482491999             0.54263507             0.19553761
## 13  0.4590825  0.556466028             1.94343262             0.70031257
## 14  1.1799385  0.904453719             2.96286573             1.06766351
## 15 -0.2643081 -0.081959097            -0.48527165            -0.17486679
## 16  0.1205841  1.328074778             0.18514379             0.06671624
## 17  0.6331654  0.157465033            -1.94085938            -0.69938530
## 18  0.9294289 -0.079506715             2.30290268             0.82984698
## 19  0.4720113 -0.830982832             0.05041339             0.01816638
## 20  1.0367670 -0.071537731             0.17750427             0.06396335
##    individual squid_pop
## 1           1         1
## 2           1         1
## 3           1         1
## 4           1         1
## 5           1         1
## 6           1         1
## 7           1         1
## 8           1         1
## 9           1         1
## 10          1         1
## 11          1         1
## 12          1         1
## 13          1         1
## 14          1         1
## 15          1         1
## 16          1         1
## 17          1         1
## 18          1         1
## 19          1         1
## 20          1         1