-
Notifications
You must be signed in to change notification settings - Fork 0
/
samplePrior.R
145 lines (126 loc) · 4.64 KB
/
samplePrior.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
#' @title samplePrior
#'
#' @description Samples the parameter vector from prior
#'
#' @param hM a fitted \code{Hmsc} model object
#' @param dataParList list of data parameters (see \code{\link{computeDataParameters}})
#'
#' @return A named list containing the Hmsc model parameters
#'
#' @importFrom stats rgamma rnorm
#' @importFrom MASS mvrnorm
#' @importFrom MCMCpack riwish
samplePrior = function(hM, dataParList=NULL){
if(is.null(dataParList))
dataParList = computeDataParameters(hM)
Qg = dataParList$Qg
iQg = dataParList$iQg
RQg = dataParList$RQg
detQg = dataParList$detQg
rLPar = dataParList$rLPar
#PRIMARY PARAMETERS NOT RELATED TO RANDOM EFFECTS
Gamma = matrix(mvrnorm(1, hM$mGamma, hM$UGamma), hM$nc, hM$nt)
V = riwish(hM$f0, hM$V0)
sigma = rep(NA, hM$ns)
for(j in 1:hM$ns){
if(hM$distr[j,2] == 1){
sigma[j] = 1 / rgamma(1, shape=hM$aSigma[j], rate=hM$bSigma[j])
} else{
switch(hM$distr[j,1],
sigma[j] <- 1,
sigma[j] <- 1,
sigma[j] <- 1e-2
)
}
}
if(is.null(hM$C)){
rho = 1
} else {
rho = sample(x = 1:dim(hM$rhopw)[1], size = 1, prob = hM$rhopw[,2])
}
#PRIMARY PARAMETERS FOR RANDOM EFFECTS
nf = rep(NA, hM$nr)
ncr = rep(NA, hM$nr)
Delta = vector("list", hM$nr)
Psi = vector("list", hM$nr)
Lambda = vector("list", hM$nr)
Eta = vector("list", hM$nr)
np = hM$np
Alpha = vector("list", hM$nr)
for(r in seq_len(hM$nr)){
if(hM$rL[[r]]$nfMax==Inf){
nf[r] = 10
} else {
nf[r] = hM$rL[[r]]$nfMax
}
ncr[r] = max(hM$rL[[r]]$xDim, 1)
if(hM$rL[[r]]$xDim == 0){
Delta[[r]] = matrix(c(rgamma(1,hM$rL[[r]]$a1,hM$rL[[r]]$b1), rgamma(nf[r]-1,hM$rL[[r]]$a2,hM$rL[[r]]$b2)))
} else
Delta[[r]] = matrix(c(rgamma(ncr[r],hM$rL[[r]]$a1,hM$rL[[r]]$b1), rgamma(ncr[r]*(nf[r]-1),hM$rL[[r]]$a2,hM$rL[[r]]$b2)), nf[r],ncr[r],byrow=TRUE)
if(hM$rL[[r]]$xDim == 0){
Psi[[r]] = matrix(rgamma(nf[r]*hM$ns, hM$rL[[r]]$nu/2, hM$rL[[r]]$nu/2), nf[r], hM$ns)
} else
Psi[[r]] = array(rgamma(nf[r]*hM$ns*ncr[r], hM$rL[[r]]$nu/2, hM$rL[[r]]$nu/2), dim=c(nf[r],hM$ns,ncr[r]))
tau = matrix(apply(Delta[[r]], 2, cumprod), nf[r], ncr[r])
if(hM$rL[[r]]$xDim == 0){
tauMat = matrix(tau,nf[r],hM$ns)
mult = sqrt(Psi[[r]]*tauMat)^-1
Lambda[[r]] = matrix(rnorm(nf[r]*hM$ns)*mult, nf[r], hM$ns)
} else{
tauArray = array(tau,dim=c(nf[r],1,ncr[r]))[,rep(1,hM$ns),,drop=FALSE]
mult = sqrt(Psi[[r]]*tauArray)^-1
Lambda[[r]] = array(rnorm(nf[r]*hM$ns*ncr[r])*mult, dim=c(nf[r],hM$ns,ncr[r]))
}
if(hM$rL[[r]]$sDim == 0){
Alpha[[r]] = rep(1,nf[r])
Eta[[r]] = matrix(rnorm(np[r]*nf[r]),np[r],nf[r])
} else {
Alpha[[r]] = sample(x = 1:dim(hM$rL[[r]]$alphapw)[1], size = nf[r], prob = hM$rL[[r]]$alphapw[,2], replace = TRUE)
Eta[[r]] = matrix(rnorm(np[r]*nf[r]),np[r],nf[r])
Wg = rLPar[[r]]$Wg
alpha = Alpha[[r]]
for(i in 1:nf[r]){
Eta[[r]][,i]=mvrnorm(mu=rep(0,np[r]),Sigma=Wg[,,alpha[i]])
}
}
}
#DERIVED PARAMETERS
Mu = tcrossprod(Gamma,hM$TrScaled)
if(is.null(hM$C)){
Beta = matrix(NA, hM$nc, hM$ns)
for(j in 1:hM$ns)
Beta[,j] = mvrnorm(1, Mu[,j], V)
}
else {
Beta = t(matrix(mvrnorm(mu=as.vector(t(Mu)),Sigma = kronecker(V,Qg[,,rho])), hM$ns, hM$nc))
}
switch(class(hM$XScaled)[1L],
matrix = {
LFix = hM$XScaled %*% Beta
},
list = {
LFix = matrix(NA,hM$ny,hM$ns)
for(j in 1:hM$ns)
LFix[,j] = hM$XScaled[[j]] %*% Beta[,j]
}
)
LRan = vector("list", hM$nr)
for(r in seq_len(hM$nr)){
if(hM$rL[[r]]$xDim == 0){
LRan[[r]] = Eta[[r]][hM$Pi[,r],]%*%Lambda[[r]]
} else{
LRan[[r]] = matrix(0,hM$ny,hM$ns)
for(k in 1:hM$rL[[r]]$xDim)
LRan[[r]] = LRan[[r]] + (Eta[[r]][hM$Pi[,r],,drop=FALSE]*hM$rL[[r]]$x[as.character(hM$dfPi[,r]),r]) %*% Lambda[[r]][,,k]
}
}
iSigma = 1 / sigma
iV = chol2inv(chol(V))
sample = combineParameters(Beta=Beta,BetaSel=NULL,wRRR=NULL,Gamma=Gamma,iV=iV,rho=rho,iSigma=iSigma,
Eta=Eta,Lambda=Lambda,Alpha=Alpha,Psi=Psi,Delta=Delta,
PsiRRR=NULL, DeltaRRR=NULL,ncNRRR=hM$ncNRRR, ncRRR=hM$ncRRR, ncsel=hM$ncsel, XSelect=NULL,
XScalePar=hM$XScalePar, XInterceptInd=hM$XInterceptInd, nt=hM$nt, TrScalePar=hM$TrScalePar,
TrInterceptInd=hM$TrInterceptInd, rhopw=hM$rhopw)
return(sample)
}