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 calculationslibrary(haven)library(metalite)library(metalite.table1)library(tern.mmrm)library(broom)library(random.cdisc.data)library(MASS)
Code
adsl <- random.cdisc.data::cadsladqs <- 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)
$emmeans
AVISIT = WEEK 1 DAY 8:
ARMCD emmean SE df lower.CL upper.CL
ARM B 3.64 0.972 647 1.73 5.55
ARM A 3.15 0.972 647 1.24 5.06
ARM C 4.14 0.978 643 2.22 6.06
AVISIT = WEEK 2 DAY 15:
ARMCD emmean SE df lower.CL upper.CL
ARM B 8.90 0.972 647 6.99 10.80
ARM A 8.41 0.972 647 6.50 10.31
ARM C 9.40 0.978 643 7.48 11.32
AVISIT = WEEK 3 DAY 22:
ARMCD emmean SE df lower.CL upper.CL
ARM B 15.12 0.972 647 13.21 17.03
ARM A 14.63 0.972 647 12.72 16.54
ARM C 15.62 0.978 643 13.70 17.54
AVISIT = WEEK 4 DAY 29:
ARMCD emmean SE df lower.CL upper.CL
ARM B 19.03 0.972 647 17.13 20.94
ARM A 18.54 0.972 647 16.64 20.45
ARM C 19.54 0.978 643 17.62 21.46
AVISIT = WEEK 5 DAY 36:
ARMCD emmean SE df lower.CL upper.CL
ARM B 24.05 0.972 647 22.14 25.96
ARM A 23.56 0.972 647 21.65 25.47
ARM C 24.56 0.978 643 22.64 26.47
Degrees-of-freedom method: kenward-roger
Confidence level used: 0.95
$contrasts
AVISIT = WEEK 1 DAY 8:
contrast estimate SE df t.ratio p.value
ARM B - ARM A 0.490 1.21 397 0.405 0.9137
ARM B - ARM C -0.502 1.21 397 -0.413 0.9103
ARM A - ARM C -0.991 1.21 397 -0.816 0.6933
AVISIT = WEEK 2 DAY 15:
contrast estimate SE df t.ratio p.value
ARM B - ARM A 0.490 1.21 397 0.405 0.9137
ARM B - ARM C -0.502 1.21 397 -0.413 0.9103
ARM A - ARM C -0.991 1.21 397 -0.816 0.6933
AVISIT = WEEK 3 DAY 22:
contrast estimate SE df t.ratio p.value
ARM B - ARM A 0.490 1.21 397 0.405 0.9137
ARM B - ARM C -0.502 1.21 397 -0.413 0.9103
ARM A - ARM C -0.991 1.21 397 -0.816 0.6933
AVISIT = WEEK 4 DAY 29:
contrast estimate SE df t.ratio p.value
ARM B - ARM A 0.490 1.21 397 0.405 0.9137
ARM B - ARM C -0.502 1.21 397 -0.413 0.9103
ARM A - ARM C -0.991 1.21 397 -0.816 0.6933
AVISIT = WEEK 5 DAY 36:
contrast estimate SE df t.ratio p.value
ARM B - ARM A 0.490 1.21 397 0.405 0.9137
ARM B - ARM C -0.502 1.21 397 -0.413 0.9103
ARM A - ARM C -0.991 1.21 397 -0.816 0.6933
Degrees-of-freedom method: kenward-roger
P value adjustment: tukey method for comparing a family of 3 estimates
Code
#Summarize the emmeans results for differencesemm_summary <-summary(pairs(emmeans_results), infer =c(TRUE, TRUE))
Code
library(gtsummary)# Use gtsummary to create a table (gtsummary works well with data frames)emm_df <-as.data.frame(emm_summary)table <- emm_df %>%tbl_summary(type =all_continuous() ~"continuous2",statistic =all_continuous() ~"{mean} ({sd})",digits =all_continuous() ~2 ) %>%modify_header(label ="**Statistic**")# Print the table with kableExtra for better formatting in R Markdown or outputlibrary(kableExtra)
Warning: package 'kableExtra' was built under R version 4.2.3
Attaching package: 'kableExtra'
The following objects are masked from 'package:flextable':
as_image, footnote
The following object is masked from 'package:dplyr':
group_rows