title: “Setting up priors and initial parameters in a Profile GLMM model”
output: rmarkdown::html_vignette
vignette: >
%
%
%
library(ProfileGLMM)
data("exposure_data")
exp_data = exposure_data$df
head(exp_data)
#> X t indiv Exp1 Exp2 Y
#> 1 -0.89691455 0.77952297 1 -0.04384600 1.2588400 2.6520873
#> 2 0.18484918 1.70443803 2 -0.66430177 -1.2629737 0.1597814
#> 3 1.58784533 2.22057485 3 -1.40052637 -0.1769742 5.3494451
#> 4 -1.13037567 0.08683717 4 -1.36745207 0.7744430 2.5207198
#> 5 -0.08025176 1.09472696 5 1.19371755 1.4146036 6.6933655
#> 6 0.13242028 2.02427989 6 0.06508781 0.3339544 0.2785757Here we work with the simulated dataset presented in the introduction vignette and set up the model in the same way:
covList = {}
covList$FE = c('X')
covList$RE = c('t')
covList$REunit = c('indiv')
covList$Lat = c('X')
covList$Assign$Cont = c('Exp1','Exp2')
covList$Assign$Cat = NULL
covList$Y = c('Y')
dataProfile = profileGLMM_preprocess(regType = 'linear',
covList = covList,
dataframe = exp_data,
nC = 30,
intercept = list(FE = T, RE = F, Lat = T))
print(dataProfile)
#> --- pglmm Data summary ---
#> - Number of observations : 4500
#>
#> -- Clustering model summary --
#> - Continuous clustering variables : ' Exp1 Exp2 '
#>
#> -- Outcome model summary --
#> - Model type: linear mixed model
#> - Outcome : ' Y '
#> - Fixed effects : ' Intercept X '
#> - ' indiv ' level random effects : ' t '
#> - Latent clusters interacting with: ' Intercept X 'The profileGLMM_preprocess() function creates the
quantities needed to sample from the posterior using
profileGLMM_Gibbs(). Alongside the design matrices and
model metadata, it sets default weakly informative priors in the
$prior element:
print(dataProfile$prior)
#> $FE
#> $FE$lambda
#> [1] 1e-06
#>
#> $FE$a
#> [1] 1e-06
#>
#> $FE$b
#> [1] 1e-06
#>
#>
#> $RE
#> $RE$Phi
#> [,1]
#> [1,] 1
#>
#> $RE$eta
#> [1] 1
#>
#>
#> $assign
#> $assign$Cont
#> $assign$Cont$lambda
#> [1] 1
#>
#> $assign$Cont$mu
#> [1] 0 0
#>
#> $assign$Cont$nu
#> [1] 3
#>
#> $assign$Cont$Psi
#> [,1] [,2]
#> [1,] 6 0
#> [2,] 0 6
#>
#>
#> $assign$Cat
#> $assign$Cat$alpha
#> numeric(0)
#>
#>
#>
#> $Lat
#> $Lat$eta
#> [1] 2
#>
#> $Lat$Phi
#> [,1] [,2]
#> [1,] 1 0
#> [2,] 0 1
#>
#>
#> $DP
#> $DP$scale
#> [1] 5.477226
#>
#> $DP$shape
#> [1] 5.477226Using these initial priors, profileGLMM_preprocess()
draws starting values for the MCMC chain and stores them in
$theta:
print(dataProfile$theta)
#> $sig2
#> [1] 1.222544
#>
#> $betaFE
#> [1] 902.6342 703.9178
#>
#> $SigRE
#> [,1]
#> [1,] 2.469808
#>
#> $SigLat
#> [,1] [,2]
#> [1,] 0.3093328 0.2522038
#> [2,] 0.2522038 0.8399440
#>
#> $gammaLat
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,] -1.2317650 -0.02499104 0.5249399 0.3303142 0.4350062 -1.106432 -0.03121751
#> [2,] -0.1083358 -0.03327017 1.0820459 1.0012216 0.4140539 -0.408436 -0.14953406
#> [,8] [,9] [,10] [,11] [,12] [,13]
#> [1,] -0.8179986 0.2324495 -0.05716816 -0.02992512 -0.2308104 -0.03298874
#> [2,] -1.0477452 1.2716278 0.26214751 -1.12114520 -0.5022120 0.84920916
#> [,14] [,15] [,16] [,17] [,18] [,19]
#> [1,] 0.4244608 -0.1409139 0.3096032 -0.39349250 0.4274403 0.4900518
#> [2,] 0.2150360 0.4402010 -0.2961290 -0.03045245 0.2590215 0.7166147
#> [,20] [,21] [,22] [,23] [,24] [,25]
#> [1,] -0.340394970 -0.6281257 1.1014528 -0.5807236 -0.07511426 -0.02182438
#> [2,] -0.005847535 0.6291984 0.6055612 -0.0197248 1.85150521 0.53154300
#> [,26] [,27] [,28] [,29] [,30]
#> [1,] 0.01557416 0.1050019 0.8151078 1.208357 -0.3948558
#> [2,] -0.57927546 -1.3519335 0.7866272 1.363907 0.1644755
#>
#> $ClusCont
#> $ClusCont$Sigma
#> , , 1
#>
#> [,1] [,2]
#> [1,] 10.6310268 -0.3618366
#> [2,] -0.3618366 1.8968902
#>
#> , , 2
#>
#> [,1] [,2]
#> [1,] 4.449598 2.398774
#> [2,] 2.398774 5.415662
#>
#> , , 3
#>
#> [,1] [,2]
#> [1,] 5.9754386 0.5444774
#> [2,] 0.5444774 2.5103807
#>
#> , , 4
#>
#> [,1] [,2]
#> [1,] 8.417507 -3.752063
#> [2,] -3.752063 3.931099
#>
#> , , 5
#>
#> [,1] [,2]
#> [1,] 7.489967 -3.061416
#> [2,] -3.061416 2.825351
#>
#> , , 6
#>
#> [,1] [,2]
#> [1,] 10.850559 -1.591032
#> [2,] -1.591032 1.810324
#>
#> , , 7
#>
#> [,1] [,2]
#> [1,] 1.713952 1.552084
#> [2,] 1.552084 3.083608
#>
#> , , 8
#>
#> [,1] [,2]
#> [1,] 7.135302 -1.600201
#> [2,] -1.600201 2.064782
#>
#> , , 9
#>
#> [,1] [,2]
#> [1,] 88.93196 61.91234
#> [2,] 61.91234 47.37414
#>
#> , , 10
#>
#> [,1] [,2]
#> [1,] 169.9188 -336.5307
#> [2,] -336.5307 673.6776
#>
#> , , 11
#>
#> [,1] [,2]
#> [1,] 2.2890443 0.4719784
#> [2,] 0.4719784 1.2516129
#>
#> , , 12
#>
#> [,1] [,2]
#> [1,] 18.844948 8.917703
#> [2,] 8.917703 5.773486
#>
#> , , 13
#>
#> [,1] [,2]
#> [1,] 3.596729 17.87043
#> [2,] 17.870433 138.25255
#>
#> , , 14
#>
#> [,1] [,2]
#> [1,] 2.0275041 -0.8791881
#> [2,] -0.8791881 7.8437537
#>
#> , , 15
#>
#> [,1] [,2]
#> [1,] 59.15627 -18.888300
#> [2,] -18.88830 7.872063
#>
#> , , 16
#>
#> [,1] [,2]
#> [1,] 2.041239 2.127326
#> [2,] 2.127326 4.267644
#>
#> , , 17
#>
#> [,1] [,2]
#> [1,] 3.971994 2.303020
#> [2,] 2.303020 5.520341
#>
#> , , 18
#>
#> [,1] [,2]
#> [1,] 7.529608 9.967627
#> [2,] 9.967627 17.562678
#>
#> , , 19
#>
#> [,1] [,2]
#> [1,] 7.90375296 -0.01954218
#> [2,] -0.01954218 0.97881975
#>
#> , , 20
#>
#> [,1] [,2]
#> [1,] 1.889075 2.551704
#> [2,] 2.551704 6.911235
#>
#> , , 21
#>
#> [,1] [,2]
#> [1,] 0.5943952 0.2620577
#> [2,] 0.2620577 3.5470681
#>
#> , , 22
#>
#> [,1] [,2]
#> [1,] 1.4687069 0.6491655
#> [2,] 0.6491655 2.2671766
#>
#> , , 23
#>
#> [,1] [,2]
#> [1,] 0.8009832 1.046720
#> [2,] 1.0467200 8.930476
#>
#> , , 24
#>
#> [,1] [,2]
#> [1,] 12.2153781 -0.1306491
#> [2,] -0.1306491 0.8661313
#>
#> , , 25
#>
#> [,1] [,2]
#> [1,] 2.785636 -2.314184
#> [2,] -2.314184 6.712419
#>
#> , , 26
#>
#> [,1] [,2]
#> [1,] 0.6151796 -0.3401424
#> [2,] -0.3401424 2.0914652
#>
#> , , 27
#>
#> [,1] [,2]
#> [1,] 3.7570553 0.8215209
#> [2,] 0.8215209 1.4479648
#>
#> , , 28
#>
#> [,1] [,2]
#> [1,] 5.675646 -2.225419
#> [2,] -2.225419 6.144679
#>
#> , , 29
#>
#> [,1] [,2]
#> [1,] 15.940860 -4.247487
#> [2,] -4.247487 2.127413
#>
#> , , 30
#>
#> [,1] [,2]
#> [1,] 55.30405 56.08747
#> [2,] 56.08747 75.61649
#>
#>
#> $ClusCont$mu
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,] -3.409589 2.677654 1.896016 -1.060138 -0.1659441 3.0504680 -1.395848
#> [2,] -1.114269 2.649250 2.615785 1.699734 -0.5612062 -0.4009079 -1.572922
#> [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,] 3.9940388 -13.746724 11.04956 2.1310983 1.2097050 2.989191 -0.2059083
#> [2,] 0.6352385 -9.373673 -26.23231 -0.1426458 0.3306834 4.474360 -2.5146337
#> [,15] [,16] [,17] [,18] [,19] [,20] [,21]
#> [1,] 3.126847 -2.163791 -0.2935087 -2.694225 4.7707338 -0.9702537 0.3865043
#> [2,] 2.026455 -2.343411 2.9835046 -2.528799 -0.2697497 -1.6105919 -1.7071183
#> [,22] [,23] [,24] [,25] [,26] [,27] [,28]
#> [1,] 1.9569217 -2.599817 5.4088246 -0.1694115 -1.252360 0.8171991 2.4645870
#> [2,] 0.8728949 -6.442154 -0.9662149 0.2340833 1.369806 2.2890832 -0.7785191
#> [,29] [,30]
#> [1,] -0.3294844 -6.599450
#> [2,] 0.6925484 -6.236636
#>
#>
#> $ClusCat
#> $ClusCat$pvecClus
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
#> [1,] 0 0 0 0 0 0 0 0 0 0 0 0
#> [,27] [,28] [,29] [,30]
#> [1,] 0 0 0 0If you want to set specific starting values to ad-hoc values, you can
edit $theta directly:
theta_init()To draw a fresh set of starting values from the prior—for example, to
initialize a second independent MCMC chain—use
theta_init():
print(paste("Old FE intercept parameter:", dataProfile$theta$betaFE[1]))
#> [1] "Old FE intercept parameter: 10"
dataProfile$theta = theta_init(dataProfile$prior, dataProfile$params)
print(paste("New FE intercept parameter:", dataProfile$theta$betaFE[1]))
#> [1] "New FE intercept parameter: 429.664592599407"Finally, you can modify prior hyperparameters directly in
$prior before running profileGLMM_Gibbs(). If
you want the initial parameters to reflect the updated prior, call
theta_init() again afterward:
# Set a stronger penalty on the fixed-effect prior
dataProfile$prior$FE$lambda = 100
# Redraw initial values using the updated prior
dataProfile$theta = theta_init(dataProfile$prior, dataProfile$params)
# Check the updated initial fixed-effect parameters
print(dataProfile$theta$betaFE)
#> [1] -0.07230461 0.07090452