-
Notifications
You must be signed in to change notification settings - Fork 1
/
.Rhistory
512 lines (512 loc) · 15 KB
/
.Rhistory
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
return(t(mat)); index = attempts}
}
}
updateMessage <- function(message) {
# Clear the previous message by printing '\r' (carriage return)
cat("\r")
# Print the updated message
cat(message)
# Flush the output immediately to ensure the message is displayed
flush.console()
}
# Function:
# nvar = Number of Variables
# auto = value of the AR coefficients
# cross = mean value of the cross-regressions
# sd.cross = SD of the cross-regressions
# bxy = The paths to be filled with non-zero cross-regressions
# attempts = The number of attempts to generate a VAR model before giving up
creat_pop_coef <- function(nvar, auto, cross, sd.cross, bxy, attempts = 50) {
index = 1
while(index <= attempts){
# Creating Dynamics Matrix
mat <- matrix(0, ncol = nvar, nrow = nvar)
diag(mat) <- auto
for (i in 1:nrow(bxy)){
mat[bxy[i,1], bxy[i,2]] <- rnorm(1, cross,sd.cross)
}
# Stationarity Test
evB1 <- eigen(matrix(mat, nvar, nvar, byrow = T))$values
if(any(sqrt(abs(Re(evB1))^2 + abs(Im(evB1))^2) > 1)){
index = index+1;
if(index == attempts){updateMessage(paste0('Could not find a stationary VAR model with your desired settings'))
break}else{
updateMessage(paste0('Non-Stationary. Retrying. Attempt #',index,' of ', attempts))}
}else{
return(t(mat)); index = attempts}
}
}
# The actual code
set.seed(321993)
# Number of Variables
nvar = 10
# Number of Cross-Regressions desired
paths = 20
# Listing all possible matrix elements
cfs = matrix(c(rep(1:nvar, each=nvar), rep(1:nvar, nvar)), ncol=2, nrow=nvar^2)
# Eliminating AR's from the list of options for possible candidate paths
cfs = cfs[-which(cfs[,1] - cfs[,2] == 0),]
# Simulating a population VAR(1) model with cross-regressions set to rnorm(1, mean = 0.2, sd = 0.05)
mat = creat_pop_coef(nvar = nvar,
auto = 0.7,
cross = -0.2, sd.cross = 0.05,
bxy = cfs[sample(nrow(cfs),paths),])
# Printing the matrix
round(mat, 2)
# Showcasing a failed example:
mat = creat_pop_coef(nvar = nvar,
auto = 0.7,
cross = 0.5, sd.cross = 0.05,
bxy = cfs[sample(nrow(cfs),27),])
# Example usage in a for loop
for (i in 1:10) {
# Construct the message
message <- paste("Processing iteration", i)
# Update the message
updateMessage(paste0('Non-Stationary. Retrying.'))
# Perform some computation or task
# ...
# Add a slight delay to simulate work being done
Sys.sleep(1)
}
# Showcasing a failed example:
mat = creat_pop_coef(nvar = nvar,
auto = 0.7,
cross = 0.5, sd.cross = 0.05,
bxy = cfs[sample(nrow(cfs),27),])
# Showcasing a failed example:
mat = creat_pop_coef(nvar = nvar,
auto = 0.7,
cross = 0.5, sd.cross = 0.05,
bxy = cfs[sample(nrow(cfs),27),])
# Showcasing a failed example:
mat = creat_pop_coef(nvar = nvar,
auto = 0.7,
cross = 0.5, sd.cross = 0.05,
bxy = cfs[sample(nrow(cfs),27),])
# Showcasing a failed example with 27 positive Cross-Regressions with a mean-value of 0.3
mat = creat_pop_coef(nvar = nvar,
auto = 0.7,
cross = 0.3, sd.cross = 0.05,
bxy = cfs[sample(nrow(cfs),27),])
# The actual code
set.seed(321993)
# Number of Variables
nvar = 100
# Number of Cross-Regressions desired
paths = 200
# Listing all possible matrix elements
cfs = matrix(c(rep(1:nvar, each=nvar), rep(1:nvar, nvar)), ncol=2, nrow=nvar^2)
# Eliminating AR's from the list of options for possible candidate paths
cfs = cfs[-which(cfs[,1] - cfs[,2] == 0),]
# Simulating a population VAR(1) model with cross-regressions set to rnorm(1, mean = 0.2, sd = 0.05)
mat = creat_pop_coef(nvar = nvar,
auto = 0.7,
cross = -0.2, sd.cross = 0.05,
bxy = cfs[sample(nrow(cfs),paths),])
mat
sum(abs(mat)>0)
library(dynr); library(qgraph)
library(igraph); library(fclust)
# Sample Characteristics
nv = 5; times = 300; N = 8
set.seed(312489)
# Variables, times, sample size
# Params for Group 1
beta1 = matrix(c(0.62, 0.00, 0.00, 0.00, 0.00,
0.00, 0.53, 0.00, 0.00, 0.00,
0.00, 0.00, 0.61, 0.00, 0.00,
0.00, 0.00, 0.00, 0.52,-0.22,
0.32,-0.16, 0.00, 0.00, 0.52), nv, nv, byrow = TRUE)
psi1 = matrix(c(0.83,0.00,0.00,-0.40,0.40,
0.00,0.99,0.00,0.00,0.00,
0.00,0.00,0.88,0.00,0.00,
-0.40,0.00,0.00,1.01,0.00,
0.40,0.00,0.00,0.00,0.91), nv, nv, byrow = TRUE)
# Params for Group 2
beta2 = matrix(c(0.62,0.00, 0.00, 0.00,0.00,
0.00,0.53, 0.00, 0.00,0.00,
0.00,0.00, 0.61, 0.00,0.00,
0.00,0.00, 0.30, 0.52,0.00,
0.00,-0.16, 0.00, -0.35,0.52), nv, nv, byrow = TRUE)
psi2 = matrix(c(0.83,0.00,0.00,0.00,0.00,
0.00,0.99,0.40,0.00,0.00,
0.00,0.40,0.88,0.40,0.00,
0.00,0.00,0.40,1.01,0.00,
0.00,0.00,0.00,0.00,0.91), nv, nv, byrow = TRUE)
# Variables, times, sample size
# Params for Group 1
beta1 = matrix(c(0.62, 0.00, 0.00, 0.00, 0.00,
0.00, 0.53, 0.00, 0.00, 0.00,
0.00, 0.00, 0.61, 0.00, 0.00,
0.00, 0.00, 0.00, 0.52,-0.22,
0.32,-0.16, 0.00, 0.00, 0.52), nv, nv, byrow = TRUE)
psi1 = matrix(c(0.83,0.00,0.00,-0.40,0.40,
0.00,0.99,0.00,0.00,0.00,
0.00,0.00,0.88,0.00,0.00,
-0.40,0.00,0.00,1.01,0.00,
0.40,0.00,0.00,0.00,0.91), nv, nv, byrow = TRUE)
# Params for Group 2
beta2 = matrix(c(0.62,0.00, 0.00, 0.00,0.00,
0.00,0.53, 0.00, 0.00,0.00,
0.00,0.00, 0.61, 0.00,0.00,
0.00,0.00, 0.30, 0.52,0.00,
0.00,-0.16, 0.00, -0.35,0.52), nv, nv, byrow = TRUE)
psi2 = matrix(c(0.83,0.00,0.00,0.00,0.00,
0.00,0.99,0.40,0.00,0.00,
0.00,0.40,0.88,0.40,0.00,
0.00,0.00,0.40,1.01,0.00,
0.00,0.00,0.00,0.00,0.91), nv, nv, byrow = TRUE)
# Variables, times, sample size
# Params for Group 1
beta1 = matrix(c(0.62, 0.00, 0.00, 0.00, 0.00,
0.00, 0.53, 0.00, 0.00, 0.00,
0.00, 0.00, 0.61, 0.00, 0.00,
0.00, 0.00, 0.00, 0.52,-0.22,
0.32,-0.16, 0.00, 0.00, 0.52), nv, nv, byrow = TRUE)
psi1 = matrix(c(0.83,0.00,0.00,-0.40,0.40,
0.00,0.99,0.00,0.00,0.00,
0.00,0.00,0.88,0.00,0.00,
-0.40,0.00,0.00,1.01,0.00,
0.40,0.00,0.00,0.00,0.91), nv, nv, byrow = TRUE)
# Params for Group 2
beta2 = matrix(c(0.62,0.00, 0.00, 0.00,0.00,
0.00,0.53, 0.00, 0.00,0.00,
0.00,0.00, 0.61, 0.00,0.00,
0.00,0.00, 0.30, 0.52,0.00,
0.00,-0.16, 0.00, -0.35,0.52), nv, nv, byrow = TRUE)
psi2 = matrix(c(0.83,0.00,0.00,0.00,0.00,
0.00,0.99,0.40,0.00,0.00,
0.00,0.40,0.88,0.40,0.00,
0.00,0.00,0.40,1.01,0.00,
0.00,0.00,0.00,0.00,0.91), nv, nv, byrow = TRUE)
# Data Generation
# N/2 in Subgroup 1
# N/2 in Subgroup 2
true.dat = NULL
for(j in 1:N){
dat2fit = matrix(NA, times, nv)
dat2fit[1,] = rnorm(nv)
if(j <= N/2){
beta = beta1; kap = psi1
}else{beta = beta2; kap = psi2}
for(i in 2:times){
dat2fit[i,] = beta %*% dat2fit[i-1,] +
MASS::mvrnorm(1, mu = rep(0, 5), Sigma = kap)
}
true.dat = rbind(true.dat, dat2fit)
}
# Adding 2 Fuzzy Subjects
for(i in 1:2){
dat2fit = matrix(NA, times, nv)
dat2fit[1,] = rnorm(nv)
fuzz = rbinom(times, 1, prob = 0.5)
for(i in 2:times){
if(fuzz[i] == 1){
beta = beta1; kap = psi1
}else{beta = beta2; kap = psi2}
dat2fit[i,] = beta %*% dat2fit[i-1,] +
MASS::mvrnorm(1, mu = rep(0, 5), Sigma = kap)
}
true.dat = rbind(true.dat, dat2fit)
}
# Adding Labels
true.dat = data.frame(true.dat)
true.dat$id = rep(1:(N+2), each = times)
true.dat$time = rep(1:times, (N+2))
source('./dynrVAR.R')
varnames = paste0('Y', 1:4)
varnames
nv=4
ini.loadings <- diag(1, nv)
# Preparing Measurement
meas <- prep.measurement(
values.load = ini.loadings,
params.load = matrix(rep("fixed", (nv^2)), ncol = nv), # Flag for customization
state.names = paste0("X", 1:nv),
obs.names = varnames
)
meas
length(varnames)
source('./dynrVAR.R')
true.dat
head(true.dat)
true.dat[,true.dat$id == 1]
true.dat[true.dat$id == 1,]
poop = true.dat[true.dat$id == 1,]
# Fitting Auto-VAR
# COMMENTED OUT TO SAVE YOU TIME
func.test = dynr.VAR(data = poop, nv = nv, ID = 'id',
time = 'time', dir = '~/Desktop/test/')
# Fitting Auto-VAR
# COMMENTED OUT TO SAVE YOU TIME
func.test = dynr.VAR(data = poop, varnames = paste0('X', 1:5), ID = 'id',
time = 'time', dir = '~/Desktop/test/')
# Fitting Auto-VAR
# COMMENTED OUT TO SAVE YOU TIME
func.test = dynr.VAR(data = poop, varnames = paste0('X', 1:5), id = 'id',
time = 'time', dir = '~/Desktop/test/')
dataframe = true.dat
head(dataframe)
id
id='id'
unique(dataframe[,id])
# 1. Create Chained Time-Series
# Take each individual, i, and chain them with the i+1 subject
# Add a blank space between i and i+1
# Knowns
# 1. Dataset
# 2. ID Variable
chain.temp = NULL
chain.temp
i=1
dataframe[,id==i]
dataframe[id==i,]
id==i
dataframe[,id[i]]
id
dataframe[,id]
dataframe[,id]==id
dataframe[,id]==i
dataframe[dataframe[,id]==i,]
# 1. Create Chained Time-Series
# Take each individual, i, and chain them with the i+1 subject
# Add a blank space between i and i+1
# Knowns
# 1. Dataset
# 2. ID Variable
chain.temp = NULL
for(i in unique(dataframe[,id])){
chain.temp = rbind(chain.temp, dataframe[dataframe[,id]==i,])
}
chain.temp
dataframe[dataframe[,id]==i,]
temp1 = dataframe[dataframe[,id]==i,]
temp1 = dataframe[dataframe[,id]==i,]
tail(temp1)
nrow(temp1)
nrow(temp1)+1
temp1 = dataframe[dataframe[,id]==i,varnames]
dataframe[dataframe[,id]==i,varnames]
varnames
varnames = paste0('X', 1:5)
dataframe[dataframe[,id]==i,varnames]
temp1 = dataframe[dataframe[,id]==i,varnames]
rep(NA, length(varnames))
temp1[nrow(temp1)+1,] = rep(NA, length(varnames))
length(unique(dataframe[,id]))
unique(dataframe[,id])[length(unique(dataframe[,id]))]
# 1. Create Chained Time-Series
# Take each individual, i, and chain them with the i+1 subject
# Add a blank space between i and i+1
# Knowns
# 1. Dataset
# 2. ID Variable
chain.temp = NULL
for(i in unique(dataframe[,id])){
temp1 = dataframe[dataframe[,id]==i,varnames]
if(i != unique(dataframe[,id])[length(unique(dataframe[,id]))]){
temp1[nrow(temp1)+1,] = rep(NA, length(varnames))
}
chain.temp = rbind(chain.temp, temp1)
}
tail(chain.temp)
nrow(chain.temp)
chain.temp[299:305,]
1:nrow(chain.temp)
rep(1, nrow(chain.temp))
chain.temp$TIMES = 1:nrow(chain.temp)
chain.temp$ID = rep(1, nrow(chain.temp))
names(chain.temp)
id
times
time
time = 'time'
names(chain.temp) = c(varnames, time, id)
names(chain.temp)
# Calculating the PDC/PCCs
# See Wild, Eichler, et al (2010)
for (p in 1:nv) {
for (q in 1:nv) {
PCC[p, q] <- -((OG.kappa[p, q]) / (sqrt(OG.kappa[p, p] * OG.kappa[q, q])))
PDC[p, q] <- (OG.betas[p, q]) / (sqrt(OG.betas[p, p] * OG.kappa[q, q] + (OG.betas[p, q]^2)))
}
}
source('./dynrVAR.R')
# Sample Characteristics
nv = 5; times = 300; N = 8
set.seed(312489)
# Variables, times, sample size
# Params for Group 1
beta1 = matrix(c(0.62, 0.00, 0.00, 0.00, 0.00,
0.00, 0.53, 0.00, 0.00, 0.00,
0.00, 0.00, 0.61, 0.00, 0.00,
0.00, 0.00, 0.00, 0.52,-0.22,
0.32,-0.16, 0.00, 0.00, 0.52), nv, nv, byrow = TRUE)
psi1 = matrix(c(0.83,0.00,0.00,-0.40,0.40,
0.00,0.99,0.00,0.00,0.00,
0.00,0.00,0.88,0.00,0.00,
-0.40,0.00,0.00,1.01,0.00,
0.40,0.00,0.00,0.00,0.91), nv, nv, byrow = TRUE)
# Params for Group 2
beta2 = matrix(c(0.62,0.00, 0.00, 0.00,0.00,
0.00,0.53, 0.00, 0.00,0.00,
0.00,0.00, 0.61, 0.00,0.00,
0.00,0.00, 0.30, 0.52,0.00,
0.00,-0.16, 0.00, -0.35,0.52), nv, nv, byrow = TRUE)
psi2 = matrix(c(0.83,0.00,0.00,0.00,0.00,
0.00,0.99,0.40,0.00,0.00,
0.00,0.40,0.88,0.40,0.00,
0.00,0.00,0.40,1.01,0.00,
0.00,0.00,0.00,0.00,0.91), nv, nv, byrow = TRUE)
# Data Generation
# N/2 in Subgroup 1
# N/2 in Subgroup 2
true.dat = NULL
for(j in 1:N){
dat2fit = matrix(NA, times, nv)
dat2fit[1,] = rnorm(nv)
if(j <= N/2){
beta = beta1; kap = psi1
}else{beta = beta2; kap = psi2}
for(i in 2:times){
dat2fit[i,] = beta %*% dat2fit[i-1,] +
MASS::mvrnorm(1, mu = rep(0, 5), Sigma = kap)
}
true.dat = rbind(true.dat, dat2fit)
}
# Adding 2 Fuzzy Subjects
for(i in 1:2){
dat2fit = matrix(NA, times, nv)
dat2fit[1,] = rnorm(nv)
fuzz = rbinom(times, 1, prob = 0.5)
for(i in 2:times){
if(fuzz[i] == 1){
beta = beta1; kap = psi1
}else{beta = beta2; kap = psi2}
dat2fit[i,] = beta %*% dat2fit[i-1,] +
MASS::mvrnorm(1, mu = rep(0, 5), Sigma = kap)
}
true.dat = rbind(true.dat, dat2fit)
}
# Adding Labels
true.dat = data.frame(true.dat)
true.dat$id = rep(1:(N+2), each = times)
true.dat$time = rep(1:times, (N+2))
poop = true.dat[true.dat$id == c(1:3),]
poop
?subset
# Fitting Auto-VAR
# COMMENTED OUT TO SAVE YOU TIME
subset(true.dat, id == c(1:3))
# Fitting Auto-VAR
# COMMENTED OUT TO SAVE YOU TIME
subset(true.dat, id == c(1))
subset(true.dat, id == 1 | id == 2 | id == 3)
# Fitting Auto-VAR
# COMMENTED OUT TO SAVE YOU TIME
poop = subset(true.dat, id == 1 | id == 2 | id == 3)
func.test = dynr.VAR(data = poop, varnames = paste0('X', 1:5),
id = 'id', time = 'time', dir = '~/Desktop/test/')
source('./dynrVAR.R')
# Fitting Auto-VAR
# COMMENTED OUT TO SAVE YOU TIME
poop = subset(true.dat, id == 1 | id == 2)# | id == 3)
func.test = dynr.VAR(data = poop, varnames = paste0('X', 1:5),
id = 'id', time = 'time', dir = '~/Desktop/test/',
MLVAR = TRUE)
func.test
func.test[[2]]
func.test[[2]]$PCC
func.test[[1]][[1]]
func.test[[1]][[i]]$PCC
i=1
func.test[[1]][[i]]$PCC
c(func.test[[1]][[i]]$PCC )
averagenet = NULL
c(func.test[[1]][[i]]$PCC)
temp.avg = NULL
averagenet = NULL
temp.avg = NULL
averagenet = NULL
for(i in 1:2){
temp.avg = rbind(temp.avg, c(func.test[[1]][[i]]$PCC))
}
temp.avg
colMeans(temp.avg)
matrix(colMeans(temp.avg), length(varnames), length(varnames), byrow=TRUE)
func.test[[1]][[i]]$PCC
func.test[[1]][[1]]$PCC
apply(temp.avg, 2, sd)
temp.avg = NULL
averagenet = NULL
temp.avg = NULL
averagenet = NULL
for(i in 1:2){
temp.avg = rbind(temp.avg, c(func.test[[1]][[i]]$PCC))
sdnet = matrix(apply(temp.avg, 2, sd), length(varnames), length(varnames), byrow = TRUE)
averagenet = matrix(colMeans(temp.avg), length(varnames), length(varnames), byrow = TRUE)
}
sdnet
func.test[[1]][[i]]$paste0('PCC')
dynrVAR.out=func.test
c(dynrVAR.out[[1]][[i]])
dynrVAR.out[[1]][[i]][1]
dynrVAR.out[[1]][[i]][[paste0('Betas')]]
length(dynrVAR.out[[1]])
avgnets = list()
avgnets = list()
list(averagenet, sdnet)
list('Average Network' = averagenet, 'SD Network' = sdnet)
avgnets$paste0(pararms)
j='Betas'
avgnets[paste0(params[j])] = list('Average Network' = averagenet, 'SD Network' = sdnet)
params = c('Betas', 'Resid','PDC', 'PCC')
avgnets = list()
avgnets[paste0(params[j])] = list('Average Network' = averagenet, 'SD Network' = sdnet)
paste0(params[j])
j
params
avgnets[paste0(j)] = list('Average Network' = averagenet, 'SD Network' = sdnet)
paste0(j)
avgnets[[paste0(j)]] = list('Average Network' = averagenet, 'SD Network' = sdnet)
avgnets
avgnets = list()
temp.avg = NULL
averagenet = NULL
for(j in params){
for(i in 1:length(dynrVAR.out[[1]])){
thenets = c(dynrVAR.out[[1]][[i]][[j]])
temp.avg = rbind(temp.avg, thenets)
sdnet = matrix(apply(temp.avg, 2, sd), length(varnames), length(varnames), byrow = TRUE)
averagenet = matrix(colMeans(temp.avg), length(varnames), length(varnames), byrow = TRUE)
}
avgnets[[paste0(j)]] = list('Average Network' = averagenet, 'SD Network' = sdnet)
}
avgnets
avgnets = list()
temp.avg = NULL
averagenet = NULL
for(j in params){
for(i in 1:length(dynrVAR.out[[1]])){
thenets = c(dynrVAR.out[[1]][[i]][[j]])
temp.avg = rbind(temp.avg, thenets)
sdnet = matrix(apply(temp.avg, 2, sd), length(varnames), length(varnames), byrow = TRUE)
averagenet = matrix(colMeans(temp.avg), length(varnames), length(varnames), byrow = TRUE)
}
avgnets[j] = list('Average Network' = averagenet, 'SD Network' = sdnet)
}
avgnets
avgnets = list()
temp.avg = NULL
averagenet = NULL
for(j in params){
for(i in 1:length(dynrVAR.out[[1]])){
thenets = c(dynrVAR.out[[1]][[i]][[j]])
temp.avg = rbind(temp.avg, thenets)
sdnet = matrix(apply(temp.avg, 2, sd), length(varnames), length(varnames), byrow = TRUE)
averagenet = matrix(colMeans(temp.avg), length(varnames), length(varnames), byrow = TRUE)
}
avgnets[[j]] = list('Average Network' = averagenet, 'SD Network' = sdnet)
}
avgnets