---
title: "MMRM table and plots"
subtitle: ""
author:
- name: Eric
corresponding: true
email: echen@ultragenyx.com
roles:
- Biostatistician
- Project administration
- Software
- Visualization
affiliations:
- test
keywords:
- tets
- test
- fracture
date: last-modified
citation:
container-title: Analysis
number-sections: true
format:
html:
self-contained: true
code-fold: true
code-tools: true
coddelink: true
comments:
hypothesis: true
html-math-method: katex
pdf: default
docx: default
jats: default
# acs-pdf: default
ipynb: default
format-links: [ipynb]
toc: true
toc-depth: 5
---
```{r}
#| label: load library
#| warning: false
rm (list= ls (all= TRUE ))
library (tinytex)
library (ggplot2)
library (plotly)
library (dplyr)
library (haven)
library (labelled)
library (bslib)
library (DT)
library (r2rtf)
library (metalite)
library (metalite.table1)
library (haven)
library (tidyr)
library (tools)
library (gtsummary)
library (stringr)
library (RColorBrewer)
library (flextable)
library (stringr)
library (tern)
library (tidyverse)
library (vistime)
library (readxl)
library (hrbrthemes)
library (rlang)
library (lubridate) # For date calculations
library (haven)
library (metalite)
library (metalite.table1)
library (tern.mmrm)
library (broom)
library (random.cdisc.data)
library (MASS)
```
```{r}
#| warning: false
adsl <- random.cdisc.data:: cadsl
adqs <- random.cdisc.data:: cadqs
# Ensure character variables are converted to factors and empty strings and NAs are explicit missing levels.
adsl <- df_explicit_na (adsl)
adqs <- df_explicit_na (adqs)
adqs_f <- adqs %>%
dplyr:: filter (PARAMCD == "FKSI-FWB" & ! AVISIT %in% c ("BASELINE" , "SCREENING" )) %>%
droplevels () %>%
dplyr:: mutate (ARMCD = factor (ARMCD, levels = c ("ARM B" , "ARM A" , "ARM C" ))) %>%
dplyr:: mutate (
AVISITN = rank (AVISITN) %>%
as.factor () %>%
as.numeric () %>%
as.factor ()
)
adsl_sub <- adqs_f %>%
dplyr:: filter (! is.na (CHG)) %>%
distinct (USUBJID) %>%
left_join (adsl, by = "USUBJID" )
var_labels (adqs_f) <- var_labels (adqs)
```
```{r}
MASS:: truehist (adqs_f$ CHG, nbins = 12 )
mmrm_results <- fit_mmrm (
vars = list (
response = "CHG" ,
covariates = c ("BASE" , "STRATA1" , "BMRKR2" ),
id = "USUBJID" ,
arm = "ARMCD" ,
visit = "AVISIT"
),
data = adqs_f,
weights_emmeans = "equal"
)
df <- tidy (mmrm_results)
attr (df$ AVISIT, "label" ) <- "Visit"
# Define the split function
split_fun <- drop_split_levels
result <- basic_table (show_colcounts = TRUE ) %>%
split_cols_by ("ARMCD" , ref_group = mmrm_results$ ref_level) %>%
split_rows_by ("AVISIT" , split_fun = split_fun, label_pos = "topleft" , split_label = obj_label (df$ AVISIT)) %>%
summarize_lsmeans (show_relative = "increase" ) %>%
append_topleft (" Statistics" ) %>%
build_table (df, alt_counts_df = adsl_sub)
result
plot <- g_mmrm_lsmeans (
mmrm_results,
select = "estimates" ,
xlab = "Visit"
)
plot
plot <- g_mmrm_diagnostic (mmrm_results, type = "q-q-residual" )
plot
```
```{R}
library (dplyr)
library (tern.mmrm)
library (nestcolor)
adsl <- random.cdisc.data:: cadsl
adqs <- random.cdisc.data:: cadqs
adqs_f <- adqs %>%
dplyr:: filter (PARAMCD == "FKSI-FWB" & ! AVISIT %in% c ("BASELINE" )) %>%
droplevels () %>%
dplyr:: mutate (ARMCD = factor (ARMCD, levels = c ("ARM B" , "ARM A" , "ARM C" ))) %>%
dplyr:: mutate (
AVISITN = rank (AVISITN) %>%
as.factor () %>%
as.numeric () %>%
as.factor ()
)
# Simulation of groups.
set.seed (2 )
adqs_f_with_groups <- rbind (
within (
adqs_f[sample (seq_len (nrow (adqs_f)), size = 1 / 2 * nrow (adqs_f)), ],
group <- "subpopulation 1"
),
within (
adqs_f,
{
group <- "subpopulation 2"
AVAL <- AVAL + rnorm (length (AVAL), mean = 10 , sd = 2 )
USUBJID <- paste0 (USUBJID, "-S2" )
}
)
)
adqs_f_with_groups$ group <- factor (adqs_f_with_groups$ group)
mmrm_results <- fit_mmrm (
data = adqs_f_with_groups,
vars = list (
response = "AVAL" ,
covariates = c (),
id = "USUBJID" ,
arm = "ARMCD" ,
visit = "AVISIT"
),
cor_struct = "unstructured" ,
weights_emmeans = "equal" ,
parallel = TRUE
)
df_a <- extract_mmrm_subgroups (
fit = mmrm_results,
visit = "SCREENING" ,
subgroups = c ("group" , "SEX" ),
treatment_arm = "ARM A"
)
t<- tidy (mmrm_results)
df_a_t<- as.data.frame (df_a)
tab_a <- basic_table () %>%
tabulate_mmrm_subgroups (
df = df_a,
vars = c ("n_tot" , "diff" , "ci" , "pval" )
)
plot <- g_forest (
tab_a,
logx = FALSE ,
xlim = c (- 5 , 2.5 ),
x_at = c (- 5 , - 2.5 , 0 , 2.5 ),
vline = 0
)
plot
```
```{r}
path<- "//dc1/project workspace/DTX401 (GSD1a)/DTX401-CL301/11.0 Biostatistics/Programs/Dev/PgmStat/EC/SAS outputs/"
totaldose<- read_sas (paste0 (path, "totaldose.sas7bdat" ), NULL )
subj<- totaldose%>% filter (AVISITN== 0 )
subj$ TRT01P<- factor (subj$ TRT01P, levels = c ("Placebo" ,"DTX401" ))
totaldose_f <- totaldose %>%
dplyr:: filter (AVISITN %in% c (168 ,210 ,252 ,294 ,336 )) %>%
dplyr:: mutate (TRT01P = factor (TRT01P, levels = c ("Placebo" ,"DTX401" ))) %>%
dplyr:: mutate (
AVISITN = rank (AVISITN) %>%
as.factor () %>%
as.numeric () %>%
as.factor ()
)
totaldose_f$ AVISIT <- as.factor (totaldose_f$ AVISIT)
```
```{r}
mmrm_results <- fit_mmrm (
vars = list (
response = "CHG" ,
covariates = c ("TRT01PN" , "AVISITN" ,"TRT01PN:AVISITN" , "STRATAGN" , "STRATGLN" , "BASE" ),
id = "USUBJID" ,
arm = "TRT01P" ,
visit = "AVISIT"
),
data = totaldose_f,
weights_emmeans = "equal"
)
df <- tidy (mmrm_results)
attr (df$ AVISIT, "label" ) <- "Visit"
# Define the split function
split_fun <- drop_split_levels
result <- basic_table (show_colcounts = TRUE ) %>%
split_cols_by ("TRT01P" , ref_group = mmrm_results$ ref_level) %>%
split_rows_by ("AVISIT" , split_fun = split_fun, label_pos = "topleft" , split_label = obj_label (df$ AVISIT)) %>%
summarize_lsmeans (show_relative = "increase" ) %>%
append_topleft (" Statistics" ) %>%
build_table (df, alt_counts_df = subj)
result
plot <- g_mmrm_lsmeans (
mmrm_results,
select = "estimates" ,
xlab = "Visit"
)
ggplotly (plot)
plot <- g_mmrm_diagnostic (mmrm_results, type = "q-q-residual" )
plot
```
```{r}
df_a <- extract_mmrm_subgroups (
fit = mmrm_results,
visit = "Week 48"
)
t<- tidy (mmrm_results)
df_a_t<- as.data.frame (df_a)
tab_a <- basic_table () %>%
tabulate_mmrm_subgroups (
df = df_a,
vars = c ("n_tot" , "diff" , "ci" , "pval" )
)
plot <- g_forest (
tab_a,
logx = FALSE ,
xlim = c (- 2 , 1 ),
x_at = c (- 2 , - 1 , 0 , 1 ),
vline = 0
)
plot
```