-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJMLE.R
More file actions
663 lines (586 loc) · 25.4 KB
/
JMLE.R
File metadata and controls
663 lines (586 loc) · 25.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
## ****************************************************#
## The Joint Multiple Multi-level Estimation (JMMLE) Method
## ****************************************************#
#****************************************************#
# One-step version: first stabilize Beta, then optimize Theta and Beta once
# USE THIS FOR PRACTICAL PURPOSES.
#****************************************************#
jmmle.1step = function(Y.list, # K-length list of Y-matrices, each n X q
Y.indices=NULL,
X.list, # K-length list of X-matrices, each n X p
B.group.array, # array of group indices for B, size p X q X K
Theta.groups, # q-length list of Y-matrices, each K X (q-1)
B_init.array=NULL, # initializer for B arrays
Theta_init.array=NULL, # initializer for theta arrays
init.gamma=NULL,
# if you supply Theta_init.array, then also
# supply the tuning parameter for the JSEM part
init.option=1, # default initializer option
lambda=NULL, # B estimation tuning parameter- scalar
gamma=NULL, # vector of Theta estimation tuning parameters
refit.B=TRUE, # refit B at the end of final iteration?
tol=1e-4, # tolerance for convergence
maxit=20, # max iterations
eps=1e-6, # threshold to set entries of Theta matrices zero
VERBOSE=TRUE # print output or not
){
#****************************************************#
# Define and initialize some quantities
#****************************************************#
arraydims = dim(B.group.array)
p = arraydims[1]; q = arraydims[2]; K = arraydims[3]
n = nrow(Y.list[[1]])
Theta.group.array = array(0, c(q,q,K))
for(j in 1:q){
Theta.group.array[j,-j,] = Theta.groups[[j]]
}
## default values of arguments if they are NULL
if(is.null(Y.indices)){
Y.indices = list()
for(k in 1:K){
Y.indices[[k]] = rep(k, nrow(Y.list[[k]]))
}
}
if(is.null(lambda)){
lambda = .5 * sqrt(log(p)/n)
}
if(is.null(gamma)){
gamma = sqrt(log(q)/n) * seq(1, 0.1, -0.1)
}
#****************************************************#
# Initialization of iterates
#****************************************************#
## Option 0: initial values supplied. Nothing to do
if(!is.null(B_init.array) & !is.null(Theta_init.array)){
if(VERBOSE){
cat("Initial values detected. Skipping initialization\n")
}
Ehat.list = list()
for(k in 1:K){
Ehat.list[[k]] = Y.list[[k]] - X.list[[k]] %*% B_init.array[,,k]
}
gamma.min = init.gamma
init.option = 0
}
## Option 1: initialize B, then run JSEM to initialize Theta
if(init.option==1){
## initialize B from separate analysis
if(VERBOSE){
cat("Initializing B array\n")
}
skeleton.hat = array(1, c(p,q)); # assuming all directed edges possibly exist
Existing.edges = diag(1:p) %*% skeleton.hat
B_init.array = array(0, dim=c(p,q,K))
Ehat.list = list()
for(k in 1:K){
LS = l1LS_Main(Y.list[[k]], X.list[[k]], skeleton.hat=skeleton.hat,
lambda=lambda,initializer="Lasso")
B_init.array[,,k] = LS$B0
Ehat.list[[k]] = Y.list[[k]] - X.list[[k]] %*% LS$B0
}
## initialize Theta
if(VERBOSE){
cat("Initializing Theta array: ")
}
init.bic.jsem <- sel.lambda.jsem(do.call(rbind, Ehat.list), do.call(rbind, Ehat.list),
unlist(Y.indices), unlist(Y.indices),
Theta.groups, lambda=gamma)
gamma.min = gamma[which.min(init.bic.jsem$BIC)]
init.jsem.model = JSEM(do.call(rbind, Ehat.list), unlist(Y.indices),
Theta.groups, lambda=gamma.min)
Theta_init.array = array(0, c(q,q,K))
for(k in 1:K){
Theta_init.array[,,k] = init.jsem.model$Theta[[k]]
}
}
## Option 2: initialize Theta only by running JSEM once
if(init.option==2){
## initialize Theta
if(VERBOSE){
cat("Initializing Theta array: ")
}
init.bic.jsem <- sel.lambda.jsem(do.call(rbind, Y.list), do.call(rbind, Y.list),
unlist(Y.indices), unlist(Y.indices),
Theta.groups, lambda=gamma)
gamma.min = gamma[which.min(init.bic.jsem$BIC)]
init.jsem.model = JSEM(do.call(rbind, Y.list), unlist(Y.indices),
Theta.groups, lambda=gamma.min)
Theta_init.array = array(0, c(q,q,K))
for(k in 1:K){
Theta_init.array[,,k] = init.jsem.model$Theta[[k]]
}
B_init.array = array(0, dim=c(p,q,K))
Ehat.list = Y.list
}
#****************************************************#
# Alternating algorithm
#****************************************************#
## make long X and Y matrices
Y = do.call(rbind, Y.list)
X = as.matrix(do.call(bdiag, X.list))
# initialize
Normdiff = c()
Objfunc = Obj(Y.list, X.list, Theta_init.array, B_init.array,
Theta.group.array, B.group.array,
lambda=lambda, gamma=.5 * sqrt(log(q)/n))
iter = 0; CONVERGE=FALSE; CONVERGE1=FALSE; refit.B=TRUE;
B_new.array = B_init.array
Theta_new.array = Theta_init.array
jsem.model = NULL
# # store bic values from JSEM models
# bic.mat = init.bic.jsem$BIC
## start with the alternating procedure
if(VERBOSE){
cat('-----\n')
}
while(!CONVERGE){
iter = iter + 1;
B_old.array = B_new.array
Theta_old.array = Theta_new.array
if(VERBOSE){
cat("Iteration ", iter, ":\n")
}
# Updating B
if(VERBOSE){
cat("Updating B array\n")
}
for(j in 1:q){
# make long vector or errors for j-th column for all k
Et.j.list = list()
for(k in 1:K){
Et.j.list[[k]] = Ehat.list[[k]][,-j] %*% Theta_old.array[-j,j,k]
}
Ehat.theta.j = unlist(Et.j.list)
rm(Et.j.list)
# build model
temp = grpreg(X, Y[,j] + Ehat.theta.j, unlist(as.numeric(B.group.array[,j,])),
family="gaussian", penalty="grLasso", lambda=lambda)
B_new.array[,j,] = matrix(temp$beta[-1], ncol=K, byrow=F)
## refit if necessary
if(refit.B){
# make long vector or errors for j-th column for all k
temp1 = temp$beta[-1]
B.j.support = which(abs(temp1)>1e-6)
if (length(B.j.support)>0){
# build model
temp2 = lm(Y[,j] + Ehat.theta.j~X[,B.j.support]+0)
# save non-zero coefs
temp1[B.j.support] = temp2$coef
B_new.array[,j,] = matrix(temp1, ncol=K, byrow=F)
}
}
# now update j-th column of all K matrices in B_new, and Ehat
for(k in 1:K){
Ehat.list[[k]][,j] = Y.list[[k]][,j] - X.list[[k]] %*% as.matrix(B_new.array[,j,k], ncol=1)
}
}
# If Beta is stablized, updating Theta then B once then break
if (iter >=10 | sqrt(sum(B_new.array - B_old.array)^2)<0.1){
# Update Theta
if(VERBOSE){
cat("Updating Theta array: ")
}
bic.jsem <- sel.lambda.jsem(do.call(rbind, Ehat.list), do.call(rbind, Ehat.list),
unlist(Y.indices), unlist(Y.indices),
Theta.groups, lambda=gamma)
gamma.min = gamma[which.min(bic.jsem$BIC)]
jsem.model = JSEM(do.call(rbind, Ehat.list), unlist(Y.indices),
Theta.groups, lambda=gamma.min)
Theta_new.array = array(0, c(q,q,K))
for(k in 1:K){
Theta_new.array[,,k] = jsem.model$Theta[[k]]
}
# Update B
B_old.array = B_new.array
if(VERBOSE){
cat("Updating B array\n")
}
for(j in 1:q){
# make long vector or errors for j-th column for all k
Et.j.list = list()
for(k in 1:K){
Et.j.list[[k]] = Ehat.list[[k]][,-j] %*% Theta_new.array[-j,j,k]
}
Ehat.theta.j = unlist(Et.j.list)
rm(Et.j.list)
# build model
temp = grpreg(X, Y[,j] + Ehat.theta.j, unlist(as.numeric(B.group.array[,j,])),
family="gaussian", penalty="grLasso", lambda=lambda)
B_new.array[,j,] = matrix(temp$beta[-1], ncol=K, byrow=F)
## refit if necessary
if(refit.B){
# make long vector or errors for j-th column for all k
temp1 = temp$beta[-1]
B.j.support = which(abs(temp1)>1e-6)
if (length(B.j.support)>0){
# build model
temp2 = lm(Y[,j] + Ehat.theta.j~X[,B.j.support]+0)
# save non-zero coefs
temp1[B.j.support] = temp2$coef
B_new.array[,j,] = matrix(temp1, ncol=K, byrow=F)
}
}
# now update j-th column of all K matrices in B_new, and Ehat
for(k in 1:K){
Ehat.list[[k]][,j] = Y.list[[k]][,j] - X.list[[k]] %*% as.matrix(B_new.array[,j,k], ncol=1)
}
CONVERGE1 = TRUE # Set CONVERGE = 1 to break outer loop
}
} else{
Theta_new.array = Theta_old.array
}
# check convergence
Objfunc[iter+1] = Obj(Y.list, X.list, Theta_new.array, B_new.array,
Theta.group.array, B.group.array,
lambda=lambda, gamma=gamma.min)
Normdiff[iter] = sqrt(sum(B_new.array - B_old.array)^2)/sqrt(sum(B_new.array^2)) +
sqrt(sum(Theta_new.array - Theta_old.array)^2)/sqrt(sum(Theta_new.array^2))
# if (iter == 1){
# Norm_diff = Normfunc[1]
# }
# else{
# Norm_diff = Normfunc[iter] - Normfunc[iter-1]
# Obj_diff = (Objfunc[iter] - Objfunc[iter-1])/Objfunc[iter-1]
# }
Obj_diff = Objfunc[iter+1]/Objfunc[iter] - 1
# convergence criterion value
nd = abs(Normdiff[iter])
if(iter>1){
nd = c(nd, abs(rev(diff(Normdiff))[1]))
}
if(iter>2){
nd = c(nd, abs(rev(diff(Normdiff,2))[1]))
}
if(VERBOSE){
cat("Norm_diff =",round(nd,4),'Obj_diff',round(abs(Obj_diff),5),'\n-----\n')
}
CONVERGE = (CONVERGE1 | (min(nd)<tol))
if (iter == maxit){
if(VERBOSE){
cat("Max iterations reached.",'\n')
}
break;
}
}
if(CONVERGE){
if(VERBOSE){
cat("Converged after",iter,"iterations.\n")
}
} else{
warning("algorithm didn't converge.\nRequired epsilon for convergence is ", tol,
" while current value is ", round(min(nd), 4))
}
## If refitting of B matrices hasn't been done inside the loop then refit in the end
B_refit.array = B_new.array
if(!refit.B){
if(VERBOSE){
cat("Refitting B\n")
}
for(j in 1:q){
# make long vector or errors for j-th column for all k
B.j.support = which(abs(B_new.array[,j,])>1e-6)
if (length(B.j.support)>0){
Et.j.list = list()
for(k in 1:K){
Et.j.list[[k]] = Ehat.list[[k]][,-j] %*% Theta_old.array[-j,j,k]
}
Ehat.theta.j = unlist(Et.j.list)
rm(Et.j.list)
# build model
temp = lm(Y[,j] + Ehat.theta.j~X[,B.j.support]+0)
# save non-zero coefs
temp1 = B_refit.array[,j,]
temp1[B.j.support] = temp$coef
B_refit.array[,j,] = temp1
}
}
}
## Refit to get Omega
if(is.null(jsem.model)){
Ahat = list()
for(k in 1:K){
Ahat[[k]] = matrix(0, q, q)
Ahat[[k]][which(abs(Theta_new.array[,,k])>eps, arr.ind=T)] = 1
diag(Ahat[[k]]) = 0
}
} else{
Ahat = jsem.model$Ahat
}
Info = list()
for (k in 1:K){
Info[[k]] = zeroInd(Ahat[[k]], 1)$zeroArr
}
Theta_refit = multi.glasso(do.call(rbind, Ehat.list), unlist(Y.indices), gamma.min, Info)
## return
return(list(B.refit=B_refit.array, # a p X q X K array of B estimates
Theta_refit=Theta_refit
# a list of outputs that consists of
# 1. Omega.hat: K-length list of Omega (neightborhood coef) matrices
# 2. Theta: K-length list of Theta (precision) matrices
# 3. Ahat: K-length list of adjacency matrices
# 4. bic.score: K-length vector of bic values for each precision matrix fit
# 5. lambda: tuning parameter for glasso fits
))
}
#****************************************************#
# Full version: first stabilize Beta, then optimize Theta and Beta until convergence
# *DO NOT* USE THIS FOR PRACTICAL PURPOSES. VERY SLOW.
#****************************************************#
jmmle = function(Y.list, Y.indices=NULL, X.list,
B.group.array, Theta.groups,
B_init.array=NULL, Theta_init.array=NULL, init.gamma=NULL, init.option=1,
lambda=NULL, gamma=NULL,
refit.B=TRUE, tol=1e-4, maxit=20, eps=1e-6, verbose=TRUE){
#****************************************************#
# Define and initialize some quantities
#****************************************************#
arraydims = dim(B.group.array)
p = arraydims[1]; q = arraydims[2]; K = arraydims[3]
n = nrow(Y.list[[1]])
Theta.group.array = array(0, c(q,q,K))
for(j in 1:q){
Theta.group.array[j,-j,] = Theta.groups[[j]]
}
## default values of arguments if they are NULL
if(is.null(Y.indices)){
Y.indices = list()
for(k in 1:K){
Y.indices[[k]] = rep(k, nrow(Y.list[[k]]))
}
}
if(is.null(lambda)){
lambda = .5 * sqrt(log(p)/n)
}
if(is.null(gamma)){
gamma = sqrt(log(q)/n) * seq(1, 0.1, -0.1)
}
#****************************************************#
# Initialization of iterates
#****************************************************#
## Option 0: initial values supplied. Nothing to do
if(!is.null(B_init.array) & !is.null(Theta_init.array)){
if(VERBOSE){
cat("Initial values detected. Skipping initialization\n")
}
Ehat.list = list()
for(k in 1:K){
Ehat.list[[k]] = Y.list[[k]] - X.list[[k]] %*% B_init.array[,,k]
}
gamma.min = init.gamma
init.option = 0
}
## Option 1: initialize B, then run JSEM to initialize Theta
if(init.option==1){
## initialize B from separate analysis
cat("Initializing B array\n")
skeleton.hat = array(1, c(p,q)); # assuming all directed edges possibly exist
Existing.edges = diag(1:p) %*% skeleton.hat
B_init.array = array(0, dim=c(p,q,K))
Ehat.list = list()
for(k in 1:K){
LS = l1LS_Main(Y.list[[k]], X.list[[k]], skeleton.hat=skeleton.hat,
lambda=lambda,initializer="Lasso")
B_init.array[,,k] = LS$B0
Ehat.list[[k]] = Y.list[[k]] - X.list[[k]] %*% LS$B0
}
## initialize Theta
cat("Initializing Theta array: ")
init.bic.jsem <- sel.lambda.jsem(do.call(rbind, Ehat.list), do.call(rbind, Ehat.list),
unlist(Y.indices), unlist(Y.indices),
Theta.groups, lambda=gamma)
gamma.min = gamma[which.min(init.bic.jsem$BIC)]
init.jsem.model = JSEM(do.call(rbind, Ehat.list), unlist(Y.indices),
Theta.groups, lambda=gamma.min)
Theta_init.array = array(0, c(q,q,K))
for(k in 1:K){
Theta_init.array[,,k] = init.jsem.model$Theta[[k]]
}
}
## Option 2: initialize Theta only by running JSEM once
if(init.option==2){
## initialize Theta
cat("Initializing Theta array: ")
init.bic.jsem <- sel.lambda.jsem(do.call(rbind, Y.list), do.call(rbind, Y.list),
unlist(Y.indices), unlist(Y.indices),
Theta.groups, lambda=gamma)
gamma.min = gamma[which.min(init.bic.jsem$BIC)]
init.jsem.model = JSEM(do.call(rbind, Y.list), unlist(Y.indices),
Theta.groups, lambda=gamma.min)
Theta_init.array = array(0, c(q,q,K))
for(k in 1:K){
Theta_init.array[,,k] = init.jsem.model$Theta[[k]]
}
B_init.array = array(0, dim=c(p,q,K))
Ehat.list = Y.list
}
#****************************************************#
# Alternating algorithm
#****************************************************#
## make long X and Y matrices
Y = do.call(rbind, Y.list)
X = as.matrix(do.call(bdiag, X.list))
# initialize
Normdiff = c()
Objfunc = Obj(Y.list, X.list, Theta_init.array, B_init.array,
Theta.group.array, B.group.array,
lambda=lambda, gamma=.5 * sqrt(log(q)/n))
iter = 0; CONVERGE=FALSE; refit.B=TRUE; update.counter=0;
updateTheta = FALSE; # we don't update Theta until B is stabilized a bit
B_new.array = B_init.array
Theta_new.array = Theta_init.array
jsem.model = NULL
# # store bic values from JSEM models
# bic.mat = init.bic.jsem$BIC
## start with the alternating procedure
cat('-----\n')
while(!CONVERGE){
iter = iter + 1;
B_old.array = B_new.array
Theta_old.array = Theta_new.array
cat("Iteration ", iter, ":\n")
# Updating B
cat("Updating B array\n")
for(j in 1:q){
# make long vector or errors for j-th column for all k
Et.j.list = list()
for(k in 1:K){
Et.j.list[[k]] = Ehat.list[[k]][,-j] %*% Theta_old.array[-j,j,k]
}
Ehat.theta.j = unlist(Et.j.list)
rm(Et.j.list)
# build model
temp = grpreg(X, Y[,j] + Ehat.theta.j, unlist(as.numeric(B.group.array[,j,])),
family="gaussian", penalty="grLasso", lambda=lambda)
B_new.array[,j,] = matrix(temp$beta[-1], ncol=K, byrow=F)
## refit if necessary
if(refit.B){
# make long vector or errors for j-th column for all k
temp1 = temp$beta[-1]
B.j.support = which(abs(temp1)>1e-6)
if (length(B.j.support)>0){
# build model
temp2 = lm(Y[,j] + Ehat.theta.j~X[,B.j.support]+0)
# save non-zero coefs
temp1[B.j.support] = temp2$coef
B_new.array[,j,] = matrix(temp1, ncol=K, byrow=F)
}
}
# now update j-th column of all K matrices in B_new, and Ehat
for(k in 1:K){
Ehat.list[[k]][,j] = Y.list[[k]][,j] - X.list[[k]] %*% as.matrix(B_new.array[,j,k], ncol=1)
}
}
# # update array of E
# for(k in 1:K){
# Ehat.list[[k]] = Y.list[[k]] - X.list[[k]] %*% B_new.array[,,k]
# }
# Updating Theta
if (iter >=10 | sqrt(sum(B_new.array - B_old.array)^2)<0.1 | updateTheta == TRUE){
updateTheta = TRUE; # once we start updating Theta, we just start from now on;
cat("Updating Theta array: ")
bic.jsem <- sel.lambda.jsem(do.call(rbind, Ehat.list), do.call(rbind, Ehat.list),
unlist(Y.indices), unlist(Y.indices),
Theta.groups, lambda=gamma)
gamma.min = gamma[which.min(bic.jsem$BIC)]
jsem.model = JSEM(do.call(rbind, Ehat.list), unlist(Y.indices),
Theta.groups, lambda=gamma.min)
Theta_new.array = array(0, c(q,q,K))
for(k in 1:K){
Theta_new.array[,,k] = jsem.model$Theta[[k]]
}
update.counter = update.counter + 1
# bic.mat = rbind(bic.mat, bic.jsem$BIC)
} else{
Theta_new.array = Theta_old.array
}
# check convergence
Objfunc[iter+1] = Obj(Y.list, X.list, Theta_new.array, B_new.array,
Theta.group.array, B.group.array,
lambda=lambda, gamma=gamma.min)
Normdiff[iter] = sqrt(sum(B_new.array - B_old.array)^2)/sqrt(sum(B_new.array^2)) +
sqrt(sum(Theta_new.array - Theta_old.array)^2)/sqrt(sum(Theta_new.array^2))
# if (iter == 1){
# Norm_diff = Normfunc[1]
# }
# else{
# Norm_diff = Normfunc[iter] - Normfunc[iter-1]
# Obj_diff = (Objfunc[iter] - Objfunc[iter-1])/Objfunc[iter-1]
# }
Obj_diff = Objfunc[iter+1]/Objfunc[iter] - 1
# convergence criterion value
nd = abs(Normdiff[iter])
if(iter>1){
nd = c(nd, abs(rev(diff(Normdiff))[1]))
}
if(iter>2){
nd = c(nd, abs(rev(diff(Normdiff,2))[1]))
}
cat("Norm_diff =",round(nd,4),'Obj_diff',round(abs(Obj_diff),5),'\n-----\n')
CONVERGE = (min(nd)<tol)
if (iter == maxit){
cat("Max iterations reached.",'\n')
break;
}
}
if(CONVERGE){
cat("Converged after",iter,"iterations.\n")
} else{
warning("algorithm didn't converge.\nRequired epsilon for convergence is ", tol,
" while current value is ", round(min(nd), 4))
}
## If refitting of B matrices hasn't been done inside the loop then refit in the end
B_refit.array = B_new.array
if(!refit.B){
cat("Refitting B\n")
for(j in 1:q){
# make long vector or errors for j-th column for all k
B.j.support = which(abs(B_new.array[,j,])>1e-6)
if (length(B.j.support)>0){
Et.j.list = list()
for(k in 1:K){
Et.j.list[[k]] = Ehat.list[[k]][,-j] %*% Theta_old.array[-j,j,k]
}
Ehat.theta.j = unlist(Et.j.list)
rm(Et.j.list)
# build model
temp = lm(Y[,j] + Ehat.theta.j~X[,B.j.support]+0)
# save non-zero coefs
temp1 = B_refit.array[,j,]
temp1[B.j.support] = temp$coef
B_refit.array[,j,] = temp1
}
}
}
## Refit to get Omega
# cat("Getting Omega 1\n")
if(is.null(jsem.model)){
Ahat = list()
for(k in 1:K){
Ahat[[k]] = matrix(0, q, q)
Ahat[[k]][which(abs(Theta_new.array[,,k])>eps, arr.ind=T)] = 1
diag(Ahat[[k]]) = 0
}
} else{
Ahat = jsem.model$Ahat
}
# cat("Getting Omega 2\n")
Info = list()
for (k in 1:K){
Info[[k]] = zeroInd(Ahat[[k]], 1)$zeroArr
}
# cat("Getting Omega 3\n")
Theta_refit = multi.glasso(do.call(rbind, Ehat.list), unlist(Y.indices), gamma.min, Info)
## return
return(list(B.refit=B_refit.array, Theta_refit=Theta_refit))
}
#****************************************************#
# Auxiliary functions
#****************************************************#
# cat = function(string, VERBOSE){
# if(VERBOSE){
# cat(string)
# }
# }
#****************************************************#
# EOF
#****************************************************#