Functional Status (Paper 10)

Author

Dr. Priscilla Opare-Addo

Published

June 4, 2026

Import data and data managemnt

Main data preparation

Show the code
df_pings2_bp <- 
    pings2_data %>%
    mutate(
        female = a_gender == "Female", 
        nihss_levcon = str_extract(z_levcon, "^\\d+") %>% as.numeric(),
        nihss_loc_quest = str_extract(z_loc_quest, "^\\d+") %>% 
            as.numeric(), 
        nihss_loc_comm = str_extract(z_loc_comm, "^\\d+") %>% as.numeric(),
        nihss_bestgaze = str_extract(z_bestgaze, "^\\d+") %>% as.numeric(),
        nihss_vfield = str_extract(z_vfield, "^\\d+") %>% as.numeric(),
        nihss_facparesis = str_extract(z_facparesis, "^\\d+") %>% 
            as.numeric(),
        nihss_motor_armr = str_extract(z_motor_armr, "^\\d+") %>% 
            as.numeric(),
        nihss_motor_arml = str_extract(z_motor_arml, "^\\d+") %>% 
            as.numeric(),
        nihss_motor_legr = str_extract(z_motor_legr, "^\\d+") %>% 
            as.numeric(),
        nihss_motor_legl = str_extract(z_motor_legl, "^\\d+") %>% 
            as.numeric(),
        nihss_limbataxia = str_extract(z_limbataxia, "^\\d+") %>% 
            as.numeric(),
        nihss_pinprick = str_extract(z_pinprick, "^\\d+") %>% as.numeric(),
        nihss_bestlang = str_extract(z_bestlang, "^\\d+") %>% as.numeric(),
        nihss_dysarthria = str_extract(z_dysarthria, "^\\d+") %>% 
            as.numeric(),
        nihss_extinction = str_extract(z_extinction, "^\\d+") %>% 
            as.numeric(),
        nihss_scale = nihss_levcon + nihss_loc_quest + nihss_loc_comm + 
            nihss_bestgaze + nihss_vfield + nihss_facparesis + 
            nihss_motor_armr + 
            nihss_motor_arml + nihss_motor_legr + nihss_motor_legl + 
            nihss_limbataxia + nihss_pinprick + nihss_bestlang + 
            nihss_dysarthria + nihss_extinction, 
        ranking = str_extract(x_ranking, "\\d+") %>% as.numeric(), 
        dm = case_when(
            ff_hba1c_0 > 6.5 ~ "Yes",  
            c_dm == "Yes" ~ "Yes",
            h_antidm == "Yes" ~ "Yes", 
            ff_fbs_0 > 7 ~ "Yes",
            eventname == "Baseline" ~ "No"),
        hpt = case_when(
            sbp >= 140 ~ "Yes", 
            dbp >= 90 ~ "Yes",
            sbp <  140 ~ "No",
            dbp <  90 ~ "No") %>%  factor(),
        hyperlipidemia = case_when(
            c_hyperlip == "Yes" ~ "Yes", 
            h_antilipid == "Yes" ~ "Yes",
            ff_totchol_0 > 5.17 ~ "Yes", 
            eventname == "Baseline" ~ "No"),
        tobacco_use = case_when(g_tobacco %in% c(
            "Formerly used tobacco products", 
            "Stopped after the stroke occurred", 
            "Currently uses tobacco products") ~ "Yes",
            g_tobacco == "Never used tobacco products" ~ "No"),
        obese_overwgt = case_when(
            ee_bmi_0 >= 25 ~ "Yes", 
            ee_bmi_0 < 25 ~ "No"),
        alcohol_curr = case_when(
            g_alcohol == "Currently uses alcohol" ~ "Yes",
            g_alcohol %in% c(
                "Never used alcohol", 
                "Past 12 months", 
                "Past 30 days", 
                "Formerly used alcohol", 
                "Stopped after the stroke occured") ~ "No"),
        alcohol_ever = case_when(
            g_alcohol %in%  c(
              "Currently uses alcohol", 
              "Past 12 months", 
              "Past 30 days", 
              "Formerly used alcohol", 
              "Stopped after the stroke occured") ~ "Yes",
            g_alcohol %in% c("Never used alcohol") ~ "No") %>% 
              factor(),
        acei = i_acei_v2 == "Checked",
        arb = i_arb_v2 == "Checked",
        bb = i_bb_v2 == "Checked",
        ccb = i_ccb_v2 == "Checked",
        h_alpha_md = i_alpha_md_v2 == "Checked",
        h_alpha_ab = i_alpha_ab_v2 == "Checked",
        diuretics = i_diuretics_v2 == "Checked",
        h_antiplt = i_antiplt_v2 == "Yes",
        h_statins = i_statins_v2 == "Yes",
        income2 = case_when(
            a_income %in% c("0-100", "101-250", "251-500") ~ "<500 GHc",
            a_income %in% c("501-1500", "1501-3000", "> 3000") ~ 
                ">= 500 GHc"),
        educ2 = case_when(
            a_formeduc %in% c("None", "Primary") ~ "None/Primary",
            a_formeduc %in% c("Secondary", "Tertiary", "Postgraduate") ~ 
                "Secondary or higher"),
        agecat = case_when(
            a_agebase  < 60 ~ "<60", a_agebase >= 60 ~ ">=60"),
        hosp_cat = case_when(
            datagrp %in% c("CCTH SITE", "KATH SITE", "KORLE-BU SITE") ~ 
                "Tertiary",
            datagrp %in% c(
                "KUMASI SOUTH HOSPITAL", "AGOGO SITE", "KNUST SITE") ~ 
                "Secondary",
            datagrp %in% c(
                "ANKAASE SITE", "MANHYIA GOVT HOSPITAL", "KWADASO SITE",
                "TAFO SITE") ~ 
              "Primary") %>% 
          factor(levels = c("Tertiary", "Secondary", "Primary")), 
        arm = case_when(
            arm == "Arm 1- Intervention Arm" ~ "Intervention", 
            arm == "Arm 2- Routine Care" ~ "Routine"),
        morisky_4_item = (unclass(k_forget) + unclass(k_miss) + 
            unclass(k_fine) + unclass(k_doctor)), 
        across(
            c(l_forget, l_decide, l_salty, l_shake, l_fasfood, l_appoint, 
            l_missched, l_prescrip, l_runout, l_skipmed, l_feelbet,
            l_feelsick, l_someone, l_careless), 
            .fns = ~factor(.x),
            .names = "{.col}_unclassed"),
        across(
            c(l_forget_unclassed, l_decide_unclassed, l_salty_unclassed, 
              l_shake_unclassed, l_fasfood_unclassed, l_appoint_unclassed, 
            l_missched_unclassed, l_prescrip_unclassed, l_runout_unclassed, 
            l_skipmed_unclassed, l_feelbet_unclassed, l_feelsick_unclassed, 
            l_someone_unclassed, l_careless_unclassed), 
            .fns = ~unclass(.x)),
        hillbone = (
            l_forget_unclassed + l_decide_unclassed + l_salty_unclassed + 
            l_shake_unclassed + l_fasfood_unclassed + l_appoint_unclassed + 
            l_missched_unclassed + l_prescrip_unclassed + 
            l_runout_unclassed + l_skipmed_unclassed + l_feelbet_unclassed + 
            l_feelsick_unclassed + l_someone_unclassed + 
                l_careless_unclassed) %>%  
            as.numeric(),
        aa_bp_115_75_corrrect = ifelse(
            aa_bp_115_75 == "Normal", "Yes", "No"),
        aa_bp160_100_correct = ifelse(
            aa_bp160_100 == "High", "Yes", "No"),
        aa_hptlasts_correct = ifelse(
            aa_hptlasts == "The Rest of their Life", "Yes", "No"),
        aa_hptmeds_correct = ifelse(aa_hptmeds == "Everyday", "Yes", "No"),
        aa_losewght_correct = ifelse(aa_losewght == "Go Down", "Yes", "No"),
        aa_eatsalt_correct = ifelse(aa_eatsalt == "Go Down", "Yes", "No"),
        aa_hrtattack_correct = ifelse(aa_hrtattack == "Yes", "Yes", "No"),
        aa_cancer_correct = ifelse(aa_cancer == "No", "Yes", "No"),
        aa_stroke_correct = ifelse(aa_stroke == "Yes", "Yes", "No"),
        aa_kidneyp_correct = ifelse(aa_kidneyp == "Yes", "Yes", "No"),
        aa_highrisk_correct = ifelse(aa_highrisk == "Yes", "Yes", "No"),
        aa_headache_correct = ifelse(aa_headache == "No", "Yes", "No"),
        aa_feelgood_correct = ifelse(aa_feelgood == "Never", "Yes", "No"),
        aa_strokeris_correct = ifelse(
            aa_strokeris == "The Rest of Their Life", "Yes", "No"),
        aa_hkq = (aa_bp_115_75_corrrect == "Yes") + 
            (aa_bp160_100_correct == "Yes") +
            (aa_hptlasts_correct =="Yes") + 
            (aa_hptmeds_correct == "Yes") + 
            (aa_losewght_correct == "Yes") + 
            (aa_eatsalt_correct == "Yes") + 
            (aa_hrtattack_correct == "Yes") + 
            (aa_cancer_correct == "Yes") + 
            (aa_stroke_correct == "Yes") + 
            (aa_kidneyp_correct == "Yes") + 
            (aa_highrisk_correct == "Yes") + 
            (aa_headache_correct =="Yes") + 
            (aa_feelgood_correct =="Yes") + 
            (aa_strokeris_correct == "Yes"),
        mobility = case_when(
            str_detect(
                p_mobility , "I have no problems in walking about") ~ "1",
            str_detect(
                p_mobility , "I have some problems in walking about") ~ "2",
            str_detect(
                p_mobility , "I am confined to bed") ~ "3")%>% 
            as.numeric(),
        selfcare = case_when(
            str_detect(
                p_selfcare , "I have no problems with self-care") ~ "1",
            str_detect(
                p_selfcare , "I have some problems with washing or") ~ "2",
            str_detect(
                p_selfcare , 
                "I am unable to wash or dress myself") ~ "3")%>%
            as.numeric(),
        usual_act = case_when(
            str_detect(
                p_usual_act , 
                "I have no problems with performing my") ~ "1",
            str_detect(
                p_usual_act , "I have some problems with performing") ~ "2",
            str_detect(
                p_usual_act , "I am unable to perform my usual activitie") ~
                "3")%>% 
            as.numeric(),
        pain_disc = case_when(
            str_detect(p_pain_disc , "I have no pain or discomfort") ~ "1",
            str_detect(
                p_pain_disc , "I have moderate pain or discomfort") ~ "2",
            str_detect(
                p_pain_disc , 
                "I have extreme pain or discomfort") ~ "3") %>%
            as.numeric(),
        anxiety = case_when(
            str_detect(p_anxiety, "I am not anxious or depressed") ~ "1",
            str_detect(
                p_anxiety, "I am moderately anxious or depressed") ~ "2",
            str_detect(
                p_anxiety, "I am extremely anxious or depressed") ~ "3")%>% 
            as.numeric(),
        eq_5d = mobility + selfcare + usual_act + pain_disc + anxiety,
         p_health = ifelse(p_health <= 1, p_health*100, p_health), 
        across(
            c(bb_physical, bb_nutfacts, bb_repsalt, bb_limsalt, bb_tabsalt, 
              bb_eatsfood, 
            bb_broilst, bb_rsfoods, bb_rephfat, bb_totcal, bb_eatveges, 
            bb_alcohol, 
            bb_smoking, bb_bpathome, bb_fbpmeds, bb_takemeds, bb_wgtdown, 
            bb_monitst, 
            bb_streslow, bb_doctor, bb_physical2, bb_nutfacts2, bb_repsalt2, 
            bb_limsalt2, 
            bb_tabsalt2, bb_eatsfood2, bb_broilst2, bb_rsfoods2, 
            bb_rephfat2, 
            bb_totcal2, 
            bb_eatveges2, bb_alcohol2, bb_smoking2, bb_bpathome2,
            bb_fbpmeds2,
            bb_takemeds2, 
            bb_wgtdown2, bb_monitst2, bb_streslow2, bb_doctor2, 
            bb_physical3,
            bb_nutfacts3,
            bb_repsalt3, bb_limsalt3, bb_tabsalt3, bb_eatsfood3, 
            bb_broilst3,
            bb_rsfoods3, 
            bb_rephfat3, bb_totcal3, bb_eatveges3, bb_alcohol3, 
            bb_smoking3,
            bb_bpathome3, 
            bb_fbpmeds3, bb_takemeds3, bb_wgtdown3, bb_monitst3,
            bb_streslow3,
            bb_doctor3), 
            .fns = ~factor(.x),
            .names = "{.col}_unclassed"),
        across(
            c(bb_physical_unclassed, bb_nutfacts_unclassed,
              bb_repsalt_unclassed, 
              bb_limsalt_unclassed, bb_tabsalt_unclassed,
              bb_eatsfood_unclassed, 
            bb_broilst_unclassed, bb_rsfoods_unclassed,
            bb_rephfat_unclassed, 
            bb_totcal_unclassed, bb_eatveges_unclassed, 
            bb_alcohol_unclassed, 
            bb_smoking_unclassed, bb_bpathome_unclassed,
            bb_fbpmeds_unclassed, 
            bb_takemeds_unclassed, bb_wgtdown_unclassed,
            bb_monitst_unclassed, 
            bb_streslow_unclassed, bb_doctor_unclassed,
            bb_physical2_unclassed, 
            bb_nutfacts2_unclassed, bb_repsalt2_unclassed,
            bb_limsalt2_unclassed, 
            bb_tabsalt2_unclassed, bb_eatsfood2_unclassed,
            bb_broilst2_unclassed, 
            bb_rsfoods2_unclassed, bb_rephfat2_unclassed,
            bb_totcal2_unclassed, 
            bb_eatveges2_unclassed, bb_alcohol2_unclassed,
            bb_smoking2_unclassed, 
            bb_bpathome2_unclassed, bb_fbpmeds2_unclassed,
            bb_takemeds2_unclassed, 
            bb_wgtdown2_unclassed, bb_monitst2_unclassed,
            bb_streslow2_unclassed, 
            bb_doctor2_unclassed, bb_physical3_unclassed,
            bb_nutfacts3_unclassed,
            bb_repsalt3_unclassed, bb_limsalt3_unclassed,
            bb_tabsalt3_unclassed, 
            bb_eatsfood3_unclassed, bb_broilst3_unclassed,
            bb_rsfoods3_unclassed, 
            bb_rephfat3_unclassed, bb_totcal3_unclassed,
            bb_eatveges3_unclassed, 
            bb_alcohol3_unclassed, bb_smoking3_unclassed,
            bb_bpathome3_unclassed, 
            bb_fbpmeds3_unclassed, bb_takemeds3_unclassed,
            bb_wgtdown3_unclassed, 
            bb_monitst3_unclassed, bb_streslow3_unclassed,
            bb_doctor3_unclassed), 
            .fns = ~unclass(.x)),
        across(
          c(bb_fbpmeds_unclassed, bb_takemeds_unclassed,
            bb_fbpmeds2_unclassed, bb_takemeds2_unclassed,
            bb_fbpmeds3_unclassed, bb_takemeds3_unclassed), 
          .fns = ~ 0 - .x + 5),
        hpt_self_care = bb_physical_unclassed + bb_nutfacts_unclassed + 
          bb_repsalt_unclassed + 
            bb_limsalt_unclassed + bb_tabsalt_unclassed + 
            bb_eatsfood_unclassed + 
            bb_broilst_unclassed + bb_rsfoods_unclassed + 
            bb_rephfat_unclassed + 
            bb_totcal_unclassed + bb_eatveges_unclassed + 
            bb_alcohol_unclassed + 
            bb_smoking_unclassed + bb_bpathome_unclassed + 
            bb_fbpmeds_unclassed + 
            bb_takemeds_unclassed + bb_wgtdown_unclassed + bb_monitst_unclassed + 
            bb_streslow_unclassed + bb_doctor_unclassed + bb_physical2_unclassed + 
            bb_nutfacts2_unclassed + bb_repsalt2_unclassed + bb_limsalt2_unclassed + 
            bb_tabsalt2_unclassed + bb_eatsfood2_unclassed + bb_broilst2_unclassed + 
            bb_rsfoods2_unclassed + bb_rephfat2_unclassed + bb_totcal2_unclassed + 
            bb_eatveges2_unclassed + bb_alcohol2_unclassed + bb_smoking2_unclassed + 
            bb_bpathome2_unclassed + bb_fbpmeds2_unclassed + bb_takemeds2_unclassed + 
            bb_wgtdown2_unclassed + bb_monitst2_unclassed + bb_streslow2_unclassed + 
            bb_doctor2_unclassed + bb_physical3_unclassed + bb_nutfacts3_unclassed +
            bb_repsalt3_unclassed + bb_limsalt3_unclassed + bb_tabsalt3_unclassed + 
            bb_eatsfood3_unclassed + bb_broilst3_unclassed + bb_rsfoods3_unclassed + 
            bb_rephfat3_unclassed + bb_totcal3_unclassed + bb_eatveges3_unclassed + 
            bb_alcohol3_unclassed + bb_smoking3_unclassed + bb_bpathome3_unclassed + 
            bb_fbpmeds3_unclassed + bb_takemeds3_unclassed + bb_wgtdown3_unclassed + 
            bb_monitst3_unclassed + bb_streslow3_unclassed + bb_doctor3_unclassed, 
        sbp_2 = sbp, 
        dbp_2 = dbp, 
        sbp_baseline = case_when(eventname == "Baseline" ~ sbp),
        dbp_baseline = case_when(eventname == "Baseline" ~ dbp),
        d_st_type =as.character(d_st_type),
        stroke_type = case_when(
          d_st_type == "Ischemic Stroke" ~ "Ischemic",
          d_st_type == "Intracerebral Hemorrhagic Stroke" ~ "Hemorrhagic", 
          d_st_type == "Ischemic With Hemorrhagic Transformation" ~ "Ischemic", 
          d_st_type == "Untyped Stroke (no CT scan available)" ~ "Untyped") %>% factor(),
        hosp_cat2 = case_when(
          hosp_cat %in% c("Primary","Secondary") ~ "Primary/Secondary",
          hosp_cat == "Tertiary" ~ "Tertiary"),
        egfr0 = parse_number(ff_egfr_0),
        egfr0 = ifelse(egfr0 < 1, egfr0*100, egfr0),
        egfr_cat = case_when(egfr0 < 60 ~ "<60", egfr0 >= 60 ~ ">=60"),
        across(
            c(m12_pratio_1, m12_pratio_2, m12_pratio_3, m12_pratio_4, 
            m12_pratio_5, m12_pratio_6, m12_pratio_7, m12_pratio_8), 
              ~parse_number(.)),
        emerg_visit = case_when(
          gg_emerg_0 == "seizure" ~ "Yes",
          gg_emerg_1 == "Stage III HPT" ~ "Yes", 
          gg_emerg_3 %in% c("Seizure","Yes") ~ "Yes", 
          gg_emerg_6 %in%  c("seizure (recurrent stroke)", "YES") ~ "Yes",
          eventname == "Baseline" ~ "No"),
        emerg_visit = emerg_visit == "Yes",
        mpr12 = rowMeans(
          across(
            c(m12_pratio_1, m12_pratio_2, m12_pratio_3, m12_pratio_4, 
              m12_pratio_5, m12_pratio_6, m12_pratio_7, m12_pratio_8)), 
            na.rm=T),
        mpr12 = ifelse(mpr12 < 10, mpr12*10, mpr12),
        mpr12 = ifelse(mpr12 > 200, mpr12/10, mpr12),
        mpr12 = ifelse(mpr12 > 100, 100, mpr12),
        sbp_base_cat = case_when(
          sbp < 160 & eventname == "Baseline" ~ "<160mmHg",
          sbp >= 160 & eventname == "Baseline" ~ ">=160mmHg")) %>% 
      arrange(pid, eventname) %>% 
      group_by(pid) %>% 
      fill(
        sbp_2, dbp_2, arm, sbp_baseline, dbp_baseline, emerg_visit, dm, 
        d_st_type, arm) %>% 
      ungroup() %>% 
      mutate(
          sbp_less_140_itt = sbp_2 < 140, 
          sbp_less_140_pp = sbp < 140, 
          bp_less_140_90_itt = (sbp_2 < 140 & dbp_2 < 90),
          bp_less_140_90_pp = (sbp < 140 & dbp < 90),
          sbp_diff = sbp_baseline - sbp,
          dbp_diff = dbp_baseline - dbp,
          sbp_diff_itt = sbp_baseline - sbp_2,
          dbp_diff_itt = dbp_baseline - dbp_2,
          sbp_less_135_itt = sbp_2 < 135, 
          sbp_less_135_pp = sbp < 135,
          sbp_less_130_itt = sbp_2 < 130, 
          sbp_less_130_pp = sbp < 130, 
          review_no = case_when(
            eventname == "Baseline" & !is.na(sbp) ~ 0,
            eventname == "Month 1"  & !is.na(sbp) ~ 1,
            eventname == "Month 3" & !is.na(sbp) ~ 3,
            eventname == "Month 6"  & !is.na(sbp) ~ 6,
            eventname == "Month 9" & !is.na(sbp) ~ 9,
            eventname == "Month12" & !is.na(sbp) ~ 12)) %>% 
      arrange(pid, eventname) %>% 
      group_by(pid) %>% 
      mutate(
        max_review = max(review_no, na.rm = T),
        completed_study = case_when(
          max_review == 12 ~ "Yes", TRUE ~ "No") %>% factor()) %>% 
      ungroup() %>% 
  mutate(
    p_mobility, p_selfcare, p_usual_act, p_pain_disc, 
        p_anxiety, p_health,
        r_hamd_total,
        s_g10worth, s_g11help, s_g12depress, s_g13hope, s_h2control, 
        s_h3ability, s_h4things, s_h5overcome,
        x_ranking, 
        z_levcon, z_loc_quest, z_loc_comm, z_bestgaze, z_vfield,
        z_facparesis, z_motor_armr, z_motor_arml, z_motor_legr, 
        z_motor_legl, z_limbataxia, z_pinprick, z_bestlang, 
        z_dysarthria, z_extinction,
        aa_bp_115_75:aa_strokeris,
        ee_bmi_0, ee_sbp_0, ee_dbp_0, ee_waistc_0, ee_whrat_1, ee_hipc_0, 
        ee_bmi_0, ee_waistc_0, ee_wgt_0, ee_heightcm, ee_sbp_12,
        ff_hba1c_0, ff_creat_0, ff_totchol_0, ff_ldl_0, ff_hdl_0,
        ff_trigly_0, ff_egfr_0, ff_fbs_0 
        ) %>% 
    mutate(
        a_livingsit2 = case_when(
            a_livingsit == "Lives Alone" ~ "Lives without family",
            a_livingsit == "Lives With Spouse and Children" ~ "Lives with family",
            a_livingsit == "Lives in a Nursing Home" ~ "Lives without family",
            a_livingsit == "Lives With Spouse" ~ "Lives with family",
            a_livingsit == "Lives With Extended Family" ~ "Lives with family",
            a_livingsit == "Lives With Children" ~ "Lives with family"),
        educ = fct_recode(a_formeduc, Tertiary = "Postgraduate"),
        income = fct_collapse(
            a_income, ">500" = c("501-1500", "1501-3000", "> 3000")),
        maristat = fct_recode(
            a_maristat, "Previously Married" = "Separated",
            "Previously Married" = "Widow/Widower",
            "Previously Married" = "Divorced",
            "Currently Married" = "Cohabitating") %>% 
            fct_relevel(
                c("Currently Married", "Previously Married", 
                  "Never Married")),
        dm = case_when(
            ff_hba1c_0 > 6.5 ~ "Yes",  c_dm == "Yes" ~ "Yes",
            h_antidm == "Yes" ~ "Yes", ff_fbs_0 > 7 ~ "Yes",
            TRUE ~ "No"),
        hyperlipidemia = case_when(
            c_hyperlip == "Yes" ~ "Yes", h_antilipid == "Yes" ~ "Yes",
            ff_totchol_0 > 5.17 ~ "Yes", TRUE ~ "No"),
        #---
        active_wh2 = as.character(g_active_wh),
        active_wh2 = case_when(active_wh2 == "" ~ "0", TRUE ~ active_wh2),
        active_wh2 = str_extract(active_wh2, "\\d+") %>% parse_number(),
        active_wm2 = as.character(g_active_wm),
        active_wm2 = case_when(active_wm2 == "" ~ "0", TRUE ~ active_wm2),
        active_wm2 = str_extract(active_wm2, "\\d+") %>% parse_number(),
        active_work_hrs = case_when(
            (g_active_wh == "" & g_active_wm == "") ~ NA, 
            TRUE ~ active_wm2 + 60*active_wh2)/60,
        #---
        active_wh2 = as.character(g_active_lei),
        active_wh2 = case_when(active_wh2 == "" ~ "0", TRUE ~ active_wh2),
        active_wh2 = str_extract(active_wh2, "\\d+") %>% parse_number(),
        active_wm2 = as.character(g_active_wm2),
        active_wm2 = case_when(active_wm2 == "" ~ "0", TRUE ~ active_wm2),
        active_wm2 = str_extract(active_wm2, "\\d+") %>% parse_number(),
        active_leisure_hrs = case_when(
            (g_active_lei == "" & g_active_wm2 == "") ~ NA, 
            TRUE ~ active_wm2 + 60*active_wh2)/60,
        bmi = ee_wgt_0/(ee_heightcm/100)^2,
        waist_hip_ratio = ee_waistc_0/ee_hipc_0,
        tobacco_use = case_when(g_tobacco %in% c(
            "Formerly used tobacco products", 
            "Stopped after the stroke occurred", 
            "Currently uses tobacco products") ~ "Yes",
            g_tobacco == "Never used tobacco products" ~ "No"),
        egfr = str_extract(ff_egfr_0, "\\d+") %>% as.numeric(),
        egfr_cat = case_when(
            egfr < 60 ~ "<60",
            egfr >= 60  & egfr <= 89~ "60 to 89",
            egfr > 89 ~ ">89"
            ) %>% factor(levels = c("<60", "60 to 89", ">89")),
        ranking = str_extract(x_ranking, "\\d+") %>% as.numeric(),
        barthels_feed = ifelse(y_feeding == "O= unable", "0", as.character(y_feeding)) %>% 
            str_extract("^\\d+") %>% 
            as.numeric(), 
        barthels_bathing = str_extract(y_bathing, "^\\d+") %>% as.numeric(),
        barthels_grooming = str_extract(y_grooming, "^\\d+") %>% as.numeric(),
        barthels_dressing = str_extract(y_dressing, "^\\d+") %>% as.numeric(),
        barthels_bowels = str_extract(y_bowels, "^\\d+") %>% as.numeric(),
        barthels_bladder = str_extract(y_bladder, "^\\d+") %>% as.numeric(),
        barthels_toilet = str_extract(y_toilet, "^\\d+") %>% as.numeric(),
        barthels_bedtochair = str_extract(y_bedtochair, "^\\d+") %>% as.numeric(),
        barthels_mobility = str_extract(y_mobility, "^\\d+") %>% as.numeric(),
        barthels_stairs = str_extract(y_stairs, "^\\d+") %>% as.numeric(),
        barthels_index = barthels_bathing + barthels_grooming + barthels_dressing + 
            barthels_bowels + barthels_bladder + barthels_toilet + 
            barthels_bedtochair + barthels_mobility + barthels_stairs,
        nihss_levcon = str_extract(z_levcon, "^\\d+") %>% as.numeric(),
        nihss_loc_quest = str_extract(z_loc_quest, "^\\d+") %>% as.numeric(), 
        nihss_loc_comm = str_extract(z_loc_comm, "^\\d+") %>% as.numeric(),
        nihss_bestgaze = str_extract(z_bestgaze, "^\\d+") %>% as.numeric(),
        nihss_vfield = str_extract(z_vfield, "^\\d+") %>% as.numeric(),
        nihss_facparesis = str_extract(z_facparesis, "^\\d+") %>% as.numeric(),
        nihss_motor_armr = str_extract(z_motor_armr, "^\\d+") %>% as.numeric(),
        nihss_motor_arml = str_extract(z_motor_arml, "^\\d+") %>% as.numeric(),
        nihss_motor_legr = str_extract(z_motor_legr, "^\\d+") %>% as.numeric(),
        nihss_motor_legl = str_extract(z_motor_legl, "^\\d+") %>% as.numeric(),
        nihss_limbataxia = str_extract(z_limbataxia, "^\\d+") %>% as.numeric(),
        nihss_pinprick = str_extract(z_pinprick, "^\\d+") %>% as.numeric(),
        nihss_bestlang = str_extract(z_bestlang, "^\\d+") %>% as.numeric(),
        nihss_dysarthria = str_extract(z_dysarthria, "^\\d+") %>% as.numeric(),
        nihss_extinction = str_extract(z_extinction, "^\\d+") %>% as.numeric(),
        nihss_scale = nihss_levcon + nihss_loc_quest + nihss_loc_comm + 
            nihss_bestgaze + nihss_vfield + nihss_facparesis + nihss_motor_armr + 
            nihss_motor_arml + nihss_motor_legr + nihss_motor_legl + 
            nihss_limbataxia + nihss_pinprick + nihss_bestlang + 
            nihss_dysarthria + nihss_extinction,
        mobility = case_when(
            str_detect(p_mobility , "
                       I have no problems in walking about") ~ "1",
            str_detect(p_mobility , 
                       "I have some problems in walking about") ~ "2",
            str_detect(p_mobility , "I am confined to bed") ~ "3")%>% 
            as.numeric(),
        selfcare = case_when(
            str_detect(p_selfcare , "I have no problems with self-care") ~ "1",
            str_detect(p_selfcare , "I have some problems with washing or") ~ "2",
            str_detect(p_selfcare , "I am unable to wash or dress myself") ~ "3")%>% 
            as.numeric(),
        usual_act = case_when(
            str_detect(p_usual_act , "I have no problems with performing my") ~ "1",
            str_detect(p_usual_act , "I have some problems with performing") ~ "2",
            str_detect(p_usual_act , "I am unable to perform my usual activitie") ~ "3")%>% 
            as.numeric(),
        pain_disc = case_when(
            str_detect(p_pain_disc , "I have no pain or discomfort") ~ "1",
            str_detect(p_pain_disc , "I have moderate pain or discomfort") ~ "2",
            str_detect(p_pain_disc , "I have extreme pain or discomfort") ~ "3") %>% 
            as.numeric(),
        anxiety = case_when(
            str_detect(p_anxiety, "I am not anxious or depressed") ~ "1",
            str_detect(p_anxiety, "I am moderately anxious or depressed") ~ "2",
            str_detect(p_anxiety, "I am extremely anxious or depressed") ~ "3")%>% 
            as.numeric(),
        eq_5d = mobility + selfcare + usual_act + pain_disc + anxiety,
        vas = p_health,
        vas_cat = ifelse(vas < median(vas, na.rm=TRUE), "Below Median", "Median & above"),
        aa_bp_115_75_corrrect = ifelse(aa_bp_115_75 == "Normal", "Yes", "No"),
        aa_bp160_100_correct = ifelse(aa_bp160_100 == "High", "Yes", "No"),
        aa_hptlasts_correct = ifelse(aa_hptlasts == "The Rest of their Life", "Yes", "No"),
        aa_hptmeds_correct = ifelse(aa_hptmeds == "Everyday", "Yes", "No"),
        aa_losewght_correct = ifelse(aa_losewght == "Go Down", "Yes", "No"),
        aa_eatsalt_correct = ifelse(aa_eatsalt == "Go Down", "Yes", "No"),
        aa_hrtattack_correct = ifelse(aa_hrtattack == "Yes", "Yes", "No"),
        aa_cancer_correct = ifelse(aa_cancer == "No", "Yes", "No"),
        aa_stroke_correct = ifelse(aa_stroke == "Yes", "Yes", "No"),
        aa_kidneyp_correct = ifelse(aa_kidneyp == "Yes", "Yes", "No"),
        aa_highrisk_correct = ifelse(aa_highrisk == "Yes", "Yes", "No"),
        aa_headache_correct = ifelse(aa_headache == "No", "Yes", "No"),
        aa_feelgood_correct = ifelse(aa_feelgood == "Never", "Yes", "No"),
        aa_strokeris_correct = ifelse(
            aa_strokeris == "The Rest of Their Life", "Yes", "No"),
        aa_hkq = (aa_bp_115_75_corrrect == "Yes") + (aa_bp160_100_correct == "Yes") +
            (aa_hptlasts_correct =="Yes") + (aa_hptmeds_correct == "Yes") + 
            (aa_losewght_correct == "Yes") + (aa_eatsalt_correct == "Yes") + 
            (aa_hrtattack_correct == "Yes") + (aa_cancer_correct == "Yes") + 
            (aa_stroke_correct == "Yes") + (aa_kidneyp_correct == "Yes") + 
            (aa_highrisk_correct == "Yes") + (aa_headache_correct =="Yes") + 
            (aa_feelgood_correct =="Yes") + (aa_strokeris_correct == "Yes"),
        aa_hkq_cat = ifelse(
            aa_hkq <= median(aa_hkq, na.rm=T), "Median & below", "Above Median"
            ) %>% factor(levels = c("Median & below", "Above Median")),
        male = case_when(a_gender == "Male" ~ "Yes", a_gender == "Female" ~ "No"),
        active_leisure_hrs = ifelse(
            active_leisure_hrs > 16,
            median(active_leisure_hrs, na.rm=TRUE), active_leisure_hrs),
        active_work_hrs = ifelse(
            active_work_hrs > 16,
            median(active_work_hrs, na.rm=TRUE), active_work_hrs),
        bmi = ifelse(bmi >70, median(bmi, na.rm = TRUE), bmi),
        ee_dbp_0 = ifelse(ee_dbp_0 <1, ee_dbp_0*100, ee_dbp_0),
        ee_waistc_0 =ifelse(
            ee_waistc_0 < 10, median(ee_waistc_0, na.rm=TRUE), ee_waistc_0),
        ff_creat_0 = ifelse(ff_creat_0 < 5, ff_creat_0*10, ff_creat_0),
        hosp_cat = case_when(
            datagrp %in% c("CCTH SITE", "KATH SITE", "KORLE-BU SITE") ~ "Tertiary",
            datagrp %in% c(
                "KUMASI SOUTH HOSPITAL", "AGOGO SITE", "KNUST SITE") ~ "Secondary",
            datagrp %in% c(
                "ANKAASE SITE", "MANHYIA GOVT HOSPITAL", "KWADASO SITE", "TAFO SITE") ~ "Primary"
            ),
        across(
            c(s_g10worth, s_g11help, s_g12depress, s_g13hope, s_h2control, 
              s_h3ability, s_h4things, s_h5overcome), 
            .fns = ~fct_na_level_to_value(.x, "Refused")),
        across(
            c(s_g10worth, s_g11help, s_g12depress, s_g13hope, s_h2control, 
              s_h3ability, s_h4things, s_h5overcome), 
            .fns = ~factor(.x),
            .names = "{.col}_unclassed"),
        across(
            c(s_g10worth_unclassed, s_g11help_unclassed, s_g12depress_unclassed, 
              s_g13hope_unclassed, s_h2control_unclassed, s_h3ability_unclassed, 
              s_h4things_unclassed, s_h5overcome_unclassed), 
            .fns = ~unclass(.x)),
        s_h3ability_unclassed = 6 - s_h3ability_unclassed,
        s_h4things_unclassed  = 6 - s_h4things_unclassed,
        across(
            c(s_g10worth_unclassed, s_g11help_unclassed, s_g12depress_unclassed, 
              s_g13hope_unclassed, s_h2control_unclassed, s_h3ability_unclassed, 
              s_h4things_unclassed, s_h5overcome_unclassed), 
            .fns = ~.x-3),
        perceived_stress = s_g10worth_unclassed + s_g11help_unclassed + 
            s_g12depress_unclassed + s_g13hope_unclassed + s_h2control_unclassed + 
            s_h3ability_unclassed + s_h4things_unclassed + s_h5overcome_unclassed,
        perceived_stress_cat = ifelse(
            perceived_stress <= median(perceived_stress, na.rm=TRUE), "Low", "High") %>% 
            factor(levels = c("Low", "High")),
        hamd_cat = case_when(
            r_hamd_total <= 8 ~ "Normal",
            r_hamd_total < 14 ~ "Mild Depression",
            r_hamd_total < 19 ~ "Moderate Depression",
            r_hamd_total < 23 ~ "Severe Depression",
            r_hamd_total >= 23 ~ "Very Severe Depression"
            ) %>% 
            factor(
                levels = c(
                    "Normal", "Mild Depression", "Moderate Depression", 
                    "Severe Depression", "Very Severe Depression"),
                ordered = TRUE),
        across(
            c(l_forget, l_decide, l_salty, l_shake, l_fasfood, l_appoint, 
            l_missched, l_prescrip, l_runout, l_skipmed, l_feelbet,
            l_feelsick, l_someone, l_careless), 
            .fns = ~factor(.x),
            .names = "{.col}_unclassed"),
        across(
            c(l_forget_unclassed, l_decide_unclassed, l_salty_unclassed, 
              l_shake_unclassed, l_fasfood_unclassed, l_appoint_unclassed, 
            l_missched_unclassed, l_prescrip_unclassed, l_runout_unclassed, 
            l_skipmed_unclassed, l_feelbet_unclassed, l_feelsick_unclassed, 
            l_someone_unclassed, l_careless_unclassed), 
            .fns = ~unclass(.x)),
        hillbone = (l_forget_unclassed + l_decide_unclassed + 
                        l_salty_unclassed + l_shake_unclassed + 
                        l_fasfood_unclassed + l_appoint_unclassed + 
                        l_missched_unclassed + l_prescrip_unclassed + 
                        l_runout_unclassed + l_skipmed_unclassed + 
                        l_feelbet_unclassed + l_feelsick_unclassed + 
                        l_someone_unclassed + l_careless_unclassed) %>%  
            as.numeric(),
        hillbone_cat = ifelse(
            hillbone >= median(hillbone, na.rm=TRUE), "High", "Low"),
        hamd_cat_2 = fct_recode(
            hamd_cat, 
            Moderate_Severe = "Moderate Depression",
            Moderate_Severe = "Severe Depression",
            Moderate_Severe = "Very Severe Depression"
            ) %>% factor(ordered = F),
        a_poccup3 = a_poccup2 %>% 
            fct_recode(
            Skilled = "Highly Skilled academic professional",
            Skilled = "Skilled Worker",
            Skilled = "Semi-Skilled Worker",
            Manual = "Manual Worker",
            Unemployed = "Home maker",
            Unemployed = "Unemployed( Unable to work)",
            Unemployed = "Unemployed ( able to work)",
            Retired = "Retired",
            Others = "Non-paid Worker",
            Others = "Home maker"
            ) %>% fct_na_level_to_value("Student"),
        morisky_4_score = (unclass(k_forget) + unclass(k_miss) + 
            unclass(k_fine) + unclass(k_doctor)),
        morisky_4_raw = factor(morisky_4_score, ordered = TRUE),
        morisky_4_cat = case_when(
            morisky_4_score < 8 ~ "Low",
            morisky_4_score == 8 ~ "High"
            ),
        across(
            c(o_headache, o_dizziness, o_thirst, o_edema, o_coldhands, 
              o_flush, o_fatigue, o_cough, o_palpitat, o_nausea, 
              o_constipat, o_drowsy, o_insomnia, o_dreams, o_frequent, 
              o_darker, o_depression, o_anxiety, o_libido, o_impotence),
            .fns = ~(.x == "Checked"),
            .names = "{.col}_new"),
        side_effects = o_headache_new + o_dizziness_new + o_thirst_new + 
            o_edema_new + o_coldhands_new + o_flush_new + o_fatigue_new + 
            o_cough_new  + o_palpitat_new + o_nausea_new  + 
            o_constipat_new + o_drowsy_new + o_insomnia_new + 
            o_dreams_new + o_frequent_new + o_darker_new + 
            o_depression_new + o_anxiety_new + o_libido_new + 
            o_impotence_new, 
        mrs_cat = case_when(
          ranking <= 2 ~ "0 to 2", 
          ranking > 2 ~ "3-6"), 
        barthel_cat = case_when(
            barthels_index < 90 ~ "< 90", 
            barthels_index >= 90 ~ ">= 90"))
Warning: There were 8 warnings in `mutate()`.
The first warning was:
ℹ In argument: `egfr0 = parse_number(ff_egfr_0)`.
Caused by warning:
! 2 parsing failures.
 row col expected actual
1276  -- a number    N/A
1336  -- a number    N/A
ℹ Run `dplyr::last_dplyr_warnings()` to see the 7 remaining warnings.
Show the code
df_pings2_bp <- 
    df_pings2_bp %>%
    labelled::set_variable_labels(
            female = "Gender, female",
            stroke_type = "Type of Stroke",
            nihss_scale ="Stroke Severity (NIHSS)",
            ranking = "Modified Ranking Score",
            dm = "Diabetes Mellitus",
            sbp = "Systolic Blood Pressure",
            dbp = "Diastolic Blood Pressure",
            hpt = "Hypertesion",
            hyperlipidemia = "Hyperlipidemia", 
            tobacco_use = "Cigarette smoking", 
            obese_overwgt  = "Overweight & obesity",
            alcohol_curr = "Alcohol use, current",
            alcohol_ever = "Alcohol use, ever",
            acei = "ACE-Inhibitors",
            arb = "ARB",
            bb = "B-blockers",
            ccb = "Calcium channel blocker",
            diuretics = "Diuretics", 
            female = "Gender, female",
            income2 = "Income level", 
            educ2 = "Educational Level",
            agecat = "Age in years",
            h_alpha_md = "Alpha Methyl Dopa",
            h_alpha_ab = "Alpha Adrenergic Blockers",
            h_antiplt = "Antiplatelets",
            h_statins = "Statins",
            morisky_4_item = "Morisky (4 Items)",
            hillbone = "Total Hillbone Score",
            aa_hkq = "Health Literacy in HPT/stroke",
            eq_5d = "Health Related QoL",
            p_health = "Health Related QoL", 
            hpt_self_care = "Hypertension Self Care", 
            sbp_less_140_itt = "SBP < 140 mmHg at month 12 (ITT)",
            sbp_less_140_pp = "SBP < 140 mmHg at month 12 (PP)", 
            bp_less_140_90_itt = "BP < 140/90 mmHg at month 12 (ITT)", 
            bp_less_140_90_pp = "BP < 140/90 mmHg at month 12 (PP)",
            sbp_diff = "Change in SBP(month 12 from baseline - PP)",
            dbp_diff = "Change in DBP(month 12 from baseline - PP)",
            sbp_less_130_itt = "SBP < 130 mmHg at month 12 (ITT)",
            sbp_less_130_pp = "SBP < 130 mmHg at month 12 (PP)",
            sbp_less_135_itt = "SBP < 135 mmHg at month 12 (ITT)",
            sbp_less_135_pp = "SBP < 135 mmHg at month 12 (PP)",
            max_review = "Follow-up period",
            review_no = "Review number",
            completed_study = "Completed Study",
            sbp_2 = "Systolic Blood Pressure (ITT)", 
            dbp_2 = "Systolic Blood Pressure (ITT)", 
            sbp_baseline = "Systolic Blood Pressure (Baseline)",
            dbp_baseline = "Diastolic Blood Pressure (Baseline)",
            sbp_diff_itt ="Change in SBP(month 12 from baseline)- ITT",
            dbp_diff_itt = "Change in DBP(month 12 from baseline) - ITT", 
            educ = "Educational Status",
        a_livingsit2 = "Living Status",
        income = "Income in GHC",
        maristat = "Marital Status",
        dm = "Diabetes Mellitus",
        hyperlipidemia = "Hyperlipidemia",
        active_leisure_hrs = "Hours active during leisure",
        active_work_hrs = "Hours active during work",
        bmi = "Body Mass Index",
        waist_hip_ratio = "Waist-Hip Ratio",
        tobacco_use = "History of tobacco use",
#        who_risk_cat_2 = "WHO Risk Category",
        egfr_cat = "eGFR",
        ranking = "Modified Ranking Score",
        barthels_index = "Barthels Index",
        nihss_scale = "NIH Stroke Scale",
        mobility = "Mobility", 
        selfcare = "Self Care",
        usual_act = "Usual Activity", 
        pain_disc = "Pain Discomfort", 
        anxiety = "Anxiety", 
        aa_bp_115_75_corrrect = 
            "If someones blood pressure is 115/75. it is",
        aa_bp160_100_correct = 
            "If someones blood pressure is 160/100. It is",
        aa_hptlasts_correct = 
            "Once someone has high blood pressure, it usually lasts for",
        aa_hptmeds_correct = 
            "People with high blood pressure should take their medicine",
        aa_losewght_correct = 
            "Losing weight usually makes blood pressure",
        aa_eatsalt_correct = "Eating less salt usually makes blood pressure",
        aa_hrtattack_correct = "High blood pressure can cause heart attacks",
        aa_cancer_correct = "High blood pressure can cause cancer",
        aa_kidneyp_correct = 
            "High blood pressure can cause can kidney problems",
        aa_stroke_correct = "High blood pressure can cause strokes",
        aa_highrisk_correct = 
            "Someone who has had a stroke is at higher risk of having another",
        aa_headache_correct = 
            "If someone is not having headaches they can stop taking medications",
        aa_feelgood_correct = 
            "If someone is feeling good it is ok to miss doses of medication",
        aa_strokeris_correct = 
            "Once someone has had a stroke, they will be at risk for stroke for",
        aa_hkq = "Total HKQ Score",
        aa_hkq_cat = "Categorised Total HKQ Score",
        male = "Male sex",
        ee_dbp_0 = "Diastolic Blood Pressure",
        ee_waistc_0 = "Waist circumference (cm)",
        ff_creat_0 = "Serum Creatinine (mmol/L)",
        hosp_cat = "Health institution category",
        vas_cat = "Categorised VAS",
        hamd_cat = "Depression (HAMD)",
        hamd_cat_2 = "Depression (HAMD)",
        hillbone = "Total Hillbone Score",
        hillbone_cat = "Categorised Hillbone Score",
        morisky_4_cat = "Morisky 4 Items Categorised",
        o_headache_new = "Headache",
        o_dizziness_new ="Dizziness",
        o_thirst_new = "Thirst/ dry mouth",
        o_edema_new = "Edema",
        o_coldhands_new = "Cold hands or feet",
        o_flush_new = "Flush", 
        o_fatigue_new = "Fatigue",
        o_cough_new = "Cough",
        o_palpitat_new = "Palpitations",
        o_nausea_new = "Nausea/ indigestion/ vomiting",
        o_constipat_new = "Change in bowel habit",
        o_drowsy_new = "Drowsiness",
        o_insomnia_new = "Insomnia",
        o_dreams_new = "Dreams/Nightmares",
        o_frequent_new = "Frequency of micturition",  
        o_darker_new = "Dark Urine/Oliguria", 
        o_depression_new = "Depression", 
        o_anxiety_new = "Anxiety",
        o_libido_new = "Decreased libido",
        o_impotence_new = "Erectile Dysfunction",
        g_alcohol = "Alcohol use",
        side_effects = "No. of Side Effects")

Table 0 - Summary of all variables

Show the code
gtsummary::theme_gtsummary_compact()
Setting theme "Compact"
Show the code
gtsummary::theme_gtsummary_eda()
Setting theme "Exploratory Data Analysis"
Show the code
df_pings2_bp %>% 
  select(
    mrs_cat, a_agebase, a_gender, a_domicile, educ, a_poccup3, 
    maristat, income, hosp_cat, stroke_type, d_stroke_ct, d_stroke_loc, 
    nihss_scale, bmi, obese_overwgt, tobacco_use, alcohol_ever, dm, 
    c_hyperlip, c_afib, barthels_index, barthel_cat) %>% 
  gtsummary::tbl_summary() %>% 
  gtsummary::bold_labels()
Characteristic N = 3,0001
mrs_cat
    0 to 2 1,893 (70.0%)
    3-6 813 (30.0%)
    Unknown 294
Age in years
    Median (Q1, Q3) 58 (51, 67)
    Mean (SD) 58 (11)
    Min, Max 29, 88
    Unknown 2,500
Gender
    Male 281 (56.2%)
    Female 219 (43.8%)
    Unknown 2,500
Domicile
    Rural 33 (6.60%)
    Semi-Urban 166 (33.2%)
    Urban 301 (60.2%)
    Unknown 2,500
Educational Status
    None 49 (9.80%)
    Primary 203 (40.6%)
    Secondary 165 (33.0%)
    Tertiary 83 (16.6%)
    Unknown 2,500
Primary Occupation
    Skilled 237 (47.4%)
    Manual 108 (21.6%)
    Others 20 (4.00%)
    Retired 59 (11.8%)
    Unemployed 76 (15.2%)
    Unknown 2,500
Marital Status
    Currently Married 333 (66.6%)
    Previously Married 144 (28.8%)
    Never Married 23 (4.60%)
    Unknown 2,500
Income in GHC
    0-100 174 (35.0%)
    101-250 150 (30.2%)
    251-500 109 (21.9%)
    >500 64 (12.9%)
    Unknown 2,503
Health institution category
    Primary 817 (29.1%)
    Secondary 619 (22.0%)
    Tertiary 1,376 (48.9%)
    Unknown 188
Type of Stroke
    Hemorrhagic 103 (22.9%)
    Ischemic 342 (76.0%)
    Untyped 5 (1.11%)
    Unknown 2,550
Stroke Subtype ( with results of Brain CT scan)
    Ischaemic 313 (73.6%)
    Haemorrhage infarct 16 (3.76%)
    Haemorrhagic 84 (19.8%)
    Ischaemic and Haemorrhagic 12 (2.82%)
    Unknown 2,575
Stroke Location
    Anterior Circulation 281 (75.3%)
    Posterior Circulation 92 (24.7%)
    Unknown 2,627
Stroke Severity (NIHSS)
    Median (Q1, Q3) 1.0 (0.0, 5.0)
    Mean (SD) 3.2 (4.6)
    Min, Max 0.0, 28.0
    Unknown 396
Body Mass Index
    Median (Q1, Q3) 26.2 (22.7, 30.0)
    Mean (SD) 26.6 (5.5)
    Min, Max 11.4, 47.9
    Unknown 2,521
Overweight & obesity 258 (59.9%)
    Unknown 2,569
Cigarette smoking 45 (9.26%)
    Unknown 2,514
Alcohol use, ever 205 (41.2%)
    Unknown 2,502
Diabetes Mellitus 169 (5.63%)
Hyperlipidemia
    Yes 41 (8.22%)
    No 437 (87.6%)
    Dont Know 21 (4.21%)
    Unknown 2,501
Atrial Fibrillation
    Yes 1 (0.20%)
    No 476 (95.4%)
    Dont Know 22 (4.41%)
    Unknown 2,501
Barthels Index
    Median (Q1, Q3) 90 (60, 90)
    Mean (SD) 74 (24)
    Min, Max 0, 90
    Unknown 387
barthel_cat
    < 90 1,134 (43.4%)
    >= 90 1,479 (56.6%)
    Unknown 387
1 n (%)

Table 1A - Sociodemographic and clinical characteristics

Show the code
gtsummary::reset_gtsummary_theme()
gtsummary::theme_gtsummary_compact()
Setting theme "Compact"
Show the code
df_pings2_bp %>%
    filter(eventname == "Baseline") %>% 
    select(
        mrs_cat, a_agebase, a_gender, a_domicile, educ, a_poccup3, 
        maristat, income, hosp_cat, stroke_type, d_stroke_ct, d_stroke_loc, 
        nihss_scale, bmi, obese_overwgt, tobacco_use, alcohol_ever, dm,  
        c_hyperlip, c_afib, nihss_scale, barthels_index, barthel_cat) %>% 
    gtsummary::tbl_summary(
        by = mrs_cat,
        digits = gtsummary::all_categorical()~ c(0,1),
        statistic = gtsummary::all_categorical() ~ "{n} ({p})",
        missing_text = "Missing") %>% 
    gtsummary::bold_labels() %>% 
    gtsummary::modify_caption(
      "**Table 1A**: Sociodemographic and clinical characteristics") %>% 
    gtsummary::modify_spanning_header(
        gtsummary::all_stat_cols() ~ "**Modified Ranking Score**") %>% 
    gtsummary::add_overall()
3 missing rows in the "mrs_cat" column have been removed.
Table 1A: Sociodemographic and clinical characteristics
Characteristic Overall
N = 4971
Modified Ranking Score
0 to 2
N = 2961
3-6
N = 2011
Age in years 58 (51, 67) 57 (50, 65) 61 (53, 70)
Gender


    Male 281 (56.5) 168 (56.8) 113 (56.2)
    Female 216 (43.5) 128 (43.2) 88 (43.8)
Domicile


    Rural 33 (6.6) 22 (7.4) 11 (5.5)
    Semi-Urban 166 (33.4) 98 (33.1) 68 (33.8)
    Urban 298 (60.0) 176 (59.5) 122 (60.7)
Educational Status


    None 48 (9.7) 28 (9.5) 20 (10.0)
    Primary 202 (40.6) 114 (38.5) 88 (43.8)
    Secondary 164 (33.0) 100 (33.8) 64 (31.8)
    Tertiary 83 (16.7) 54 (18.2) 29 (14.4)
Primary Occupation


    Skilled 237 (47.7) 153 (51.7) 84 (41.8)
    Manual 107 (21.5) 70 (23.6) 37 (18.4)
    Others 20 (4.0) 13 (4.4) 7 (3.5)
    Retired 57 (11.5) 30 (10.1) 27 (13.4)
    Unemployed 76 (15.3) 30 (10.1) 46 (22.9)
Marital Status


    Currently Married 332 (66.8) 197 (66.6) 135 (67.2)
    Previously Married 142 (28.6) 83 (28.0) 59 (29.4)
    Never Married 23 (4.6) 16 (5.4) 7 (3.5)
Income in GHC


    0-100 174 (35.2) 88 (30.0) 86 (42.8)
    101-250 150 (30.4) 92 (31.4) 58 (28.9)
    251-500 108 (21.9) 65 (22.2) 43 (21.4)
    >500 62 (12.6) 48 (16.4) 14 (7.0)
    Missing 3 3 0
Health institution category


    Primary 148 (29.8) 80 (27.0) 68 (33.8)
    Secondary 116 (23.3) 68 (23.0) 48 (23.9)
    Tertiary 233 (46.9) 148 (50.0) 85 (42.3)
Type of Stroke


    Hemorrhagic 103 (23.0) 75 (28.1) 28 (15.6)
    Ischemic 339 (75.8) 191 (71.5) 148 (82.2)
    Untyped 5 (1.1) 1 (0.4) 4 (2.2)
    Missing 50 29 21
Stroke Subtype ( with results of Brain CT scan)


    Ischaemic 310 (73.5) 174 (69.0) 136 (80.0)
    Haemorrhage infarct 16 (3.8) 8 (3.2) 8 (4.7)
    Haemorrhagic 84 (19.9) 64 (25.4) 20 (11.8)
    Ischaemic and Haemorrhagic 12 (2.8) 6 (2.4) 6 (3.5)
    Missing 75 44 31
Stroke Location


    Anterior Circulation 279 (75.2) 170 (76.6) 109 (73.2)
    Posterior Circulation 92 (24.8) 52 (23.4) 40 (26.8)
    Missing 126 74 52
Stroke Severity (NIHSS) 3.0 (0.0, 8.0) 0.0 (0.0, 3.0) 7.0 (4.0, 12.0)
    Missing 10 6 4
Body Mass Index 26.2 (22.7, 30.0) 26.7 (23.2, 30.5) 25.4 (22.2, 28.4)
    Missing 18 6 12
Overweight & obesity 258 (59.9) 167 (65.0) 91 (52.3)
    Missing 66 39 27
Cigarette smoking 45 (9.3) 21 (7.4) 24 (12.1)
    Missing 13 11 2
Alcohol use, ever 204 (41.1) 126 (42.7) 78 (38.8)
    Missing 1 1 0
Diabetes Mellitus 166 (33.4) 93 (31.4) 73 (36.3)
Hyperlipidemia


    Yes 40 (8.1) 23 (7.8) 17 (8.5)
    No 435 (87.7) 261 (88.5) 174 (86.6)
    Dont Know 21 (4.2) 11 (3.7) 10 (5.0)
    Missing 1 1 0
Atrial Fibrillation


    Yes 1 (0.2) 1 (0.3) 0 (0.0)
    No 473 (95.4) 279 (94.6) 194 (96.5)
    Dont Know 22 (4.4) 15 (5.1) 7 (3.5)
    Missing 1 1 0
Barthels Index 80 (40, 90) 90 (80, 90) 40 (25, 65)
    Missing 13 9 4
barthel_cat


    < 90 284 (58.7) 98 (34.1) 186 (94.4)
    >= 90 200 (41.3) 189 (65.9) 11 (5.6)
    Missing 13 9 4
1 Median (Q1, Q3); n (%)

Table 1B

Show the code
df_pings2_bp %>% #select(barthel_index) %>% dlookr::describe()
   filter(eventname == "Baseline") %>%
    select(
        mrs_cat, a_agebase, a_gender, a_domicile, educ, a_poccup3, 
        maristat, income, hosp_cat, stroke_type, d_stroke_ct, d_stroke_loc, 
        nihss_scale, bmi, obese_overwgt, tobacco_use, alcohol_ever, dm,  
        c_hyperlip, c_afib,  barthel_cat, nihss_scale) %>% 
    gtsummary::tbl_summary(
        by = barthel_cat,
        digits = gtsummary::all_categorical()~ c(0,1),
        statistic = gtsummary::all_categorical() ~ "{n} ({p})",
        missing_text = "Missing") %>% 
    gtsummary::bold_labels() %>% 
    gtsummary::modify_caption(
      "**Table 1B**: Sociodemographic and clinical characteristics") %>% 
    gtsummary::modify_spanning_header(
        gtsummary::all_stat_cols() ~ "**Barthel's Index**") %>% 
    gtsummary::add_overall()
16 missing rows in the "barthel_cat" column have been removed.
Table 1B: Sociodemographic and clinical characteristics
Characteristic Overall
N = 4841
Barthel’s Index
< 90
N = 2841
>= 90
N = 2001
mrs_cat


    0 to 2 287 (59.3) 98 (34.5) 189 (94.5)
    3-6 197 (40.7) 186 (65.5) 11 (5.5)
Age in years 58 (51, 67) 60 (53, 69) 57 (48, 65)
Gender


    Male 270 (55.8) 159 (56.0) 111 (55.5)
    Female 214 (44.2) 125 (44.0) 89 (44.5)
Domicile


    Rural 33 (6.8) 24 (8.5) 9 (4.5)
    Semi-Urban 162 (33.5) 89 (31.3) 73 (36.5)
    Urban 289 (59.7) 171 (60.2) 118 (59.0)
Educational Status


    None 48 (9.9) 30 (10.6) 18 (9.0)
    Primary 199 (41.1) 124 (43.7) 75 (37.5)
    Secondary 158 (32.6) 87 (30.6) 71 (35.5)
    Tertiary 79 (16.3) 43 (15.1) 36 (18.0)
Primary Occupation


    Skilled 230 (47.5) 122 (43.0) 108 (54.0)
    Manual 104 (21.5) 60 (21.1) 44 (22.0)
    Others 19 (3.9) 14 (4.9) 5 (2.5)
    Retired 56 (11.6) 33 (11.6) 23 (11.5)
    Unemployed 75 (15.5) 55 (19.4) 20 (10.0)
Marital Status


    Currently Married 324 (66.9) 190 (66.9) 134 (67.0)
    Previously Married 138 (28.5) 80 (28.2) 58 (29.0)
    Never Married 22 (4.5) 14 (4.9) 8 (4.0)
Income in GHC


    0-100 171 (35.6) 131 (46.1) 40 (20.3)
    101-250 147 (30.6) 72 (25.4) 75 (38.1)
    251-500 106 (22.0) 52 (18.3) 54 (27.4)
    >500 57 (11.9) 29 (10.2) 28 (14.2)
    Missing 3 0 3
Health institution category


    Primary 146 (30.2) 108 (38.0) 38 (19.0)
    Secondary 110 (22.7) 74 (26.1) 36 (18.0)
    Tertiary 228 (47.1) 102 (35.9) 126 (63.0)
Type of Stroke


    Hemorrhagic 99 (22.8) 44 (17.2) 55 (30.9)
    Ischemic 330 (76.0) 207 (80.9) 123 (69.1)
    Untyped 5 (1.2) 5 (2.0) 0 (0.0)
    Missing 50 28 22
Stroke Subtype ( with results of Brain CT scan)


    Ischaemic 302 (73.5) 190 (78.5) 112 (66.3)
    Haemorrhage infarct 16 (3.9) 12 (5.0) 4 (2.4)
    Haemorrhagic 82 (20.0) 32 (13.2) 50 (29.6)
    Ischaemic and Haemorrhagic 11 (2.7) 8 (3.3) 3 (1.8)
    Missing 73 42 31
Stroke Location


    Anterior Circulation 272 (75.6) 160 (74.1) 112 (77.8)
    Posterior Circulation 88 (24.4) 56 (25.9) 32 (22.2)
    Missing 124 68 56
Stroke Severity (NIHSS) 3.0 (0.0, 8.0) 6.0 (3.0, 10.0) 0.0 (0.0, 1.0)
    Missing 8 7 1
Body Mass Index 26.2 (22.7, 30.0) 25.9 (22.5, 29.3) 26.4 (23.3, 30.7)
    Missing 18 12 6
Overweight & obesity 250 (59.7) 146 (56.6) 104 (64.6)
    Missing 65 26 39
Cigarette smoking 42 (8.9) 28 (10.1) 14 (7.2)
    Missing 12 6 6
Alcohol use, ever 198 (41.0) 109 (38.4) 89 (44.7)
    Missing 1 0 1
Diabetes Mellitus 160 (33.1) 96 (33.8) 64 (32.0)
Hyperlipidemia


    Yes 38 (7.9) 20 (7.0) 18 (9.0)
    No 425 (88.0) 250 (88.0) 175 (87.9)
    Dont Know 20 (4.1) 14 (4.9) 6 (3.0)
    Missing 1 0 1
Atrial Fibrillation


    Yes 1 (0.2) 1 (0.4) 0 (0.0)
    No 461 (95.4) 273 (96.1) 188 (94.5)
    Dont Know 21 (4.3) 10 (3.5) 11 (5.5)
    Missing 1 0 1
1 n (%); Median (Q1, Q3)

Table 2

Show the code
df_pings2_bp %>% 
    select(mrs_cat, barthel_cat, eventname) %>% 
    gtsummary::tbl_summary(by = eventname) %>% 
    gtsummary::bold_labels()
Characteristic Baseline
N = 5001
Month 1
N = 5001
Month 3
N = 5001
Month 6
N = 5001
Month 9
N = 5001
Month12
N = 5001
mrs_cat





    0 to 2 296 (60%) 309 (65%) 306 (69%) 317 (74%) 323 (76%) 342 (79%)
    3-6 201 (40%) 169 (35%) 138 (31%) 111 (26%) 102 (24%) 92 (21%)
    Unknown 3 22 56 72 75 66
barthel_cat





    < 90 284 (59%) 215 (47%) 184 (43%) 162 (39%) 147 (36%) 142 (34%)
    >= 90 200 (41%) 246 (53%) 247 (57%) 251 (61%) 256 (64%) 279 (66%)
    Unknown 16 39 69 87 97 79
1 n (%)

Table 2A: Univariate

Show the code
df_for_table_2 <- 
  df_pings2_bp %>% 
  mutate(
    timep = case_when(
      eventname == "Baseline" ~ 0, 
      eventname == "Month 1" ~ 1, 
      eventname == "Month 3" ~ 3, 
      eventname == "Month 6" ~ 6,
      eventname == "Month 9" ~ 9, 
      eventname == "Month12" ~ 12)) %>% 
  select(
    a_gender, timep, pid, mrs_cat, a_domicile, a_agebase, 
    a_agebase, a_gender, a_domicile, educ, a_poccup3, 
    maristat, income2, hosp_cat, stroke_type, d_stroke_ct, 
    d_stroke_loc, nihss_scale, bmi, tobacco_use, g_alcohol, 
    dm, c_hyperlip, c_afib, nihss_scale) %>% 
  arrange(pid, timep) %>% 
  group_by(pid) %>% 
  fill(
    a_gender, a_domicile, a_agebase, educ, a_poccup3, maristat, income2, 
    hosp_cat, stroke_type, d_stroke_ct, d_stroke_loc, bmi, tobacco_use, 
    g_alcohol, c_hyperlip, c_afib, nihss_scale) %>% 
  ungroup() %>% 
  mutate(
    a_agebase = scale(a_agebase), 
    mrs_cat = factor(mrs_cat) %>% unclass(), 
    mrs_cat = mrs_cat -1, 
    timep = scale(timep, center = TRUE, scale = FALSE))


# Define your list of predictors
predictors <- 
  c("a_agebase", "a_gender", "a_domicile", "educ", "a_poccup3", 
    "maristat", "income2", "hosp_cat", "hosp_cat", "stroke_type", 
    "d_stroke_ct", "d_stroke_loc", "bmi", "tobacco_use", "g_alcohol", 
    "dm",  "c_hyperlip", "c_afib", "nihss_scale")

# Map over the predictors to create a list of univariate models



univariate_tables <- 
  predictors %>%
  map(
    function(var) {
      formula_str <- 
      as.formula(
        paste0("mrs_cat ~ timep*", var, "+ (1|pid)")) 
      lme4::glmer(
        formula_str, 
        family = binomial, 
        data = df_for_table_2,
        control = lme4::glmerControl(
          optimizer = "bobyqa", optCtrl = list(maxfun = 2e5))) %>% 
      gtsummary::tbl_regression(
        label = list(timep = "Time (Baseline Slope)"), exponentiate = TRUE) %>%
      gtsummary::modify_header(
        label = paste0("**Predictor: ", str_to_title(var), "**")) %>% 
        gtsummary::add_global_p()})

# Stack them into one table
final_uni_table <- 
  gtsummary::tbl_stack(univariate_tables, quiet = TRUE) %>%
  gtsummary::modify_caption(
    "**Table 1. Univariate Growth Trajectory Interactions**") %>%
  gtsummary::bold_p() %>% 
  gtsummary::bold_labels()


final_uni_table
Table 1. Univariate Growth Trajectory Interactions
Predictor: A_agebase OR 95% CI p-value
Time (Baseline Slope) 0.80 0.77, 0.84 <0.001
a_agebase 4.94 2.78, 8.77 <0.001
Time (Baseline Slope) * a_agebase 1.03 0.99, 1.08 0.13
Time (Baseline Slope) 0.75 0.70, 0.80 <0.001
a_gender

0.3
    Male
    Female 1.80 0.55, 5.92
Time (Baseline Slope) * a_gender

0.033
    Time (Baseline Slope) * Female 1.10 1.01, 1.20
Time (Baseline Slope) 0.84 0.73, 0.98 0.029
a_domicile

0.3
    Rural
    Semi-Urban 1.73 0.16, 19.1
    Urban 4.48 0.45, 44.4
Time (Baseline Slope) * a_domicile

0.2
    Time (Baseline Slope) * Semi-Urban 0.90 0.76, 1.07
    Time (Baseline Slope) * Urban 0.97 0.83, 1.14
Time (Baseline Slope) 0.83 0.71, 0.96 0.012
educ

0.4
    None
    Primary 0.57 0.04, 7.74
    Secondary 0.30 0.02, 4.09
    Tertiary 0.14 0.01, 2.21
Time (Baseline Slope) * educ

0.2
    Time (Baseline Slope) * Primary 0.93 0.80, 1.10
    Time (Baseline Slope) * Secondary 0.98 0.84, 1.15
    Time (Baseline Slope) * Tertiary 1.06 0.89, 1.26
Time (Baseline Slope) 0.81 0.76, 0.86 <0.001
a_poccup3

<0.001
    Skilled
    Manual 0.70 0.17, 2.92
    Others 1.41 0.07, 30.3
    Retired 12.3 0.79, 191
    Unemployed 126 10.9, 1,460
Time (Baseline Slope) * a_poccup3

0.4
    Time (Baseline Slope) * Manual 0.98 0.87, 1.11
    Time (Baseline Slope) * Others 1.11 0.86, 1.44
    Time (Baseline Slope) * Retired 1.04 0.91, 1.19
    Time (Baseline Slope) * Unemployed 0.92 0.82, 1.03
Time (Baseline Slope) 0.76 0.71, 0.80 <0.001
maristat

0.7
    Currently Married
    Previously Married 1.76 0.45, 6.81
    Never Married 1.67 0.11, 25.1
Time (Baseline Slope) * maristat

0.016
    Time (Baseline Slope) * Previously Married 1.08 0.98, 1.19
    Time (Baseline Slope) * Never Married 1.28 1.07, 1.54
Time (Baseline Slope) 0.80 0.76, 0.83 <0.001
income2

<0.001
    <500 GHc
    >= 500 GHc 0.08 0.02, 0.36
Time (Baseline Slope) * income2

0.3
    Time (Baseline Slope) * >= 500 GHc 1.08 0.94, 1.23
Time (Baseline Slope) 0.81 0.81, 0.81 <0.001
hosp_cat

<0.001
    Primary
    Secondary 0.19 0.19, 0.19
    Tertiary 0.05 0.05, 0.05
Time (Baseline Slope) * hosp_cat

<0.001
    Time (Baseline Slope) * Secondary 1.02 1.02, 1.02
    Time (Baseline Slope) * Tertiary 0.97 0.97, 0.97
Time (Baseline Slope) 0.81 0.81, 0.81 <0.001
hosp_cat

<0.001
    Primary
    Secondary 0.19 0.19, 0.19
    Tertiary 0.05 0.05, 0.05
Time (Baseline Slope) * hosp_cat

<0.001
    Time (Baseline Slope) * Secondary 1.02 1.02, 1.02
    Time (Baseline Slope) * Tertiary 0.97 0.97, 0.97
Time (Baseline Slope) 0.76 0.67, 0.86 <0.001
stroke_type

<0.001
    Hemorrhagic
    Ischemic 32.5 8.98, 117
    Untyped 417 0.95, 184,069
Time (Baseline Slope) * stroke_type

0.2
    Time (Baseline Slope) * Ischemic 1.10 0.96, 1.25
    Time (Baseline Slope) * Untyped 0.74 0.35, 1.56
Time (Baseline Slope) 0.83 0.79, 0.86 <0.001
d_stroke_ct

<0.001
    Ischaemic
    Haemorrhage infarct 0.92 0.02, 36.2
    Haemorrhagic 0.02 0.00, 0.08
    Ischaemic and Haemorrhagic 1.24 0.02, 93.0
Time (Baseline Slope) * d_stroke_ct

0.4
    Time (Baseline Slope) * Haemorrhage infarct 0.86 0.65, 1.13
    Time (Baseline Slope) * Haemorrhagic 0.89 0.76, 1.04
    Time (Baseline Slope) * Ischaemic and Haemorrhagic 0.96 0.69, 1.33
Time (Baseline Slope) 0.83 0.79, 0.87 <0.001
d_stroke_loc

0.9
    Anterior Circulation
    Posterior Circulation 1.12 0.21, 5.96
Time (Baseline Slope) * d_stroke_loc

0.2
    Time (Baseline Slope) * Posterior Circulation 0.93 0.83, 1.04
Time (Baseline Slope) 0.79 0.64, 0.96 0.018
bmi 0.84 0.75, 0.93 <0.001
Time (Baseline Slope) * bmi 1.00 0.99, 1.01 0.8
Time (Baseline Slope) 0.79 0.75, 0.83 <0.001
tobacco_use

0.4
    No
    Yes 4.06 0.18, 92.9
Time (Baseline Slope) * tobacco_use

0.7
    Time (Baseline Slope) * Yes 0.98 0.85, 1.12
Time (Baseline Slope) 0.80 0.77, 0.84 <0.001
g_alcohol

0.6
    Never used alcohol
    Currently uses alcohol 0.71 0.17, 2.96
    Past 12 months 0.04 0.00, 1.82
    Past 30 days 0.37 0.01, 17.4
    Formerly used alcohol 0.80 0.27, 2.35
    Stopped after the stroke occured 2.00 0.30, 13.3
Time (Baseline Slope) * g_alcohol

0.4
    Time (Baseline Slope) * Currently uses alcohol 1.08 0.97, 1.20
    Time (Baseline Slope) * Past 12 months 0.71 0.40, 1.26
    Time (Baseline Slope) * Past 30 days 1.22 0.89, 1.66
    Time (Baseline Slope) * Formerly used alcohol 1.04 0.95, 1.15
    Time (Baseline Slope) * Stopped after the stroke occured 1.05 0.90, 1.23
Time (Baseline Slope) 0.79 0.75, 0.82 <0.001
dm

0.6
    No
    Yes 1.24 0.60, 2.57
Time (Baseline Slope) 0.86 0.76, 0.98 0.021
c_hyperlip

>0.9
    Yes
    No 0.88 0.11, 7.06
    Dont Know 1.45 0.03, 68.1
Time (Baseline Slope) * c_hyperlip

0.3
    Time (Baseline Slope) * No 0.90 0.79, 1.03
    Time (Baseline Slope) * Dont Know 0.88 0.65, 1.21
Time (Baseline Slope) 0.43 0.00, Inf >0.9
c_afib

0.5
    Yes
    No 1,420,202 0.00, Inf
    Dont Know 186,181 0.00, Inf
Time (Baseline Slope) * c_afib

>0.9
    Time (Baseline Slope) * No 1.88 0.00, Inf
    Time (Baseline Slope) * Dont Know 1.82 0.00, Inf
Time (Baseline Slope) 0.88 0.88, 0.88 <0.001
nihss_scale 1.93 1.93, 1.93 <0.001
Time (Baseline Slope) * nihss_scale 1.01 1.01, 1.01 <0.001
Abbreviations: CI = Confidence Interval, OR = Odds Ratio

Table 2A - Multivariate (Using dummy variables)

Show the code
df_for_table_2 <- 
  df_pings2_bp %>%
  mutate(
    timep = case_when(
      eventname == "Baseline" ~ 0, 
      eventname == "Month 1" ~ 1, 
      eventname == "Month 3" ~ 3, 
      eventname == "Month 6" ~ 6,
      eventname == "Month 9" ~ 9, 
      eventname == "Month12" ~ 12)) %>% 
  select(
    a_gender, timep, pid, mrs_cat, a_domicile, a_agebase, 
    a_agebase, a_gender, a_domicile, educ, a_poccup3, 
    maristat, income, hosp_cat, stroke_type, d_stroke_ct, 
    d_stroke_loc, nihss_scale, bmi, tobacco_use, g_alcohol, 
    dm,  c_hyperlip, c_afib, nihss_scale) %>% 
  mutate(hosp_cat = factor(hosp_cat)) %>% 
  arrange(pid, timep) %>% 
  group_by(pid) %>% 
  fill(
    a_gender, a_domicile, a_agebase, educ, a_poccup3, 
    maristat, income, hosp_cat, stroke_type, d_stroke_ct, 
    d_stroke_loc, bmi, tobacco_use, g_alcohol, 
    c_hyperlip, c_afib, nihss_scale) %>% 
  ungroup() %>% 
  fastDummies::dummy_cols(select_columns = "hosp_cat") %>% 
  fastDummies::dummy_cols(select_columns = "maristat") %>% 
  mutate(
    a_agebase = scale(a_agebase), 
    mrs_cat = factor(mrs_cat) %>% unclass(), 
    mrs_cat = mrs_cat -1, 
    timep = scale(timep, center = TRUE, scale = FALSE))

mv_model <- 
  lme4::glmer(
    mrs_cat ~ timep * (
      a_agebase + a_gender + hosp_cat_Secondary + hosp_cat_Primary + 
        `maristat_Previously Married` + `maristat_Never Married` + nihss_scale) + 
      (1 | pid), 
    data = df_for_table_2, 
    family = binomial)

mv_table <- 
  tbl_regression(
    mv_model, 
    label = list(
      timep ~ "Time", 
      a_agebase ~ "Age"), 
    exponentiate = T) %>%
  add_global_p() %>%
  bold_labels() %>% 
  gtsummary::add_glance_table(include = c(nobs, sigma)) %>% 
  gtsummary::modify_header(label = "**Predictor**") %>%
  gtsummary::bold_p(t = 0.05) %>%
  gtsummary::italicize_levels()

mv_table
Predictor OR 95% CI p-value
Time 0.82 0.75, 0.89 <0.001
Age 1.93 1.32, 2.83 <0.001
a_gender

0.3
    Male
    Female 1.49 0.68, 3.26
hosp_cat_Secondary 0.17 0.06, 0.47 <0.001
hosp_cat_Primary 0.17 0.07, 0.42 <0.001
maristat_Previously Married 0.66 0.27, 1.59 0.4
maristat_Never Married 1.06 0.19, 5.83 >0.9
nihss_scale 2.05 1.88, 2.24 <0.001
Time * Age 1.02 0.97, 1.07 0.4
Time * a_gender

0.076
    Time * Female 1.09 0.99, 1.20
Time * hosp_cat_Secondary 1.01 0.88, 1.15 >0.9
Time * hosp_cat_Primary 0.95 0.85, 1.06 0.4
Time * maristat_Previously Married 0.95 0.85, 1.06 0.3
Time * maristat_Never Married 1.13 0.92, 1.40 0.3
Time * nihss_scale 1.02 1.01, 1.04 0.008
No. Obs. 2,694

Sigma 1.00

Abbreviations: CI = Confidence Interval, OR = Odds Ratio

Figure 1 - Ranking

Show the code
df_pings2_bp %>%
  mutate(
    timep = case_when(
      eventname == "Baseline" ~ 0, 
      eventname == "Month 1" ~ 1, 
      eventname == "Month 3" ~ 3, 
      eventname == "Month 6" ~ 6,
      eventname == "Month 9" ~ 9, 
      eventname == "Month12" ~ 12)) %>% 
  select(
    a_gender, timep, pid, mrs_cat, a_domicile, a_agebase, 
    a_agebase, a_gender, a_domicile, educ, a_poccup3, 
    maristat, income, hosp_cat, stroke_type, d_stroke_ct, 
    d_stroke_loc, nihss_scale, bmi, tobacco_use, g_alcohol, 
    dm,  c_hyperlip, c_afib, nihss_scale) %>% 
  mutate(hosp_cat = factor(hosp_cat)) %>% 
  arrange(pid, timep) %>% 
  group_by(pid) %>% 
  fill(
    a_gender, a_domicile, a_agebase, educ, a_poccup3, 
    maristat, income, hosp_cat, stroke_type, d_stroke_ct, 
    d_stroke_loc, bmi, tobacco_use, g_alcohol, 
    c_hyperlip, c_afib, nihss_scale) %>% 
  ungroup() %>% 
  fastDummies::dummy_cols(select_columns = "hosp_cat") %>% 
  fastDummies::dummy_cols(select_columns = "maristat") %>% 
  mutate(
    a_agebase = scale(a_agebase), 
    mrs_cat = factor(mrs_cat) %>% unclass(), 
    mrs_cat = mrs_cat -1) %>%
  filter(!is.na(hosp_cat)) %>% 
  select(mrs_cat, timep, hosp_cat) %>% 
  group_by(hosp_cat, timep) %>% 
    reframe(across(mrs_cat, ~epiDisplay::ci.binomial(.x))) %>% 
    unnest(mrs_cat) %>% 
  ggplot(
    aes(
      x = timep, 
      y = probability, 
      ymin = probability - se, 
      ymax = probability + se,
      group = hosp_cat, 
      color = hosp_cat, 
      linetype = hosp_cat))+
  geom_errorbar(
        position = position_dodge2(width = 0.4),
        width = 0.2) +    
    geom_point(
        position = position_dodge2(width = 0.2),
        size = 3)+
  geom_line()+
  labs(
    x = "Follow-Up period", 
    y = "Probability of high MRS",
    color = "Hospital Category",
    linetype = "Hospital Category")+
    theme_bw()+
  scale_x_continuous(
        breaks = c(0, 1, 3, 6, 9, 12),
        labels = c(
            "Baseline", "     Month 1","Month 3", 
            "Month 6", "Month 9", "Month 12"))+
    scale_y_continuous(
        breaks = seq(0.1, 0.6, 0.1),
        limits = c(0.1, 0.6))+
    scale_color_manual(
        breaks = c("Primary", "Secondary", "Tertiary"),
        values = c("#C952B9","dodgerblue", "#222222"))+
    theme(
        legend.position = "inside",
        legend.position.inside = c(0.9, 0.85),
        legend.background = element_rect(color = "black"),
        axis.text.y = ggtext::element_markdown(),
        axis.text = element_text(color = "black"),
        panel.grid = element_blank(),
        legend.spacing = unit(0, "pt"), 
        axis.text.x = element_text(size = 11),
        legend.margin = margin(t = 1, b = 2,r = 3, l = 3),
        legend.key.spacing = unit(0, "pt")
        )

Show the code
df_for_table_3 <- 
  df_pings2_bp %>% 
  mutate(
    timep = case_when(
      eventname == "Baseline" ~ 0, 
      eventname == "Month 1" ~ 1, 
      eventname == "Month 3" ~ 3, 
      eventname == "Month 6" ~ 6,
      eventname == "Month 9" ~ 9, 
      eventname == "Month12" ~ 12)) %>% 
  select(
    a_gender, timep, pid, barthel_cat, a_domicile, a_agebase, 
    a_agebase, a_gender, a_domicile, educ, a_poccup3, 
    maristat, income2, hosp_cat, stroke_type, d_stroke_ct, 
    d_stroke_loc, nihss_scale, bmi, tobacco_use, g_alcohol, 
    dm, c_hyperlip, c_afib, nihss_scale) %>% 
  arrange(pid, timep) %>% 
  group_by(pid) %>% 
  fill(
    a_gender, a_domicile, a_agebase, educ, a_poccup3, maristat, income2, 
    hosp_cat, stroke_type, d_stroke_ct, d_stroke_loc, bmi, tobacco_use, 
    g_alcohol, c_hyperlip, c_afib, nihss_scale) %>% 
  ungroup() %>% 
  mutate(
    a_agebase = scale(a_agebase), 
    barthel_cat = factor(barthel_cat) %>% unclass(), 
    barthel_cat = barthel_cat -1, 
    timep = scale(timep, center = TRUE, scale = FALSE))


# Define your list of predictors
predictors <- 
  c("a_agebase", "a_gender", "a_domicile", "educ", "a_poccup3", 
    "maristat", "income2", "hosp_cat", "hosp_cat", "stroke_type", 
    "d_stroke_ct", "d_stroke_loc", "bmi", "tobacco_use", "g_alcohol", 
    "dm",  "c_hyperlip", "c_afib", "nihss_scale")

# Map over the predictors to create a list of univariate models



univariate_tables <- 
  predictors %>%
  map(
    function(var) {
      formula_str <- 
      as.formula(
        paste0("barthel_cat ~ timep*", var, "+ (1|pid)")) 
      lme4::glmer(
        formula_str, 
        family = binomial, 
        data = df_for_table_3,
        control = lme4::glmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 2e5))) %>% 
      gtsummary::tbl_regression(
        label = list(timep = "Time (Baseline Slope)")) %>%
      gtsummary::modify_header(
        label = paste0("**Predictor: ", str_to_title(var), "**")) %>% 
        gtsummary::add_global_p()})

# Stack them into one table
final_uni_table <- 
  gtsummary::tbl_stack(univariate_tables, quiet = TRUE) %>%
  gtsummary::modify_caption(
    "**Table 2. Univariate Growth Trajectory Interactions**") %>%
  gtsummary::bold_p() %>% 
  gtsummary::bold_labels()


final_uni_table
Table 2. Univariate Growth Trajectory Interactions
Predictor: A_agebase log(OR) 95% CI p-value
Time (Baseline Slope) 0.20 0.16, 0.23 <0.001
a_agebase -1.2 -1.7, -0.78 <0.001
Time (Baseline Slope) * a_agebase -0.01 -0.05, 0.03 0.6
Time (Baseline Slope) 0.22 0.17, 0.27 <0.001
a_gender

0.6
    Male
    Female -0.24 -1.1, 0.63
Time (Baseline Slope) * a_gender

0.2
    Time (Baseline Slope) * Female -0.05 -0.12, 0.02
Time (Baseline Slope) 0.25 0.12, 0.38 <0.001
a_domicile

0.12
    Rural
    Semi-Urban 1.3 -0.47, 3.1
    Urban 0.41 -1.3, 2.1
Time (Baseline Slope) * a_domicile

0.5
    Time (Baseline Slope) * Semi-Urban -0.04 -0.18, 0.11
    Time (Baseline Slope) * Urban -0.07 -0.20, 0.07
Time (Baseline Slope) 0.42 0.27, 0.57 <0.001
educ

0.088
    None
    Primary -1.3 -2.8, 0.25
    Secondary -0.42 -2.0, 1.2
    Tertiary 0.13 -1.6, 1.9
Time (Baseline Slope) * educ

0.003
    Time (Baseline Slope) * Primary -0.21 -0.36, -0.05
    Time (Baseline Slope) * Secondary -0.29 -0.44, -0.13
    Time (Baseline Slope) * Tertiary -0.23 -0.40, -0.07
Time (Baseline Slope) 0.19 0.14, 0.24 <0.001
a_poccup3

<0.001
    Skilled
    Manual -0.39 -1.5, 0.72
    Others -0.26 -2.5, 2.0
    Retired -1.4 -2.8, 0.03
    Unemployed -3.5 -4.9, -2.1
Time (Baseline Slope) * a_poccup3

0.2
    Time (Baseline Slope) * Manual 0.00 -0.09, 0.09
    Time (Baseline Slope) * Others 0.30 0.04, 0.56
    Time (Baseline Slope) * Retired -0.04 -0.15, 0.06
    Time (Baseline Slope) * Unemployed 0.02 -0.09, 0.14
Time (Baseline Slope) 0.21 0.17, 0.26 <0.001
maristat

0.7
    Currently Married
    Previously Married 0.18 -0.79, 1.1
    Never Married -0.78 -2.9, 1.3
Time (Baseline Slope) * maristat

0.5
    Time (Baseline Slope) * Previously Married -0.04 -0.12, 0.04
    Time (Baseline Slope) * Never Married -0.05 -0.23, 0.12
Time (Baseline Slope) 0.20 0.16, 0.24 <0.001
income2

0.036
    <500 GHc
    >= 500 GHc 1.4 0.09, 2.7
Time (Baseline Slope) * income2

>0.9
    Time (Baseline Slope) * >= 500 GHc 0.00 -0.10, 0.10
Time (Baseline Slope) 0.14 0.08, 0.20 <0.001
hosp_cat

<0.001
    Primary
    Secondary 2.2 1.1, 3.4
    Tertiary 4.5 3.4, 5.7
Time (Baseline Slope) * hosp_cat

0.078
    Time (Baseline Slope) * Secondary 0.09 0.00, 0.18
    Time (Baseline Slope) * Tertiary 0.08 0.00, 0.17
Time (Baseline Slope) 0.14 0.08, 0.20 <0.001
hosp_cat

<0.001
    Primary
    Secondary 2.2 1.1, 3.4
    Tertiary 4.5 3.4, 5.7
Time (Baseline Slope) * hosp_cat

0.078
    Time (Baseline Slope) * Secondary 0.09 0.00, 0.18
    Time (Baseline Slope) * Tertiary 0.08 0.00, 0.17
Time (Baseline Slope) 0.27 0.18, 0.36 <0.001
stroke_type

<0.001
    Hemorrhagic
    Ischemic -3.3 -4.5, -2.1
    Untyped -4.9 -9.3, -0.55
Time (Baseline Slope) * stroke_type

0.13
    Time (Baseline Slope) * Ischemic -0.10 -0.19, 0.00
    Time (Baseline Slope) * Untyped -0.15 -0.43, 0.13
Time (Baseline Slope) 0.17 0.13, 0.21 <0.001
d_stroke_ct

<0.001
    Ischaemic
    Haemorrhage infarct 0.81 -1.6, 3.2
    Haemorrhagic 3.8 2.4, 5.2
    Ischaemic and Haemorrhagic 0.53 -2.3, 3.4
Time (Baseline Slope) * d_stroke_ct

0.11
    Time (Baseline Slope) * Haemorrhage infarct 0.21 -0.01, 0.42
    Time (Baseline Slope) * Haemorrhagic 0.07 -0.03, 0.18
    Time (Baseline Slope) * Ischaemic and Haemorrhagic 0.14 -0.11, 0.38
Time (Baseline Slope) 0.20 0.16, 0.24 <0.001
d_stroke_loc

0.3
    Anterior Circulation
    Posterior Circulation -0.53 -1.6, 0.56
Time (Baseline Slope) * d_stroke_loc

0.8
    Time (Baseline Slope) * Posterior Circulation -0.01 -0.09, 0.07
Time (Baseline Slope) 0.36 0.18, 0.53 <0.001
bmi 0.08 0.00, 0.16 0.051
Time (Baseline Slope) * bmi -0.01 -0.01, 0.00 0.073
Time (Baseline Slope) 0.20 0.16, 0.23 <0.001
tobacco_use

0.063
    No
    Yes -1.5 -3.0, 0.08
Time (Baseline Slope) * tobacco_use

>0.9
    Time (Baseline Slope) * Yes -0.01 -0.13, 0.11
Time (Baseline Slope) 0.24 0.19, 0.28 <0.001
g_alcohol

0.13
    Never used alcohol
    Currently uses alcohol 1.1 -0.42, 2.6
    Past 12 months 3.3 0.27, 6.3
    Past 30 days 2.2 -1.8, 6.2
    Formerly used alcohol 0.20 -0.90, 1.3
    Stopped after the stroke occured -1.0 -3.0, 0.94
Time (Baseline Slope) * g_alcohol

0.007
    Time (Baseline Slope) * Currently uses alcohol -0.17 -0.28, -0.07
    Time (Baseline Slope) * Past 12 months 0.02 -0.26, 0.30
    Time (Baseline Slope) * Past 30 days 0.04 -0.35, 0.42
    Time (Baseline Slope) * Formerly used alcohol -0.02 -0.11, 0.07
    Time (Baseline Slope) * Stopped after the stroke occured -0.19 -0.33, -0.06
Time (Baseline Slope) 0.19 0.15, 0.23 <0.001
dm

0.2
    No
    Yes -0.45 -1.1, 0.20
Time (Baseline Slope) 0.18 0.06, 0.29 0.002
c_hyperlip

0.2
    Yes
    No -0.25 -1.8, 1.3
    Dont Know -2.3 -5.0, 0.31
Time (Baseline Slope) * c_hyperlip

>0.9
    Time (Baseline Slope) * No 0.02 -0.10, 0.14
    Time (Baseline Slope) * Dont Know 0.00 -0.21, 0.20
Time (Baseline Slope) 16 -4,075, 4,107 >0.9
c_afib

0.8
    Yes
    No -50 -14,325, 14,225
    Dont Know -49 -14,325, 14,226
Time (Baseline Slope) * c_afib

>0.9
    Time (Baseline Slope) * No -16 -4,106, 4,075
    Time (Baseline Slope) * Dont Know -16 -4,106, 4,075
Time (Baseline Slope) 0.15 0.10, 0.20 <0.001
nihss_scale -0.85 -0.94, -0.75 <0.001
Time (Baseline Slope) * nihss_scale -0.03 -0.04, -0.01 <0.001
Abbreviations: CI = Confidence Interval, OR = Odds Ratio

Figure 1 - Barthel

Show the code
  df_pings2_bp %>% 
  mutate(
    timep = case_when(
      eventname == "Baseline" ~ 0, 
      eventname == "Month 1" ~ 1, 
      eventname == "Month 3" ~ 3, 
      eventname == "Month 6" ~ 6,
      eventname == "Month 9" ~ 9, 
      eventname == "Month12" ~ 12)) %>% 
  select(
    a_gender, timep, pid, barthel_cat, a_domicile, a_agebase, 
    a_agebase, a_gender, a_domicile, educ, a_poccup3, 
    maristat, income, hosp_cat, stroke_type, d_stroke_ct, 
    d_stroke_loc, nihss_scale, bmi, tobacco_use, g_alcohol, 
    dm,  c_hyperlip, c_afib, nihss_scale) %>% 
  mutate(hosp_cat = factor(hosp_cat)) %>% 
  arrange(pid, timep) %>% 
  group_by(pid) %>% 
  fill(
    a_gender, a_domicile, a_agebase, educ, a_poccup3, 
    maristat, income, hosp_cat, stroke_type, d_stroke_ct, 
    d_stroke_loc, bmi, tobacco_use, g_alcohol, 
    c_hyperlip, c_afib, nihss_scale) %>% 
  ungroup() %>% 
  fastDummies::dummy_cols(select_columns = "hosp_cat") %>% 
  fastDummies::dummy_cols(select_columns = "maristat") %>% 
  mutate(
    a_agebase = scale(a_agebase), 
    barthel_cat = factor(barthel_cat) %>% unclass(), 
    barthel_cat = barthel_cat -1) %>%
  filter(!is.na(educ)) %>% 
  select(barthel_cat, timep, educ) %>% 
  group_by(educ, timep) %>% 
    reframe(across(barthel_cat, ~epiDisplay::ci.binomial(.x))) %>% 
    unnest(barthel_cat) %>% 
  ggplot(
    aes(
      x = timep, 
      y = probability, 
      ymin = probability - se, 
      ymax = probability + se,
      group = educ, 
      color = educ, 
      linetype = educ))+
  geom_errorbar(
        position = position_dodge2(width = 0.4),
        width = 0.2) +    
    geom_point(
        position = position_dodge2(width = 0.2),
        size = 3)+
  geom_line()+
  labs(
    x = "Follow-Up period", 
    y = "probability of high Barthel",
    color = "Level of Education",
    linetype = "Level of Education")+
    theme_bw()+
  scale_x_continuous(
        breaks = c(0, 1, 3, 6, 9, 12),
        labels = c(
            "Baseline", "     Month 1","Month 3", 
            "Month 6", "Month 9", "Month 12"))+
    scale_y_continuous(
        breaks = seq(0, 1, 0.1),
        limits = c(0, 1))+
    scale_color_manual(
        breaks = c("None", "Primary", "Secondary", "Tertiary"),
        values = c("#C952B9","dodgerblue", "#222222", "#99d8c9"))+
    theme(
        legend.position = "inside",
        legend.position.inside = c(0.9, 0.2),
        legend.background = element_rect(color = "black"),
        axis.text.y = ggtext::element_markdown(),
        axis.text = element_text(color = "black"),
        panel.grid = element_blank(),
        legend.spacing = unit(0, "pt"), 
        axis.text.x = element_text(size = 11),
        legend.margin = margin(t = 1, b = 2,r = 3, l = 3),
        legend.key.spacing = unit(0, "pt")
        )