-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelComp_2_2022.jl
818 lines (594 loc) · 20.8 KB
/
ModelComp_2_2022.jl
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
### Model Comparison - MMRM v BHM
# October 7th
using DataFrames, Turing, StatsBase, StatsPlots, Plots, LinearAlgebra, Random, CSV, PrettyTables
using BSON: @save,@load
# Run Functions in ModelComp.jl
### Comparing BHM and MMRM data generating processes under different model specifications
#= BHM - AR, Log Trend, Spectral
MMRM - Time Effects, Log Trend, Log Trend + Quadratic Trend with Error Structures - Compound Symmetry and Homogeneous AR1 Errors =#
### DGPs
#= AR, Log Trend + IID Normal Error, Time Effects + Error Structures, TADS =#
# Time Effect + Compound Symmetry Error Structure - si, st are variances not SD.
function mix_cs((n, t, fes, si, st, rho),)
om = zeros(t, t)
for i in 1:t, j in 1:t
if i == j
om[i, j] = 1.0 * st
else
om[i, j] = st * rho
end
end
x = rand(Bernoulli(0.5), n)
xt = repeat(x, inner=t)
ts = [repeat(Matrix(I, t, t)[:, i], outer=n) for i in 1:t]
t_fes = reduce(hcat, ts)
X = [ones(n * t) xt t_fes[:, 2:end] xt .* t_fes[:, 2:end]]
xb = X * fes
xb_s = xb .+ reduce(vcat, rand(MvNormal(fill(0.0, t), om), n)) .+ rand(Normal(0, sqrt(si)), (n * t))
y0 = repeat(rand(5:7, n), inner=t)
yt = zeros(n * t)
yt[1:t:end] = y0[1:t:end]
yt[Not(1:t:end)] = y0[Not(1:t:end)] .+ xb_s[Not(1:t:end)]
return yt, DataFrame(X, :auto)
end
# Time Effect + IID Error Structure
function mix((n, t, fes, sig),)
x = rand(Bernoulli(0.5), n)
xt = repeat(x, inner=t)
e = sqrt(sig) .* randn(n * t)
ts = [repeat(Matrix(I, t, t)[:, i], outer=n) for i in 1:t]
t_fes = reduce(hcat, ts)
X = [ones(n * t) xt t_fes[:, 2:end] xt .* t_fes[:, 2:end]]
xb = X * fes
xb_s = xb .+ e
y0 = repeat(rand(5:7, n), inner=t)
yt = zeros(n * t)
yt[1:t:end] = y0[1:t:end]
yt[Not(1:t:end)] = y0[Not(1:t:end)] .+ xb_s[Not(1:t:end)]
return yt, DataFrame(X, :auto)
end
#= # Time Effect + Toeplitz Error Structure - si, st are variances not SD. rho is Vector with rho1 rho2 rho3 i.e., (t-1) Toeplitz has issues
function mix_t(n, t, b, si, st, rho)
om = zeros(t, t)
for i in 1:t, j in 1:t
if i == j
om[i, j] = 1.0 * st
else
om[i, j] = st * rho[abs(i - j)]
end
end
x = randn(n)
xt = repeat(x, inner=t)
ts = [repeat(Matrix(I, t, t)[:, i], outer=n) for i in 1:t]
t_fes = reduce(hcat, ts)
X = [ones(n * t) xt t_fes[:, 2:end]]
xb = X * b
xb_s = xb .+ reduce(vcat, rand(MvNormal(fill(0.0, 4), om), n)) .+ rand(Normal(0, sqrt(si)), (n * t))
y0 = repeat(rand(5:7, n), inner=4)
y = y0 .+ xb_s
y2 = copy(y)
yt = y2
return yt, DataFrame(X, ["intercept"; "x"; "t1"; "t2"; "t3"])
end =#
# Time Effect + AR1 Error Structure - si, st are variances not SD.
function mix_ar1((n, t, b, si, st, rho),)
om = [st * rho^abs(i - j) for i in 1:t, j in 1:t]
x = rand(Bernoulli(0.5), n)
xt = repeat(x, inner=t)
ts = [repeat(Matrix(I, t, t)[:, i], outer=n) for i in 1:t]
t_fes = reduce(hcat, ts)
X = [ones(n * t) xt t_fes[:, 2:end] xt .* t_fes[:, 2:end]]
xb = X * b
xb_s = xb .+ reduce(vcat, rand(MvNormal(fill(0.0, t), om), n)) .+ rand(Normal(0, sqrt(si)), (n * t))
y0 = repeat(rand(5:7, n), inner=t)
yt = zeros(n*t)
yt[1:t:end] = y0[1:t:end]
yt[Not(1:t:end)] = y0[Not(1:t:end)] .+ xb_s[Not(1:t:end)]
return yt, DataFrame(X, :auto)
end
# Log Trend + IID Normal Errors
function lt_dgp((n, t, b, d, sig),)
x = rand(Bernoulli(0.5), n)
xt = repeat(x, inner=t)
lt = repeat(log.(1:t), outer=n)
xb = (xt .* lt) * b
y0 = rand(5:7, n)
yt = zeros(n * t)
e = sqrt(sig) .* randn(n * t)
for i in 2:n*t
if rem(i, t) == 1
yt[i] = 0.0
else
yt[i] = d * lt[i] + xb[i]
end
end
return repeat(y0, inner=t) .+ yt .+ e, [lt xt]
end
# Log Trend + AR1 Errors
function lt_ar1((n, t, b, d, si, st, rho),)
x = rand(Bernoulli(0.5), n)
xt = repeat(x, inner=t)
lt = repeat(log.(1:t), outer=n)
xb = (xt .* lt) * b
y0 = rand(5:7, n)
yt = zeros(n * t)
om = [st * rho^abs(i - j) for i in 1:t, j in 1:t]
e = reduce(vcat, rand(MvNormal(fill(0.0, t), om), n)) .+ rand(Normal(0, sqrt(si)), (n * t))
for i in 2:n*t
if rem(i, t) == 1
yt[i] = 0.0
else
yt[i] = d * lt[i] + xb[i]
end
end
return repeat(y0, inner=t) .+ yt .+ e, [lt xt]
end
# Log Trend + CS Errors
function lt_cs((n, t, b, d, si, st, rho),)
x = rand(Bernoulli(0.5), n)
xt = repeat(x, inner=t)
lt = repeat(log.(1:t), outer=n)
xb = (xt .* lt) * b
y0 = rand(5:7, n)
yt = zeros(n * t)
om = zeros(t, t)
for i in 1:t, j in 1:t
if i == j
om[i, j] = 1.0 * st
else
om[i, j] = st * rho
end
end
e = reduce(vcat, rand(MvNormal(fill(0.0, t), om), n)) .+ rand(Normal(0, sqrt(si)), (n * t))
for i in 2:n*t
if rem(i, t) == 1
yt[i] = 0.0
else
yt[i] = d * lt[i] + xb[i]
end
end
return repeat(y0, inner=t) .+ yt .+ e, [lt xt]
end
#= # Log Trend + Toeplitz Errors
function lt_t(n, t, b, d, si, st, rho)
x = randn(n)
xt = repeat(x, inner=t)
lt = repeat(log.(1:t), outer=n)
xb = (xt .* lt) * b
y0 = rand(5:7, n)
yt = zeros(n * t)
om = zeros(t, t)
for i in 1:t, j in 1:t
if i == j
om[i, j] = 1.0 * st
else
om[i, j] = st * rho[abs(i - j)]
end
end
e = reduce(vcat, rand(MvNormal(fill(0.0, 4), om), n)) .+ rand(Normal(0, sqrt(si)), (n * t))
for i in 2:n*t
if rem(i, t) == 1
yt[i] = 0.0
else
yt[i] = d * lt[i] + xb[i]
end
end
return repeat(y0, inner=t) .+ yt .+ e, [lt xt]
end
=#
# AR1
function ar_dgp((n, t, a, b, r, sig),)
x = rand(Bernoulli(0.5), n)
xt = repeat(x, inner=t)
xb1 = xt * b[1]
xb2 = xt * b[2]
y0 = rand(5:7, n)
yt = zeros(n * t)
e = sqrt(sig) .* randn(n * t)
yt[1:t:end] = y0
for i in deleteat!(collect(1:n*t), collect(1:t:n*t))
yt[i] = r * yt[i-1] + xb1[i] * yt[i-1] + xb2[i] + a
end
return yt .+ e, xt
end
#= Model Statements
BHM - Spectral, Log Trend IID Errors, AR1
=#
# Spectral Trend Model
@model bhm_onefreq(y, X1, ::Type{TV}=Float64) where {TV} = begin
n1, k1 = size(X1) # TORDIA
sig ~ Uniform(0.01, 5.0)
w ~ Uniform(0, 1)
a_s ~ Normal(0, 5)
b_s ~ Normal(0, 5)
# alp ~ Normal(0, 5)
b = Vector{TV}(undef, (k1 - 1))
for i = 1:(k1-1)
b[i] ~ Normal(0, 1)
end
xb = Matrix(X1[:, 2:end]) * b
mu1 = a_s .* sin.(2π .* w .* X1[:, 1]) .+ b_s .* cos.(2π .* w .* X1[:, 1]) .+ xb
for i = 1:n1
y[i] ~ Normal(mu1[i], sqrt(sig))
end
end
# Log Trend Model
@model bhm_logt(y, X1, ::Type{TV}=Float64) where {TV} = begin
n1, k1 = size(X1)
sig ~ Uniform(0.01, 5.0)
d1 ~ TruncatedNormal(-1, 1.0, -5, 0)
b = Vector{TV}(undef, (k1 - 1))
for i = 1:(k1-1)
b[i] ~ Normal(0, 5)
end
mu1 = d1 .* X1[:, 1] .+ (Matrix(X1[:, 2:end])) * b
for i = 1:n1
y[i] ~ Normal(mu1[i], sqrt(sig))
end
end
# AR1 Model
@model bhm_ar1(y, X1, ::Type{TV}=Float64) where {TV} = begin
n1, k1 = size(X1)
sig ~ Uniform(0.01, 5.0)
rho ~ Uniform(-1, 1)
b = Vector{TV}(undef, k1)
for i = 1:k1
b[i] ~ Normal(0, 3)
end
a ~ Normal(0,3)
mu1 = rho .* X1[:, 1] .+ [X1[:, 1] .* Matrix(X1[:, 2:end]) X1[:,2]] * b .+ a
for i = 1:n1
y[i] ~ Normal(mu1[i], sqrt(sig))
end
end
#=
MMRM - Time Effects with AR1, CS, Toeplitz,
Log Trend with AR1, CS, Toeplitz
=#
# MMRM Time Effects with AR1 Errors
@model mmrm_t_ar1(y, x, t, ::Type{TV}=Float64) where {TV} = begin
n, k = size(x)
ind = n ÷ t
s_i ~ Uniform(0.001, 5.0)
s_t ~ Uniform(0.001, 5.0)
rho ~ Uniform(0.0, 0.999)
b = Vector{TV}(undef, k)
for i in 1:k
b[i] ~ Normal(0.0, 5.0)
end
omega = [s_t * rho^abs(i - j) for i in 1:t, j in 1:t] .+ Diagonal(fill(s_i, t))
xb = x * b
for i in 1:ind
y[(i-1)*t+1:i*t] ~ MvNormal(xb[(i-1)*t+1:i*t], omega)
end
end
# MMRM Time Effects with CS Errors
@model mmrm_t_cs(y, x, t, ::Type{TV}=Float64) where {TV} = begin
n, k = size(x)
ind = n ÷ t
s_i ~ Uniform(0.001, 5.0)
s_t ~ Uniform(0.001, 5.0)
rho ~ Uniform(0.0, 0.999)
b = Vector{TV}(undef, k)
for i in 1:k
b[i] ~ Normal(0.0, 5.0)
end
omega = Matrix{TV}(undef, t, t)
for i in 1:t, j in 1:t
if i == j
omega[i, j] = s_t + s_i
else
omega[i, j] = s_t * rho
end
end
xb = x * b
for i in 1:ind
y[(i-1)*t+1:i*t] ~ MvNormal(xb[(i-1)*t+1:i*t], omega)
end
end
#= # MMRM Time Effects with Toeplitz Errors
@model mmrm_t_t(y, x, t, ::Type{TV}=Float64) where {TV} = begin
n, k = size(x)
ind = n ÷ t
s_i ~ Uniform(0.001, 5.0)
s_t ~ Uniform(0.001, 5.0)
r = Vector{TV}(undef, t-1)
for i in 1:t-1
r[i] ~ Uniform(0.0, 0.999)
end
b = Vector{TV}(undef, k)
for i in 1:k
b[i] ~ Normal(0.0, 2.0)
end
omega = Matrix{TV}(undef, t, t)
for i in 1:t, j in 1:t
if i == j
omega[i, j] = s_t + s_i
else
omega[i, j] = s_t * r[abs(i - j)]
end
end
xb = x * b
for i in 1:ind
y[(i-1)*t+1:i*t] ~ MvNormal(xb[(i-1)*t+1:i*t], omega)
end
end =#
# MMRM Log Trend with AR1 Errors
@model mmrm_l_ar1(y, X1, t, ::Type{TV}=Float64) where {TV} = begin
n1, k1 = size(X1)
ind = n1 ÷ t
s_t ~ Uniform(0.01, 10.0)
s_i ~ Uniform(0.01, 10.0)
rho ~ Uniform(0.0, 0.999)
omega = [s_t * rho^abs(i - j) for i in 1:t, j in 1:t] .+ Diagonal(fill(s_i, t))
d1 ~ TruncatedNormal(-1, 1.0, -5, 0)
b = Vector{TV}(undef, (k1 - 1))
for i = 1:(k1-1)
b[i] ~ Normal(0, 5)
end
mu1 = d1 .* X1[:, 1] .+ (Matrix(X1[:, 2:end])) * b
for i in 1:ind
y[(i-1)*t+1:i*t] ~ MvNormal(mu1[(i-1)*t+1:i*t], omega)
end
end
# MMRM Log Trend with CS Errors
@model mmrm_l_cs(y, X1, t, ::Type{TV}=Float64) where {TV} = begin
n1, k1 = size(X1)
ind = n1 ÷ t
s_t ~ Uniform(0.01, 5.0)
s_i ~ Uniform(0.01, 5.0)
rho ~ Uniform(0.0, 0.999)
omega = Matrix{TV}(undef, t, t)
for i in 1:t, j in 1:t
if i == j
omega[i, j] = s_t + s_i
else
omega[i, j] = s_t * rho
end
end
d1 ~ TruncatedNormal(-1, 1.0, -5, 0)
b = Vector{TV}(undef, (k1 - 1))
for i = 1:(k1-1)
b[i] ~ Normal(0, 5)
end
mu1 = d1 .* X1[:, 1] .+ (Matrix(X1[:, 2:end])) * b
for i in 1:ind
y[(i-1)*t+1:i*t] ~ MvNormal(mu1[(i-1)*t+1:i*t], omega)
end
end
# MMRM Log Trend with CS Errors
@model te(y, X1, t, ::Type{TV}=Float64) where {TV} = begin
n1, k1 = size(X1)
sig ~ Uniform(0.01, 5.0)
b = Vector{TV}(undef, k1)
for i = 1:k1
b[i] ~ Normal(0, 5)
end
mu1 = Matrix(X1) * b
for i in 1:n1
y[i] ~ Normal(mu1[i], sqrt(sig))
end
end
#= # MMRM Log Trend with Toeplitz Errors
@model mmrm_l_t(y, X1, t, ::Type{TV}=Float64) where {TV} = begin
n1, k1 = size(X1)
ind = n1 ÷ t
s_t ~ Uniform(0.01, 5.0)
s_i ~ Uniform(0.01, 5.0)
r = Vector{TV}(undef, t-1)
for i in 1:t-1
r[i] ~ Uniform(0.0, 0.999)
end
omega = Matrix{TV}(undef, t, t)
for i in 1:t, j in 1:t
if i == j
omega[i, j] = s_t + s_i
else
omega[i, j] = s_t * r[abs(i - j)]
end
end
d1 ~ TruncatedNormal(-1, 1.0, -5, 0)
b = Vector{TV}(undef, (k1 - 1))
for i = 1:(k1-1)
b[i] ~ Normal(0, 1)
end
mu1 = d1 .* X1[:, 1] .+ (Matrix(X1[:, 2:end]) .* X1[:, 1]) * b
for i in 1:ind
y[(i-1)*t+1:i*t] ~ MvNormal(mu1[(i-1)*t+1:i*t], omega)
end
end =#
#= Data Generation from all 8 DGPs =#
# Individuals = 100, Time Measurements = 4, sig = 1.0, si = 1.0, st = 1.0, rho = 0.65, r1 = 0.65, r2 = 0.5, r3 = 0.4
# time effects = -1.0,-1.5,-2.5, d logt = -2.5, ar = 0.75, intercept = 0.0, beta = -0.5
# Global DGP variables
n = 100
n2 = 50
t = 4
M = 2000
fes = [0.0; -0.3; -1.0; -1.5; -2.5; -0.3; -0.3; -0.3] # Intercept beta t1 t2 t3 for the Time Effect dgps
b = -0.3 # Beta for the Ar and Log Trend dgps
ar = 0.75 # Ar coeffcient
d = -2.0 # Delta for Log Trend
sig = 1.0 # Ar, Log Trend IID DGP VARIANCE
si = 1.0 # measurement noise
st = 1.0 # random process noise
r = [0.35; 0.2; 0.15] # Toeplitz covariances
rho = 0.65 # AR covariance and CS covariance
#= Log Trend AR DGP =#
y, x = ar_dgp(n, t, b, ar, sig)
y2, x2 = ar_dgp(n2, t, b, ar, sig) # Out of Sample
yt = reshape(y, (t, length(y) ÷ t))
plot(yt, label=false, title="AR DGP", dpi=600)
savefig("AR.png")
#= y
x
y = y_test
x = x_test
n = length(y) ÷ t =#
ts = [repeat(Matrix(I, t, t)[:, i], outer=n) for i in 1:t]
t_fes = reduce(hcat, ts)
#= Model Statements =#
# BHM - Spectral
dy = y .- repeat(y[1:4:end], inner=t)
# x_sp = [repeat(1:t, outer=length(y) ÷ t) x[:,2]]
x_sp = [repeat(1:t, outer=length(y) ÷ t) x]
model_sp = bhm_onefreq(dy, x_sp)
cc = sample(model_sp, NUTS(0.65), M)
table(cc)[2]
bh_sp = mean.(eachcol(table(cc)[1][:, end-3:end]))
mean((dy .- (bh_sp[2] .* sin.(bh_sp[1] .* x_sp[:, 1]) .+ bh_sp[3] .* cos.(bh_sp[1] .* x_sp[:, 1]) .+ x_sp[:, 2] .* bh_sp[4])) .^ 2)
# In Sample RMSE = 5.498781
# beta = -0.738, p-val = 0.0, True = -0.5
dy2 = y2 .- repeat(y2[1:4:end], inner=t)
# x_sp2 = [repeat(1:t, outer=n2) x2[:, 2]]
x_sp2 = [repeat(1:t, outer=n2) x2]
mean((dy2 .- (bh_sp[2] .* sin.(bh_sp[1] .* x_sp2[:, 1]) .+ bh_sp[3] .* cos.(bh_sp[1] .* x_sp2[:, 1]) .+ x_sp2[:, 2] .* bh_sp[4])) .^ 2)
# Out Sample RMSE = 6.365449
# BHM - Log Trend
# x_lt = [repeat(log.(1:t), outer=n) x[:,2]]
x_lt = [repeat(log.(1:t), outer=n) x]
dy = y .- repeat(y[1:4:end], inner=t)
model_lt = bhm_logt(dy, x_lt)
cc = sample(model_lt, NUTS(0.65), M)
table(cc)[2]
mean((dy .- x_lt[:, 1] .* mean.(eachcol(table(cc)[1][:, end-size(x_lt, 2)+1:end]))[1] .+ x_lt[:, 1] .* x_lt[:, 2] .* mean.(eachcol(table(cc)[1][:, end-size(x_lt, 2)+1:end]))[2]) .^ 2)
# In Sample RMSE = 2.077773
# beta = 0.2, p-val = 0.008????, True = -0.5
# n2 = length(x2)÷4
# x_lt2 = [repeat(log.(1:t), outer=n2) x2[:, 2]]
x_lt2 = [repeat(log.(1:t), outer=n2) x2]
dy2 = y2 .- repeat(y2[1:4:end], inner=t)
mean((dy2 .- x_lt2[:, 1] .* mean.(eachcol(table(cc)[1][:, end-size(x_lt2, 2)+1:end]))[1] .+ x_lt2[:, 1] .* x_lt2[:, 2] .* mean.(eachcol(table(cc)[1][:, end-size(x_lt2, 2)+1:end]))[2]) .^ 2)
# Out Sample RMSE = 2.180812
# BHM - AR1
y
y_ar = y[Not(1:4:end)]
# x_ar = [y[Not(4:4:end)] x[Not(4:4:end),2]]
x_ar = [y[Not(4:4:end)] x[Not(4:4:end)]]
model_ar = bhm_ar1(y_ar, x_ar)
cc = sample(model_ar, NUTS(0.65), M)
table(cc)[2]
mean((y_ar .- x_ar[:, 1] .* mean.(eachcol(table(cc)[1][:, end-size(x_ar, 2)+1:end]))[1]) .^ 2)
# In Sample RMSE = 2.108816
# beta = -0.124, p-val = 0.167, True = -0.5
y_ar2 = y2[Not(1:4:end)]
# x_ar2 = [y2[Not(4:4:end)] x2[Not(4:4:end),2]]
x_ar2 = [y2[Not(4:4:end)] x2[Not(4:4:end)]]
mean((y_ar2 .- x_ar2 * mean.(eachcol(table(cc)[1][:, end-size(x_ar2, 2)+1:end]))) .^ 2)
# Out Sample RMSE = 2.341054
# MMRM - Time Effects CS
# X = [ones(n * t) x[:, 2] t_fes[:, 2:end]]
X = [ones(n * t) x t_fes[:, 2:end] x .* t_fes[:, 2:end]]
model_t_cs = mmrm_t_cs(y, X, t)
cc = sample(model_t_cs, NUTS(0.65), M)
table(cc)[2]
mean((y .- X * mean.(eachcol(table(cc)[1][:, end-size(X, 2)+1:end]))) .^ 2)
# In Sample RMSE = 2.283926
# beta = -0.368, p-val = 0.009, True = -0.5
# X2 = [ones(n2 * t) x2[:, 2] t_fes[1:n2*t, 2:end]]
X2 = [ones(n2 * t) x2 t_fes[1:n2*t, 2:end]]
mean((y2 .- X2 * mean.(eachcol(table(cc)[1][:, end-size(X2, 2)+1:end]))) .^ 2)
# Out Sample RMSE = 2.652892
# MMRM - Time Effects AR
# X = [ones(n * t) x[:, 2] t_fes[:, 2:end]]
X = [ones(n * t) x t_fes[:, 2:end]]
model_t_ar = mmrm_t_ar1(y, X, t)
cc = sample(model_t_ar, NUTS(0.65), M)
table(cc)[2]
mean((y .- X * mean.(eachcol(table(cc)[1][:, end-size(X, 2)+1:end]))) .^ 2)
# In Sample RMSE = 2.283669
# beta = -0.374, p-val = 0.005, True = -0.5
# X2 = [ones(n2 * t) x2[:, 2] t_fes[1:n2*t, 2:end]]
X2 = [ones(n2 * t) x2 t_fes[1:n2*t, 2:end]]
mean((y2 .- X2 * mean.(eachcol(table(cc)[1][:, end-size(X2, 2)+1:end]))) .^ 2)
# Out Sample RMSE = 2.653316
# MMRM - Log Trend CS
dy = y .- repeat(y[1:t:end], inner=t)
# x_l_cs = [repeat(log.(1:t), outer=n) x[:, 2]]
x_l_cs = [repeat(log.(1:t), outer=n) x]
model_l_cs = mmrm_l_cs(dy, x_l_cs, t)
cc = sample(model_l_cs, NUTS(0.65), M)
table(cc)[2]
mean((dy .- x_l_cs[:, 1] .* mean.(eachcol(table(cc)[1][:, end-size(x_l_cs, 2)+1:end]))[1] .+ x_l_cs[:, 1] .* x_l_cs[:, 2] .* mean.(eachcol(table(cc)[1][:, end-size(x_l_cs, 2)+1:end]))[2]) .^ 2)
# In Sample RMSE = 2.075241
# beta = 0.196, p-val = 0.015. True = -0.5
dy2 = y2 .- repeat(y2[1:t:end], inner=t)
# x_l_cs2 = [repeat(log.(1:t), outer=n2) x2[:, 2]]
x_l_cs2 = [repeat(log.(1:t), outer=n2) x2]
mean((dy2 .- x_l_cs2[:, 1] .* mean.(eachcol(table(cc)[1][:, end-size(x_l_cs2, 2)+1:end]))[1] .+ x_l_cs2[:, 1] .* x_l_cs2[:, 2] .* mean.(eachcol(table(cc)[1][:, end-size(x_l_cs2, 2)+1:end]))[2]) .^ 2)
# Out Sample RMSE = 2.162925
# MMRM - Log Trend AR1
dy = y .- repeat(y[1:t:end], inner=t)
# x_l_ar = [repeat(log.(1:t), outer=n) x[:,2]]
x_l_ar = [repeat(log.(1:t), outer=n) x]
model_l_ar = mmrm_l_ar1(dy, x_l_ar, t)
cc = sample(model_l_ar, NUTS(0.65), M)
table(cc)[2]
mean((dy .- x_l_ar[:, 1] .* mean.(eachcol(table(cc)[1][:, end-size(x_l_ar, 2)+1:end]))[1] .+ x_l_ar[:, 1] .* x_l_ar[:, 2] .* mean.(eachcol(table(cc)[1][:, end-size(x_l_ar, 2)+1:end]))[2]) .^ 2)
# In Sample RMSE = 2.076148
# beta = 0.197, p-val = 0.024. True = -0.5
dy2 = y2 .- repeat(y2[1:t:end], inner=t)
# x_l_ar2 = [repeat(log.(1:t), outer=n2) x2[:, 2]]
x_l_ar2 = [repeat(log.(1:t), outer=n2) x2]
mean((dy2 .- x_l_ar2[:, 1] .* mean.(eachcol(table(cc)[1][:, end-size(x_l_ar2, 2)+1:end]))[1] .+ x_l_ar2[:, 1] .* x_l_ar2[:, 2] .* mean.(eachcol(table(cc)[1][:, end-size(x_l_ar2, 2)+1:end]))[2]) .^ 2)
# In Sample RMSE = 2.160160
#### TORDIA Application
rest = DataFrame(CSV.File("combined_data_final_9_2020.csv"))
tads = DataFrame(CSV.File("tads_wtht.csv"))
torhtwt = DataFrame(CSV.File("Tordia htwt.csv"))
cdr = DataFrame(CSV.File("Tordia_Final.csv"))
tor_ht = torhtwt[:, 1]
tor_wt = torhtwt[:, 2]
replace!(tor_ht, 999 => missing)
replace!(tor_wt, 999 => missing)
torht = tor_ht .* 2.54
torwt = tor_wt .* 0.45
tordia = (rest[rest.cams.==0, :])[(rest[rest.cams.==0, :]).tads.==0, :]
tordia.ht = repeat(torht, inner=4)
tordia.wt = repeat(torwt, inner=4)
tordia.cdrs = cdr.cdrsrtot
@show(names(tads))
tads.weeks
@show(names(tordia))
tads.nonwh = ifelse.(tads.white .== 1, 0, 1)
tordia.ht
tads.sex = ifelse.(tads.gender .== "F", 1, 0)
tordia.age = floor.(tordia.agem ./ 12)
t1 = tads[:, [:weeks, :cgis, :sex, :age, :anx, :snri, :nonwh, :height_met, :weight_met, :tads_cbt]]
t2 = tordia[:, [:wk, :cgis, :sex, :age, :anx, :snri, :nonwh, :ht, :wt, :cbt]]
rename!(t1, [1 => :wk, 8 => :ht, 9 => :wt, 10 => :cbt])
t1.tordia = zeros(size(t1, 1))
t2.tordia = ones(size(t2, 1))
t1.tads = ones(size(t1, 1))
t2.tads = zeros(size(t2, 1))
tt2 = vcat(t2, t1)
tt2.lt = repeat(log.([1, 2, 3, 4]), outer=Int64(size(tt2, 1) / 4))
tt2.bmi = tt2.wt ./ (tt2.ht ./ 100) .^ 2
tt2[tt2.lt.==0, :cgis]
tt2.base = repeat(tt2[tt2.lt.==0, :cgis], inner=4)
tt = tt2[tt2.base.>2, :]
tt.baselt = tt.base .* tt.lt
tt.sexlt = tt.sex .* tt.lt
tt.anxlt = tt.anx .* tt.lt
tt.snrilt = tt.snri .* tt.lt
tt.ssrilt = (1 .- tt.snri) .* tt.lt
tt.nonwhlt = tt.nonwh .* tt.lt
tt.agelt = tt.age .* tt.lt
tt.bmilt = tt.bmi .* tt.lt
tt.nonwhlt = tt.nonwh .* tt.lt
tt.torlt = tt.tordia .* tt.lt
tt.tadslt = tt.tordia .* tt.lt
tt.resp = ifelse.(tt.cgis .<= 2, 1, 0)
tt.anxsnrilt = tt.anxlt .* tt.snri
tt.sexsnrilt = tt.sexlt .* tt.snri
tt.chngbase = tt.cgis .- tt.base
tt.cbtlt = tt.cbt .* tt.lt
names(tt)
tortd = tt[:, [:cgis, :lt, :nonwh]]
samp = sample(Random.seed!(12), 1:541, floor(Int, 541 * 0.6), replace=false, ordered=true)
train = (repeat(samp, inner=4) .* 4) .- repeat([3; 2; 1; 0], outer=324)
test = repeat(deleteat!(collect(1:541), samp), inner=4) .* 4 .- repeat([3; 2; 1; 0], outer=541 - 324)
y_test = tortd[train, :cgis]
x_test = tortd[train, :nonwh]
y2 = tortd[test, :cgis]
x2 = tortd[test, :nonwh]