-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0_functions.R
More file actions
591 lines (520 loc) · 17.3 KB
/
0_functions.R
File metadata and controls
591 lines (520 loc) · 17.3 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
#### Functions for WMM and ARMM ####
library(ddalpha)
library(forecast)
library(LaplacesDemon)
library(matrixcalc)
library(mclust)
#### Fill in missing value of a time series with NA ####
fill_missing <- function(t, y){
# t: time point index e.g. 1 to n
# y: time series, repeated observation
df1 <- data.frame("t"=t,
"y"=y)
df2 <- data.frame("t"=c(1:max(df1$t)),
"temp"=rep(NA, max(df1$t)) )
df3 <- merge(x = df1, y = df2, by="t", all.y=T )[c("t","y")]
return(df3)
}
#### convert a time series into a data matrix ####
data_matrix <- function(y, lag){
# y: time series, repeated observation
# lag: number of past time points used in model e.g k-1
lag <- lag+1
df = NULL
for (i in 1:lag) {
temp <-c( rep(NA,i-1), y[1:(length(y)-i+1)] )
df <- cbind( df, temp )
}
return(df)
}
#### turn autocorrelation vector into autocorrelation matrix ####
cov_to_matrix <- function(covar){
# covar: autocovariance values, can be obtained using acf()
mat <- matrix(covar, nrow = 1)
dim <- length(covar)
for (j in 2:dim){
mat <- rbind( mat,
matrix( c(rep(0,j-1), covar[1:(dim-j+1)] ), nrow = 1)
)
}
mat <- mat + t(mat) - diag(diag(mat))
return(mat)
}
#### calculates AR coefficient phi using Yule-Walker ####
AR_coeff <- function(matrix){
# matrix: autocorrelation/autocovariance/sigma matrix
d <- dim( matrix )[1]
return( as.numeric( solve( matrix[2:d,2:d] ) %*% matrix[2:d,1] ) )
}
#### calculate mean from a list of matrices ####
matrix_sum <- function(matrix_list){
out <- 0*matrix_list[[1]]
for(i in 1:length(matrix_list) ){
out = out + matrix_list[[i]]
}
return( out )
}
#### EM functions for all algorithms ####
#### E step ####
# E-step for z, returns a list of length G, with vector z of length I estimate for each group ####
E_step_z <- function(matrix_list, pi_list, sigma_list,
n_vector, z_list, lambda_list = NULL,
method = "EM1"){
# matrix_list: list of length I, estimated autocorrelation matrices for each subject
# pi_list: list of length G, estimated pi values for each group
# sigma_list: list of length G, estimated sigma matrices for each group
# n_vector: degree of freedom values for each subject i as a vector, i.e. n_i
# z_list: list of length G, with estimated vector z of length I for each group
# lambda_list: lambda value for each group
# method: WMM model, EM1, EM2, EM3
#
I <- length(matrix_list)
G <- length(pi_list)
#
if(method == "EM1"){
new_list <- list()
for(i in 1:I){
new_list[[i]] <- list( "matrix" = matrix_list[[i]], "df"=n_vector[i])
}
#
denom <- rep(0,I)
# calculate the denominator
for( g in 1:G ){
denom <- denom + pi_list[[g]] *
sapply(new_list, function(x) LaplacesDemon::dwishart(x$matrix, x$df, sigma_list[[g]]) )
}
# calculate the numerator and estimate z for each group 1,...,G
for( g in 1:G ){
numer <- pi_list[[g]] *
sapply(new_list, function(x) LaplacesDemon::dwishart(x$matrix, x$df, sigma_list[[g]]) )
out <- numer/denom
out[denom==0] <- 1/G
z_list[[g]] <- out
}
}
if(method == "EM2"){
new_list <- list()
for(i in 1:I){
new_list[[i]] <- list( "matrix" = matrix_list[[i]], "df"=n_vector[i])
}
#
denom <- rep(0,I)
for( g in 1:G ){
denom <- denom +
pi_list[[g]] *
sapply(new_list, function(x) LaplacesDemon::dwishart(x$matrix, x$df*lambda_list[[g]], sigma_list[[g]]) )
}
for( g in 1:G ){
numer <- pi_list[[g]] *
sapply(new_list, function(x) LaplacesDemon::dwishart(x$matrix, x$df*lambda_list[[g]], sigma_list[[g]]) )
out <- numer/denom
out[denom==0] <- 1/G
z_list[[g]] <- out
}
}
#
return(z_list)
}
#### M step ####
# M-step for pi, return a list of length G, with scalar pi estimates for each group ####
M_step_pi <- function(z_list, method = NULL){
# z_list: list of length G, with estimated vector z of length I for each group
# method: WMM model, EM1, EM2, EM3
G <- length(z_list)
pi_list <- list()
#
for(g in 1:G){
pi_list[[g]] <- mean(z_list[[g]])
}
return(pi_list)
}
# M-step for sigma, return a list of length G, with matrix sigma estimate for each group ####
M_step_sigma <- function(matrix_list, z_list,
n_vector = NULL, lambda_list = NULL,
method = "EM1"){
# matrix_list: list of length I, estimated autocorrelation matrices for each subject
# z_list: list of length G, with estimated vector z of length I for each group
# n_vector: degree of freedom values for each subject i as a vector, i.e. n_i
# lambda_list: lambda value for each group
# method: WMM model, EM1, EM2, EM3
G <- length(z_list)
I <- length(matrix_list)
sigma_list <- list()
#
for(g in 1:G){
numer <- 0
denom <- 0
if(method == "EM1"){
for(i in 1:I){
numer <- numer + matrix_list[[i]] * z_list[[g]][i]
denom <- denom + n_vector[i] * z_list[[g]][i]
}
}
#
if(method == "EM2"){
for(i in 1:I){
numer <- numer + matrix_list[[i]] * z_list[[g]][i]
denom <- denom + (n_vector[i] * lambda_list[[g]]) * z_list[[g]][i]
}
}
#
sigma_list[[g]] <- numer/denom
}
return(sigma_list)
}
# Sum of digamma function with lambda used in the M-step of lambda, EM2 ####
sum_digamma_lambda <- function(dim, z, n_vector, lambda){
# dim: dimension of wishart
# z: z estimate for group g
# n_vector: degree of freedom vector for subjects
# lambda: scalar value for group g
C <- 0
for( j in 1:dim){
C <- C + sum( z * n_vector * digamma(0.5*(n_vector*lambda-j+1) ) )
}
return( C )
}
# M-step for lambda_g, return a list of length G, with scalar lambda estimate for each group ####
M_step_lambda <- function(matrix_list, z_list, sigma_list, n_vector, lambda_list, upper=2){
# matrix_list: list of length I, estimated autocorrelation matrices for each subject
# z_list: list of length G, with estimated vector z of length I for each group
# sigma_list: list of length G, estimated sigma matrices for each group
# n_vector: degree of freedom values for each subject i as a vector, i.e. n_i
# lambda_list: lambda value for each group
I <- length(matrix_list)
G <- length(z_list)
dim <- dim(sigma_list[[1]])[1]
lower = (dim) / min(n_vector)
# lambda_list
output_list <- list()
#
for(g in 1:G){
A <- 0
for(i in 1:I){
A <- A + z_list[[g]][i]*n_vector[i]*log( det( matrix_list[[i]] %*% solve(sigma_list[[g]])/2 ) )
}
output <- optim( lambda_list[[g]], # use square loss
function(x) (A-sum_digamma_lambda(dim, z_list[[g]], n_vector, x))**2,
lower = lower, upper = upper, method = "L-BFGS-B")
output_list[[g]] <- as.numeric(output[1])
}
return( output_list )
}
#### EM algorithm for WMM ####
WMM <- function(matrix_list, n_vector = NULL, G = 2,
method = "EM1", upper=2, tol = 1e-5,
burn_in = 100, max_iter = 1000,
pi_list_init = NULL, sigma_list_init = NULL,
z_list_init = NULL, lambda_list_init = NULL){
# matrix_list: list of length I, estimated autocorrelation matrices for each subject
# n_vector: degree of freedom values for each subject i as a vector, i.e. n_i
# G: total number of groups
# method: method, EM1, EM2, EM3
# upper: upper bound for n_g or lambda_g
# tol: log likelihood gain convergence threshold
# burn_in: minimum number of iterations
# max_iter: maximum number of iterations
# pi_list_init: initial values
# sigma_list_init: initial values
# z_list_init: initial values
# lambda_list_init: initial values
# initialize pi values
if( is.null(pi_list_init) ){
pi_list <- as.list( LaplacesDemon::rdirichlet( 1,rep(100,G) ) )
} else {
pi_list <- pi_list_init
}
# initialize z vector
if( is.null(z_list_init) ){
indices <- split(1:length(matrix_list),
f = sample(1:G, size = length(matrix_list), replace = T) )
z_list <- list()
for(g in 1:G){
ind <- array( indices[ as.character(g) ] )[[1]]
z_list[[g]] <- 1*( 1:length(matrix_list) %in% ind )
}
} else {
z_list <- z_list_init
}
# initialize sigma matrices
if( is.null(sigma_list_init) ){
sigma_list <- list()
for(g in 1:G){
ind <- z_list[[g]]==1
sigma_list[[g]] <- matrix_sum( matrix_list[ ind ] )/sum(n_vector[ind])
}
} else {
sigma_list <- sigma_list_init
}
# initialize lambda_g values
if( is.null(lambda_list_init) ){
lambda_list <- as.list( rep( 1, G ) )
} else {
lambda_list <- lambda_list_init
}
#
sigma_old <- sigma_list_init
converged <- F
for(iter in 1:max_iter){
# E step #
z_list <- E_step_z(matrix_list=matrix_list,
pi_list=pi_list, sigma_list=sigma_list,
n_vector = n_vector, z_list = z_list,
lambda_list = lambda_list,
method = method)
# M step #
pi_list <- M_step_pi(z_list, method = NULL)
#
sigma_list <- M_step_sigma(matrix_list=matrix_list, z_list=z_list,
n_vector = n_vector, lambda_list = lambda_list,
method = method)
#
if(method == "EM2"){
lambda_list <- M_step_lambda(matrix_list=matrix_list, z_list=z_list,
sigma_list=sigma_list, n_vector=n_vector,
lambda_list=lambda_list, upper=upper)
}
# Stopping Criteria
sigma_delta <- 0
for(g in 1:G){
sigma_delta <- sigma_delta +
sum( abs( as.vector( sigma_old[[g]] - sigma_list[[g]] ) ) )
}
sigma_old <- sigma_list
#
#if(iter %% 10 == 0){
if(F){
print("Iteration number:")
print(iter)
print("lambda list")
print(lambda_list)
print("sigma list")
print(sigma_list)
}
if(iter > burn_in & sigma_delta <= tol){
converged <- T
break
}
}
output <- list(
"method" = method,
"z_list" = z_list,
"pi_list" = pi_list,
"sigma_list" = sigma_list,
"lambda_list" = lambda_list,
"converged" = converged,
"iter" = iter
)
return(output)
}
#### calculates maximum a posteriori of z ####
z_max <- function(z_list){
# z_list: list of length G, with estimated vector z of length I for each group
I <- length(z_list[[1]])
G <- length(z_list)
z_map <- diag( rep(1,G) )
#
z_df <- NULL
for (g in 1:G){
# convert z_list to a I x G df
z_df <- cbind( z_df, z_list[[g]] )
}
for(i in 1:I){
# obtain argmax group index, from I x G df
z_df[i,] <- z_map[which.max(z_df[i,]),]
}
return(z_df)
}
#### calculates estimate phi and asymptotic dist. ####
summary_coeff <- function(z_list,matrix_list,sigma_list,n_vector){
# z_list: list of length G, with estimated vector z of length I for each group
# matrix_list: list of length I, estimated autocorrelation matrices for each subject
# sigma_list: list of length G, estimated sigma matrices for each group
# n_vector: degree of freedom values for each subject i as a vector, i.e. n_i
I <- length(z_list[[1]])
G <- length(z_list)
K <- dim(sigma_list[[1]])[1]
#
out_list <- list()
for(g in 1:G){
#
phi_g <- solve( sigma_list[[g]][2:K,2:K] ) %*% sigma_list[[g]][2:K]
#
bread <- sigma_list[[1]][2:K,2:K]*0
center <- sigma_list[[1]][2:K,2:K]*0
for(i in 1:I){
bread <- bread + z_list[[g]][i]*matrix_list[[i]][2:K,2:K]
#
v_i <- matrix_list[[i]][1,1]/n_vector[i] *
(1 - t(sigma_list[[g]][2:K]) %*%
solve( sigma_list[[g]][2:K,2:K] ) %*%
sigma_list[[g]][2:K] / (sigma_list[[g]][1,1]) )
if(v_i < 0 ){
print(i)
print(g)
}
center <- center + as.numeric(v_i) * z_list[[g]][i]**2 * matrix_list[[i]][2:K,2:K]
}
out_list[[g]] <- list("coeff"=phi_g,
"var" = solve(bread) %*% center %*% solve(bread) )
}
return(out_list)
}
#### calculates log likelihood of ARMM model using sigma_list ####
logLik_ARMM <- function(z_list, matrix_list, sigma_list, n_vector){
# z_list: list of length G, with estimated vector z of length I for each group
# matrix_list: list of length I, estimated autocorrelation matrices for each subject
# sigma_list: list of length G, estimated sigma matrices for each group
# n_vector: degree of freedom values for each subject i as a vector, i.e. n_i
# initial log likelihood
ll <- 0
I <- length(z_list[[1]])
G <- length(z_list)
K <- dim(sigma_list[[1]])[1]
z_map <- diag( rep(1,G) )
#
z_df <- NULL
for (g in 1:G){
# convert z_list to a I x G df
z_df <- cbind( z_df, z_list[[g]] )
}
for(i in 1:I){
# obtain argmax group index, from I x G df
z_df[i,] <- z_map[which.max(z_df[i,]),]
for (g in 1:G){
# estimate alpha_i and tau_i for each subject
if(z_df[i,g]==1){
v_i <- matrix_list[[i]][1,1]/n_vector[i] * (1 - t(sigma_list[[g]][2:K]) %*%
solve( sigma_list[[g]][2:K,2:K] ) %*%
sigma_list[[g]][2:K] / (sigma_list[[g]][1,1]) )
if(v_i < 0 ){
print(i)
print(g)
}
# add to the log likelihood for each subject
ll <- ll + n_vector[i] * log(v_i)
}
}
}
return(ll)
}
#### TSclust functions ####
diss.ACF <- function(x, y, lag.max = 50){
rhox <- acf(x, lag.max = lag.max, plot = FALSE)$acf[-1]
rhoy <- acf(y, lag.max = lag.max, plot = FALSE)$acf[-1]
return(sqrt(t(rhox - rhoy) %*% diag(length(rhox)) %*% (rhox - rhoy)))
}
diss.PACF <- function(x, y, lag.max = 50){
rhox <- as.vector(pacf(x, lag.max = lag.max, plot = FALSE)$acf)
rhoy <- as.vector(pacf(y, lag.max = lag.max, plot = FALSE)$acf)
return(sqrt(t(rhox - rhoy) %*% diag(length(rhox)) %*% (rhox - rhoy)))
}
diss.AR.PIC <- function(x.mat, y.mat){
return( as.numeric(dist(rbind(AR_coeff(x.mat), AR_coeff(y.mat)))) )
}
#
diss.ACF2 <- function(rhox, rhoy){
return(sqrt(t(rhox - rhoy) %*% diag(length(rhox)) %*% (rhox - rhoy)))
}
diss.PACF2 <- function(rhox, rhoy){
return(sqrt(t(rhox - rhoy) %*% diag(length(rhox)) %*% (rhox - rhoy)))
}
diss.AR.PIC2 <- function(coef1, coef2){
return(sqrt(t(coef1 - coef2) %*% diag(length(coef1)) %*% (coef1 - coef2)))
}
#### Simulation functions ####
sim <- function(ar=c(0,0), ma=c(0,0), sigma_i, n_i, I=50){
y <- NULL
for(i in 1:I){
y_i <- arima.sim( model=list(ar=ar,ma=ma, mean=0, sd=sigma_i), n = n_i, n.start = 100 )
y <- cbind(y,y_i)
}
colnames(y) <- NULL
return(y)
}
sim_fit <- function(z_true,
y, # time series data as columns
return_data=T){
#
matrix_list <- list()
coeff_df <- NULL
acf_df <- NULL
pacf_df <- NULL
acf_dist <- diag(rep(0,dim(y)[2]))
pacf_dist <- diag(rep(0,dim(y)[2]))
pic_dist <- diag(rep(0,dim(y)[2]))
n_vector <- c()
#
for(i in 1:dim(y)[2]){
n_vector <- c(n_vector,sum(!is.na(y[,i])))
matrix_list[[i]] <- sum(!is.na(y[,i]))*
cov_to_matrix(acf(y[,i], lag.max = 2, plot = F,
type = "covariance",
na.action = na.pass)$acf)
#
coeff <- arima(y[,i],order = c(2,0,0),method = "ML",include.mean = F)$coef[1:2]
coeff_df <- rbind(coeff_df, coeff)
#
acf_i <- acf(y[,i],lag.max = 2,plot = F,na.action = na.pass)$acf[2:3]
acf_df <- rbind(acf_df, acf_i)
#
pacf_i <- pacf(y[,i],lag.max = 2,plot = F,na.action = na.pass)$acf
pacf_df <- rbind(pacf_df,pacf_i)
}
#
for(r in 1:dim(y)[2] ){
for( c in 1:dim(y)[2] ){
pic_dist[r,c] <- diss.AR.PIC2(coef1 = coeff_df[r,],
coef2 = coeff_df[c,])
acf_dist[r,c] <- diss.ACF2(acf_df[r,],acf_df[c,])
pacf_dist[r,c] <- diss.PACF2(pacf_df[r,],pacf_df[c,])
}
}
#
acf_dist <- as.dist(acf_dist)
pacf_dist <- as.dist(pacf_dist)
pic_dist <- as.dist(pic_dist)
#
out_acf <- cutree(hclust(acf_dist), k = 2)
out_pacf <- cutree(hclust(pacf_dist), k = 2)
out_pic <- cutree(hclust(pic_dist), k = 2)
GMM <- Mclust(data = coeff_df, G=2)
out1 <- WMM(matrix_list=matrix_list, n_vector = n_vector, G = 2,
method = "EM1",
burn_in = 10, max_iter = 1000,
pi_list_init = NULL, sigma_list_init = NULL,
z_list_init = NULL,
lambda_list_init = NULL)
out2 <- WMM(matrix_list=matrix_list, n_vector = n_vector, G = 2,
method = "EM2",
burn_in = 10, max_iter = 200,
pi_list_init = NULL,
sigma_list_init = out1$sigma_list,
z_list_init = NULL,
lambda_list_init = list(1.0,1.0) )
#####
pGMM <- mean( (GMM$z[,1] > 0.5) == z_true )
pGMM <- max(pGMM,1-pGMM)
pACF <- mean( (out_acf==1) == z_true )
pACF <- max(pACF,1-pACF)
pPACF <- mean( (out_pacf==1) == z_true )
pPACF <- max(pPACF,1-pPACF)
pPIC <- mean( (out_pic==1) == z_true )
pPIC <- max(pPIC,1-pPIC)
pEM1 <- mean( (out1$z_list[[1]] > 0.5) == z_true )
pEM1 <- max(pEM1,1-pEM1)
pEM2 <- mean( (out2$z_list[[1]] > 0.5) == z_true )
pEM2 <- max(pEM2,1-pEM2)
output <- list(
"pACF" = pACF,
"pPACF" = pPACF,
"pPIC" = pPIC,
"pGMM" = pGMM,
"pEM1" = pEM1,
"pEM2" = pEM2
)
if(return_data){
output$data <- y
}
return(output)
}