-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFluco_Revise_210324.R
2417 lines (1727 loc) · 85.6 KB
/
Fluco_Revise_210324.R
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Call libraries ----------------------------------------------------------
library(styler)
library(tidyverse)
library(conflicted)
library(mice) #for 2l.pan imputation
library(pan) #for 2l.pan imputation
library(miceadds) #for 2l.pmm imputation
library(viridis) #for using viridis color palette
library(grid) #to put annotation
library(gridExtra) #to make plot grids
library(cowplot) #to make plot grids
library(pROC) #for making roc curves
library(readr) #part of tidyr #data wrangling
library(purrr) #to sample from a ecdf
library(xpose) #for making vpcs
library(xpose4) #for making vpcs #having issues with lattice package
library(vpc) #for making vpcs
library(PsNR) #for making vpcs
library(ggtext) #create latex axis labels - does not work
library(latex2exp) #to create latex label
library(table1) #create descriptive statistic table1
# Explanatory Data Analysis -----------------------------------------------
## Load the data ------------------------------------------------------------
# setwd("./Datasets/Original")
FlucTotIV_clean <- read.csv("./Datasets/Original/FlucTotIV_clean.csv", na.strings = "NA")
View(FlucTotIV_clean)
## Clean the dataset, make IHD from non CRRT to CRRT -------------------------
# When IHD == 1, converts CRRT == 1, otherwise remains
FlucTotIV_clean$CRRT[FlucTotIV_clean$IHD == 1] <- 1
# Convert AMT == 0 back into NA
FlucTotIV_clean$AMT[FlucTotIV_clean$AMT == 0] <- NA
# Convert DV == 0 back into NA
FlucTotIV_clean$DV[FlucTotIV_clean$DV == 0] <- NA
# Convert TAD and RATE == 0 back into NA
FlucTotIV_clean$TAD[FlucTotIV_clean$TAD == 0] <- NA
FlucTotIV_clean$RATE[FlucTotIV_clean$RATE == 0] <- NA
# Export into Fluco_clean_revised
write.csv(FlucTotIV_clean, "Fluco_clean_revised.csv", quote = F, row.names = F)
## Check the CRRT pattern -------------------------
# Filter every patient that has at least one observation with CRRT == 1
CRRT_one <- Fluco_clean_revised |>
group_by(ID) |>
dplyr::filter(any(CRRT == 1))
# 34 patients
# Check how many patients have CRRT == 1 all the time
CRRT_one |>
group_by(ID) |>
summarise(CRRT = all(CRRT == 1)) |>
summarise(n = sum(CRRT))
# 18 patients have CRRT == 1 all the time
# Add DAY column, which equals 1 if TIME <=24, 2 if 24 < TIME <= 48, and so on
CRRT_one <- CRRT_one |>
mutate(DAY = cut(TIME, breaks = seq(0, 648, 24), labels = 1:27))
# If TIME == 0, DAY == 1
CRRT_one$DAY[CRRT_one$TIME == 0] <- 1
# For those who have both CRRT == 1 and CRRT == 0, calculate the percentage of
# DAY that CRRT == 1 over all DAY
# First select ID that has CRRT == 1 and CRRT == 0
Miscel_CRRT <- CRRT_one |>
group_by(ID) |>
summarise(CRRT = n_distinct(CRRT)) |>
dplyr::filter(CRRT == 2) |>
select(ID)
# Percentage of days that CRRT == 1
CRRT_one |>
dplyr::filter(ID %in% Miscel_CRRT$ID) |>
group_by(ID) |>
group_by(DAY) |>
summarise(CRRT = mean(CRRT == 1, na.rm = T)) |>
summarise(mean = mean(CRRT, na.rm = T))
# Try another way, calculate the total number of days that CRRT == 1, then divided
# by the total number of days
CRRT_one |>
dplyr::filter(ID %in% Miscel_CRRT$ID) |>
group_by(DAY) |>
select(DAY, CRRT) |>
summarise(total_days = n(), CRRT_days = sum(CRRT == 1, na.rm = T)) |>
summarise(mean = sum(CRRT_days) / sum(total_days))
# Another way, extract the unique combination of ID, DAY and CRRT == 1,
# Sum up the number of days, then divided by the total number of days
# Total CRRT days
CRRT_one |>
dplyr::filter(ID %in% Miscel_CRRT$ID) |>
group_by(ID, DAY) |>
dplyr::filter(CRRT == 1) |>
unique() |>
ungroup() |>
group_by(ID) |>
select(ID, DAY) |>
summarise(CRRT_days = n()) |>
summarise(Total_CRRT_days = sum(CRRT_days))
# 199 CRRT days
# Total number of days
CRRT_one |>
dplyr::filter(ID %in% Miscel_CRRT$ID) |>
group_by(ID, DAY) |>
unique() |>
ungroup() |>
group_by(ID) |>
select(ID, DAY) |>
summarise(total_days = n()) |>
summarise(Total_days = sum(total_days))
# 340 total days
## Conclude about data for population simulation: I have 18 (10.2%) patients
## that have CRRT == 1 all the time, and 16 patients (9%) that have CRRT == 1 only
## for 199 days out of 340 days (58.5% of days). The rest of the patients
## have CRRT == 0.
## Check the availability of APACHE score ---------------------------------
### The percentage of missing APACHE originally ---------------------------
FlucTotIV_clean |>
select(APACHE) |>
summary()
FlucTotIV_clean |>
summarise(missing_percent = mean(is.na(APACHE)) * 100)
# 65.8%, when using the single imputation, which means 65.8% patients do not
# have the information of APACHE score.
### After removing duplicates in APACHE ----------------------------------------
FlucTotIV_clean |>
distinct(APACHE) |>
summarise(n = n())
# Only 34/3170 unique APACHE values, which is slightly more than 1%
# Thus, it's just imfeasible to include APACHE as a covariate
## Report BW at a patient level --------------------------------------------
# Extract data that each ID has only 1 observation
FlucTotIV_clean2 <- FlucTotIV_clean[!duplicated(FlucTotIV_clean$ID),]
# Now summarise BW with table1 at a patient level
table1(~ BW|HOSPITAL, data = FlucTotIV_clean2,render.continuous =
c(. = "Median [Q1-Q3]"))
# Multiple imputation - 70 models ------------------------------------------
## Create datasets for imputation ----------------------------------------------
### Some data manipulation ---------------------------------------------------
# Converting variables into appropriate types
FlucTotIV_clean$SEX <- as.factor(FlucTotIV_clean$SEX)
FlucTotIV_clean$HOSPITAL <- as.factor(FlucTotIV_clean$HOSPITAL)
FlucTotIV_clean$CRRT <- as.factor(FlucTotIV_clean$CRRT)
FlucTotIV_clean$OCC <- as.factor(FlucTotIV_clean$OCC)
FlucTotIV_clean$EVID <- as.factor(FlucTotIV_clean$EVID)
# Converting all the NAs in TAD, AMT, RATE to 0, and First row of DV to 0
FlucTotIV_clean$TAD[is.na(FlucTotIV_clean$TAD)] <- 0
FlucTotIV_clean$AMT[is.na(FlucTotIV_clean$AMT)] <- 0
FlucTotIV_clean$RATE[is.na(FlucTotIV_clean$RATE)] <- 0
FlucTotIV_clean$DV[FlucTotIV_clean$TIME == 0] <- 0
# Converting BW2 into BW
FlucTotIV_clean$BW <- FlucTotIV_clean$BW2
### Make crrt and non-crrt datasets ------------------------------------------
crrt_patients <- unique(FlucTotIV_clean[FlucTotIV_clean$CRRT == 1, "ID"])
FlucTotIV_clean_crrt <- FlucTotIV_clean |>
dplyr::filter(ID %in% crrt_patients)
FlucTotIV_clean_nocrrt <- FlucTotIV_clean |>
dplyr::filter(!ID %in% crrt_patients)
### Best imputation model for crrt and non-crrt datasets ---------------------
impute_CREAT_DOS_INT_crrt <- FlucTotIV_clean_crrt[, c("ID","TIME","RATE","AMT",
"DV","AGE","SEX","BW",
"LENGTH", "CRRT","OCC",
"HOSPITAL","CREAT_DOS_INT")]
impute_CREAT_DOS_INT_nocrrt <- FlucTotIV_clean_nocrrt[, c("ID","TIME","RATE","AMT",
"DV","AGE","SEX","BW",
"LENGTH","OCC","HOSPITAL",
"CREAT_DOS_INT")]
## Performing MI, 2l.pan ----------------------------------------------
# For crrt dataset
imp0 <- mice(impute_CREAT_DOS_INT_crrt, maxit = 0)
predmat <- imp0$predictorMatrix
predmat["CREAT_DOS_INT","ID"] <- -2
meth <- imp0$method
meth["CREAT_DOS_INT"] <- "2l.pan"
maxit <- 20
nimp <- 70
imputed_CREAT_DOS_INT_crrt <- mice::mice(data = impute_CREAT_DOS_INT_crrt,
method = meth, predictorMatrix = predmat,
maxit = maxit, m = nimp, printFlag = F,
seed = 123)
for (i in 1:70) {
FlucTotIV_clean_crrt[[paste0("CREAT", sprintf("%02d", i))]] <- (complete(imputed_CREAT_DOS_INT_crrt, action = i))$CREAT_DOS_INT
}
# For non-crrt dataset
imp0 <- mice(impute_CREAT_DOS_INT_nocrrt, maxit = 0)
predmat <- imp0$predictorMatrix
predmat["CREAT_DOS_INT", "ID"] <- -2
meth <- imp0$method
meth["CREAT_DOS_INT"] <- "2l.pan"
maxit <- 20
nimp <- 70
imputed_CREAT_DOS_INT_nocrrt <- mice::mice(data = impute_CREAT_DOS_INT_nocrrt,
method = meth,predictorMatrix = predmat,
maxit = maxit, m = nimp, printFlag = F,
seed = 123)
for (i in 1:70) {
FlucTotIV_clean_nocrrt[[paste0("CREAT", sprintf("%02d", i))]] <- (complete(imputed_CREAT_DOS_INT_nocrrt, action = i))$CREAT_DOS_INT
}
### BW and LENGTH treatment -------------------------------------------------
## Adding BW and LENGTH to crrt and non-crrt datasets
# Define a function to add variables to a dataset
add_variables <- function(dataset, imputed_data, var_name) {
for (i in 1:70) {
new_var_name <- paste0(var_name, sprintf("%02d", i))
dataset[[new_var_name]] <- complete(imputed_data, action = i)[[var_name]]
}
return(dataset)
}
# Apply the function to FlucTotIV_clean_crrt and FlucTotIV_clean_nocrrt
FlucTotIV_clean_crrt <- add_variables(FlucTotIV_clean_crrt, imputed_CREAT_DOS_INT_crrt, "BW")
FlucTotIV_clean_crrt <- add_variables(FlucTotIV_clean_crrt, imputed_CREAT_DOS_INT_crrt, "LENGTH")
FlucTotIV_clean_nocrrt <- add_variables(FlucTotIV_clean_nocrrt, imputed_CREAT_DOS_INT_nocrrt, "BW")
FlucTotIV_clean_nocrrt <- add_variables(FlucTotIV_clean_nocrrt, imputed_CREAT_DOS_INT_nocrrt, "LENGTH")
## Combine 2 datasets into 1 FlucTotIV_clean_imputed dataset
FlucTotIV_clean_imputed <- rbind(FlucTotIV_clean_crrt, FlucTotIV_clean_nocrrt)
FlucTotIV_clean_imputed <- FlucTotIV_clean_imputed[order(FlucTotIV_clean_imputed$ID,
FlucTotIV_clean_imputed$HOSPITAL,
FlucTotIV_clean_imputed$TIME), ]
## Now average BW and LENGTH per each ID
# Creating subset of hospital 3 and 4 patients
subset <- FlucTotIV_clean_imputed |>
dplyr::filter(HOSPITAL %in% c(3,4))
# Creating new variables for mean values
bw_mean_cols <- paste0("BW", sprintf("%02d", 1:70), "_mean")
length_mean_cols <- paste0("LENGTH", sprintf("%02d", 1:70), "_mean")
subset[, bw_mean_cols] <- NA
subset[, length_mean_cols] <- NA
# Filling mean values per ID
for (i in unique(subset$ID)) {
for (j in 1:70) {
bw_col <- paste0("BW", sprintf("%02d", 1:70))
length_col <- paste0("LENGTH", sprintf("%02d", 1:70))
bw_mean_col <- paste0("BW", sprintf("%02d", 1:70), "_mean")
length_mean_col <- paste0("LENGTH", sprintf("%02d", 1:70), "_mean")
subset[subset$ID == i, bw_mean_col[j]] <- mean(subset[subset$ID == i, ][[bw_col[j]]], na.rm = TRUE)
subset[subset$ID == i, length_mean_col[j]] <- mean(subset[subset$ID == i, ][[length_col[j]]], na.rm = TRUE)
}
}
# Integrating mean values to the original dataset
FlucTotIV_clean_imputed[, bw_mean_cols] <- NA
FlucTotIV_clean_imputed[, length_mean_cols] <- NA
FlucTotIV_clean_imputed[FlucTotIV_clean_imputed$ID %in% subset$ID, bw_mean_cols] <- subset[, bw_mean_cols]
FlucTotIV_clean_imputed[FlucTotIV_clean_imputed$ID %in% subset$ID, length_mean_cols] <- subset[, length_mean_cols]
# Replace values of 70 columns with mean values in hospital 3 and 4
for (j in 1:70) {
bw_col <- paste0("BW", sprintf("%02d", j))
length_col <- paste0("LENGTH", sprintf("%02d", j))
bw_mean_col <- paste0("BW", sprintf("%02d", j), "_mean")
length_mean_col <- paste0("LENGTH", sprintf("%02d", j), "_mean")
FlucTotIV_clean_imputed[FlucTotIV_clean_imputed$HOSPITAL %in% c(3, 4), bw_col] <- FlucTotIV_clean_imputed[FlucTotIV_clean_imputed$HOSPITAL %in% c(3, 4), bw_mean_col]
FlucTotIV_clean_imputed[FlucTotIV_clean_imputed$HOSPITAL %in% c(3, 4), length_col] <- FlucTotIV_clean_imputed[FlucTotIV_clean_imputed$HOSPITAL %in% c(3, 4), length_mean_col]
}
# Delete the 70 mean columns
bw_mean_cols <- paste0("BW", sprintf("%02d", 1:70), "_mean")
length_mean_cols <- paste0("LENGTH", sprintf("%02d", 1:70), "_mean")
FlucTotIV_clean_imputed <- FlucTotIV_clean_imputed |> select(-one_of(bw_mean_cols), -one_of(length_mean_cols))
### CREAT treatment ---------------------------------------------------------
# Create a subset of rows with missing CREAT_DOS_INT
subset <- FlucTotIV_clean_imputed |>
dplyr::filter(is.na(CREAT_DOS_INT))
# Calculate the mean values per ID and OCC
mean_cols <- paste0("CREAT", sprintf("%02d", 1:70))
subset <- subset |>
group_by(ID, OCC) |>
mutate(across(mean_cols, mean, na.rm = TRUE)) |>
ungroup()
# Replace the mean across ID and OCC in the actual dataset
FlucTotIV_clean_imputed[is.na(FlucTotIV_clean_imputed$CREAT_DOS_INT), mean_cols] <- subset[, mean_cols]
# Fixing all the negative imputed values to the minimum observed value
FlucTotIV_clean_imputed <- FlucTotIV_clean_imputed |>
mutate_at(vars(CREAT01:CREAT70), ~ ifelse(. < 0, min(CREAT,na.rm=TRUE), .))
### MI diagnostics ---------------------------------------------------------
## Make a violin plot
# Select the 11 columns by specifying their names explicitly
CREAT_columns <- FlucTotIV_clean_imputed |>
select(CREAT_DOS_INT, CREAT01, CREAT02, CREAT03, CREAT04, CREAT05, CREAT06, CREAT07, CREAT08, CREAT09, CREAT10)
# Convert from wide to long
CREAT_long_data <- CREAT_columns |>
pivot_longer(cols = everything(),
names_to = "Imputation",
values_to = "Value")
# Rename the levels of the "Imputation" column
CREAT_long_data$Imputation <- factor(CREAT_long_data$Imputation,
levels = c("CREAT_DOS_INT", "CREAT01",
"CREAT02", "CREAT03", "CREAT04",
"CREAT05", "CREAT06", "CREAT07",
"CREAT08", "CREAT09", "CREAT10"))
# Remove NA from CREAT_long_data
CREAT_long_data <- na.omit(CREAT_long_data)
# Define a custom color palette
#custom_palette <- c("skyblue", "lightgreen", "pink", "orange", "purple",
#"lightblue", "salmon", "gold", "lightpink", "lightgray", "lightcyan")
# Create a violin plot with custom aesthetics
violin_plot<- ggplot(data = CREAT_long_data, aes(x = Imputation, y = Value, fill = Imputation)) +
geom_violin(trim = TRUE) +
labs(title = "Distribution of observed and 10 imputed serum creatinine",
x = NULL,
y = "Serum Creatinine (mg/dl)") +
scale_x_discrete(labels = c("Observed", "Imputation 01", "Imputation 02",
"Imputation 03", "Imputation 04", "Imputation 05",
"Imputation 06", "Imputation 07", "Imputation 08",
"Imputation 09", "Imputation 10")) +
scale_fill_viridis_d() +
theme_bw() +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5, color = "black", size = 16, face = "bold"),
axis.text.x = element_text(color = "black", size = 12),
axis.text.y = element_text(color = "black", size = 12),
axis.title.y = element_text(color = "black", size = 14, face = "bold"))
# Exporting the plot
# setwd("C:/Users/u0164053/OneDrive - KU Leuven/Fluconazole PoPPK/Fluconazol_project/Revision 210324/Plots/MI_diagnostics")
ggsave("./Plots/MI_diagnostics/obs_vs_imp_violin.png", plot = violin_plot, dpi = 300, width = 13, height = 7)
## Make a density plot
# Define the title and label options
title_options <- list(
label = "Density plot of 10 imputed SCr vs the original SCr",
size = 14,
color = "black",
hjust = 0.5,
vjust = 1,
face = "bold"
)
x_label_options <- list(
label = "SCr (mg/dl)",
size = 12,
color = "black",
hjust = 0.5,
vjust = 0
)
y_label_options <- list(
label = "Density",
size = 12,
color = "black",
hjust = 0,
vjust = 0.5,
angle = 0
)
axis_text_options <- list(
size = 10
)
legend_options <- list(
title = "Variables",
size = 8
)
# Density plot first 10 imputations
density_plot<-ggplot(FlucTotIV_clean_imputed, aes(x = CREAT01)) +
geom_density(aes(fill = "CREAT01"), alpha = 0.7) +
geom_density(aes(x = CREAT02, fill = "CREAT02"), alpha = 0.7) +
geom_density(aes(x = CREAT03, fill = "CREAT03"), alpha = 0.7) +
geom_density(aes(x = CREAT04, fill = "CREAT04"), alpha = 0.7) +
geom_density(aes(x = CREAT05, fill = "CREAT05"), alpha = 0.7) +
geom_density(aes(x = CREAT06, fill = "CREAT06"), alpha = 0.7) +
geom_density(aes(x = CREAT07, fill = "CREAT07"), alpha = 0.7) +
geom_density(aes(x = CREAT08, fill = "CREAT08"), alpha = 0.7) +
geom_density(aes(x = CREAT09, fill = "CREAT09"), alpha = 0.7) +
geom_density(aes(x = CREAT10, fill = "CREAT10"), alpha = 0.7) +
geom_density(aes(x = CREAT_DOS_INT, fill = "CREAT"), alpha = 0.7) +
theme_bw() +
scale_fill_viridis_d(
name = "Variables",
option = "A"
) +
labs(title = title_options,
x = x_label_options,
y = y_label_options) +
theme(
plot.title = element_text(hjust = 0.5),
axis.text = element_text(axis_text_options),
legend.position = "right"
)
# Exporting the plot
# setwd("C:/Users/u0164053/OneDrive - KU Leuven/Fluconazole PoPPK/Fluconazol_project/Revision 210324/Plots/MI_diagnostics")
ggsave("./Plots/MI_diagnostics/density_plot.png", plot = density_plot, dpi = 300, width = 13, height = 7)
### Calculating the CKDEPI from CREAT -------------------------------
Fluc_NONMEM_mul_impute <- FlucTotIV_clean_imputed
# Define a function to calculate CKDEPI for each CKDEPI column
calculate_CKDEPI <- function(data, creatinine_column, sex_column, age_column, multiplier) {
CKDEPI <- ifelse(data[[sex_column]] == 1 & data[[creatinine_column]] < 0.9,
multiplier * ((data[[creatinine_column]]/0.9)^-0.302) * (0.9938^data[[age_column]]),
ifelse(data[[sex_column]] == 1 & data[[creatinine_column]] >= 0.9,
multiplier * ((data[[creatinine_column]]/0.9)^-1.200) * (0.9938^data[[age_column]]),
ifelse(data[[sex_column]] == 2 & data[[creatinine_column]] < 0.7,
multiplier * ((data[[creatinine_column]]/0.7)^-0.241) * (0.9938^data[[age_column]]) * 1.012,
multiplier * ((data[[creatinine_column]]/0.7)^-1.200) * (0.9938^data[[age_column]]) * 1.012)
)
)
return(CKDEPI)
}
# Calculate CKDEPI01 to CKDEPI70
for (i in 1:70) {
CKDEPI_column <- paste0("CKDEPI", sprintf("%02d", i))
Fluc_NONMEM_mul_impute[[CKDEPI_column]] <- calculate_CKDEPI(Fluc_NONMEM_mul_impute,
paste0("CREAT", sprintf("%02d", i)),
"SEX", "AGE", 142)
}
# View the resulting dataset
View(Fluc_NONMEM_mul_impute)
## Treatment after MI ----------------------------------------------
# Converting all NAs to -99
Fluc_NONMEM_mul_impute[is.na(Fluc_NONMEM_mul_impute)] <- -99
# Convert DV from 0 & -99 to "."
Fluc_NONMEM_mul_impute$DV <- ifelse(Fluc_NONMEM_mul_impute$DV %in% c(0, -99), ".", Fluc_NONMEM_mul_impute$DV)
# AMT TAD and RATE from 0 to "."
Fluc_NONMEM_mul_impute$TAD <- ifelse(Fluc_NONMEM_mul_impute$TAD == 0, ".", Fluc_NONMEM_mul_impute$TAD)
Fluc_NONMEM_mul_impute$AMT <- ifelse(Fluc_NONMEM_mul_impute$AMT == 0, ".", Fluc_NONMEM_mul_impute$AMT)
Fluc_NONMEM_mul_impute$RATE <- ifelse(Fluc_NONMEM_mul_impute$RATE == 0, ".", Fluc_NONMEM_mul_impute$RATE)
### Export into different datasets for modelling -------------------
# Remove variables that are not needed
FlucTotIV_clean_imputed <- Fluc_NONMEM_mul_impute[, !names(Fluc_NONMEM_mul_impute)
%in% c("CMT", "APACHE", "RACE",
"CL24", "GGT", "AFT", "ALT",
"AST", "BILI", "ALB", "SOFA",
"IHD", "UF", "ECMO", "OCC3",
"ARCCKD", "ARC24", "ARCAlg",
"BMI", "BSA", "dup","FLUID",
paste0("CREAT", sprintf("%02d", 1:70)),
paste0("LENGTH", sprintf("%02d", 1:70)),
"CREAT_DOS_INT","unique_CREAT",
"CREAT3","CRRT2","dosing_interval","Creat_imputed",
"OCC", "TAD", "SS","II",
"ADMIN", "HOSPITAL", "LENGTH",
"AGE", "SEX", "CREAT",
"CKDEPI", "BW", "CKDEPI",
"BW2", "CREAT2")]
# Then, make Fluco_revised_imputed_01 dataset, which is FlucTotIV_clean_imputed
# removing BW19 to BW70 and CKDEPI19 to CKDEPI70
Fluco_revised_imputed_01 <- FlucTotIV_clean_imputed[, !names(FlucTotIV_clean_imputed) %in%
c(paste0("BW", sprintf("%02d", 19:70)),
paste0("CKDEPI", sprintf("%02d", 19:70)))]
# Next, make Fluco_revised_imputed_02 dataset, which is FlucTotIV_clean_imputed
# removing BW01 to BW18 and BW37 to BW70; similarly for CKDEPI
Fluco_revised_imputed_02 <- FlucTotIV_clean_imputed[, !names(FlucTotIV_clean_imputed) %in%
c(paste0("BW", sprintf("%02d", c(1:18, 37:70))),
paste0("CKDEPI", sprintf("%02d", c(1:18, 37:70))))]
# Next, make Fluco_revised_imputed_03 dataset, which is FlucTotIV_clean_imputed
# removing BW01 to BW36 and BW55 to BW70; similarly for CKDEPI
Fluco_revised_imputed_03 <- FlucTotIV_clean_imputed[, !names(FlucTotIV_clean_imputed) %in%
c(paste0("BW", sprintf("%02d", c(1:36, 55:70))),
paste0("CKDEPI", sprintf("%02d", c(1:36, 55:70))))]
# Finally, make Fluco_revised_imputed_04 dataset, which is FlucTotIV_clean_imputed
# removing BW01 to BW54 and CKDEPI01 to CKDEPI54
Fluco_revised_imputed_04 <- FlucTotIV_clean_imputed[, !names(FlucTotIV_clean_imputed) %in%
c(paste0("BW", sprintf("%02d", 01:54)),
paste0("CKDEPI", sprintf("%02d", 01:54)))]
# Export the datasets
setwd("./Datasets/Imputed")
write.csv(Fluco_revised_imputed_01, "Fluco_revised_imputed_01.csv", quote = F, row.names = F)
write.csv(Fluco_revised_imputed_02, "Fluco_revised_imputed_02.csv", quote = F, row.names = F)
write.csv(Fluco_revised_imputed_03, "Fluco_revised_imputed_03.csv", quote = F, row.names = F)
write.csv(Fluco_revised_imputed_04, "Fluco_revised_imputed_04.csv", quote = F, row.names = F)
# Return the working directory to the original
Path <- getwd()
setwd(dirname(dirname(Path)))
### Generate the overall dataset for making GOF plots and VPC -----------------
# Generate two new columns in Fluc_NONMEM_mul_impute, BW_ave and CKDEPI_ave
# BW_ave is the average of BW01 to BW70, and similarly for CKDEPI_ave
Fluc_NONMEM_mul_impute$BW_ave <- rowMeans(Fluc_NONMEM_mul_impute[,paste0("BW", sprintf("%02d", 1:70))], na.rm = TRUE)
Fluc_NONMEM_mul_impute$CKDEPI_ave <- rowMeans(Fluc_NONMEM_mul_impute[,paste0("CKDEPI", sprintf("%02d", 1:70))], na.rm = TRUE)
# Then make Fluco_revised_imputed_overall dataset, which should have TAD & HOSPITAL
# to make GOF plots
Fluco_revised_imputed_overall <- Fluc_NONMEM_mul_impute[, !names(Fluc_NONMEM_mul_impute) %in%
c("CMT", "APACHE", "RACE",
"CL24", "GGT", "AFT", "ALT",
"AST", "BILI", "ALB", "SOFA",
"IHD", "UF", "ECMO", "OCC3",
"ARCCKD", "ARC24", "ARCAlg",
"BMI", "BSA", "dup","FLUID",
paste0("CREAT", sprintf("%02d", 1:70)),
paste0("LENGTH", sprintf("%02d", 1:70)),
paste0("BW", sprintf("%02d", 1:70)),
paste0("CKDEPI", sprintf("%02d", 1:70)),
"CREAT_DOS_INT","unique_CREAT",
"CREAT3","CRRT2","dosing_interval","Creat_imputed",
"OCC", "SS","II",
"ADMIN", "LENGTH",
"AGE", "SEX", "CREAT",
"CKDEPI", "BW", "CKDEPI",
"BW2", "CREAT2")]
# Export the dataset
setwd("./Datasets/Imputed")
write.csv(Fluco_revised_imputed_overall, "Fluco_revised_imputed_overall.csv", quote = F, row.names = F)
# Return the working directory to the original
Path <- getwd()
setwd(dirname(dirname(Path)))
## Pooling model parameters ----------------------------------------------
# Load the dataset
# setwd("./Datasets/Pooled_MI")
Pooling_Parameters_2l.pan <- read.csv("./Datasets/Pooled_MI/Pooling_Parameters_2l.pan_220324.csv", sep = ",")
# Creating Var (Variance) column
Pooling_Parameters_2l.pan$Var <- (Pooling_Parameters_2l.pan$RSE*Pooling_Parameters_2l.pan$Estimates/100)^2
### First of all, CKDEPI -----------------
# Mean estimate
CKDEPI <- mean(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "CKDEPI"]) #0.5316571
W_CKDEPI <- mean(Pooling_Parameters_2l.pan$Var[Pooling_Parameters_2l.pan$Parameters == "CKDEPI"]) #0.03150853 #within imputation variance
B_CKDEPI <- var(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "CKDEPI"]) #0.00929304 #between imputation variance
CKDEPI
W_CKDEPI
B_CKDEPI
# Variance estimate
m <- 70
T_CKDEPI <- W_CKDEPI + (1 + 1/m) * B_CKDEPI #0.04093433
T_CKDEPI
# Confidence interval - T distribution
m <- 70 #number of imputations
k <- 12 #my model has 12 parameters (including 6 fixed effects parameters and 6 random effect parameters where covariance is one of them)
n <- 177
lambda_CKDEPI <- (B_CKDEPI + B_CKDEPI/m)/T_CKDEPI #proportion of variation attributable to the missing data
r_CKDEPI <- (lambda_CKDEPI)/(1 - lambda_CKDEPI) #relative increase in variance due to nonresponse
v_old_CKDEPI <- (m - 1) * (1 + 1/r_CKDEPI^2) #old degree of freedom
v_com = n - k #degree of freedom of parameter estimate (CKDEPI) in the hypothetically complete data
v_obs_CKDEPI = ((v_com + 1)/(v_com + 3)) * v_com * (1 - lambda_CKDEPI) #observed data degrees of freedom that accounts for the missing information
v_CKDEPI <- (v_old_CKDEPI*v_obs_CKDEPI)/(v_old_CKDEPI + v_obs_CKDEPI)
t_crit_CKDEPI <- qt(0.975, v_CKDEPI)
lower_bound_CKDEPI <- CKDEPI - t_crit_CKDEPI*sqrt(T_CKDEPI) #0.1306684
upper_bound_CKDEPI <- CKDEPI + t_crit_CKDEPI*sqrt(T_CKDEPI) #0.9326459
lower_bound_CKDEPI
upper_bound_CKDEPI
### Second of all, CLcrrt -----------------
# Mean estimate
CLcrrt <- mean(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "CLcrrt"]) #1.561
W_CLcrrt <- mean(Pooling_Parameters_2l.pan$Var[Pooling_Parameters_2l.pan$Parameters == "CLcrrt"]) #0.02459816 #within imputation variance
B_CLcrrt <- var(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "CLcrrt"]) #2.362319e-05 #between imputation variance
CLcrrt
W_CLcrrt
B_CLcrrt
# Variance estimate
m <- 70
T_CLcrrt <- W_CLcrrt + (1 + 1/m)*B_CLcrrt #0.02462213
T_CLcrrt
# Confidence interval - T distribution
m <- 70 #number of imputations
k <- 12 #my model has 12 parameters (including 6 fixed effects parameters and 6 random effect parameters where covariance is one of them)
n <- 177
lambda_CLcrrt <- (B_CLcrrt + B_CLcrrt/m)/T_CLcrrt #proportion of variation attributable to the missing data
r_CLcrrt <- (lambda_CLcrrt)/(1 - lambda_CLcrrt) #relative increase in variance due to nonresponse
v_old_CLcrrt <- (m - 1)*(1 + 1/r_CLcrrt^2) #old degree of freedom
v_com = n - k #degree of freedom of parameter estimate (CLcrrt) in the hypothetically complete data
v_obs_CLcrrt = ((v_com + 1)/(v_com + 3))*v_com*(1 - lambda_CLcrrt) #observed data degrees of freedom that accounts for the missing information
v_CLcrrt <- (v_old_CLcrrt*v_obs_CLcrrt)/(v_old_CLcrrt + v_obs_CLcrrt)
t_crit_CLcrrt <- qt(0.975, v_CLcrrt)
lower_bound_CLcrrt <- CLcrrt - t_crit_CLcrrt*sqrt(T_CLcrrt) #1.251151
upper_bound_CLcrrt <- CLcrrt + t_crit_CLcrrt*sqrt(T_CLcrrt) #1.870849
lower_bound_CLcrrt
upper_bound_CLcrrt
### Third of all, CLr -----------------
# Mean estimate
CLr <- mean(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "CLr"]) #0.6137429
W_CLr <- mean(Pooling_Parameters_2l.pan$Var[Pooling_Parameters_2l.pan$Parameters == "CLr"]) #0.001016242 #within imputation variance
B_CLr <- var(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "CLr"]) #8.886046e-05
#between imputation variance
CLr
W_CLr
B_CLr
# Variance estimate
m <- 70
T_CLr <- W_CLr + (1 + 1/m)*B_CLr #0.001106372
T_CLr
# Confidence interval - T distribution
m <- 70 #number of imputations
k <- 12 #my model has 12 parameters (including 6 fixed effects parameters and 6 random effect parameters where covariance is one of them)
n <- 177
lambda_CLr <- (B_CLr + B_CLr/m)/T_CLr #proportion of variation attributable to the missing data
r_CLr <- (lambda_CLr)/(1 - lambda_CLr) #relative increase in variance due to nonresponse
v_old_CLr <- (m - 1)*(1 + 1/r_CLr^2) #old degree of freedom
v_com = n - k #degree of freedom of parameter estimate (CLr) in the hypothetically complete data
v_obs_CLr = ((v_com + 1)/(v_com + 3))*v_com*(1 - lambda_CLr) #observed data degrees of freedom that accounts for the missing information
v_CLr <- (v_old_CLr*v_obs_CLr)/(v_old_CLr + v_obs_CLr)
t_crit_CLr <- qt(0.975, v_CLr)
lower_bound_CLr <- CLr - t_crit_CLr*sqrt(T_CLr) #0.54801
upper_bound_CLr <- CLr + t_crit_CLr*sqrt(T_CLr) #0.6794757
lower_bound_CLr
upper_bound_CLr
### Next, V1 -----------------
# Mean estimate
V1 <- mean(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "V1"]) #39.39571
W_V1 <- mean(Pooling_Parameters_2l.pan$Var[Pooling_Parameters_2l.pan$Parameters == "V1"]) #10.21739 #within imputation variance
B_V1 <- var(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "V1"]) #0.09345963
#between imputation variance
V1
W_V1
B_V1
# Variance estimate
m <- 70
T_V1 <- W_V1 + (1 + 1/m)*B_V1 #10.31219
T_V1
# Confidence interval - T distribution
m <- 70 #number of imputations
k <- 12 #my model has 12 parameters (including 6 fixed effects parameters and 6 random effect parameters where covariance is one of them)
n <- 177
lambda_V1 <- (B_V1 + B_V1/m)/T_V1 #proportion of variation attributable to the missing data
r_V1 <- (lambda_V1)/(1 - lambda_V1) #relative increase in variance due to nonresponse
v_old_V1 <- (m - 1)*(1 + 1/r_V1^2) #old degree of freedom
v_com = n - k #degree of freedom of parameter estimate (V1) in the hypothetically complete data
v_obs_V1 = ((v_com + 1)/(v_com + 3))*v_com*(1 - lambda_V1) #observed data degrees of freedom that accounts for the missing information
v_V1 <- (v_old_V1*v_obs_V1)/(v_old_V1 + v_obs_V1)
t_crit_V1 <- qt(0.975, v_V1)
lower_bound_V1 <- V1 - t_crit_V1*sqrt(T_V1) #33.05424
upper_bound_V1 <- V1 + t_crit_V1*sqrt(T_V1) #45.73719
lower_bound_V1
upper_bound_V1
### After that, Q -----------------
# Mean estimate
Q <- mean(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "Q"]) #12.11286
W_Q <- mean(Pooling_Parameters_2l.pan$Var[Pooling_Parameters_2l.pan$Parameters == "Q"]) #18.14542 #within imputation variance
B_Q <- var(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "Q"]) #0.03620911
#between imputation variance
Q
W_Q
B_Q
# Variance estimate
m <- 70
T_Q <- W_Q + (1 + 1/m)*B_Q #18.18215
T_Q
# Confidence interval - T distribution
m <- 70 #number of imputations
k <- 12 #my model has 12 parameters (including 6 fixed effects parameters and 6 random effect parameters where covariance is one of them)
n <- 177
lambda_Q <- (B_Q + B_Q/m)/T_Q #proportion of variation attributable to the missing data
r_Q <- (lambda_Q)/(1 - lambda_Q) #relative increase in variance due to nonresponse
v_old_Q <- (m - 1)*(1 + 1/r_Q^2) #old degree of freedom
v_com = n - k #degree of freedom of parameter estimate (Q) in the hypothetically complete data
v_obs_Q = ((v_com + 1)/(v_com + 3))*v_com*(1 - lambda_Q) #observed data degrees of freedom that accounts for the missing information
v_Q <- (v_old_Q * v_obs_Q)/(v_old_Q + v_obs_Q)
t_crit_Q <- qt(0.975, v_Q)
lower_bound_Q <- Q - t_crit_Q * sqrt(T_Q) #3.692838
upper_bound_Q <- Q + t_crit_Q * sqrt(T_Q) #20.53288
lower_bound_Q
upper_bound_Q
### Next, V2 -----------------
# Mean estimate
V2 <- mean(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "V2"]) #8.465714
W_V2 <- mean(Pooling_Parameters_2l.pan$Var[Pooling_Parameters_2l.pan$Parameters == "V2"]) #8.436197 #within imputation variance
B_V2 <- var(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "V2"]) #0.006737888
#between imputation variance
V2
W_V2
B_V2
# Variance estimate
m <- 70
T_V2 <- W_V2 + (1 + 1/m)*B_V2 #8.443031
T_V2
# Confidence interval - T distribution
m <- 70 #number of imputations
k <- 12 #my model has 12 parameters (including 6 fixed effects parameters and 6 random effect parameters where covariance is one of them)
n <- 177
lambda_V2 <- (B_V2 + B_V2/m)/T_V2 #proportion of variation attributable to the missing data
r_V2 <- (lambda_V2)/(1 - lambda_V2) #relative increase in variance due to nonresponse
v_old_V2 <- (m - 1)*(1 + 1/r_V2^2) #old degree of freedom
v_com = n - k #degree of freedom of parameter estimate (V2) in the hypothetically complete data
v_obs_V2 = ((v_com + 1)/(v_com + 3))*v_com*(1 - lambda_V2) #observed data degrees of freedom that accounts for the missing information
v_V2 <- (v_old_V2*v_obs_V2)/(v_old_V2 + v_obs_V2)
t_crit_V2 <- qt(0.975, v_V2)
lower_bound_V2 <- V2 - t_crit_V2*sqrt(T_V2) #2.728043
upper_bound_V2 <- V2 + t_crit_V2*sqrt(T_V2) #14.20339
lower_bound_V2
upper_bound_V2
### Next, BW -----------------
# Mean estimate
BW <- mean(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "BW"]) #0.9076
W_BW <- mean(Pooling_Parameters_2l.pan$Var[Pooling_Parameters_2l.pan$Parameters == "BW"]) #0.04289933 #within imputation variance
B_BW <- var(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "BW"]) #0.0001656638
#between imputation variance
BW
W_BW
B_BW
# Variance estimate
m <- 70
T_BW <- W_BW + (1 + 1/m)*B_BW #0.04306736
T_BW
# Confidence interval - T distribution
m <- 70 #number of imputations
k <- 12 #my model has 12 parameters (including 6 fixed effects parameters and 6 random effect parameters where covariance is one of them)
n <- 177
lambda_BW <- (B_BW + B_BW/m)/T_BW #proportion of variation attributable to the missing data
r_BW <- (lambda_BW)/(1 - lambda_BW) #relative increase in variance due to nonresponse
v_old_BW <- (m - 1)*(1 + 1/r_BW^2) #old degree of freedom
v_com = n - k #degree of freedom of parameter estimate (BW) in the hypothetically complete data
v_obs_BW = ((v_com + 1)/(v_com + 3))*v_com*(1 - lambda_BW) #observed data degrees of freedom that accounts for the missing information
v_BW <- (v_old_BW * v_obs_BW)/(v_old_BW + v_obs_BW)
t_crit_BW <- qt(0.975, v_BW)
lower_bound_BW <- BW - t_crit_BW*sqrt(T_BW) #0.4978011
upper_bound_BW <- BW + t_crit_BW*sqrt(T_BW) #1.317399
lower_bound_BW
upper_bound_BW
### Next, Cov_CLr_V1 -----------------
# First of all, replacing the Var with the values in RSE column
Pooling_Parameters_2l.pan <- Pooling_Parameters_2l.pan |>
mutate(Var = ifelse(Parameters == "Cov_CLr_V1", RSE^2, Var))
# Second of all, replacing the values of the RSE column
Pooling_Parameters_2l.pan <- Pooling_Parameters_2l.pan |>
mutate(RSE = ifelse(Parameters == "Cov_CLr_V1", (RSE/Estimates)*100, RSE))
# Mean estimate
Cov_CLr_V1 <- mean(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "Cov_CLr_V1"]) #0.06732857
W_Cov_CLr_V1 <- mean(Pooling_Parameters_2l.pan$Var[Pooling_Parameters_2l.pan$Parameters == "Cov_CLr_V1"]) #0.001466198 #within imputation variance
B_Cov_CLr_V1 <- var(Pooling_Parameters_2l.pan$Estimates[Pooling_Parameters_2l.pan$Parameters == "Cov_CLr_V1"]) #1.70554e-05
#between imputation variance
Cov_CLr_V1
W_Cov_CLr_V1
B_Cov_CLr_V1
# Variance estimate
m <- 70
T_Cov_CLr_V1 <- W_Cov_CLr_V1 + (1 + 1/m)*B_Cov_CLr_V1 #0.001483497
T_Cov_CLr_V1
# Confidence interval - T distribution
m <- 70 #number of imputations
k <- 12 #my model has 12 parameters (including 6 fixed effects parameters and 6 random effect parameters where covariance is one of them)
n <- 177
lambda_Cov_CLr_V1 <- (B_Cov_CLr_V1 + B_Cov_CLr_V1/m)/T_Cov_CLr_V1 #proportion of variation attributable to the missing data
r_Cov_CLr_V1 <- (lambda_Cov_CLr_V1)/(1 - lambda_Cov_CLr_V1) #relative increase in variance due to nonresponse
v_old_Cov_CLr_V1 <- (m - 1)*(1 + 1/r_Cov_CLr_V1^2) #old degree of freedom
v_com = n - k #degree of freedom of parameter estimate (Cov_CLr_V1) in the hypothetically complete data
v_obs_Cov_CLr_V1 = ((v_com + 1)/(v_com + 3))*v_com*(1 - lambda_Cov_CLr_V1) #observed data degrees of freedom that accounts for the missing information
v_Cov_CLr_V1 <- (v_old_Cov_CLr_V1*v_obs_Cov_CLr_V1)/(v_old_Cov_CLr_V1 + v_obs_Cov_CLr_V1)
t_crit_Cov_CLr_V1 <- qt(0.975, v_Cov_CLr_V1)
lower_bound_Cov_CLr_V1 <- Cov_CLr_V1 - t_crit_Cov_CLr_V1*sqrt(T_Cov_CLr_V1) #-0.008733233
upper_bound_Cov_CLr_V1 <- Cov_CLr_V1 + t_crit_Cov_CLr_V1*sqrt(T_Cov_CLr_V1) #0.1433904
lower_bound_Cov_CLr_V1
upper_bound_Cov_CLr_V1
### Do transformation for log terms -----------------
# create a log-transformed Log column
Pooling_Parameters_2l.pan$Log <- log(Pooling_Parameters_2l.pan$Estimates)
# Variance of the log-transformed variable
Pooling_Parameters_2l.pan$Var_Log <- (1/(Pooling_Parameters_2l.pan$Estimates))^2*Pooling_Parameters_2l.pan$Var
#Var(log(x))=[1/(mean(x))]^2*var(x) (log(x) in R is actually ln(x))
### IIV_CLcrrt -----------------
# Mean estimate
IIV_CLcrrt_log <- mean(Pooling_Parameters_2l.pan$Log[Pooling_Parameters_2l.pan$Parameters == "IIV_CLcrrt"]) #-1.264295
W_IIV_CLcrrt_log <- mean(Pooling_Parameters_2l.pan$Var_Log[Pooling_Parameters_2l.pan$Parameters == "IIV_CLcrrt"]) #0.1782487 #within imputation variance
B_IIV_CLcrrt_log <- var(Pooling_Parameters_2l.pan$Log[Pooling_Parameters_2l.pan$Parameters == "IIV_CLcrrt"]) #0.0001359124 #between imputation variance
IIV_CLcrrt_log
W_IIV_CLcrrt_log
B_IIV_CLcrrt_log
# Variance estimate
m <- 70
T_IIV_CLcrrt_log <- W_IIV_CLcrrt_log + (1 + 1/m)*B_IIV_CLcrrt_log #0.1783865
T_IIV_CLcrrt_log
# Confidence interval - T distribution