[go: up one dir, main page]

Menu

[3bfb4b]: / source / R / m3.R  Maximize  Restore  History

Download this file

371 lines (328 with data), 16.4 kB

  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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
## Function translated automatically using 'matlab.to.r()'
## Author: Andrew Hooker
m3 <- function(model_switch,xt_ind,x,a,bpop,b_ind,bocc_ind,d,sigma,docc,bUseVarSigmaDerivative,poped.db){
#
# size: (samps per subject^2 x (number of random effects + number of occasion variances + number of sigmas))
NumSigma = size(sigma,1)
NumDocc = size(docc,1)
if((isempty(sigma))){
NumSigma=0
}
ns=size(xt_ind,1)^2
dv_db_new=zeros(ns,sum(poped.db$parameters$notfixed_d)+
sum(poped.db$parameters$notfixed_covd)+
sum(poped.db$parameters$notfixed_docc)+
sum(poped.db$parameters$notfixed_covdocc)+
sum(poped.db$parameters$notfixed_sigma)+
sum(poped.db$parameters$notfixed_covsigma))
if((poped.db$settings$m2_switch[1] == 30) ){#Automatic differentiation of M3 dosen't work with ud variance term
returnArgs <- LinMatrixL(model_switch,xt_ind,x,a,bpop,b_ind,bocc_ind,poped.db)
l <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
returnArgs <- LinMatrixH(model_switch,xt_ind,x,a,bpop,b_ind,bocc_ind,poped.db)
h <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
returnArgs <- LinMatrixLH(model_switch,xt_ind,x,a,bpop,b_ind,bocc_ind,size(sigma,1),poped.db)
lh <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
locc=cell(1,poped.db$parameters$NumOcc)
for(i in 1:poped.db$parameters$NumOcc){
if(poped.db$parameters$NumOcc==0) next
returnArgs <- LinMatrixL_occ(model_switch,xt_ind,x,a,bpop,b_ind,bocc_ind,i,poped.db)
locc[[i]] <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
}
j=1
#Differentiate the variance w$r.t iiv
for(i in 1:poped.db$parameters$NumRanEff){
if((poped.db$parameters$notfixed_d[i]==1)){
dv_db_new[,j]=reshape_matlab(l[,i,drop=F]*t(l[,i,drop=F])+diag_matlab(diag_matlab(lh[,(i-1)*NumSigma+1:i*NumSigma,drop=F]*sigma*t(lh[,(i-1)*NumSigma+1:i*NumSigma,drop=F]))),ns,1) #Last term is interaction
j=j+1
}
}
if((sum(poped.db$parameters$notfixed_covd)!=0)){
for(i in 1:length(poped.db$parameters$notfixed_covd) ){
if((poped.db$parameters$notfixed_covd[i]==1)){
returnArgs <- get_cov_matrix_index(d,i)
m <- returnArgs[[1]]
n <- returnArgs[[2]]
if((m==-1 || n==-1)){
stop(sprintf('Wrong index in get_covariance_matrix_index d, PopED is }ing!'))
}
lh1 = diag_matlab(diag_matlab(lh[,(m-1)*NumSigma+1:m*NumSigma,drop=F]*sigma*t(lh[,(n-1)*NumSigma+1:n*NumSigma,drop=F]))) #Interaction term
lh2 = diag_matlab(diag_matlab(lh[,(n-1)*NumSigma+1:n*NumSigma,drop=F]*sigma*t(lh[,(m-1)*NumSigma+1:m*NumSigma,drop=F]))) #Interaction term
dv_db_new[,j]=reshape_matlab(l[,m,drop=F]*t(l[,n,drop=F])+l[,n,drop=F]*t(l[,m,drop=F])+lh1+lh2,ns,1)
j=j+1
}
}
}
#Differentiate the variance w$r.t occasion_variability
for(i in 1:NumDocc){
if((poped.db$parameters$notfixed_docc[i]==1)){
tmp = zeros(size(xt_ind,1),size(xt_ind,1))
for(k in 1:poped.db$parameters$NumOcc){
tmp = tmp+locc[[k]][,i,drop=F]*t(locc[[k]][,i,drop=F])
}
dv_db_new[,j]=reshape_matlab(tmp,ns,1)
j=j+1
}
}
#Differentiate the variance w$r.t. occasion covariances
if((sum(poped.db$parameters$notfixed_covdocc)!=0) ){
for(i in 1:length(poped.db$parameters$notfixed_covdocc) ){
if((poped.db$parameters$notfixed_covdocc[i]==1)){
returnArgs <- get_cov_matrix_index(docc,i)
m <- returnArgs[[1]]
n <- returnArgs[[2]]
if((m==-1 || n==-1)){
stop(sprintf('Wrong index in get_covariance_matrix_index docc, PopED is }ing!'))
}
tmp = zeros(size(xt_ind,1),size(xt_ind,1))
for(k in 1:poped.db$parameters$NumOcc){
tmp = tmp+locc[[k]][,m,drop=F]*t(locc[[k]][,n,drop=F])+locc[[k]][,n,drop=F]*t(locc[[k]][,m,drop=F])
}
dv_db_new[,j]=reshape_matlab(tmp,ns,1)
j=j+1
}
}
}
#Differentiate the variance w$r.t. sigma
for(i in 1:NumSigma){
if((poped.db$parameters$notfixed_sigma[i]==1)){
tmp_lh = zeros(size(xt_ind,1),poped.db$parameters$NumRanEff)
for(k in 1:poped.db$parameters$NumRanEff ){#Only use the Random Eff interacting with sigma_i
tmp_lh[,k] = lh[,i+(k-1)*NumSigma,drop=F]
}
if((bUseVarSigmaDerivative) ){#Derivative w$r.t. sigma as variance
dv_db_new[,j]= reshape_matlab(diag_matlab(diag_matlab(h(,i)*t(h(,i))))+diag_matlab(diag_matlab(tmp_lh*d*t(tmp_lh))),ns,1)
} else { #Derivarite w$r.t. sigma as stdev
dv_db_new[,j]= reshape_matlab(2*sqrt(sigma[i,i])*diag_matlab(diag_matlab(h(,i)*t(h(,i))))+2*sqrt(sigma[i,i])*diag_matlab(diag_matlab(tmp_lh*d*t(tmp_lh))),ns,1)
}
j=j+1
}
}
if((sum(poped.db$parameters$notfixed_covsigma)!=0)){
for(i in 1:length(poped.db$parameters$notfixed_covsigma) ){
if((poped.db$parameters$notfixed_covsigma[i]==1)){
returnArgs <- get_cov_matrix_index(sigma,i)
m <- returnArgs[[1]]
n <- returnArgs[[2]]
if((m==-1 || n==-1)){
stop(sprintf('Wrong index in get_covariance_matrix_index sigma, PopED is }ing!'))
}
tmp_lh_m = zeros(size(xt_ind,1),poped.db$parameters$NumRanEff)
tmp_lh_n = zeros(size(xt_ind,1),poped.db$parameters$NumRanEff)
for(k in 1:poped.db$parameters$NumRanEff ){#Only use the Random Eff interacting with sigma_m or sigma_n
tmp_lh_m[,k] = lh[,m+(k-1)*NumSigma,drop=F]
tmp_lh_n[,k] = lh[,n+(k-1)*NumSigma,drop=F]
}
dv_db_new[,j]=reshape_matlab(diag_matlab(diag_matlab(h(,m)*t(h(,n))))+diag_matlab(diag_matlab(h(,n)*t(h(,m))))+diag_matlab(diag_matlab(tmp_lh_m*d*t(tmp_lh_n)))+diag_matlab(diag_matlab(tmp_lh_n*d*t(tmp_lh_m))),ns,1)
j=j+1
}
}
}
ret=dv_db_new
} else { #If complex or central differentiation
k=1
if(!isempty(d)){
for(i in 1:poped.db$parameters$NumRanEff){
if((poped.db$parameters$notfixed_d[i]==1)){
d_plus=d
# Central approximation
d_plus[i,i]=d_plus[i,i]+poped.db$settings$hm2
d_minus=d
d_minus[i,i]=d_minus[i,i]-poped.db$settings$hm2
if((poped.db$settings$bCalculateEBE)){
start_bind = t(b_ind)%*%zeros(size(t(b_ind)))%*%t(b_ind)
b_ind_plus = ind_estimates(poped.db$mean_data,bpop,d_plus,sigma,start_bind,(poped.db$settings$iApproximationMethod==2),FALSE,model_switch,xt_ind,x,a,b_ind,bocc_ind,poped.db)
b_ind_minus = ind_estimates(poped.db$mean_data,bpop,d_minus,sigma,start_bind,(poped.db$settings$iApproximationMethod==2),FALSE,model_switch,xt_ind,x,a,b_ind,bocc_ind,poped.db)
# b_ind_plus = b_ind/sqrt(diag_matlab(d))*sqrt(diag_matlab(d+poped.db$settings$hm2))
# b_ind_minus = b_ind/sqrt(diag_matlab(d))*sqrt(diag_matlab(d-poped.db$settings$hm2))
#b_ind_plus = b_ind
#b_ind_minus = b_ind
} else {
b_ind_plus = b_ind
b_ind_minus = b_ind
}
returnArgs <- v(model_switch,xt_ind,x,a,bpop,b_ind_plus,bocc_ind,d_plus,sigma,docc,poped.db)
v_plus <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
returnArgs <- v(model_switch,xt_ind,x,a,bpop,b_ind_minus,bocc_ind,d_minus,sigma,docc,poped.db)
v_minus <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
dv=v_plus-v_minus
if((!isempty(dv))){
ir=dv/(2*poped.db$settings$hm2)
ir=reshape_matlab(ir,ns,1)
dv_db_new[,k]=ir
}
k=k+1
}
}
for(i in 1:length(poped.db$parameters$notfixed_covd)){
if((poped.db$parameters$notfixed_covd[i]==1)){
d_plus=update_offdiag(d,i,poped.db$settings$hm2)
d_minus=update_offdiag(d,i,-poped.db$settings$hm2)
if((poped.db$settings$bCalculateEBE)){
start_bind = t(b_ind)%*%zeros(size(t(b_ind)))%*% t(b_ind)
b_ind_plus = ind_estimates(poped.db$mean_data,bpop,d_plus,sigma,start_bind,(poped.db$settings$iApproximationMethod==2),FALSE,model_switch,xt_ind,x,a,b_ind,bocc_ind,poped.db)
b_ind_minus = ind_estimates(poped.db$mean_data,bpop,d_minus,sigma,start_bind,(poped.db$settings$iApproximationMethod==2),FALSE,model_switch,xt_ind,x,a,b_ind,bocc_ind,poped.db)
# b_ind_plus = b_ind/sqrt(diag_matlab(d))*sqrt(diag_matlab(d+poped.db$settings$hm2))
# b_ind_minus = b_ind/sqrt(diag_matlab(d))*sqrt(diag_matlab(d-poped.db$settings$hm2))
#b_ind_plus = b_ind
#b_ind_minus = b_ind
} else {
b_ind_plus = b_ind
b_ind_minus = b_ind
}
returnArgs <- v(model_switch,xt_ind,x,a,bpop,b_ind_plus,bocc_ind,d_plus,sigma,docc,poped.db)
v_plus <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
returnArgs <- v(model_switch,xt_ind,x,a,bpop,b_ind_minus,bocc_ind,d_minus,sigma,docc,poped.db)
v_minus <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
dv=v_plus-v_minus
if((!isempty(dv))){
ir=dv/(2*poped.db$settings$hm2)
ir=reshape_matlab(ir,ns,1)
dv_db_new[,k]=ir
}
k=k+1
}
}
}
if((!isempty(docc))){
for(i in 1:NumDocc){
if((poped.db$parameters$notfixed_docc[i]==1)){
docc_plus=docc
# Central approximation
docc_plus[i,i]=docc_plus[i,i]+poped.db$settings$hm2
docc_minus=docc
docc_minus[i,i]=docc_minus[i,i]-poped.db$settings$hm2
if((poped.db$settings$bCalculateEBE)){
start_bind = t(b_ind)
warning('EBE calculation with occasions is not available in the current version!')
b_ind_plus = b_ind#ind_estimates(poped.db$mean_data,bpop,d_plus,sigma,start_bind,(poped.db$settings$iApproximationMethod==2),FALSE,model_switch,xt_ind,x,a,b_ind,bocc_ind,poped.db)
b_ind_minus = b_ind#ind_estimates(poped.db$mean_data,bpop,d_minus,sigma,start_bind,(poped.db$settings$iApproximationMethod==2),FALSE,model_switch,xt_ind,x,a,b_ind,bocc_ind,poped.db)
} else {
b_ind_plus = b_ind
b_ind_minus = b_ind
}
returnArgs <- v(model_switch,xt_ind,x,a,bpop,b_ind_plus,bocc_ind,d,sigma,docc_plus,poped.db)
v_plus <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
returnArgs <- v(model_switch,xt_ind,x,a,bpop,b_ind_minus,bocc_ind,d,sigma,docc_minus,poped.db)
v_minus <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
dv=v_plus-v_minus
if((!isempty(dv))){
ir=dv/(2*poped.db$settings$hm2)
ir=reshape_matlab(ir,ns,1)
dv_db_new[,k]=ir
}
k=k+1
}
}
for(i in 1:length(poped.db$parameters$notfixed_covdocc)){
if((poped.db$parameters$notfixed_covdocc[i]==1)){
docc_plus=update_offdiag(docc,i,poped.db$settings$hm2)
docc_minus=update_offdiag(docc,i,-poped.db$settings$hm2)
if((poped.db$settings$bCalculateEBE)){
start_bind = t(b_ind)
warning('EBE calculation with covariance of occasions is not available in the current version!')
b_ind_plus = b_ind#ind_estimates(poped.db$mean_data,bpop,d_plus,sigma,start_bind,(poped.db$settings$iApproximationMethod==2),FALSE,model_switch,xt_ind,x,a,b_ind,bocc_ind,poped.db)
b_ind_minus = b_ind#ind_estimates(poped.db$mean_data,bpop,d_minus,sigma,start_bind,(poped.db$settings$iApproximationMethod==2),FALSE,model_switch,xt_ind,x,a,b_ind,bocc_ind,poped.db)
} else {
b_ind_plus = b_ind
b_ind_minus = b_ind
}
returnArgs <- v(model_switch,xt_ind,x,a,bpop,b_ind_plus,bocc_ind,d,sigma,docc_plus,poped.db)
v_plus <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
returnArgs <- v(model_switch,xt_ind,x,a,bpop,b_ind_minus,bocc_ind,d,sigma,docc_minus,poped.db)
v_minus <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
dv=v_plus-v_minus
if((!isempty(dv))){
ir=dv/(2*poped.db$settings$hm2)
ir=reshape_matlab(ir,ns,1)
dv_db_new[,k]=ir
}
k=k+1
}
}
}
for(i in 1:NumSigma){
# [S,R]=cov2corr(sigma) If off-diag covariances should be updated when
# differentiating a variance term
if((poped.db$parameters$notfixed_sigma[i]==1)){
sigma_plus=sigma
# Central approximation
sigma_plus[i,i]=sigma_plus[i,i]+poped.db$settings$hm2
# sigma_plus = corr2cov(sqrt(diag_matlab(sigma_plus)),R)
sigma_minus=sigma
sigma_minus[i,i]=sigma_minus[i,i]-poped.db$settings$hm2
# sigma_minus = corr2cov(sqrt(diag_matlab(sigma_minus)),R)
if((poped.db$settings$bCalculateEBE)){
start_bind = t(b_ind)
b_ind_plus = ind_estimates(poped.db$mean_data,bpop,d,sigma_plus,start_bind,(poped.db$settings$iApproximationMethod==2),FALSE,model_switch,xt_ind,x,a,b_ind,bocc_ind,poped.db)
b_ind_minus = ind_estimates(poped.db$mean_data,bpop,d,sigma_minus,start_bind,(poped.db$settings$iApproximationMethod==2),FALSE,model_switch,xt_ind,x,a,b_ind,bocc_ind,poped.db)
} else {
b_ind_plus = b_ind
b_ind_minus = b_ind
}
returnArgs <- v(model_switch,xt_ind,x,a,bpop,b_ind_plus,bocc_ind,d,sigma_plus,docc,poped.db)
v_plus <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
returnArgs <- v(model_switch,xt_ind,x,a,bpop,b_ind_minus,bocc_ind,d,sigma_minus,docc,poped.db)
v_minus <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
dv=v_plus-v_minus
if((!isempty(dv))){
if((bUseVarSigmaDerivative) ){#Derivative w$r.t. sigma as variance
ir=dv/(2*poped.db$settings$hm2)
} else {
ir=2*sqrt(sigma[i,i])*dv/(2*poped.db$settings$hm2) #Derivative w$r.t. sigma as stdev
}
ir=reshape_matlab(ir,ns,1)
dv_db_new[,k]=ir
}
k=k+1
}
}
for(i in 1:length(poped.db$parameters$notfixed_covsigma)){
if(any(size(poped.db$parameters$notfixed_covsigma)==0)) next
if((poped.db$parameters$notfixed_covsigma[i]==1)){
sigma_plus=update_offdiag(sigma,i,poped.db$settings$hm2)
sigma_minus=update_offdiag(sigma,i,-poped.db$settings$hm2)
if((poped.db$settings$bCalculateEBE)){
start_bind = t(b_ind)
b_ind_plus = ind_estimates(poped.db$mean_data,bpop,d,sigma_plus,start_bind,(poped.db$settings$iApproximationMethod==2),FALSE,model_switch,xt_ind,x,a,b_ind,bocc_ind,poped.db)
b_ind_minus = ind_estimates(poped.db$mean_data,bpop,d,sigma_minus,start_bind,(poped.db$settings$iApproximationMethod==2),FALSE,model_switch,xt_ind,x,a,b_ind,bocc_ind,poped.db)
} else {
b_ind_plus = b_ind
b_ind_minus = b_ind
}
returnArgs <- v(model_switch,xt_ind,x,a,bpop,b_ind_plus,bocc_ind,d,sigma_plus,docc,poped.db)
v_plus <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
returnArgs <- v(model_switch,xt_ind,x,a,bpop,b_ind_minus,bocc_ind,d,sigma_minus,docc,poped.db)
v_minus <- returnArgs[[1]]
poped.db <- returnArgs[[2]]
dv=v_plus-v_minus
if((!isempty(dv))){
#if (bUseVarSigmaDerivative) #Derivative w$r.t. sigma as variance
ir=dv/(2*poped.db$settings$hm2)
#else
# ir=2*sqrt(sigma[i,i])*dv/(2*poped.db$settings$hm2) #Derivative w$r.t. sigma as stdev
#end
ir=reshape_matlab(ir,ns,1)
dv_db_new[,k]=ir
}
k=k+1
}
}
ret = dv_db_new
}
return(list( ret= ret,poped.db=poped.db))
}