Commit 3c1955ba authored by Kristien Wouters's avatar Kristien Wouters

generalisation for 3/6/12 month imputation

parent 69324e04
rmarkdown::render(
input = 'imputation-report.Rmd',
params = list(imputation_time=90, per_protocol_window=c(77,126)),
output_file = 'Imputation_GOSE_3month.html')
rmarkdown::render(
input = 'imputation-report.Rmd',
params = list(imputation_time=180.0, per_protocol_window=c(150,240)),
output_file = 'Imputation_GOSE_6month.html')
rmarkdown::render(
input = 'imputation-report.Rmd',
params = list(imputation_time=360.0, per_protocol_window=c(300,420)),
output_file = 'Imputation_GOSE_12month.html')
--- ---
title: "Impute 6-months GOSe for CENTER-TBI" title: "`r sprintf('Impute %i month GOSe for CENTER-TBI',params$imputation_time/30)`"
date: "`r Sys.time()`" date: "`r Sys.time()`"
author: "Kevin Kunzmann (kevin.kunzmann@mrc-bsu.cam.ac.uk)" author:
- Kevin Kunzmann (kevin.kunzmann@mrc-bsu.cam.ac.uk)
- Kristien Wouters (kristien.wouters@uza.be)
output: output:
html_document: html_document:
...@@ -12,6 +14,7 @@ output: ...@@ -12,6 +14,7 @@ output:
params: params:
seed: 42 seed: 42
age_range: !r c(0, Inf) age_range: !r c(0, Inf)
imputation_time: 180.0
per_protocol_window: !r c(150.0, 240) per_protocol_window: !r c(150.0, 240)
--- ---
...@@ -84,9 +87,9 @@ tbl_combined <- readr::read_csv("tbl_gose.csv", col_types = "cinncD") %>% ...@@ -84,9 +87,9 @@ tbl_combined <- readr::read_csv("tbl_gose.csv", col_types = "cinncD") %>%
!is.na(Outcomes.DerivedCompositeGOSE), !is.na(Outcomes.DerivedCompositeGOSE),
# throw out all observations without a precise timestamp # throw out all observations without a precise timestamp
!is.na(Outcomes.DerivedCompositeGOSEDaysPostInjury), !is.na(Outcomes.DerivedCompositeGOSEDaysPostInjury),
# throw out all observations after 18 months # throw out all observations after 18 months for 3 and 6 months imputation
# (for 12 months imputation!) # (for 12 months imputation: throw out observations > 24 months)
Outcomes.DerivedCompositeGOSEDaysPostInjury < 18*30 Outcomes.DerivedCompositeGOSEDaysPostInjury < ifelse(params$imputation_time < 360,18*30,24*30)
) %>% ) %>%
mutate( mutate(
Subject.PatientType = case_when( Subject.PatientType = case_when(
...@@ -136,7 +139,7 @@ tbl_combined <- tbl_combined %>% ...@@ -136,7 +139,7 @@ tbl_combined <- tbl_combined %>%
group_by(gupi) %>% group_by(gupi) %>%
filter( filter(
# all GOSEs before 180 days post injury must be > 1 (dead) # all GOSEs before 180 days post injury must be > 1 (dead)
all(Outcomes.DerivedCompositeGOSE[Outcomes.DerivedCompositeGOSEDaysPostInjury <= 180] > 1), all(Outcomes.DerivedCompositeGOSE[Outcomes.DerivedCompositeGOSEDaysPostInjury <= params$imputation_time] > 1),
# ensure age-bracket # ensure age-bracket
Subject.Age >= params$age_range[1], Subject.Age >= params$age_range[1],
Subject.Age <= params$age_range[2], Subject.Age <= params$age_range[2],
...@@ -164,11 +167,11 @@ tbl_gose_msm <- tbl_combined %>% ...@@ -164,11 +167,11 @@ tbl_gose_msm <- tbl_combined %>%
gose = Outcomes.DerivedCompositeGOSE gose = Outcomes.DerivedCompositeGOSE
) %>% ) %>%
rbind( rbind(
# add censored states at 180.1 days to make prediction for # add censored states at imputation time + 0.1 days to make prediction for
tibble( tibble(
gupi = unique(.$gupi), gupi = unique(.$gupi),
# needed to offset to get a prediction in all cases # needed to offset to get a prediction in all cases
days_post_injury = 180 + .1, days_post_injury = params$imputation_time + .1,
gose = 99 gose = 99
) )
) %>% ) %>%
...@@ -208,7 +211,7 @@ fit <- msm::msm( ...@@ -208,7 +211,7 @@ fit <- msm::msm(
gen.inits = TRUE, gen.inits = TRUE,
qmatrix = Q, qmatrix = Q,
censor = 99, censor = 99,
pci = c(90, 270), pci = c(ifelse(params$imputation_time==90,80,90), 270),
censor.states = 2:7, # cannot be dead since these were filtered previously censor.states = 2:7, # cannot be dead since these were filtered previously
control = list( control = list(
fnscale = 12000, fnscale = 12000,
...@@ -235,7 +238,7 @@ tbl_posteriors <- tibble( ...@@ -235,7 +238,7 @@ tbl_posteriors <- tibble(
`2_or_3` = `3` `2_or_3` = `3`
) %>% ) %>%
filter( filter(
t == 180.1 t == params$imputation_time + 0.1
) %>% ) %>%
select( select(
gupi, `1`, `2_or_3`, `4`, `5`, `6`, `7`, `8` gupi, `1`, `2_or_3`, `4`, `5`, `6`, `7`, `8`
...@@ -252,7 +255,7 @@ tbl_posteriors <- tibble( ...@@ -252,7 +255,7 @@ tbl_posteriors <- tibble(
``` ```
```{r final-imputations} ```{r final-imputations}
# start by adding confirmed <= 180d deaths back in # start by adding confirmed deaths (<= imputation time) back in
tbl_final_imputations <- readr::read_csv( tbl_final_imputations <- readr::read_csv(
"tbl_gose.csv", "tbl_gose.csv",
col_types = "cinncD" col_types = "cinncD"
...@@ -272,7 +275,7 @@ tbl_final_imputations <- readr::read_csv( ...@@ -272,7 +275,7 @@ tbl_final_imputations <- readr::read_csv(
) %>% ) %>%
mutate( mutate(
probability = ifelse( probability = ifelse(
is.na(death_days) | (death_days > 180), is.na(death_days) | (death_days > params$imputation_time),
probability, probability,
ifelse(gose != "gose_1_probability", 0.0, 1.0) ifelse(gose != "gose_1_probability", 0.0, 1.0)
) )
...@@ -312,8 +315,8 @@ tbl_final_imputations <- readr::read_csv( ...@@ -312,8 +315,8 @@ tbl_final_imputations <- readr::read_csv(
) %>% ) %>%
group_by(gupi) %>% group_by(gupi) %>%
summarize( summarize(
gose = gose[which.min(abs(days - 180))], gose = gose[which.min(abs(days - params$imputation_time))],
days = days[which.min(abs(days - 180))], days = days[which.min(abs(days - params$imputation_time))],
.groups = "drop" .groups = "drop"
) %>% ) %>%
transmute( transmute(
...@@ -346,7 +349,6 @@ tbl_final_imputations <- readr::read_csv( ...@@ -346,7 +349,6 @@ tbl_final_imputations <- readr::read_csv(
) )
``` ```
```{r plausi-check} ```{r plausi-check}
tbl_final_imputations %>% tbl_final_imputations %>%
select( select(
...@@ -374,25 +376,24 @@ tbl_final_imputations %>% ...@@ -374,25 +376,24 @@ tbl_final_imputations %>%
coord_fixed(expand = FALSE) + coord_fixed(expand = FALSE) +
scale_fill_continuous(low = "#FFFFFF", high = "#555555") + scale_fill_continuous(low = "#FFFFFF", high = "#555555") +
theme_bw() + theme_bw() +
ggtitle("Confusion matrix: imputed vs. per-protocol") ggtitle(sprintf("Confusion matrix: imputed vs. per-protocol: %i days",params$imputation_time))
``` ```
```{r save-imputed-values} ```{r save-imputed-values}
tbl_final_imputations %>% { tbl_final_imputations %>% {
tmp <- . tmp <- .
names(tmp) <- gsub("gose", "Subject.DerivedImputed180DaysGOSE", x = names(.)) names(tmp) <- gsub("gose", sprintf("Subject.DerivedImputed%iDaysGOSE",params$imputation_time), x = names(.))
tmp } %>% tmp } %>%
rename( rename(
"Subject.GOSE6monthEndpointDerived" = "Subject.DerivedImputed180DaysGOSE_map_per_protocol_combined" !! sprintf("Subject.GOSE%imonthEndpointDerived",round(params$imputation_time/30)) := sprintf("Subject.DerivedImputed%iDaysGOSE_map_per_protocol_combined",params$imputation_time)
) %>% ) %>%
write_csv("tbl_gose_imputed.csv") write_csv(sprintf("tbl_gose_imputed%i.csv",params$imputation_time))
``` ```
<!-- # Session Info# -->
<!-- ```{r session-info} -->
# Session Info <!-- sessionInfo() %>% -->
<!-- knitr::kable() -->
```{r session-info} <!-- ``` -->
sessionInfo() %>%
knitr::kable()
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment